@ -0,0 +1,28 @@ | |||
#!/usr/bin/env bash | |||
# | |||
# Summary: Detect the file that sets the current Python virtual environment | |||
# | |||
# Usage: pyenv virtualenv-file | |||
set -e | |||
[ -n "$PYENV_DEBUG" ] && set -x | |||
find_local_virtualenv_file() { | |||
local root="$1" | |||
while [ -n "$root" ]; do | |||
if [ -e "${root}/.python-venv" ]; then | |||
echo "${root}/.python-venv" | |||
exit | |||
elif [ -e "${root}/.pyenv-venv" ]; then | |||
echo "${root}/.pyenv-venv" | |||
exit | |||
fi | |||
[ "${root}" = "${root%/*}" ] && break | |||
root="${root%/*}" | |||
done | |||
} | |||
find_local_virtualenv_file "$PYENV_DIR" | |||
[ "$PYENV_DIR" = "$PWD" ] || find_local_virtualenv_file "$PWD" | |||
exit 1 |
@ -0,0 +1,24 @@ | |||
#!/usr/bin/env bash | |||
# | |||
# Usage: pyenv virtualenv-file-read <file> | |||
set -e | |||
[ -n "$PYENV_DEBUG" ] && set -x | |||
VIRTUALENV_FILE="$1" | |||
if [ -e "$VIRTUALENV_FILE" ]; then | |||
# Read the first non-whitespace word from the specified version file. | |||
# Be careful not to load it whole in case there's something crazy in it. | |||
IFS="${IFS}"$'\r' | |||
words=( $(cut -b 1-1024 "$VIRTUALENV_FILE" | $(type -p gawk awk | head -1) '{ print($1) }') ) | |||
virtualenvs=("${words[@]}") | |||
if [ -n "$virtualenvs" ]; then | |||
IFS=":" | |||
echo "${virtualenvs[*]}" | |||
exit | |||
fi | |||
fi | |||
exit 1 |
@ -0,0 +1,25 @@ | |||
#!/usr/bin/env bash | |||
# | |||
# Usage: pyenv virtualenv-file-write <file> <venv-name> | |||
set -e | |||
[ -n "$PYENV_DEBUG" ] && set -x | |||
PYENV_VIRTUALENV_FILE="$1" | |||
shift || true | |||
virtualenvs=("$@") | |||
if [ -z "$virtualenvs" ] || [ -z "$PYENV_VIRTUALENV_FILE" ]; then | |||
pyenv-help --usage virtualenv-file-write >&2 | |||
exit 1 | |||
fi | |||
# Make sure the specified virtual environment is created | |||
pyenv-virtualenv-prefix "${virtualenvs[@]}" >/dev/null | |||
# Write the virtual environment name out to disk. | |||
# Create an empty file. Using "rm" might cause a permission error. | |||
> "$PYENV_VIRTUALENV_FILE" | |||
for virtualenv in "${virtualenvs[@]}"; do | |||
echo "$virtualenv" >> "$PYENV_VIRTUALENV_FILE" | |||
done |
@ -0,0 +1,43 @@ | |||
#!/usr/bin/env bash | |||
# | |||
# Summary: Show the current Python virtual environment | |||
# | |||
# Usage: pyenv virtualenv-name | |||
set -e | |||
[ -n "$PYENV_DEBUG" ] && set -x | |||
if [ -z "$PYENV_VIRTUALENV" ]; then | |||
PYENV_VIRTUALENV_FILE="$(pyenv-virtualenv-file)" | |||
PYENV_VIRTUALENV="$(pyenv-virtualenv-file-read "$PYENV_VIRTUALENV_FILE" || true)" | |||
fi | |||
if [ -z "$PYENV_VIRTUALENV" ]; then | |||
exit 1 | |||
fi | |||
virtualenv_exists() { | |||
local virtualenv="$1" | |||
PREFIX="${PYENV_ROOT}/versions/${virtualenv}" | |||
[ -f "${PREFIX}/bin/activate" ] && [ ! -e "${PREFIX}/bin/conda" ] | |||
} | |||
virtualenvs=() | |||
OLDIFS="$IFS" | |||
{ IFS=: | |||
for virtualenv in ${PYENV_VIRTUALENV}; do | |||
if virtualenv_exists "$virtualenv"; then | |||
virtualenvs=("${virtualenvs[@]}" "${virtualenv}") | |||
else | |||
echo "pyenv-virtualenv: version \`$virtualenv' is not a virtualenv" >&2 | |||
exit 1 | |||
fi | |||
done | |||
} | |||
IFS="$OLDIFS" | |||
OLDIFS="$IFS" | |||
{ IFS=: | |||
echo "${virtualenvs[*]}" | |||
} | |||
IFS="$OLDIFS" |
@ -0,0 +1,14 @@ | |||
#!/usr/bin/env bash | |||
# | |||
# Summary: Explain how the current Python virtual environment is set | |||
# | |||
# Usage: pyenv virtualenv-origin | |||
set -e | |||
[ -n "$PYENV_DEBUG" ] && set -x | |||
if [ -n "$PYENV_VIRTUALENV" ]; then | |||
echo "PYENV_VIRTUALENV environment variable" | |||
else | |||
pyenv-virtualenv-file | |||
fi |
@ -0,0 +1,71 @@ | |||
#!/usr/bin/env bats | |||
load test_helper | |||
setup() { | |||
mkdir -p "${PYENV_VIRTUALENV_TEST_DIR}/myproject" | |||
cd "${PYENV_VIRTUALENV_TEST_DIR}/myproject" | |||
} | |||
@test "fails without arguments" { | |||
run pyenv-virtualenv-file-read | |||
assert_failure "" | |||
} | |||
@test "fails for invalid file" { | |||
run pyenv-virtualenv-file-read "non-existent" | |||
assert_failure "" | |||
} | |||
@test "fails for blank file" { | |||
echo > my-venv | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_failure "" | |||
} | |||
@test "reads simple venv file" { | |||
cat > my-venv <<<"foo" | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} | |||
@test "ignores leading spaces" { | |||
cat > my-venv <<<" foo" | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} | |||
@test "reads only the first word from file" { | |||
cat > my-venv <<<"foo bar baz" | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} | |||
@test "loads *not* only the first line in file" { | |||
cat > my-venv <<IN | |||
foo bar | |||
baz qux | |||
IN | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo:baz" | |||
} | |||
@test "ignores leading blank lines" { | |||
cat > my-venv <<IN | |||
foo | |||
IN | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} | |||
@test "handles the file with no trailing newline" { | |||
echo -n "foo" > my-venv | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} | |||
@test "ignores carriage returns" { | |||
cat > my-venv <<< $'foo\r' | |||
run pyenv-virtualenv-file-read my-venv | |||
assert_success "foo" | |||
} |
@ -0,0 +1,49 @@ | |||
#!/usr/bin/env bats | |||
load test_helper | |||
setup() { | |||
export PYENV_ROOT="${TMP}/pyenv" | |||
mkdir -p "$PYENV_VIRTUALENV_TEST_DIR" | |||
cd "$PYENV_VIRTUALENV_TEST_DIR" | |||
} | |||
create_virtualenv() { | |||
mkdir -p "${PYENV_ROOT}/versions/$1/bin" | |||
touch "${PYENV_ROOT}/versions/$1/bin/activate" | |||
} | |||
@test "invocation without 2 arguments prints usage" { | |||
stub pyenv-help "echo \"Usage: pyenv virtualenv-file-write <file> <venv-name>\"" | |||
run pyenv-virtualenv-file-write | |||
unstub pyenv-help | |||
assert_failure "Usage: pyenv virtualenv-file-write <file> <venv-name>" | |||
run pyenv-virtualenv-file-write "one" "" | |||
assert_failure | |||
} | |||
@test "setting nonexistent virtualenv fails" { | |||
assert [ ! -e ".python-venv" ] | |||
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" | |||
run pyenv-virtualenv-file-write ".python-venv" "venv" | |||
unstub pyenv-prefix | |||
assert_failure "pyenv-virtualenv: version \`venv' is not a virtualenv" | |||
assert [ ! -e ".python-venv" ] | |||
} | |||
@test "writes value to arbitrary file" { | |||
create_virtualenv "venv" | |||
assert [ ! -e "my-venv" ] | |||
stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" | |||
run pyenv-virtualenv-file-write "${PWD}/my-venv" "venv" | |||
unstub pyenv-prefix | |||
assert_success "" | |||
assert [ "$(cat my-venv)" = "venv" ] | |||
} |
@ -0,0 +1,72 @@ | |||
#!/usr/bin/env bats | |||
load test_helper | |||
setup() { | |||
mkdir -p "$PYENV_VIRTUALENV_TEST_DIR" | |||
cd "$PYENV_VIRTUALENV_TEST_DIR" | |||
} | |||
create_file() { | |||
mkdir -p "$(dirname "$1")" | |||
touch "$1" | |||
} | |||
@test "in current directory" { | |||
create_file ".python-venv" | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/.python-venv" | |||
} | |||
@test "alternate file in current directory" { | |||
create_file ".pyenv-venv" | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/.pyenv-venv" | |||
} | |||
@test ".python-venv has precedence over alternate file" { | |||
create_file ".python-venv" | |||
create_file ".pyenv-venv" | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/.python-venv" | |||
} | |||
@test "in parent directory" { | |||
create_file ".python-venv" | |||
mkdir -p project | |||
cd project | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/.python-venv" | |||
} | |||
@test "topmost file has precedence" { | |||
create_file ".python-venv" | |||
create_file "project/.python-venv" | |||
cd project | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/project/.python-venv" | |||
} | |||
@test "alternate file has precedence if higher" { | |||
create_file ".python-venv" | |||
create_file "project/.pyenv-venv" | |||
cd project | |||
run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/project/.pyenv-venv" | |||
} | |||
@test "PYENV_DIR has precedence over PWD" { | |||
create_file "widget/.python-venv" | |||
create_file "project/.python-venv" | |||
cd project | |||
PYENV_DIR="${PYENV_VIRTUALENV_TEST_DIR}/widget" run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/widget/.python-venv" | |||
} | |||
@test "PWD is searched if PYENV_DIR yields no results" { | |||
mkdir -p "widget/blank" | |||
create_file "project/.python-venv" | |||
cd project | |||
PYENV_DIR="${PYENV_VIRTUALENV_TEST_DIR}/widget/blank" run pyenv-virtualenv-file | |||
assert_success "${PYENV_VIRTUALENV_TEST_DIR}/project/.python-venv" | |||
} |
@ -0,0 +1,44 @@ | |||
#!/usr/bin/env bats | |||
load test_helper | |||
setup() { | |||
export PYENV_ROOT="${TMP}/pyenv" | |||
mkdir -p "${PYENV_VIRTUALENV_TEST_DIR}/myproject" | |||
cd "${PYENV_VIRTUALENV_TEST_DIR}/myproject" | |||
} | |||
create_virtualenv() { | |||
mkdir -p "${PYENV_ROOT}/versions/$1/bin" | |||
touch "${PYENV_ROOT}/versions/$1/bin/activate" | |||
} | |||
@test "no virtualenv selected" { | |||
assert [ ! -d "${PYENV_ROOT}/versions" ] | |||
run pyenv-virtualenv-name | |||
assert_failure "" | |||
} | |||
@test "PYENV_VIRTUALENV has precedence over local" { | |||
create_virtualenv "foo" | |||
create_virtualenv "bar" | |||
cat > ".python-venv" <<<"foo" | |||
run pyenv-virtualenv-name | |||
assert_success "foo" | |||
PYENV_VIRTUALENV=bar run pyenv-virtualenv-name | |||
assert_success "bar" | |||
} | |||
@test "should fail if the virtualenv is the system" { | |||
PYENV_VIRTUALENV=system run pyenv-virtualenv-name | |||
assert_failure "pyenv-virtualenv: version \`system' is not a virtualenv" | |||
} | |||
@test "missing virtualenv" { | |||
PYENV_VIRTUALENV=foo run pyenv-virtualenv-name | |||
assert_failure "pyenv-virtualenv: version \`foo' is not a virtualenv" | |||
} |
@ -0,0 +1,25 @@ | |||
#!/usr/bin/env bats | |||
load test_helper | |||
setup() { | |||
mkdir -p "$PYENV_VIRTUALENV_TEST_DIR" | |||
cd "$PYENV_VIRTUALENV_TEST_DIR" | |||
} | |||
@test "detects PYENV_VIRTUALENV" { | |||
PYENV_VIRTUALENV=1 run pyenv-virtualenv-origin | |||
assert_success "PYENV_VIRTUALENV environment variable" | |||
} | |||
@test "detects local file" { | |||
touch .python-venv | |||
run pyenv-virtualenv-origin | |||
assert_success "${PWD}/.python-venv" | |||
} | |||
@test "detects alternate file" { | |||
touch .pyenv-venv | |||
run pyenv-virtualenv-origin | |||
assert_success "${PWD}/.pyenv-venv" | |||
} |