Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default python binary for mkvenv to be set with AUTOSWITCH_DEFAULT_PYTHON #73

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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``. For example:

::

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