浏览代码

Add unittest

check-usage-before-uninstall
Yamashita Yuu 10 年前
父节点
当前提交
dcd3b12a97
共有 6 个文件被更改,包括 507 次插入0 次删除
  1. +33
    -0
      test/hooks.bats
  2. +45
    -0
      test/installer.bats
  3. +128
    -0
      test/pyvenv.bats
  4. +109
    -0
      test/stubs/stub
  5. +98
    -0
      test/test_helper.bash
  6. +94
    -0
      test/virtualenv.bats

+ 33
- 0
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" <<OUT
before_virtualenv 'echo before: \$VIRTUALENV_PATH'
after_virtualenv 'echo after: \$STATUS'
OUT
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 '$HOOK_PATH'/virtualenv.bash"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-rehash "echo rehashed"
mkdir -p "${PYENV_ROOT}/versions/3.2.1"
run pyenv-virtualenv "3.2.1" venv
assert_success
assert_output <<-OUT
before: ${PYENV_ROOT}/versions/venv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
after: 0
rehashed
OUT
}

+ 45
- 0
test/installer.bats 查看文件

@ -0,0 +1,45 @@
#!/usr/bin/env bats
load test_helper
@test "installs pyenv-virtualenv into PREFIX" {
cd "$TMP"
PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
cd usr
assert [ -x bin/pyenv-sh-activate ]
assert [ -x bin/pyenv-sh-deactivate ]
assert [ -x bin/pyenv-virtualenv ]
assert [ -x bin/pyenv-virtualenv-prefix ]
assert [ -x bin/pyenv-virtualenvs ]
}
@test "overwrites old installation" {
cd "$TMP"
mkdir -p bin
touch bin/pyenv-virtualenv
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -x bin/pyenv-virtualenv ]
run grep "virtualenv" bin/pyenv-virtualenv
assert_success
}
@test "unrelated files are untouched" {
cd "$TMP"
mkdir -p bin share/bananas
chmod g-w bin
touch bin/bananas
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -e bin/bananas ]
run ls -ld bin
assert_equal "r-x" "${output:4:3}"
}

+ 128
- 0
test/pyvenv.bats 查看文件

@ -0,0 +1,128 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
stub_pyenv() {
export PYENV_VERSION="$1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/\${PYENV_VERSION}'"
stub pyenv-hooks "virtualenv : echo"
stub pyenv-rehash " : echo rehashed"
}
unstub_pyenv() {
unset PYENV_VERSION
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-hooks
unstub pyenv-rehash
}
create_executable() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
touch "${PYENV_ROOT}/versions/$1/bin/$2"
chmod +x "${PYENV_ROOT}/versions/$1/bin/$2"
}
remove_executable() {
rm -f "${PYENV_ROOT}/versions/$1/bin/$2"
}
@test "use pyvenv if virtualenv is not available" {
stub_pyenv "3.4.0"
stub pyenv-which "virtualenv : false" \
"pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv'"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" \
"bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip3.4\";echo PYENV_VERSION=\${PYENV_VERSION} ensurepip" \
"echo pip3.4"
stub pyenv-prefix "venv : echo '${PYENV_ROOT}/versions/venv'"
run pyenv-virtualenv venv
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv ensurepip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
}
@test "not use pyvenv if virtualenv is available" {
stub_pyenv "3.4.0"
stub pyenv-which "virtualenv : echo '${PYENV_ROOT}/versions/bin/virtualenv'" \
"pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
run pyenv-virtualenv venv
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
}
@test "install virtualenv if pyvenv is not avaialble" {
stub_pyenv "3.2.1"
stub pyenv-which "virtualenv : false" \
"pyvenv : false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" \
"echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
run pyenv-virtualenv venv
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 pip install virtualenv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
}
@test "install pip without using ensurepip" {
stub_pyenv "3.3.0"
stub pyenv-which "virtualenv : false" \
"pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv'" \
"pip : echo no pip; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" \
"echo PYENV_VERSION=\${PYENV_VERSION} no ensurepip; false" \
"echo PYENV_VERSION=\${PYENV_VERSION} no setuptools; false" \
"echo PYENV_VERSION=\${PYENV_VERSION} setuptools" \
"bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip\";echo PYENV_VERSION=\${PYENV_VERSION} pip"
stub curl "echo ez_setup.py" \
"echo get_pip.py"
run pyenv-virtualenv venv
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.3.0 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv no ensurepip
PYENV_VERSION=venv setuptools
PYENV_VERSION=venv pip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
}

+ 109
- 0
test/stubs/stub 查看文件

@ -0,0 +1,109 @@
#!/usr/bin/env bash
set -e
status=0
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
if [ -n "${!_STUB_DEBUG}" ]; then
echo "$program" "$@" >&${!_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

+ 98
- 0
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
}
}

+ 94
- 0
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 <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
}
@test "create virtualenv from current version" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
run pyenv-virtualenv venv
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
}
@test "create virtualenv with short options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
run pyenv-virtualenv -v -p python venv
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
}
@test "create virtualenv with long options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
run pyenv-virtualenv --verbose --python=python venv
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
}

正在加载...
取消
保存