Skip to content

Commit

Permalink
Allow default python binary for mkvenv to be set with AUTOSWITCH_DEFA…
Browse files Browse the repository at this point in the history
…ULT_PYTHON
  • Loading branch information
MichaelAquilina committed Jan 8, 2019
1 parent 6928197 commit 5270508
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ export ``AUTOSWITCH_DEFAULTENV`` in your ``.zshrc`` file.
export AUTOSWITCH_DEFAULTENV="mydefaultenv"
antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv

**Setting a default python binary**

You may specify a default python binary to use when creating virtualenvs
by setting the value of ``AUTOSWITCH_DEFAULT_PYTHON``.

::

export AUTOSWITCH_DEFAULT_PYTHON="/usr/bin/python3"

You may still override this default as usual by passing the --python parameter to
the mkvenv command.

**Set verbosity when changing environments**

You can prevent verbose messages from being displayed when moving
Expand Down
13 changes: 10 additions & 3 deletions autoswitch_virtualenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,17 @@ function mkvenv()

printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"

if [[ ${@[(ie)--verbose]} -eq ${#@} ]]; then
virtualenv $@ "$(_virtual_env_dir)/$venv_name"
# Copy parameters variable so that we can mutate it
params=("${@[@]}")

if [[ -n "$AUTOSWITCH_DEFAULT_PYTHON" && ${params[(I)--python*]} -eq 0 ]]; then
params+="--python=$AUTOSWITCH_DEFAULT_PYTHON"
fi

if [[ ${params[(I)--verbose]} -eq 0 ]]; then
virtualenv $params "$(_virtual_env_dir)/$venv_name"
else
virtualenv $@ "$(_virtual_env_dir)/$venv_name" > /dev/null
virtualenv $params "$(_virtual_env_dir)/$venv_name" > /dev/null
fi

printf "$venv_name\n" > ".venv"
Expand Down
47 changes: 47 additions & 0 deletions tests/test_mkvenv.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,53 @@

assert $status equals 0
assert "$TARGET/myproject/.venv" exists
# assert "$lines[1]" same_as "Creating \e[35mmyproject\e[0m virtualenv"

run cat "$TARGET/myproject/.venv"

assert $status equals 0
assert "$output" same_as "myproject"
}

@test 'mkvenv - uses default python if set and not specified' {
# mock virtualenv function to test its inputs
function virtualenv {
echo virtualenv $@
}

mkdir myproject
cd myproject
AUTOSWITCH_DEFAULT_PYTHON="python_foo"

run mkvenv

assert $status equals 0
assert "$TARGET/myproject/.venv" exists
# Assert mock output
assert "$lines[2]" same_as "virtualenv --python=python_foo $HOME/.virtualenvs/myproject"

run cat "$TARGET/myproject/.venv"

assert $status equals 0
assert "$output" same_as "myproject"
}

@test 'mkvenv - uses specified python if default set' {
# mock virtualenv function to test its inputs
function virtualenv {
echo virtualenv $@
}

mkdir myproject
cd myproject
AUTOSWITCH_DEFAULT_PYTHON="python_foo"

run mkvenv --python=python_bar

assert $status equals 0
assert "$TARGET/myproject/.venv" exists
# Assert mock output
assert "$lines[2]" same_as "virtualenv --python=python_bar $HOME/.virtualenvs/myproject"

run cat "$TARGET/myproject/.venv"

Expand Down

0 comments on commit 5270508

Please sign in to comment.