Skip to content

Commit

Permalink
feat(package): add a command to list selections
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
yunielrc committed Sep 28, 2023
1 parent 02ace92 commit c6c9907
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 3 deletions.
75 changes: 73 additions & 2 deletions src/usr/lib/ydf/components/package/ydf-package-command.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/usr/lib/ydf/components/package/ydf-package-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Empty file.
Empty file.
Empty file.
49 changes: 48 additions & 1 deletion tests/usr/lib/ydf/components/package/ydf-package-command.f.bats
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# shellcheck disable=SC2153
# shellcheck disable=SC2153,SC2030,SC2031
load test_helper

setup() {
Expand Down Expand Up @@ -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"
}
37 changes: 37 additions & 0 deletions tests/usr/lib/ydf/components/package/ydf-package-service.i.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit c6c9907

Please sign in to comment.