73f2b2ff95fb06ff96b3de2bec65820f62238206

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

Message

Merge pull request #157 from MichaelAquilina/feat/non-shadowed-venvs

feat: Update mkvenv to generate virtualenvs that dont shadow

Diff

  1diff --git a/.circleci/config.yml b/.circleci/config.yml
  2index 27c74f7047d21b62364ce9375f1d28b864232b5d..4938b6e26a238838f7276b796d6348b307ebd8bd 100644
  3--- a/.circleci/config.yml
  4+++ b/.circleci/config.yml
  5@@ -6,7 +6,16 @@ test: &test
  6     - checkout
  7     - run:
  8         name: Install Dependencies
  9-        command: apt-get update && apt-get install -y curl python3-pip ncurses-bin python python-virtualenv git
 10+        command: >
 11+          apt-get update &&
 12+          apt-get install -y
 13+          python3-pip
 14+          python
 15+          python-virtualenv
 16+          curl
 17+          ncurses-bin
 18+          git
 19+          pwgen
 20     - run:
 21         name: install pipenv
 22         command: pip3 install pipenv
 23diff --git a/README.rst b/README.rst
 24index 3182727b15dbbc9f1a5d1c8b58099b22bbf41f14..517ec0feedbfb3da595c10c41094a436debc3471 100644
 25--- a/README.rst
 26+++ b/README.rst
 27@@ -68,7 +68,7 @@ projects (or equivalent file for the Version Control you are using).
 28 Installing
 29 ----------
 30 
 31-``autoswitch-virtualenv`` requires `virtualenv <https://pypi.org/project/virtualenv/>`__ to be installed.
 32+``autoswitch-virtualenv`` requires `virtualenv <https://pypi.org/project/virtualenv/>`__ and ``pwgen`` to be installed.
 33 
 34 Once ``virtualenv`` is installed, add one of the following lines to your ``.zshrc`` file depending on the
 35 package manager you are using:
 36diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 37index 59b8b4f4485e78304ba564a9a4f68424acb94daf..a76bd56e048187012bceb5c448c518a6698bd0a5 100644
 38--- a/autoswitch_virtualenv.plugin.zsh
 39+++ b/autoswitch_virtualenv.plugin.zsh
 40@@ -7,7 +7,6 @@ PURPLE="\e[35m"
 41 BOLD="\e[1m"
 42 NORMAL="\e[0m"
 43 
 44-
 45 function _validated_source() {
 46     local target_path="$1"
 47 
 48@@ -315,12 +314,16 @@ function mkvenv()
 49         if [[ -f "$AUTOSWITCH_FILE" ]]; then
 50             printf "$AUTOSWITCH_FILE file already exists. If this is a mistake use the rmvenv command\n"
 51         else
 52-            local venv_name="$(basename $PWD)"
 53+            local venv_name="$(basename $PWD)-$(pwgen 8 1)"
 54 
 55             printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
 56 
 57 
 58             if [[ -n "$AUTOSWITCH_DEFAULT_PYTHON" && ${params[(I)--python*]} -eq 0 ]]; then
 59+                printf "${PURPLE}"
 60+                printf 'Using $AUTOSWITCH_DEFAULT_PYTHON='
 61+                printf "$AUTOSWITCH_DEFAULT_PYTHON"
 62+                printf "${NONE}\n"
 63                 params+="--python=$AUTOSWITCH_DEFAULT_PYTHON"
 64             fi
 65 
 66@@ -380,7 +383,6 @@ function install_requirements() {
 67 
 68 
 69 function enable_autoswitch_virtualenv() {
 70-    autoload -Uz add-zsh-hook
 71     disable_autoswitch_virtualenv
 72     add-zsh-hook chpwd check_venv
 73 }
 74@@ -393,12 +395,18 @@ function disable_autoswitch_virtualenv() {
 75 # This function is only used to startup zsh-autoswitch-virtualenv
 76 # the first time a terminal is started up
 77 # it waits for the terminal to be ready using precmd and then
 78-# imediately removes itself from the zsh-hook
 79+# immediately removes itself from the zsh-hook.
 80+# This seems important for "instant prompt" zsh themes like powerlevel10k
 81 function _autoswitch_startup() {
 82     add-zsh-hook -D precmd _startup
 83-    check_venv
 84-}
 85 
 86+    if ! type pwgen 1>/dev/null; then
 87+        printf "${PURPLE}pwgen is required for zsh-autoswitch-virtualenv to run${NONE}\n"
 88+    else
 89+        enable_autoswitch_virtualenv
 90+        check_venv
 91+    fi
 92+}
 93 
 94-enable_autoswitch_virtualenv
 95+autoload -Uz add-zsh-hook
 96 add-zsh-hook precmd _autoswitch_startup
 97diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
 98index ae18ab4ebb5a6d1d080f9e5133fb717ccf9b63a4..b1bdeaffe23b40787499ea064598b934b5a83cf5 100644
 99--- a/tests/test_mkvenv.zunit
100+++ b/tests/test_mkvenv.zunit
101@@ -50,12 +50,11 @@
102 
103     assert $status equals 0
104     assert "$TARGET/myproject/.venv" exists
105-    # assert "$lines[1]" same_as "Creating \e[35mmyproject\e[0m virtualenv"
106 
107     run cat "$TARGET/myproject/.venv"
108 
109     assert $status equals 0
110-    assert "$output" same_as "myproject"
111+    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
112 }
113 
114 @test 'mkvenv - (poetry project) runs correct command' {
115@@ -92,12 +91,16 @@
116     assert $status equals 0
117     assert "$TARGET/myproject/.venv" exists
118     # Assert mock output
119-    assert "$lines[2]" same_as "virtualenv --python=python_foo $HOME/.virtualenvs/myproject"
120+
121+    expected='Using $AUTOSWITCH_DEFAULT_PYTHON=python_foo'
122+    assert "$lines[2]" contains "$expected"
123+    expected="virtualenv --python=python_foo $HOME/.virtualenvs/myproject-"
124+    assert "$lines[3]" contains "$expected"
125 
126     run cat "$TARGET/myproject/.venv"
127 
128     assert $status equals 0
129-    assert "$output" same_as "myproject"
130+    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
131 }
132 
133 @test 'mkvenv - uses specified python if default set' {
134@@ -115,12 +118,12 @@
135     assert $status equals 0
136     assert "$TARGET/myproject/.venv" exists
137     # Assert mock output
138-    assert "$lines[2]" same_as "virtualenv --python=python_bar $HOME/.virtualenvs/myproject"
139+    assert "$lines[2]" contains "virtualenv --python=python_bar $HOME/.virtualenvs/myproject-"
140 
141     run cat "$TARGET/myproject/.venv"
142 
143     assert $status equals 0
144-    assert "$output" same_as "myproject"
145+    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
146 }
147 
148 @test 'prints help message and disables plugin if virtualenv not setup' {
149diff --git a/tests/test_plugin.zunit b/tests/test_plugin.zunit
150index eeb2c5fe46f9111caa61d9762f296afcad97aead..8a3ebd89b16fe383d37faa8a4049cdaec0ca4f1b 100644
151--- a/tests/test_plugin.zunit
152+++ b/tests/test_plugin.zunit
153@@ -39,6 +39,9 @@
154 @test 'plugin - switches env when .venv found' {
155     unset AUTOSWITCH_DEFAULTENV
156     load ../autoswitch_virtualenv.plugin.zsh
157+    # zsh-hook precmd does not seem to work correctly in tests
158+    # so we need to manually enable the plugin
159+    enable_autoswitch_virtualenv
160 
161     TEMP_DIR="$(mktemp -d)"
162     echo "foobar" > "$TEMP_DIR/.venv"
163diff --git a/tests/test_rmvenv.zunit b/tests/test_rmvenv.zunit
164index c7fde66c80f7d479c6d00ed5db0c1e0c74598efc..53a6a0285123e21b2a61151262d506d717c27fba 100644
165--- a/tests/test_rmvenv.zunit
166+++ b/tests/test_rmvenv.zunit
167@@ -68,11 +68,12 @@
168 @test 'rmvenv - removes .venv and deactivates if currently active' {
169     test_venv="$(basename $PWD)"
170     run mkvenv
171+    target_venv="$(<.venv)"
172 
173     run rmvenv
174 
175     assert $state equals 0
176-    assert "$output" same_as "Removing \e[35m$test_venv\e[0m..."
177+    assert "$output" same_as "Removing \e[35m$target_venv\e[0m..."
178     test ! -f .venv
179     test ! -d "$HOME/.virtualenvs/$test_venv"
180 }