From 91e46f80deeca91189a4f90148601ddb877a9cc1 Mon Sep 17 00:00:00 2001 From: macayu17 Date: Thu, 2 Jul 2026 15:11:05 +0530 Subject: [PATCH] pyenv-binary: test the `save` subcommand Cover argument validation, the version-name guard, and packaging an installed version into an archive with matching metadata. --- plugins/pyenv-binary/test/save.bats | 51 ++++++++++++++++ plugins/pyenv-binary/test/test_helper.bash | 68 ++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 plugins/pyenv-binary/test/save.bats create mode 100644 plugins/pyenv-binary/test/test_helper.bash diff --git a/plugins/pyenv-binary/test/save.bats b/plugins/pyenv-binary/test/save.bats new file mode 100644 index 00000000..b2986d8e --- /dev/null +++ b/plugins/pyenv-binary/test/save.bats @@ -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 []" +} + +@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" +} diff --git a/plugins/pyenv-binary/test/test_helper.bash b/plugins/pyenv-binary/test/test_helper.bash new file mode 100644 index 00000000..2bc03df8 --- /dev/null +++ b/plugins/pyenv-binary/test/test_helper.bash @@ -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 + } +}