Skip to content

Commit

Permalink
feat(package): install multiple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yunielrc committed Sep 18, 2023
1 parent 755deaf commit 50f6a1d
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/usr/lib/ydf/components/package/ydf-package-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,27 @@ ydf::package_service::install_one() {
ydf::package_service::install_one_from_dir "$@"
}

#
# Install a ydotfile package
#
# Arguments:
# os_name string operating system
# package_name string package name
#
# Output:
# writes installed package name to the stdout
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::package_service::__install_one_batch() {
local -r os_name="$1"
local -r package_name="$2"

ydf::package_service::install_one \
"$package_name" "$os_name"
}

#
# Install one or many ydotfile packages
#
Expand All @@ -600,5 +621,15 @@ ydf::package_service::install_one() {
# 0 on success, non-zero on error.
#
ydf::package_service::install() {
ydf::package_service::install_one "$@"
local -r packages_names="$1"
local -r os_name="${2:-}"
# validate arguments
if [[ -z "$packages_names" ]]; then
err "Packages names must not be empty"
return "$ERR_INVAL_ARG"
fi

ydf::utils::for_each \
"$packages_names" \
"ydf::package_service::__install_one_batch '${os_name}'"
}
40 changes: 40 additions & 0 deletions src/usr/lib/ydf/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,43 @@ ydf::utils::mark_concat_with_envar_sub() {
sudo -u "$_user" tee --append "$dest_file" >/dev/null

}

#
# Execute a function for each element
#
# Arguments:
# elements string elements
# func string function name
#
# Output:
# Writes function output to stdout
#
# Returns:
# 0 on success, non-zero on error.
#
ydf::utils::for_each() {
local -r elements="$1"
local -r func="$2"
# validate arguments
if [[ -z "$elements" ]]; then
return 0
fi
if [[ -z "$func" ]]; then
err "Argument function can't be empty"
return "$ERR_INVAL_ARG"
fi

local -a elements_arr
# shellcheck disable=SC2206,SC2317
elements_arr=($elements)
readonly elements_arr

local el

for el in "${elements_arr[@]}"; do
eval "$func" "'${el}'" || {
err "Executing function for element '${el}'"
return "$ERR_FAILED"
}
done
}
26 changes: 26 additions & 0 deletions tests/usr/lib/ydf/components/package/ydf-package-command.f.bats
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,29 @@ line 11
assert_output --regexp ".* root root .* /.my/dir1/file11
.* root root .* /.my/file1"
}

# Tests for ydf package install bat rustscan
@test "ydf package install bat rustscan, Should install multiple packages" {
local -r _package_dir="${TEST_FIXTURES_DIR}/packages"

cd "$_package_dir"

run ydf package install bat rustscan

assert_success
assert_output --regexp "bat: install succeed
bat: postinstall succeed
.*
rustscan: install succeed
rustscan: postinstall succeed"

run command -v bat

assert_success
assert_output "/usr/bin/bat"

run command -v rustscan

assert_success
assert_output "/usr/bin/rustscan"
}
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 @@ -1019,3 +1019,40 @@ line 11
@test "ydf::package_service::__instruction_rootcats() DUMMY" {
:
}

# Tests for ydf::package_service::install_one()
@test "ydf::package_service::install_one() DUMMY" {
:
}

# Tests for ydf::package_service::install_one_batch()
@test "ydf::package_service::install_one_batch() DUMMY" {
:
}

# Tests for ydf::package_service::install()
@test "ydf::package_service::install() Should fail without packages_names" {
local -r _packages_names=""
local -r _os_name=""

run ydf::package_service::install \
"$_packages_names" "$_os_name"

assert_failure
assert_output "ERROR> Packages names must not be empty"
}

@test "ydf::package_service::install() Should succeed" {
local -r _packages_names="p1 p2 p3"
local -r _os_name=""

ydf::utils::for_each() {
assert_equal "$*" "p1 p2 p3 ydf::package_service::__install_one_batch ''"
}

run ydf::package_service::install \
"$_packages_names" "$_os_name"

assert_success
assert_output ""
}
69 changes: 69 additions & 0 deletions tests/usr/lib/ydf/utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,72 @@ added line1 to file1
added line2 to file1"
}

# Tests for ydf::utils::for_each()
@test "ydf::utils::for_each() Should succed Without elements" {
local -r _elements=""
local -r _func=""

run ydf::utils::for_each "$_elements" "$_func"

assert_success
assert_output ""
}

@test "ydf::utils::for_each() Should fail Without function" {
local -r _elements="e1 e2"
local -r _func=""

run ydf::utils::for_each "$_elements" "$_func"

assert_failure
assert_output "ERROR> Argument function can't be empty"
}

@test "ydf::utils::for_each() Should fail If one execution fails" {
local -r _elements="e1 e2 e3"
local -r _func="func1"

func1() {
case "$*" in
e1 )
return 0
;;
e2 )
return 1
;;
e3 )
return 0
;;
* )
return 0
;;
esac
}

run ydf::utils::for_each "$_elements" "$_func"

assert_failure
assert_output "ERROR> Executing function for element 'e2'"
}

@test "ydf::utils::for_each() Should succeed" {
local -r _elements="e1 e2 e3"
local -r _func="func1"

func1() {
case "$*" in
e1 | e2 | e3 )
return 0
;;
* )
return 1
;;
esac
}

run ydf::utils::for_each "$_elements" "$_func"

assert_success
assert_output ""
}

0 comments on commit 50f6a1d

Please sign in to comment.