Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Merge branch 'unittest'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashita Yuu committed Jan 20, 2014
2 parents e4aeb30 + 120d62d commit d0a82e8
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
install: git clone https://github.com/sstephenson/bats.git
script: bats/bin/bats --tap test
language: c
notifications:
email:
on_success: never
2 changes: 1 addition & 1 deletion etc/pyenv.d/exec/pip.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abs_dirname() {
cd "$cwd"
}

PYENV_PIP_REHASH_ROOT="$(abs_dirname "${BASH_SOURCE[0]}")/../../.."
PYENV_PIP_REHASH_ROOT="$(abs_dirname "$(abs_dirname "${BASH_SOURCE[0]}")/../../../..")"

if [ -x "${PYENV_PIP_REHASH_ROOT}/libexec/${PYENV_COMMAND##*/}" ]; then
PYENV_COMMAND_PATH="${PYENV_PIP_REHASH_ROOT}/libexec/${PYENV_COMMAND##*/}"
Expand Down
21 changes: 21 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
# Usage: PREFIX=/usr/local ./install.sh
#
# Installs pyenv-virtualenv under $PREFIX.

set -e

cd "$(dirname "$0")"

if [ -z "${PREFIX}" ]; then
PREFIX="/usr/local"
fi

ETC_PATH="${PREFIX}/etc/pyenv.d"
LIBEXEC_PATH="${PREFIX}/libexec"

mkdir -p "$ETC_PATH"
mkdir -p "$LIBEXEC_PATH"

cp -RPp etc/pyenv.d/* "$ETC_PATH"
install -p libexec/* "$LIBEXEC_PATH"
42 changes: 42 additions & 0 deletions test/installer.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bats

load test_helper

@test "installs pyenv-pip-rehash into PREFIX" {
cd "$TMP"
PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""

cd usr

assert [ -x libexec/pip ]
assert [ -f etc/pyenv.d/exec/pip.bash ]
}

@test "overwrites old installation" {
cd "$TMP"
mkdir -p libexec
touch libexec/pip

PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""

assert [ -x libexec/pip ]
run grep "pyenv-rehash" libexec/pip
assert_success
}

@test "unrelated files are untouched" {
cd "$TMP"
mkdir -p libexec share/bananas
chmod g-w libexec
touch libexec/bananas

PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""

assert [ -e libexec/bananas ]

run ls -ld libexec
assert_equal "r-x" "${output:4:3}"
}
17 changes: 17 additions & 0 deletions test/libexec/pyenv-exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e

PYENV_COMMAND="$1"
PYENV_COMMAND_PATH="$(pyenv-which "$PYENV_COMMAND")"
PYENV_BIN_PATH="${PYENV_COMMAND_PATH%/*}"

shopt -s nullglob
for script in "$(dirname "${BASH_SOURCE[0]}")/../../etc/pyenv.d/exec/"*.bash; do
source "${script}"
done
shopt -u nullglob

shift 1
echo PYENV_BIN_PATH="${PYENV_BIN_PATH}" exec -a "${PYENV_COMMAND}" "$PYENV_COMMAND_PATH" "$@"
exec -a "${PYENV_COMMAND}" "$PYENV_COMMAND_PATH" "$@"
133 changes: 133 additions & 0 deletions test/pip.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env bats

load test_helper

setup() {
export PYENV_PIP_REHASH_ROOT="$(abs_dirname "${BATS_TEST_DIRNAME}/../..")"
}

resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done

pwd
cd "$cwd"
}

@test "should not invoke rehash after pip freeze" {
stub pyenv-which "pip : echo \"${TMP}/bin/pip\"" \
"pip : echo \"${TMP}/bin/pip\""
stub pyenv-rehash "echo rehashed"
stub pip "echo \"pip \$@\""

run pyenv-exec pip freeze

assert_success
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a pip ${PYENV_PIP_REHASH_ROOT}/libexec/pip freeze
pip freeze
EOS
}

@test "should invoke rehash after successful pip install" {
stub pyenv-which "pip : echo \"${TMP}/bin/pip\"" \
"pip : echo \"${TMP}/bin/pip\""
stub pyenv-rehash "echo rehashed"
stub pip "echo \"pip \$@\""

run pyenv-exec pip install -U tornado

assert_success
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a pip ${PYENV_PIP_REHASH_ROOT}/libexec/pip install -U tornado
pip install -U tornado
rehashed
EOS
}

@test "should invoke rehash after unsuccessful pip install" {
stub pyenv-which "pip : echo \"${TMP}/bin/pip\"" \
"pip : echo \"${TMP}/bin/pip\""
stub pyenv-rehash "echo rehashed"
stub pip "echo \"pip \$@\"; false"

run pyenv-exec pip install tornado

assert_failure
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a pip ${PYENV_PIP_REHASH_ROOT}/libexec/pip install tornado
pip install tornado
EOS
}

@test "should invoke rehash after successful pip uninstall" {
stub pyenv-which "pip : echo \"${TMP}/bin/pip\"" \
"pip : echo \"${TMP}/bin/pip\""
stub pyenv-rehash "echo rehashed"
stub pip "echo \"pip \$@\""

run pyenv-exec pip uninstall --yes tornado

assert_success
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a pip ${PYENV_PIP_REHASH_ROOT}/libexec/pip uninstall --yes tornado
pip uninstall --yes tornado
rehashed
EOS
}

@test "should not invoke rehash after unsuccessful pip uninstall" {
stub pyenv-which "pip : echo \"${TMP}/bin/pip\"" \
"pip : echo \"${TMP}/bin/pip\""
stub pyenv-rehash "echo rehashed"
stub pip "echo \"pip \$@\"; false"

run pyenv-exec pip uninstall tornado

assert_failure
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a pip ${PYENV_PIP_REHASH_ROOT}/libexec/pip uninstall tornado
pip uninstall tornado
EOS
}

@test "should invoke rehash after successful easy_install" {
stub pyenv-which "easy_install : echo \"${TMP}/bin/easy_install\"" \
"easy_install : echo \"${TMP}/bin/easy_install\""
stub pyenv-rehash "echo rehashed"
stub easy_install "echo \"easy_install \$@\""

run pyenv-exec easy_install tornado

assert_success
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a easy_install ${PYENV_PIP_REHASH_ROOT}/libexec/easy_install tornado
easy_install tornado
rehashed
EOS
}

@test "should not invoke rehash after unsuccessful easy_install" {
stub pyenv-which "easy_install : echo \"${TMP}/bin/easy_install\"" \
"easy_install : echo \"${TMP}/bin/easy_install\""
stub pyenv-rehash "echo rehashed"
stub easy_install "echo \"easy_install \$@\"; false"

run pyenv-exec easy_install -U tornado

assert_failure
assert_output <<EOS
PYENV_BIN_PATH=${PYENV_PIP_REHASH_ROOT}/libexec exec -a easy_install ${PYENV_PIP_REHASH_ROOT}/libexec/easy_install -U tornado
easy_install -U tornado
EOS
}
109 changes: 109 additions & 0 deletions test/stubs/stub
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit d0a82e8

Please sign in to comment.