From dcd3b12a97af02746ed8fcef029bdac09502e6d9 Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Sat, 18 Jan 2014 10:39:52 +0900 Subject: [PATCH] Add unittest --- test/hooks.bats | 33 +++++++++++ test/installer.bats | 45 +++++++++++++++ test/pyvenv.bats | 128 ++++++++++++++++++++++++++++++++++++++++++ test/stubs/stub | 109 +++++++++++++++++++++++++++++++++++ test/test_helper.bash | 98 ++++++++++++++++++++++++++++++++ test/virtualenv.bats | 94 +++++++++++++++++++++++++++++++ 6 files changed, 507 insertions(+) create mode 100644 test/hooks.bats create mode 100644 test/installer.bats create mode 100644 test/pyvenv.bats create mode 100755 test/stubs/stub create mode 100644 test/test_helper.bash create mode 100644 test/virtualenv.bats diff --git a/test/hooks.bats b/test/hooks.bats new file mode 100644 index 0000000..e96a2f8 --- /dev/null +++ b/test/hooks.bats @@ -0,0 +1,33 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + export PYENV_ROOT="${TMP}/pyenv" + export HOOK_PATH="${TMP}/i has hooks" + mkdir -p "$HOOK_PATH" +} + +@test "pyenv-virtualenv hooks" { + cat > "${HOOK_PATH}/virtualenv.bash" <&${!_STUB_DEBUG} +fi + +[ -e "${!_STUB_PLAN}" ] || exit 1 +[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run" + + +# Initialize or load the stub run information. +eval "${_STUB_INDEX}"=1 +eval "${_STUB_RESULT}"=0 +[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}" + + +# Loop over each line in the plan. +index=0 +while IFS= read -r line; do + index=$(($index + 1)) + + if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then + # We found the plan line we're interested in. + # Start off by assuming success. + result=0 + + # Split the line into an array of arguments to + # match and a command to run to produce output. + command=" $line" + if [ "$command" != "${command/ : }" ]; then + patterns="${command%% : *}" + command="${command#* : }" + fi + + # Naively split patterns by whitespace for now. + # In the future, use a sed script to split while + # respecting quoting. + set -f + patterns=($patterns) + set +f + arguments=("$@") + + # Match the expected argument patterns to actual + # arguments. + for (( i=0; i<${#patterns[@]}; i++ )); do + pattern="${patterns[$i]}" + argument="${arguments[$i]}" + + case "$argument" in + $pattern ) ;; + * ) result=1 ;; + esac + done + + # If the arguments matched, evaluate the command + # in a subshell. Otherwise, log the failure. + if [ $result -eq 0 ] ; then + set +e + ( eval "$command" ) + status="$?" + set -e + else + eval "${_STUB_RESULT}"=1 + fi + fi +done < "${!_STUB_PLAN}" + + +if [ -n "${!_STUB_END}" ]; then + # Clean up the run file. + rm -f "${!_STUB_RUN}" + + # If the number of lines in the plan is larger than + # the requested index, we failed. + if [ $index -ge "${!_STUB_INDEX}" ]; then + eval "${_STUB_RESULT}"=1 + fi + + # Return the result. + exit "${!_STUB_RESULT}" + +else + # If the requested index is larger than the number + # of lines in the plan file, we failed. + if [ "${!_STUB_INDEX}" -gt $index ]; then + eval "${_STUB_RESULT}"=1 + fi + + # Write out the run information. + { echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))" + echo "${_STUB_RESULT}=${!_STUB_RESULT}" + } > "${!_STUB_RUN}" + + exit "$status" + +fi diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 0000000..f6e9a07 --- /dev/null +++ b/test/test_helper.bash @@ -0,0 +1,98 @@ +export TMP="$BATS_TEST_DIRNAME/tmp" + +PATH=/usr/bin:/usr/sbin:/bin/:/sbin +PATH="$BATS_TEST_DIRNAME/../bin:$PATH" +PATH="$TMP/bin:$PATH" +export PATH + +teardown() { + rm -fr "$TMP"/* +} + +stub() { + local program="$1" + local prefix="$(echo "$program" | tr a-z- A-Z_)" + shift + + export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan" + export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run" + export "${prefix}_STUB_END"= + + mkdir -p "${TMP}/bin" + ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}" + + touch "${TMP}/${program}-stub-plan" + for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done +} + +unstub() { + local program="$1" + local prefix="$(echo "$program" | tr a-z- A-Z_)" + local path="${TMP}/bin/${program}" + + export "${prefix}_STUB_END"=1 + + local STATUS=0 + "$path" || STATUS="$?" + + rm -f "$path" + rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run" + return "$STATUS" +} + +assert() { + if ! "$@"; then + flunk "failed: $@" + fi +} + +flunk() { + { if [ "$#" -eq 0 ]; then cat - + else echo "$@" + fi + } | sed "s:${TMP}:\${TMP}:g" >&2 + return 1 +} + +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 + } +} diff --git a/test/virtualenv.bats b/test/virtualenv.bats new file mode 100644 index 0000000..0f6d098 --- /dev/null +++ b/test/virtualenv.bats @@ -0,0 +1,94 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + export PYENV_ROOT="${TMP}/pyenv" +} + +stub_pyenv() { + export PYENV_VERSION="$1" + stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/\${PYENV_VERSION}'" + stub pyenv-which "virtualenv : echo '${PYENV_ROOT}/versions/bin/virtualenv'" \ + "pyvenv : false" + stub pyenv-hooks "virtualenv : echo" + stub pyenv-rehash " : echo rehashed" +} + +unstub_pyenv() { + unset PYENV_VERSION + unstub pyenv-prefix + unstub pyenv-which + unstub pyenv-hooks + unstub pyenv-rehash +} + +@test "create virtualenv from given version" { + stub_pyenv "3.2.1" + stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" + + run pyenv-virtualenv "3.2.1" "venv" + + unstub_pyenv + unstub pyenv-exec + + assert_success + assert_output <