ab856477dffdabca482501893d481cc309a18939

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

Message

Merge pull request #74 from MichaelAquilina/next

1.7.0

Diff

  1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
  2index d3aab9eb9a3ebd07797cc03f5fef7a9a6b271cc8..80b9f2c50e2c24157b86bda56cbef6fef7be2f18 100644
  3--- a/CHANGELOG.rst
  4+++ b/CHANGELOG.rst
  5@@ -1,6 +1,11 @@
  6 Changelog
  7 =========
  8 
  9+1.7.0
 10+-----
 11+* Add option to set default python binary to use when creating virtualenvs
 12+* Add option to set default requirements file to install when creating virtualenvs
 13+
 14 1.6.0
 15 -----
 16 * Display snake emoji by default when switching virtualenvs
 17diff --git a/README.rst b/README.rst
 18index bbfcf2d1694f98be61dcd75ea6709bffc02d1282..6f8701a85ca3a009d05fd1fb52d798109883597c 100644
 19--- a/README.rst
 20+++ b/README.rst
 21@@ -235,6 +235,31 @@ export ``AUTOSWITCH_DEFAULTENV`` in your ``.zshrc`` file.
 22     export AUTOSWITCH_DEFAULTENV="mydefaultenv"
 23     antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv
 24 
 25+**Setting a default python binary**
 26+
 27+You may specify a default python binary to use when creating virtualenvs
 28+by setting the value of ``AUTOSWITCH_DEFAULT_PYTHON``. For example:
 29+
 30+::
 31+
 32+    export AUTOSWITCH_DEFAULT_PYTHON="/usr/bin/python3"
 33+
 34+You may still override this default as usual by passing the --python parameter to
 35+the mkvenv command.
 36+
 37+**Default requirements file**
 38+
 39+You may specify a default requirements file to use when creating a virtualenv by
 40+setting the value of ``AUTOSWTICH_DEFAULT_REQUIREMENTS``. For example:
 41+
 42+::
 43+
 44+    export AUTOSWITCH_DEFAULT_REQUIREMENTS="$HOME/.requirements.txt"
 45+
 46+If the value is set and the target file exists you will be prompted to install with that file
 47+each time you create a new virtualenv.
 48+
 49+
 50 **Set verbosity when changing environments**
 51 
 52 You can prevent verbose messages from being displayed when moving
 53diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 54index d2207d76a95da89567762b71dcb000ec9cfdd3e0..96c4fe1a47cb09734bd62fa38151c1a681bee72a 100644
 55--- a/autoswitch_virtualenv.plugin.zsh
 56+++ b/autoswitch_virtualenv.plugin.zsh
 57@@ -1,4 +1,4 @@
 58-export AUTOSWITCH_VERSION='1.6.0'
 59+export AUTOSWITCH_VERSION='1.7.0'
 60 
 61 RED="\e[31m"
 62 GREEN="\e[32m"
 63@@ -180,10 +180,17 @@ function mkvenv()
 64 
 65         printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
 66 
 67-        if [[ ${@[(ie)--verbose]} -eq ${#@} ]]; then
 68-            virtualenv $@ "$(_virtual_env_dir)/$venv_name"
 69+        # Copy parameters variable so that we can mutate it
 70+        params=("${@[@]}")
 71+
 72+        if [[ -n "$AUTOSWITCH_DEFAULT_PYTHON" && ${params[(I)--python*]} -eq 0 ]]; then
 73+            params+="--python=$AUTOSWITCH_DEFAULT_PYTHON"
 74+        fi
 75+
 76+        if [[ ${params[(I)--verbose]} -eq 0 ]]; then
 77+            virtualenv $params "$(_virtual_env_dir)/$venv_name"
 78         else
 79-            virtualenv $@ "$(_virtual_env_dir)/$venv_name" > /dev/null
 80+            virtualenv $params "$(_virtual_env_dir)/$venv_name" > /dev/null
 81         fi
 82 
 83         printf "$venv_name\n" > ".venv"
 84@@ -197,6 +204,15 @@ function mkvenv()
 85 
 86 
 87 function install_requirements() {
 88+    if [[ -f "$AUTOSWITCH_DEFAULT_REQUIREMENTS" ]]; then
 89+        printf "Install default requirements? (${PURPLE}$AUTOSWITCH_DEFAULT_REQUIREMENTS${NORMAL}) [y/N]: "
 90+        read ans
 91+
 92+        if [[ "$ans" = "y" || "$ans" == "Y" ]]; then
 93+            pip install -r "$AUTOSWITCH_DEFAULT_REQUIREMENTS"
 94+        fi
 95+    fi
 96+
 97     if [[ -f "$PWD/setup.py" ]]; then
 98         printf "Found a ${PURPLE}setup.py${NORMAL} file. Install dependencies? [y/N]: "
 99         read ans
100diff --git a/tests/test_install_requirements.txt b/tests/test_install_requirements.txt
101index 3ce2192e67ddb2cf63bcdab3dac37c0fc26efafb..9513251da44328e995813219a3794a5b23e34d87 100644
102--- a/tests/test_install_requirements.txt
103+++ b/tests/test_install_requirements.txt
104@@ -47,6 +47,24 @@
105     assert "$lines[2]" same_as "Found a \e[35mrequirements.txt\e[0m file. Install? [y/N]: "
106 }
107 
108+@test 'install_requirements - installs default requirements' {
109+    AUTOSWITCH_DEFAULT_REQUIREMENTS="$(mktemp -d)/default-requirements.txt"
110+    echo "django" > "$AUTOSWITCH_DEFAULT_REQUIREMENTS"
111+
112+    # mock pip to make it testable
113+    function pip {
114+        echo "pip $@"
115+        cat "$3"
116+    }
117+
118+    run install_requirements
119+
120+    assert $status equals 0
121+    assert "$lines[1]" same_as "Install default requirements? (\e[35m$AUTOSWITCH_DEFAULT_REQUIREMENTS\e[0m) [y/N]: y"
122+    assert "$lines[2]" same_as "pip install -r $AUTOSWITCH_DEFAULT_REQUIREMENTS"
123+    assert "$lines[3]" same_as "django"
124+}
125+
126 @test 'install_requirements - installs *requirements.txt' {
127     echo "django" > requirements.txt
128     echo "pytest" > dev-requirements.txt
129diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
130index 6a5c384a115529fdf9e2d1c2079bde72cfa3187c..6a5854c1791ab21b517d9315f7756062eb1bdfb8 100644
131--- a/tests/test_mkvenv.zunit
132+++ b/tests/test_mkvenv.zunit
133@@ -30,6 +30,53 @@
134 
135     assert $status equals 0
136     assert "$TARGET/myproject/.venv" exists
137+    # assert "$lines[1]" same_as "Creating \e[35mmyproject\e[0m virtualenv"
138+
139+    run cat "$TARGET/myproject/.venv"
140+
141+    assert $status equals 0
142+    assert "$output" same_as "myproject"
143+}
144+
145+@test 'mkvenv - uses default python if set and not specified' {
146+    # mock virtualenv function to test its inputs
147+    function virtualenv {
148+        echo virtualenv $@
149+    }
150+
151+    mkdir myproject
152+    cd myproject
153+    AUTOSWITCH_DEFAULT_PYTHON="python_foo"
154+
155+    run mkvenv
156+
157+    assert $status equals 0
158+    assert "$TARGET/myproject/.venv" exists
159+    # Assert mock output
160+    assert "$lines[2]" same_as "virtualenv --python=python_foo $HOME/.virtualenvs/myproject"
161+
162+    run cat "$TARGET/myproject/.venv"
163+
164+    assert $status equals 0
165+    assert "$output" same_as "myproject"
166+}
167+
168+@test 'mkvenv - uses specified python if default set' {
169+    # mock virtualenv function to test its inputs
170+    function virtualenv {
171+        echo virtualenv $@
172+    }
173+
174+    mkdir myproject
175+    cd myproject
176+    AUTOSWITCH_DEFAULT_PYTHON="python_foo"
177+
178+    run mkvenv --python=python_bar
179+
180+    assert $status equals 0
181+    assert "$TARGET/myproject/.venv" exists
182+    # Assert mock output
183+    assert "$lines[2]" same_as "virtualenv --python=python_bar $HOME/.virtualenvs/myproject"
184 
185     run cat "$TARGET/myproject/.venv"
186