Skip to content

Commit

Permalink
Merge branch 'apt_add' into 'main'
Browse files Browse the repository at this point in the history
Add apt_add to add deb repository

See merge request xebis/shellib!24
  • Loading branch information
bruzina committed Jan 7, 2022
2 parents c4ff3d2 + f4b22f0 commit 7fddc12
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The goal of the library is to have a small yet useful set of functions. It is **
- `npm` for npm packages by `npm`
- `snap` for Snap packages
- `curl2bash` for package specification `command=URL` - if command exists then the package is marked as installed, if doesn't it is installed source by `curl -s "$URL" | bash`
- `apt_add 'repository specification' 'repository_key_URL'`: Add apt repository by apt, equivalent to `curl -fsSL 'repository_key_URL' | apt-key add - && apt-add-repository 'repository specification' && apt-get update`

### Constants

Expand Down
48 changes: 47 additions & 1 deletion src/packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ function apt_install() {
fi
}

# Add apt repository by apt
# $1 ... Repository specification
# $2 ... Repository key URL
# Stderr: events
# Status: "$status_ok" on success, one of "$status_*" otherwise
# Side effects: repository is added
function apt_add() {
local repository="${1:-}"
local key="${2:-}"

if [ -z "$repository" ]; then
err 'Missing repository specification'
return "$status_err"
fi

if [ -z "$key" ]; then
err 'Missing repository key URL'
return "$status_err"
fi

if ! apt-add-repository -h &>/dev/null || ! apt-key -h &>/dev/null; then
err 'apt-add-repository or apt-key not found'
return "$status_err"
fi

if is_root; then
info "deb repository '$repository' adding" "$symbol_doing"
if curl -fsSL "$key" | apt-key add -; then
if apt-add-repository "$repository"; then
info "deb repository '$repository' added" "$symbol_done"
else
err "deb repository '$repository' adding failed" "$symbol_failed"
return "$status_err"
fi
else
err "deb repository key URL '$key' adding failed" "$symbol_failed"
return "$status_err"
fi
else
info "deb repository '$repository' is not added" "$symbol_todo"
warn "deb repository '$repository' could be added by root only"
info 'Try again as root' "$symbol_tip"
return "$status_err"
fi
}

# Install Python package by pip
# $1 ... Package name
# Stderr: events
Expand Down Expand Up @@ -217,7 +263,7 @@ function curl2bash_install() {
fi
else
info "Package '$command' is not installed" "$symbol_todo"
warn "Package '$command' should be installed by root only"
warn "Package '$command' could be installed by root only"
info 'Try again as root' "$symbol_tip"
return "$status_err"
fi
Expand Down
167 changes: 166 additions & 1 deletion tests/packages.sh.bats
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,171 @@ setup() {
assert_line -n 3 "scripts/test 🗹 deb package 'package' installed"
}

@test 'src/packages.sh apt_add without parameters test' {
run apt_add

assert_failure
assert_output 'scripts/test ✗ Missing repository specification'
}

@test 'src/packages.sh apt_add with one parameter test' {
run apt_add 'deb [arch=amd64] https://example.com nocturnal main'

assert_failure
assert_output 'scripts/test ✗ Missing repository key URL'
}

@test 'src/packages.sh apt_add without apt-add-repository test' {
function apt-add-repository() {
return 1
}
export -f apt-add-repository

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/gpg'

assert_failure
assert_output 'scripts/test ✗ apt-add-repository or apt-key not found'
}

@test 'src/packages.sh apt_add without apt-key test' {
function apt-add-repository() {
echo 'Help'
}
export -f apt-add-repository

function apt-key() {
return 1
}
export -f apt-key

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/gpg'

assert_failure
assert_output 'scripts/test ✗ apt-add-repository or apt-key not found'
}

@test 'src/packages.sh apt_add repository as non-root test' {
function apt-add-repository() {
echo 'Help'
}
export -f apt-add-repository

function apt-key() {
echo 'Help'
}
export -f apt-key

function is_root() {
return 1
}
export -f is_root

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/gpg'

assert_failure
assert_line -n 0 "scripts/test ☐ deb repository 'deb [arch=amd64] https://example.com nocturnal main' is not added"
assert_line -n 1 "scripts/test ⚠ deb repository 'deb [arch=amd64] https://example.com nocturnal main' could be added by root only"
assert_line -n 2 "scripts/test 💡 Try again as root"
}

@test 'src/packages.sh apt_add repository key adding fail test' {
function apt-add-repository() {
echo 'Help'
}
export -f apt-add-repository

function apt-key() {
case "$1" in
-h) echo 'Help' ;;
add)
echo "No key at '$2'"
return 1
;;
esac
}
export -f apt-key

function is_root() {
return 0
}
export -f is_root

function curl() {
return 1
}
export -f curl

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/no-key'

assert_failure
assert_line -n 0 "scripts/test … deb repository 'deb [arch=amd64] https://example.com nocturnal main' adding"
assert_line -n 1 "No key at '-'"
assert_line -n 2 "scripts/test ☒ deb repository key URL 'https://example.com/no-key' adding failed"
}

@test 'src/packages.sh apt_add repository adding fail test' {
function apt-add-repository() {
case "$1" in
-h) echo ;;
*)
echo "Error adding '$1'"
return 1
;;
esac
}
export -f apt-add-repository

function apt-key() {
echo
}
export -f apt-key

function is_root() {
return 0
}
export -f is_root

function curl() {
return 0
}
export -f curl

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/gpg'

assert_failure
assert_line -n 0 "scripts/test … deb repository 'deb [arch=amd64] https://example.com nocturnal main' adding"
assert_line -n 1 "Error adding 'deb [arch=amd64] https://example.com nocturnal main'"
assert_line -n 2 "scripts/test ☒ deb repository 'deb [arch=amd64] https://example.com nocturnal main' adding failed"
}

@test 'src/packages.sh apt_add package installation success test' {
function apt-add-repository() {
echo
}
export -f apt-add-repository

function apt-key() {
echo
}
export -f apt-key

function is_root() {
return 0
}
export -f is_root

function curl() {
return 0
}
export -f curl

run apt_add 'deb [arch=amd64] https://example.com nocturnal main' 'https://example.com/gpg'

assert_success
assert_line -n 0 "scripts/test … deb repository 'deb [arch=amd64] https://example.com nocturnal main' adding"
assert_line -n 1 "scripts/test 🗹 deb repository 'deb [arch=amd64] https://example.com nocturnal main' added"
}

@test 'src/packages.sh pip_install without parameters test' {
run pip_install

Expand Down Expand Up @@ -563,7 +728,7 @@ setup() {

assert_failure
assert_line -n 0 "scripts/test ☐ Package 'nonsense' is not installed"
assert_line -n 1 "scripts/test ⚠ Package 'nonsense' should be installed by root only"
assert_line -n 1 "scripts/test ⚠ Package 'nonsense' could be installed by root only"
assert_line -n 2 "scripts/test 💡 Try again as root"
}

Expand Down

0 comments on commit 7fddc12

Please sign in to comment.