c6a5c56bf5d300ac15f7337f7d566f0af169b089

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
Michael Aquilina <michaelaquilina@gmail.com>
Date

Message

Allow default python binary for mkvenv to be set with AUTOSWITCH_DEFAULT_PYTHON

Diff

  1diff --git a/README.rst b/README.rst
  2index bbfcf2d1694f98be61dcd75ea6709bffc02d1282..b694ba12fb85a6ff3d0a5251705a154fa2fd066b 100644
  3--- a/README.rst
  4+++ b/README.rst
  5@@ -235,6 +235,18 @@ export ``AUTOSWITCH_DEFAULTENV`` in your ``.zshrc`` file.
  6     export AUTOSWITCH_DEFAULTENV="mydefaultenv"
  7     antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv
  8 
  9+**Setting a default python binary**
 10+
 11+You may specify a default python binary to use when creating virtualenvs
 12+by setting the value of ``AUTOSWITCH_DEFAULT_PYTHON``. For example:
 13+
 14+::
 15+
 16+    export AUTOSWITCH_DEFAULT_PYTHON="/usr/bin/python3"
 17+
 18+You may still override this default as usual by passing the --python parameter to
 19+the mkvenv command.
 20+
 21 **Set verbosity when changing environments**
 22 
 23 You can prevent verbose messages from being displayed when moving
 24diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 25index d2207d76a95da89567762b71dcb000ec9cfdd3e0..0424fb806acc3cbf9cb68c8622c526759cb281bd 100644
 26--- a/autoswitch_virtualenv.plugin.zsh
 27+++ b/autoswitch_virtualenv.plugin.zsh
 28@@ -180,10 +180,17 @@ function mkvenv()
 29 
 30         printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
 31 
 32-        if [[ ${@[(ie)--verbose]} -eq ${#@} ]]; then
 33-            virtualenv $@ "$(_virtual_env_dir)/$venv_name"
 34+        # Copy parameters variable so that we can mutate it
 35+        params=("${@[@]}")
 36+
 37+        if [[ -n "$AUTOSWITCH_DEFAULT_PYTHON" && ${params[(I)--python*]} -eq 0 ]]; then
 38+            params+="--python=$AUTOSWITCH_DEFAULT_PYTHON"
 39+        fi
 40+
 41+        if [[ ${params[(I)--verbose]} -eq 0 ]]; then
 42+            virtualenv $params "$(_virtual_env_dir)/$venv_name"
 43         else
 44-            virtualenv $@ "$(_virtual_env_dir)/$venv_name" > /dev/null
 45+            virtualenv $params "$(_virtual_env_dir)/$venv_name" > /dev/null
 46         fi
 47 
 48         printf "$venv_name\n" > ".venv"
 49diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
 50index 6a5c384a115529fdf9e2d1c2079bde72cfa3187c..6a5854c1791ab21b517d9315f7756062eb1bdfb8 100644
 51--- a/tests/test_mkvenv.zunit
 52+++ b/tests/test_mkvenv.zunit
 53@@ -30,6 +30,53 @@
 54 
 55     assert $status equals 0
 56     assert "$TARGET/myproject/.venv" exists
 57+    # assert "$lines[1]" same_as "Creating \e[35mmyproject\e[0m virtualenv"
 58+
 59+    run cat "$TARGET/myproject/.venv"
 60+
 61+    assert $status equals 0
 62+    assert "$output" same_as "myproject"
 63+}
 64+
 65+@test 'mkvenv - uses default python if set and not specified' {
 66+    # mock virtualenv function to test its inputs
 67+    function virtualenv {
 68+        echo virtualenv $@
 69+    }
 70+
 71+    mkdir myproject
 72+    cd myproject
 73+    AUTOSWITCH_DEFAULT_PYTHON="python_foo"
 74+
 75+    run mkvenv
 76+
 77+    assert $status equals 0
 78+    assert "$TARGET/myproject/.venv" exists
 79+    # Assert mock output
 80+    assert "$lines[2]" same_as "virtualenv --python=python_foo $HOME/.virtualenvs/myproject"
 81+
 82+    run cat "$TARGET/myproject/.venv"
 83+
 84+    assert $status equals 0
 85+    assert "$output" same_as "myproject"
 86+}
 87+
 88+@test 'mkvenv - uses specified python if default set' {
 89+    # mock virtualenv function to test its inputs
 90+    function virtualenv {
 91+        echo virtualenv $@
 92+    }
 93+
 94+    mkdir myproject
 95+    cd myproject
 96+    AUTOSWITCH_DEFAULT_PYTHON="python_foo"
 97+
 98+    run mkvenv --python=python_bar
 99+
100+    assert $status equals 0
101+    assert "$TARGET/myproject/.venv" exists
102+    # Assert mock output
103+    assert "$lines[2]" same_as "virtualenv --python=python_bar $HOME/.virtualenvs/myproject"
104 
105     run cat "$TARGET/myproject/.venv"
106