From da8fa2812294335145fcd8d74d57a12359e6b42e Mon Sep 17 00:00:00 2001 From: yunielrc87 Date: Wed, 13 Sep 2023 13:32:00 -0400 Subject: [PATCH] feat(package): add a command to install a package --- Makefile | 3 +- src/etc/skel/.ydf.env | 3 + src/etc/ydf/ydf.env | 1 + .../package/ydf-package-command.bash | 22 ++- .../package/ydf-package-service.bash | 102 ++++++++--- src/usr/lib/ydf/errors.bash | 4 +- src/usr/lib/ydf/utils.bash | 4 +- .../packages/0freedom-fail/postinstall | 3 + .../packages/0freedom-fail/preinstall | 4 + tests/fixtures/packages/1freedom/postinstall | 3 + tests/fixtures/packages/1freedom/preinstall | 3 + .../fixtures/packages/2preinstall/preinstall | 1 + tests/fixtures/packages/3install/install | 1 + tests/fixtures/packages/3install/preinstall | 1 + tests/fixtures/packages/aws-cli-v2/.pacman | 0 .../packages/aws-cli-v2/aws-cli-v2.plugin.zsh | 13 -- .../fixtures/packages/aws-cli-v2/post-install | 8 - tests/test_helper_base.bash | 2 + .../ydf/components/package/test_helper.bash | 2 +- .../package/ydf-package-command.bats | 9 - .../package/ydf-package-command.f.bats | 76 ++++++++ .../package/ydf-package-service.bats | 169 +++++++++++++++++- tests/usr/lib/ydf/ydf.bats | 13 -- tests/usr/lib/ydf/ydf.f.bats | 27 +++ tools/bats-functional | 2 +- tools/bats-integration | 2 +- tools/bats-unit | 2 +- 27 files changed, 402 insertions(+), 78 deletions(-) create mode 100644 tests/fixtures/packages/0freedom-fail/postinstall create mode 100644 tests/fixtures/packages/0freedom-fail/preinstall create mode 100644 tests/fixtures/packages/1freedom/postinstall create mode 100644 tests/fixtures/packages/1freedom/preinstall create mode 100644 tests/fixtures/packages/2preinstall/preinstall create mode 100644 tests/fixtures/packages/3install/install create mode 100644 tests/fixtures/packages/3install/preinstall delete mode 100644 tests/fixtures/packages/aws-cli-v2/.pacman delete mode 100644 tests/fixtures/packages/aws-cli-v2/aws-cli-v2.plugin.zsh delete mode 100644 tests/fixtures/packages/aws-cli-v2/post-install delete mode 100644 tests/usr/lib/ydf/components/package/ydf-package-command.bats create mode 100644 tests/usr/lib/ydf/components/package/ydf-package-command.f.bats delete mode 100644 tests/usr/lib/ydf/ydf.bats diff --git a/Makefile b/Makefile index 82cff8f..948c40b 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,8 @@ test-integration: test-functional: ./tools/bats-functional -test-all: test-unit test-integration test-functional +test-all: + ./tools/bats --recursive tests # ci server does not support VT-x so we can't run integration or functional tests test-all-ci: test-unit diff --git a/src/etc/skel/.ydf.env b/src/etc/skel/.ydf.env index e69de29..b5d49c7 100644 --- a/src/etc/skel/.ydf.env +++ b/src/etc/skel/.ydf.env @@ -0,0 +1,3 @@ +# shellcheck disable=SC2034,SC2148 + +# YDF_PACKAGE_SERVICE_DEFAULT_OS=manjaro diff --git a/src/etc/ydf/ydf.env b/src/etc/ydf/ydf.env index e69de29..755c1dc 100644 --- a/src/etc/ydf/ydf.env +++ b/src/etc/ydf/ydf.env @@ -0,0 +1 @@ +# shellcheck disable=SC2034,SC2148 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 276a677..55da421 100644 --- a/src/usr/lib/ydf/components/package/ydf-package-command.bash +++ b/src/usr/lib/ydf/components/package/ydf-package-command.bash @@ -42,13 +42,16 @@ ydf::package_command::constructor() { ydf::package_command::__install_help() { cat <<-HELPMSG Usage: -${__YDF_SCRIPT_NAME} package install PACKAGE [PACKAGE...] +${__YDF_SCRIPT_NAME} package install [OPTIONS] PACKAGE [PACKAGE...] Install packages Flags: -h, --help Show this help +Options: + --os Operating system + HELPMSG } @@ -58,6 +61,9 @@ HELPMSG # Flags: # -h | --help Show help # +# Options: +# --os Operating system +# # Arguments: # PACKAGE [PACKAGE...] one or more packages # @@ -69,6 +75,7 @@ HELPMSG # ydf::package_command::__install() { local packages='' + local os='' if [[ $# == 0 ]]; then set -- '-h'; fi @@ -79,6 +86,17 @@ ydf::package_command::__install() { ydf::package_command::__install_help return 0 ;; + # options + --os) + readonly os="${2:-}" + # validate argument + if [[ -z "$os" ]]; then + err "No os name specified\n" + ydf::package_command::__install_help + return "$ERR_MISSING_ARG" + fi + shift 2 + ;; # arguments *) readonly packages="$*" @@ -93,7 +111,7 @@ ydf::package_command::__install() { return "$ERR_MISSING_ARG" fi - ydf::package_service::install "$packages" + ydf::package_service::install "$packages" "$os" } 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 28e8b46..6d06c20 100644 --- a/src/usr/lib/ydf/components/package/ydf-package-service.bash +++ b/src/usr/lib/ydf/components/package/ydf-package-service.bash @@ -24,9 +24,13 @@ fi # [ubuntu]="apt" # ) -readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='home homeln homecp homecps homecat root rootcp rootcps rootln rootcat flatpack dconf.ini plugin_zsh docker_compose' -readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="pre_install pacman yay install post_install ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" -readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="pre_install apt install post_install ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" +# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='home homeln homecp homecps homecat root rootcp rootcps rootln rootcat flatpack dconf.ini plugin_zsh docker_compose' +# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall pacman yay install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" +# readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall apt install postinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" + +readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON='' +readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_MANJARO="preinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" +readonly __YDF_PACKAGE_SERVICE_INSTRUCTIONS_UBUNTU="preinstall ${__YDF_PACKAGE_SERVICE_INSTRUCTIONS_COMMON}" # # FUNCTIONS @@ -44,8 +48,45 @@ ydf::package_service::constructor() { readonly __YDF_PACKAGE_SERVICE_DEFAULT_OS="$1" } -ydf::package_service::__instruction_iname() { - : +# +# Get instructions names +# +# Arguments: +# [os_name] string os name +# +# Output: +# writes instructions_names (string) to the stdout +# +# Returns: +# 0 on success, non-zero on error. +# +ydf::package_service::get_instructions_names() { + local -r os_name="${1:-"$__YDF_PACKAGE_SERVICE_DEFAULT_OS"}" + + local instr + instr="__YDF_PACKAGE_SERVICE_INSTRUCTIONS_${os_name^^}" + + if [[ -z "${!instr:-}" ]]; then + err "There is no instructions for os: ${os_name}" + return "$ERR_INVAL_ARG" + fi + + echo "${!instr}" +} + +# +# Execute preinstall script +# +# +# Returns: +# 0 on success, non-zero on error. +# +ydf::package_service::__instruction_preinstall() { + if [[ ! -f ./preinstall ]]; then + return 0 + fi + + bash ./preinstall } # @@ -53,7 +94,7 @@ ydf::package_service::__instruction_iname() { # # Arguments: # package_dir string package directory -# os_name string os name +# [os_name] string os name # # Output: # writes installed package name to the stdout @@ -63,7 +104,7 @@ ydf::package_service::__instruction_iname() { # ydf::package_service::install_one_from_dir() { local -r package_dir="$1" - local -r os_name="${2:-"$__YDF_PACKAGE_SERVICE_DEFAULT_OS"}" + local -r os_name="${2:-}" # validate arguments if [[ ! -d "$package_dir" ]]; then @@ -71,23 +112,38 @@ ydf::package_service::install_one_from_dir() { return "$ERR_NO_DIR" fi - local inst_specific - inst_specific="__YDF_PACKAGE_SERVICE_INSTRUCTIONS_${os_name^^}" - readonly inst_specific + local instr + instr="$(ydf::package_service::get_instructions_names "$os_name")" || { + err "Getting instructions names for os: ${os_name}" + return "$ERR_YPS_GENERAL" + } + readonly instr - local -a instructions_arr - # shellcheck disable=SC2206,SC2317 - instructions_arr=(${!inst_specific}) - readonly instructions_arr + if [[ -z "$instr" ]]; then + err "There is no instructions" + return "$ERR_INVAL_VALUE" + fi - for iname in "${instructions_arr[@]}"; do - local ifunction="ydf::package_service::__instruction_${iname}" + local -a instr_arr + # shellcheck disable=SC2206,SC2317 + instr_arr=($instr) + readonly instr_arr - "$ifunction" "$package_dir" || { - err "Executing instruction '${iname}' on '${package_dir}'" - return "$ERR_YPS_INSTRUCTION_FAIL" + ( + cd "$package_dir" 2>/dev/null || { + err "Changing the current directory to ${package_dir}" + return "$ERR_CHANGING_WORKDIR" } - done + + for iname in "${instr_arr[@]}"; do + local ifunction="ydf::package_service::__instruction_${iname}" + + "$ifunction" || { + err "Executing instruction '${iname}' on '${package_dir}'" + return "$ERR_YPS_INSTRUCTION_FAIL" + } + done + ) } # @@ -95,6 +151,7 @@ ydf::package_service::install_one_from_dir() { # # Arguments: # package_name string package name +# [os_name] string operating system # # Output: # writes installed package name to the stdout @@ -103,7 +160,7 @@ ydf::package_service::install_one_from_dir() { # 0 on success, non-zero on error. # ydf::package_service::install_one() { - : + ydf::package_service::install_one_from_dir "$@" } # @@ -111,6 +168,7 @@ ydf::package_service::install_one() { # # Arguments: # packages_names string[] packages names +# [os_name] string operating system # # Output: # writes installed packages names to the stdout @@ -119,5 +177,5 @@ ydf::package_service::install_one() { # 0 on success, non-zero on error. # ydf::package_service::install() { - ydf::package_service::install_one_from_dir "$@" + ydf::package_service::install_one "$@" } diff --git a/src/usr/lib/ydf/errors.bash b/src/usr/lib/ydf/errors.bash index ac75586..e4c34fb 100644 --- a/src/usr/lib/ydf/errors.bash +++ b/src/usr/lib/ydf/errors.bash @@ -7,6 +7,8 @@ readonly ERR_INVAL_VALUE=66 readonly ERR_INVAL_ARG=67 readonly ERR_MISSING_ARG=68 readonly ERR_NO_DIR=69 +readonly ERR_CHANGING_WORKDIR=69 ## package-service -readonly ERR_YPS_INSTRUCTION_FAIL=80 +readonly ERR_YPS_GENERAL=80 +readonly ERR_YPS_INSTRUCTION_FAIL=81 diff --git a/src/usr/lib/ydf/utils.bash b/src/usr/lib/ydf/utils.bash index c24335c..354d37a 100644 --- a/src/usr/lib/ydf/utils.bash +++ b/src/usr/lib/ydf/utils.bash @@ -2,6 +2,6 @@ err() { echo -e "ERROR> $*" >&2 } -inf() { - echo -e "INFO> $*" +ech() { + echo -e "$*" } diff --git a/tests/fixtures/packages/0freedom-fail/postinstall b/tests/fixtures/packages/0freedom-fail/postinstall new file mode 100644 index 0000000..991c41a --- /dev/null +++ b/tests/fixtures/packages/0freedom-fail/postinstall @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo postinstall diff --git a/tests/fixtures/packages/0freedom-fail/preinstall b/tests/fixtures/packages/0freedom-fail/preinstall new file mode 100644 index 0000000..23e33b8 --- /dev/null +++ b/tests/fixtures/packages/0freedom-fail/preinstall @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +echo 'preinstall fails' +exit 1 diff --git a/tests/fixtures/packages/1freedom/postinstall b/tests/fixtures/packages/1freedom/postinstall new file mode 100644 index 0000000..991c41a --- /dev/null +++ b/tests/fixtures/packages/1freedom/postinstall @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo postinstall diff --git a/tests/fixtures/packages/1freedom/preinstall b/tests/fixtures/packages/1freedom/preinstall new file mode 100644 index 0000000..0b7fe63 --- /dev/null +++ b/tests/fixtures/packages/1freedom/preinstall @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo 'preinstall succeed' diff --git a/tests/fixtures/packages/2preinstall/preinstall b/tests/fixtures/packages/2preinstall/preinstall new file mode 100644 index 0000000..08032a4 --- /dev/null +++ b/tests/fixtures/packages/2preinstall/preinstall @@ -0,0 +1 @@ +echo 'preinstall: preinstall succeed' diff --git a/tests/fixtures/packages/3install/install b/tests/fixtures/packages/3install/install new file mode 100644 index 0000000..c51a685 --- /dev/null +++ b/tests/fixtures/packages/3install/install @@ -0,0 +1 @@ +echo '3install: install succeed' diff --git a/tests/fixtures/packages/3install/preinstall b/tests/fixtures/packages/3install/preinstall new file mode 100644 index 0000000..0c8d2dd --- /dev/null +++ b/tests/fixtures/packages/3install/preinstall @@ -0,0 +1 @@ +echo '3install: preinstall succeed' diff --git a/tests/fixtures/packages/aws-cli-v2/.pacman b/tests/fixtures/packages/aws-cli-v2/.pacman deleted file mode 100644 index e69de29..0000000 diff --git a/tests/fixtures/packages/aws-cli-v2/aws-cli-v2.plugin.zsh b/tests/fixtures/packages/aws-cli-v2/aws-cli-v2.plugin.zsh deleted file mode 100644 index 577e46b..0000000 --- a/tests/fixtures/packages/aws-cli-v2/aws-cli-v2.plugin.zsh +++ /dev/null @@ -1,13 +0,0 @@ -# Aliases -alias aws-ec2-spot-prices="aws ec2 describe-spot-price-history --start-time \"\$(date -Idate)\" --product-descriptions 'Linux/UNIX' --query 'sort_by(SpotPriceHistory, &SpotPrice)'" -alias aws-ec2-volumes="aws ec2 describe-volumes --query 'Volumes[*].{State:State,ID:VolumeId,AZ:AvailabilityZone,Size:Size,Created:CreateTime,Instance:Attachments[0].InstanceId}'" -alias aws-ec2-instances="aws ec2 describe-instances --query 'Reservations[].Instances[].{State:State.Name, Type:InstanceType, ID:InstanceId, PublicIp:PublicIpAddress, Name:Tags[?Key==\`Name\`].Value | [0]}'" -alias aws-ec2-instances-full="aws ec2 describe-instances --query 'Reservations[].Instances[].{State:State.Name, Type:InstanceType, ID:InstanceId, Launched:LaunchTime, PublicIp:PublicIpAddress, Name:Tags[?Key==\`Name\`].Value | [0]}'" -alias ec2ls='aws-ec2-instances' -alias ec2lsf='aws-ec2-instances-full' -alias aws-ec2-stop="aws ec2 stop-instances --instance-ids" -alias ec2stop="aws-ec2-stop" -alias aws-ec2-start="aws ec2 start-instances --instance-ids" -alias ec2start="aws-ec2-start" -alias aws-ec2-terminate="aws ec2 terminate-instances --instance-ids" -alias ec2term="aws-ec2-terminate" diff --git a/tests/fixtures/packages/aws-cli-v2/post-install b/tests/fixtures/packages/aws-cli-v2/post-install deleted file mode 100644 index 114dd6e..0000000 --- a/tests/fixtures/packages/aws-cli-v2/post-install +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env sh -set -eu - -# Configure -aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" -aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" -aws configure set output table -aws configure set region us-east-2 diff --git a/tests/test_helper_base.bash b/tests/test_helper_base.bash index 681e475..eac934c 100644 --- a/tests/test_helper_base.bash +++ b/tests/test_helper_base.bash @@ -8,6 +8,8 @@ # VARIABLES +readonly TEST_FIXTURES_DIR="${TESTS_DIR}/fixtures" + # HELPER FUNCTIONS ydf() { diff --git a/tests/usr/lib/ydf/components/package/test_helper.bash b/tests/usr/lib/ydf/components/package/test_helper.bash index 355177e..883805e 100644 --- a/tests/usr/lib/ydf/components/package/test_helper.bash +++ b/tests/usr/lib/ydf/components/package/test_helper.bash @@ -1,5 +1,5 @@ # shellcheck source=../../test_helper.bash -. "${TESTS_DIR}/test_helper_base.bash" +. "${TESTS_DIR}/usr/lib/ydf/test_helper.bash" # shellcheck source=../../../../../../src/usr/lib/ydf/components/package/ydf-package-entity.bash . "${SRC_DIR}/usr/lib/ydf/components/package/ydf-package-entity.bash" # shellcheck source=../../../../../../src/usr/lib/ydf/components/package/ydf-package-service.bash diff --git a/tests/usr/lib/ydf/components/package/ydf-package-command.bats b/tests/usr/lib/ydf/components/package/ydf-package-command.bats deleted file mode 100644 index 90801d1..0000000 --- a/tests/usr/lib/ydf/components/package/ydf-package-command.bats +++ /dev/null @@ -1,9 +0,0 @@ -load test_helper - -# setup() { -# -# } - -# teardown() { -# -# } 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 new file mode 100644 index 0000000..e19174f --- /dev/null +++ b/tests/usr/lib/ydf/components/package/ydf-package-command.f.bats @@ -0,0 +1,76 @@ +load test_helper + +# setup() { +# +# } + +# teardown() { +# +# } + +# Tests for ydf package +@test "ydf package, Should show help" { + + for p in '' -h --help; do + run ydf package $p + + assert_success + assert_output --partial 'Usage: +ydf package COMMAND' + done +} + +# Tests for ydf package install +@test "ydf package install, Should show help" { + + for p in '' -h --help; do + run ydf package install $p + + assert_success + assert_output --partial 'ydf package install [OPTIONS] PACKAGE [PACKAGE...]' + done +} + +@test "ydf package install --os, Should fail with missing argument os" { + + run ydf package install --os + + assert_failure + assert_output --partial 'ERROR> No os name specified' +} + +@test "ydf package install --os manjaro, Should fail with missing argument PACKAGE" { + + run ydf package install --os manjaro + + assert_failure + assert_output --partial "ERROR> Missing argument 'PACKAGE'" +} + +# Tests for ydf package install ../2preinstall +@test "ydf package install --os manjaro ../2preinstall, Should succeed With no preinstall script" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages" + + run ydf package install --os manjaro "$_package_dir" + + assert_success + assert_output "" +} + +@test "ydf package install --os manjaro ../2preinstall, Should succeed" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages/2preinstall" + + run ydf package install --os manjaro "$_package_dir" + + assert_success + assert_output "preinstall: preinstall succeed" +} + +@test "ydf package install ../2preinstall, Should succeed" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages/2preinstall" + + run ydf package install "$_package_dir" + + assert_success + assert_output "preinstall: preinstall succeed" +} diff --git a/tests/usr/lib/ydf/components/package/ydf-package-service.bats b/tests/usr/lib/ydf/components/package/ydf-package-service.bats index 90801d1..9780abb 100644 --- a/tests/usr/lib/ydf/components/package/ydf-package-service.bats +++ b/tests/usr/lib/ydf/components/package/ydf-package-service.bats @@ -1,9 +1,172 @@ load test_helper -# setup() { -# -# } +setup() { + ydf::package_service::constructor "$YDF_PACKAGE_SERVICE_DEFAULT_OS" + export __YDF_PACKAGE_SERVICE_DEFAULT_OS +} # teardown() { # # } + + +# Tests for ydf::package_service::get_instructions_names() +@test "ydf::package_service::get_instructions_names() Should list instructions names for default_os" { + + run ydf::package_service::get_instructions_names + + assert_success + assert_output --partial 'preinstall' +} + +@test "ydf::package_service::get_instructions_names() Should fail with invalid os_name" { + local -r _os_name='invalid' + + run ydf::package_service::get_instructions_names "$_os_name" + + assert_failure + assert_output 'ERROR> There is no instructions for os: invalid' +} + +@test "ydf::package_service::get_instructions_names() Should succeed" { + local -r _os_name='ubuntu' + + run ydf::package_service::get_instructions_names "$_os_name" + + assert_success + assert_output --partial 'preinstall' +} + +# Tests for ydf::package_service::__instruction_preinstall() +@test "ydf::package_service::__instruction_preinstall() Should succeed if there is no preinstall script" { + + # cd "${TEST_FIXTURES_DIR}/packages/1freedom" + + run ydf::package_service::__instruction_preinstall + + assert_success + assert_output '' +} + +@test "ydf::package_service::__instruction_preinstall() Should succeed if preinstall script succeed" { + + cd "${TEST_FIXTURES_DIR}/packages/1freedom" + + run ydf::package_service::__instruction_preinstall + + assert_success + assert_output 'preinstall succeed' +} + +@test "ydf::package_service::__instruction_preinstall() Should fail if preinstall script fails" { + + cd "${TEST_FIXTURES_DIR}/packages/0freedom-fail" + + run ydf::package_service::__instruction_preinstall + + assert_failure + assert_output 'preinstall fails' +} + +# Tests for ydf::package_service::install_one_from_dir() +@test "ydf::package_service::install_one_from_dir() Should fail if package_dir directory doesn't exist" { + local -r _package_dir='asdjflk3408rgsjl' + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_failure + assert_output "ERROR> Directory 'asdjflk3408rgsjl' doesn't exist" +} + +@test "ydf::package_service::install_one_from_dir() Should fail if get_instructions_names fails" { + local -r _package_dir="$BATS_TEST_TMPDIR" + + ydf::package_service::get_instructions_names() { + assert_equal "$*" '' + + return 1 + } + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_failure + assert_output "ERROR> Getting instructions names for os: " +} + +@test "ydf::package_service::install_one_from_dir() Should fail if there is no instructions" { + local -r _package_dir="$BATS_TEST_TMPDIR" + + ydf::package_service::get_instructions_names() { + assert_equal "$*" '' + } + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_failure + assert_output "ERROR> There is no instructions" +} + +@test "ydf::package_service::install_one_from_dir() Should fail if changing dir fails" { + local -r _package_dir="$BATS_TEST_TMPDIR" + chmod 000 "$_package_dir" + + ydf::package_service::get_instructions_names() { + assert_equal "$*" '' + + echo preinstall + } + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_failure + assert_output --partial "ERROR> Changing the current directory to " +} + +@test "ydf::package_service::install_one_from_dir() Should fail if at least one instruction fails" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages/0freedom-fail" + + ydf::package_service::get_instructions_names() { + assert_equal "$*" '' + + echo 'instruction1 preinstall' + } + ydf::package_service::__instruction_instruction1() { + assert_equal "$*" '' + echo instruction1 + } + + ydf::package_service::__instruction_preinstall() { + assert_equal "$*" '' + return 1 + } + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_failure + assert_output --regexp "ERROR> Executing instruction 'preinstall' on '.*/0freedom-fail'" +} + +@test "ydf::package_service::install_one_from_dir() Should succeed if all instructions are success" { + local -r _package_dir="${TEST_FIXTURES_DIR}/packages/0freedom-fail" + + ydf::package_service::get_instructions_names() { + assert_equal "$*" '' + + echo 'instruction1 preinstall' + } + ydf::package_service::__instruction_instruction1() { + assert_equal "$*" '' + echo instruction1 + } + + ydf::package_service::__instruction_preinstall() { + assert_equal "$*" '' + echo preinstall + } + + run ydf::package_service::install_one_from_dir "$_package_dir" + + assert_success + assert_output "instruction1 +preinstall" +} diff --git a/tests/usr/lib/ydf/ydf.bats b/tests/usr/lib/ydf/ydf.bats deleted file mode 100644 index 608ebef..0000000 --- a/tests/usr/lib/ydf/ydf.bats +++ /dev/null @@ -1,13 +0,0 @@ -load test_helper - -# setup_file() { -# -# } - -# setup() { -# -# } - -# teardown() { -# -# } diff --git a/tests/usr/lib/ydf/ydf.f.bats b/tests/usr/lib/ydf/ydf.f.bats index 608ebef..78f4650 100644 --- a/tests/usr/lib/ydf/ydf.f.bats +++ b/tests/usr/lib/ydf/ydf.f.bats @@ -11,3 +11,30 @@ load test_helper # teardown() { # # } + +# Tests for ydf +@test "ydf, Should show help" { + + run ydf + + assert_success + assert_output --partial 'Usage: +ydf COMMAND' +} + +@test "ydf package, Should show help" { + + run ydf package + + assert_success + assert_output --partial 'Usage: +ydf package COMMAND' +} + +@test "ydf invalid, Should fail with invalid command" { + + run ydf invalid + + assert_failure + assert_output --partial 'ERROR> Invalid command: invalid' +} diff --git a/tools/bats-functional b/tools/bats-functional index e1cd452..b8c3526 100755 --- a/tools/bats-functional +++ b/tools/bats-functional @@ -1,4 +1,4 @@ #!/usr/bin/env bash # shellcheck disable=SC2046 cd "$(dirname "${BASH_SOURCE[0]}")/.." && - tools/bats $(fd --full-path 'test/\S+\.f\.bats$') + tools/bats $(fd --full-path 'tests/\S+\.f\.bats$') diff --git a/tools/bats-integration b/tools/bats-integration index f5979f5..c9c269b 100755 --- a/tools/bats-integration +++ b/tools/bats-integration @@ -1,4 +1,4 @@ #!/usr/bin/env bash # shellcheck disable=SC2046 cd "$(dirname "${BASH_SOURCE[0]}")/.." && - tools/bats $(fd --full-path 'test/\S+\.i\.bats$') + tools/bats $(fd --full-path 'tests/\S+\.i\.bats$') diff --git a/tools/bats-unit b/tools/bats-unit index aa3c89f..a3c934e 100755 --- a/tools/bats-unit +++ b/tools/bats-unit @@ -1,4 +1,4 @@ #!/usr/bin/env bash # shellcheck disable=SC2046 cd "$(dirname "${BASH_SOURCE[0]}")/.." && - tools/bats $(fd --full-path 'test/\S+[^.if]\.bats$') + tools/bats $(fd --full-path 'tests/\S+[^.if]\.bats$')