From c6c99077bd09ff22fb0dc0020fa02c53ebdf2668 Mon Sep 17 00:00:00 2001 From: yunielrc Date: Wed, 27 Sep 2023 23:28:49 -0400 Subject: [PATCH] feat(package): add a command to list selections closes #6 --- .../package/ydf-package-command.bash | 75 ++++++++++++++++++- .../package/ydf-package-service.bash | 33 ++++++++ tests/fixtures/packages3/.hid-file | 0 tests/fixtures/packages3/.hid-selection1.pkgs | 0 tests/fixtures/packages3/other-file | 0 .../package/ydf-package-command.f.bats | 49 +++++++++++- .../package/ydf-package-service.i.bats | 37 +++++++++ 7 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/packages3/.hid-file create mode 100644 tests/fixtures/packages3/.hid-selection1.pkgs create mode 100644 tests/fixtures/packages3/other-file diff --git a/src/usr/lib/ydf/components/package/ydf-package-command.bash b/src/usr/lib/ydf/components/package/ydf-package-command.bash index 2038869..4724344 100644 --- a/src/usr/lib/ydf/components/package/ydf-package-command.bash +++ b/src/usr/lib/ydf/components/package/ydf-package-command.bash @@ -202,6 +202,71 @@ ydf::package_command::__list() { ydf::package_service::list "$packages_dir" } +# +# Show help for __list_selections command +# +# Output: +# Writes the help to the stdout +# +ydf::package_command::__list_selections_help() { + cat <<-HELPMSG +Usage: +${__YDF_SCRIPT_NAME} package list-selections [OPTIONS] + +List packages selections in the packages directory + +Flags: + -h, --help Show this help + +Options: + --packages-dir Packages directory + +HELPMSG +} + +# +# List selections packages in the packages directory +# +# Flags: +# -h, --help Show help +# +# Options: +# --packages-dir Packages directory +# +# Output: +# write packages selections names (list) to stdout +# +# Returns: +# 0 on success, non-zero on error. +# +ydf::package_command::__list_selections() { + # shellcheck disable=SC2155 + local packages_dir="$(ydf::package_service::get_packages_dir)" + + while [[ $# -gt 0 ]]; do + case "$1" in + # flags + -h | --help) + ydf::package_command::__list_selections_help + return 0 + ;; + # options + --packages-dir) + readonly packages_dir="${2:-}" + # validate argument + if [[ -z "$packages_dir" ]]; then + err "No packages dir specified\n" + ydf::package_command::__list_selections_help + return "$ERR_MISSING_ARG" + fi + shift 2 + ;; + esac + done + + ydf::package_service::list_selections "$packages_dir" +} + ydf::package_command::__help() { cat <<-HELPMSG Usage: @@ -213,8 +278,9 @@ Flags: -h, --help Show this help Commands: - install install packages - list list packages + install Install packages + list List packages + list-selections List packages selections Run '${__YDF_SCRIPT_NAME} package --help' for more information on a command. HELPMSG @@ -239,6 +305,11 @@ ydf::package_service::run_cmd() { ydf::package_command::__list "$@" return $? ;; + list-selections) + shift + ydf::package_command::__list_selections "$@" + return $? + ;; *) err "Invalid command: ${1}\n" ydf::package_command::__help diff --git a/src/usr/lib/ydf/components/package/ydf-package-service.bash b/src/usr/lib/ydf/components/package/ydf-package-service.bash index 2c7870d..7a4e28b 100644 --- a/src/usr/lib/ydf/components/package/ydf-package-service.bash +++ b/src/usr/lib/ydf/components/package/ydf-package-service.bash @@ -802,3 +802,36 @@ ydf::package_service::list() { sort ) } + +# +# List selections packages in the packages directory +# +# Arguments: +# [packages_dir] string packages dir +# +# Output: +# writes packages_names (list) to the stdout +# +# Returns: +# 0 on success, non-zero on error. +# +ydf::package_service::list_selections() { + local -r packages_dir="${1:-"$(ydf::package_service::get_packages_dir)"}" + + # validate arguments + if [[ ! -d "$packages_dir" ]]; then + err "Packages directory '${packages_dir}' doesn't exist" + return "$ERR_NO_DIR" + fi + + ( + cd "$packages_dir" 2>/dev/null || { + err "Changing current directory to '${packages_dir}'" + return "$ERR_CHANGING_WORKDIR" + } + + find . -maxdepth 1 -type f -name '*.pkgs' -printf '%f\n' | + grep -Ev '^\.' | + sort + ) +} diff --git a/tests/fixtures/packages3/.hid-file b/tests/fixtures/packages3/.hid-file new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/packages3/.hid-selection1.pkgs b/tests/fixtures/packages3/.hid-selection1.pkgs new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/packages3/other-file b/tests/fixtures/packages3/other-file new file mode 100644 index 0000000..e69de29 diff --git a/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats b/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats index e82ed55..7193faa 100644 --- a/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats +++ b/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats @@ -1,4 +1,4 @@ -# shellcheck disable=SC2153 +# shellcheck disable=SC2153,SC2030,SC2031 load test_helper setup() { @@ -1000,3 +1000,50 @@ pkg3" pkg2 pkg3" } + +# Tests for ydf package list-selections +@test "ydf package list-selections, Should show help" { + + for p in -h --help; do + run ydf package list-selections $p + + assert_success + assert_output --partial 'ydf package list-selections [OPTIONS]' + done +} + +@test "ydf package list-selections --packages-dir, Should fail with missing argument packages-dir" { + + run ydf package list-selections --packages-dir + + assert_failure + assert_output --partial 'ERROR> No packages dir specified' +} + +@test "ydf package list-selections --packages-dir, Should fail If packages-dir doesn't exist" { + + run ydf package list-selections --packages-dir 'asdfadf324325623afddwg11' + + assert_failure + assert_output "ERROR> Packages directory 'asdfadf324325623afddwg11' doesn't exist" +} + +@test "ydf package list-selections, Should list selections" { + + export E_YDF_PACKAGE_SERVICE_PACKAGES_DIR="${TEST_FIXTURES_DIR}/packages3" + + run ydf package list-selections + + assert_success + assert_output "selection1.pkgs +selection2.pkgs" +} + +@test "ydf package list-selections --packages-dir ..., Should list selections packages with" { + + run ydf package list-selections --packages-dir "${TEST_FIXTURES_DIR}/packages3" + + assert_success + assert_output "selection1.pkgs +selection2.pkgs" +} diff --git a/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats b/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats index 0503d18..d8cb587 100644 --- a/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats +++ b/tests/usr/lib/ydf/components/package/ydf-package-service.i.bats @@ -1456,3 +1456,40 @@ install: HOME: ${TEST_HOME_DIR}" pkg2 pkg3" } + +# Tests for ydf::package_service::list_selections() +@test "ydf::package_service::list_selections() Should fail If packages_dir doesn't exist" { + local -r _packages_dir="adfadsf23423098587209" + + run ydf::package_service::list_selections "$_packages_dir" + + assert_failure + assert_output "ERROR> Packages directory '${_packages_dir}' doesn't exist" +} + +@test "ydf::package_service::list_selections() Should fail If change dir fails" { + local -r _packages_dir="$(mktemp -d)" + + cd() { + if [[ "$*" == "$_packages_dir" ]]; then + return 1 + fi + command cd "$@" + } + + run ydf::package_service::list_selections "$_packages_dir" + + assert_failure + assert_output "ERROR> Changing current directory to '${_packages_dir}'" +} + +@test "ydf::package_service::list_selections() Should succeed" { + + local -r _packages_dir="${TEST_FIXTURES_DIR}/packages3" + + run ydf::package_service::list_selections "$_packages_dir" + + assert_success + assert_output "selection1.pkgs +selection2.pkgs" +}