4aa4a63f928a963bc7890909541f43679514ad86

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

Message

Merge pull request #166 from hauntsaninja/master

Remove pwgen dependency

Diff

  1diff --git a/README.rst b/README.rst
  2index 517ec0feedbfb3da595c10c41094a436debc3471..3182727b15dbbc9f1a5d1c8b58099b22bbf41f14 100644
  3--- a/README.rst
  4+++ b/README.rst
  5@@ -68,7 +68,7 @@ projects (or equivalent file for the Version Control you are using).
  6 Installing
  7 ----------
  8 
  9-``autoswitch-virtualenv`` requires `virtualenv <https://pypi.org/project/virtualenv/>`__ and ``pwgen`` to be installed.
 10+``autoswitch-virtualenv`` requires `virtualenv <https://pypi.org/project/virtualenv/>`__ to be installed.
 11 
 12 Once ``virtualenv`` is installed, add one of the following lines to your ``.zshrc`` file depending on the
 13 package manager you are using:
 14diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 15index 80576fb38b864c61ca1c7275aaf25aa000181b75..e4d49a0503c7640a2a54d2669fa8778d9efd20a9 100644
 16--- a/autoswitch_virtualenv.plugin.zsh
 17+++ b/autoswitch_virtualenv.plugin.zsh
 18@@ -277,6 +277,11 @@ function _missing_error_message() {
 19     printf "\n"
 20 }
 21 
 22+function randstr()
 23+{
 24+    python -c "from __future__ import print_function; import string, random; print(''.join(random.choice(string.ascii_lowercase) for _ in range(4)))"
 25+}
 26+
 27 
 28 # helper function to create a project environment for the current directory
 29 function mkvenv()
 30@@ -314,7 +319,7 @@ function mkvenv()
 31         if [[ -f "$AUTOSWITCH_FILE" ]]; then
 32             printf "$AUTOSWITCH_FILE file already exists. If this is a mistake use the rmvenv command\n"
 33         else
 34-            local venv_name="$(basename $PWD)-$(pwgen 8 1)"
 35+            local venv_name="$(basename $PWD)-$(randstr)"
 36 
 37             printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
 38 
 39@@ -399,13 +404,8 @@ function disable_autoswitch_virtualenv() {
 40 # This seems important for "instant prompt" zsh themes like powerlevel10k
 41 function _autoswitch_startup() {
 42     add-zsh-hook -D precmd _autoswitch_startup
 43-
 44-    if ! type pwgen 1>/dev/null; then
 45-        printf "${PURPLE}pwgen is required for zsh-autoswitch-virtualenv to run${NONE}\n"
 46-    else
 47-        enable_autoswitch_virtualenv
 48-        check_venv
 49-    fi
 50+    enable_autoswitch_virtualenv
 51+    check_venv
 52 }
 53 
 54 autoload -Uz add-zsh-hook
 55diff --git a/tests/test_mkvenv.zunit b/tests/test_mkvenv.zunit
 56index b1bdeaffe23b40787499ea064598b934b5a83cf5..39882d53f4f94945a43c030e5b1e72161ce7bda4 100644
 57--- a/tests/test_mkvenv.zunit
 58+++ b/tests/test_mkvenv.zunit
 59@@ -54,7 +54,7 @@
 60     run cat "$TARGET/myproject/.venv"
 61 
 62     assert $status equals 0
 63-    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
 64+    assert "$output" matches "myproject-[a-z]{4}"
 65 }
 66 
 67 @test 'mkvenv - (poetry project) runs correct command' {
 68@@ -100,7 +100,7 @@
 69     run cat "$TARGET/myproject/.venv"
 70 
 71     assert $status equals 0
 72-    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
 73+    assert "$output" matches "myproject-[a-z]{4}"
 74 }
 75 
 76 @test 'mkvenv - uses specified python if default set' {
 77@@ -123,7 +123,7 @@
 78     run cat "$TARGET/myproject/.venv"
 79 
 80     assert $status equals 0
 81-    assert "$output" matches "myproject-[a-zA-Z0-9]{8}"
 82+    assert "$output" matches "myproject-[a-z]{4}"
 83 }
 84 
 85 @test 'prints help message and disables plugin if virtualenv not setup' {
 86diff --git a/tests/test_plugin.zunit b/tests/test_plugin.zunit
 87index 8a3ebd89b16fe383d37faa8a4049cdaec0ca4f1b..506a8cf7d0a00d8dc017bb8cfe39ce5c2e89047f 100644
 88--- a/tests/test_plugin.zunit
 89+++ b/tests/test_plugin.zunit
 90@@ -60,3 +60,14 @@
 91     assert $status equals 0
 92     assert "$output" same_as "Switching virtualenv: \e[1m\e[35mfoobar\e[0m \e[32m[🐍$PYTHON_VERSION]\e[0m"
 93 }
 94+
 95+
 96+@test 'plugin - check randstr' {
 97+    unset AUTOSWITCH_DEFAULTENV
 98+    load ../autoswitch_virtualenv.plugin.zsh
 99+
100+    # check that $(randstr n) returns a string of length n
101+    assert "${#$(randstr)}" equals 4
102+
103+    assert "$(randstr)" different_to "$(randstr)"
104+}