#!/usr/bin/env bats load helpers setup() { setup_fake_home source "$REPO/lib.sh" } @test "expand_vars replaces only HOME, HAMPACK_DIR and USER" { run expand_vars 'python $HOME/x.pyz; $HAMPACK_DIR/icon.png $USER' [ "$output" = "python $HOME/x.pyz; $REPO/icon.png $USER" ] } @test "expand_vars keeps semicolons, quotes and spacing" { run expand_vars "HamRadio;Utility;" [ "$output" = "HamRadio;Utility;" ] run expand_vars "it's (two spaces) & \$OTHER" [ "$output" = "it's (two spaces) & \$OTHER" ] } collect() { SEEN+=("${CONF[_name]}:${CONF[version]}:${CONF[url]}"); } @test "parse_conf calls back once per section, including the last, skipping comments" { cat > "$BATS_TEST_TMPDIR/t.conf" << 'EOF' # a comment [one] version=1 url=https://a/b?c=d=e # an indented comment [two] version=2 EOF SEEN=() parse_conf "$BATS_TEST_TMPDIR/t.conf" collect [ "${#SEEN[@]}" -eq 2 ] [ "${SEEN[0]}" = "one:1:https://a/b?c=d=e" ] [ "${SEEN[1]}" = "two:2:" ] } uses_regex() { [[ "x" =~ (x) ]]; SEEN+=("${CONF[_name]}"); } @test "parse_conf section names survive callbacks that use =~" { printf '[a]\n[b]\n[c]\n' > "$BATS_TEST_TMPDIR/t.conf" SEEN=() parse_conf "$BATS_TEST_TMPDIR/t.conf" uses_regex [ "${SEEN[*]}" = "a b c" ] } swallow_stdin() { cat > /dev/null; SEEN+=("${CONF[_name]}"); } @test "parse_conf callbacks can't swallow the rest of the file through stdin" { printf '[a]\nx=1\n[b]\nx=2\n[c]\nx=3\n' > "$BATS_TEST_TMPDIR/t.conf" SEEN=() parse_conf "$BATS_TEST_TMPDIR/t.conf" swallow_stdin < /dev/null [ "${SEEN[*]}" = "a b c" ] } @test "versions are saved, replaced and forgotten by exact name" { save_installed_version dx.py abc save_installed_version dxApy other save_installed_version dx.py def [ "$(get_recorded_version dx.py)" = "def" ] [ "$(get_recorded_version dxApy)" = "other" ] [ "$(grep -c '^dx\.py=' "$VERSION_FILE")" -eq 1 ] forget_version dx.py [ -z "$(get_recorded_version dx.py)" ] [ "$(get_recorded_version dxApy)" = "other" ] } @test "get_recorded_version prints nothing without a version file" { run get_recorded_version anything [ "$status" -eq 0 ] [ -z "$output" ] } @test "record_installed keeps a sorted list without duplicates" { record_installed installed-packages b a record_installed installed-packages a c record_installed installed-packages [ "$(recorded_installs installed-packages | tr '\n' ' ')" = "a b c " ] } @test "install_staged copies home files as the user, uses sudo outside home, and writes a manifest" { local stage="$BATS_TEST_TMPDIR/stage" mkdir -p "$stage$HOME/.local/bin" "$stage/etc/udev/rules.d" echo new > "$stage$HOME/.local/bin/tool" echo rule > "$stage/etc/udev/rules.d/99-test.rules" echo old > "$HOME/.local/bin/tool" HAMPACK_APP=testapp install_staged "$stage" [ "$(cat "$HOME/.local/bin/tool")" = "new" ] grep -qx "sudo cp -P --remove-destination $stage/etc/udev/rules.d/99-test.rules /etc/udev/rules.d/99-test.rules" "$STUB_LOG" grep -qx "$HOME/.local/bin/tool" "$HAMPACK_STATE/manifests/testapp" grep -qx "/etc/udev/rules.d/99-test.rules" "$HAMPACK_STATE/manifests/testapp" } @test "install_staged needs HAMPACK_APP" { mkdir -p "$BATS_TEST_TMPDIR/stage" unset HAMPACK_APP run install_staged "$BATS_TEST_TMPDIR/stage" [ "$status" -ne 0 ] }