pyenv-binary: test the save subcommand

Cover argument validation, the version-name guard, and packaging an
installed version into an archive with matching metadata.
This commit is contained in:
macayu17 2026-07-02 15:11:05 +05:30
parent f0ac849b16
commit 91e46f80de
2 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,51 @@
#!/usr/bin/env bats
load test_helper
create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
}
platform() {
echo "$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)"
}
@test "fails with no version given" {
run pyenv-binary-save
assert_failure "Usage: pyenv binary save <version> [<output-dir>]"
}
@test "fails for a version that is not installed" {
run pyenv-binary-save 9.9.9
assert_failure "pyenv-binary: version \`9.9.9' is not installed"
}
@test "rejects a version name containing a slash" {
run pyenv-binary-save "foo/bar"
assert_failure "pyenv-binary: invalid version name \`foo/bar'"
}
@test "rejects a version name that walks out of versions/" {
run pyenv-binary-save "../../etc"
assert_failure "pyenv-binary: invalid version name \`../../etc'"
}
@test "packages an installed version" {
create_version "3.12.7"
local out="${BATS_TEST_TMPDIR}/dist"
run pyenv-binary-save "3.12.7" "$out"
assert_success "Saved 3.12.7-$(platform).tar.gz and 3.12.7-$(platform).meta to $out"
assert [ -f "${out}/3.12.7-$(platform).tar.gz" ]
assert [ -f "${out}/3.12.7-$(platform).meta" ]
}
@test "records the platform in the metadata" {
create_version "3.12.7"
local out="${BATS_TEST_TMPDIR}/dist"
pyenv-binary-save "3.12.7" "$out" >/dev/null
run cat "${out}/3.12.7-$(platform).meta"
assert_success
assert_output_contains "version=3.12.7"
assert_output_contains "platform=$(platform)"
assert_output_contains "archive=3.12.7-$(platform).tar.gz"
}

View file

@ -0,0 +1,68 @@
setup() {
export PYENV_ROOT="${BATS_TEST_TMPDIR}/root"
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH"
export PATH
# If test specific setup exist, run it
if [[ $(type -t _setup) == function ]]; then
_setup
fi
}
flunk() {
{ if [ "$#" -eq 0 ]; then cat -
else echo "$@"
fi
} | sed "s:${BATS_TEST_TMPDIR}:\${BATS_TEST_TMPDIR}:g" >&2
return 1
}
assert() {
if ! "$@"; then
flunk "failed: $@"
fi
}
assert_success() {
if [ "$status" -ne 0 ]; then
{ echo "command failed with exit status $status"
echo "output: $output"
} | flunk
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_failure() {
if [ "$status" -eq 0 ]; then
flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_equal() {
if [ "$1" != "$2" ]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_output_contains() {
local expected="$1"
echo "$output" | grep -F "$expected" >/dev/null || {
{ echo "expected output to contain: $expected"
echo "actual: $output"
} | flunk
}
}