71c4d2f90c840502efa24743ae9a5e8e602ab037

Author
sungminoh <smoh2044@gmail.com>
Committer
sungminoh <smoh2044@gmail.com>
Date

Message

add TC for autoswitching pipenv

Diff

  1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
  2index 7f88a0b19316ea1db8dc303ba0be827ad9406526..c91abb190cab12faeef0ede6063544f37df23fec 100644
  3--- a/autoswitch_virtualenv.plugin.zsh
  4+++ b/autoswitch_virtualenv.plugin.zsh
  5@@ -45,12 +45,12 @@ function _maybeworkon() {
  6 
  7 
  8 function _maybepipenv() {
  9-  if [[ -z $VIRTUAL_ENV ]]; then
 10+  if [[ -z "$VIRTUAL_ENV" || "$1" != "$VIRTUAL_ENV" ]]; then
 11      if [ -z "$AUTOSWITCH_SILENT" ]; then
 12-        printf "Switching pipenv: %s  " $(basename $(pipenv --venv))
 13+        printf "Switching pipenv: %s  " "$(basename "$1")"
 14      fi
 15 
 16-     . $(pipenv --venv)/bin/activate
 17+     . "$1"/bin/activate
 18 
 19      if [ -z "$AUTOSWITCH_SILENT" ]; then
 20         _print_python_version
 21@@ -116,7 +116,7 @@ function check_venv()
 22         if [[ -n "$SWITCH_TO" ]]; then
 23           _maybeworkon "$SWITCH_TO"
 24         elif [[ -n `pipenv --venv 2>/dev/null` ]]; then
 25-          _maybepipenv
 26+          _maybepipenv "$(pipenv --venv)"
 27         else
 28           _default_venv
 29         fi
 30diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
 31index 31f7bf8ede74bb4f36927b8c7f7506a69eb4fccb..3bc7decfedda017d073847ceca7aac7b80eb7bfa 100644
 32--- a/tests/test_check_venv.zunit
 33+++ b/tests/test_check_venv.zunit
 34@@ -116,7 +116,7 @@
 35 }
 36 
 37 
 38-@test 'check_venv - go to default if .venv available' {
 39+@test 'check_venv - go to default if .venv unavailable' {
 40     PWD="$TARGET"
 41     MYOLDPWD="$(dirname $TARGET)"
 42     AUTOSWITCH_DEFAULTENV="foodefault"
 43@@ -128,9 +128,25 @@
 44 }
 45 
 46 
 47-@test 'check_venv - deactivate if no .venv available' {
 48+@test 'check_venv - deactivate if .venv unavailable but pipenv available' {
 49     PWD="$TARGET"
 50-    MYOLDPWD="$(dirname TARGET)"
 51+    MYOLDPWD="$(dirname $TARGET)"
 52+    VIRTUAL_ENV="foo"
 53+
 54+    function pipenv {
 55+        echo "$HOME/.virtualenvs/foobar"
 56+    }
 57+
 58+    run check_venv
 59+
 60+    assert $status equals 0
 61+    assert "$output" same_as "Switching pipenv: foobar  [$PYTHON_VERSION]"
 62+}
 63+
 64+
 65+@test 'check_venv - deactivate if neither .venv nor pipenv available' {
 66+    PWD="$TARGET"
 67+    MYOLDPWD="$(dirname $TARGET)"
 68     VIRTUAL_ENV="foo"
 69     unset AUTOSWITCH_DEFAULTENV
 70     function deactivate {
 71diff --git a/tests/test_maybepipenv.zunit b/tests/test_maybepipenv.zunit
 72new file mode 100644
 73index 0000000000000000000000000000000000000000..655fbd2d6b01d5c9cf03897dd8ae97a747c09f52
 74--- /dev/null
 75+++ b/tests/test_maybepipenv.zunit
 76@@ -0,0 +1,61 @@
 77+#!/usr/bin/env zunit
 78+
 79+@setup {
 80+    source =virtualenvwrapper.sh
 81+
 82+    echo "Creating test pipenv"
 83+    run pipenv --python 3
 84+
 85+    PYTHON_VERSION="$(python3 --version 2>&1)"
 86+
 87+    export DISABLE_AUTOSWITCH_VENV="1"
 88+    load "../autoswitch_virtualenv.plugin.zsh"
 89+    TARGET="$(mktemp -d)"
 90+}
 91+
 92+@teardown {
 93+    rm -rf "$TARGET"
 94+    run deactivate 2>/dev/null
 95+    run pipenv --rm
 96+    rm -rf Pipfile
 97+}
 98+
 99+
100+@test '_maybepipenv - switches virtualenv if nothing is activated' {
101+    VIRTUAL_ENV=""
102+
103+    run _maybepipenv "$(pipenv --venv)"
104+
105+    assert $state equals 0
106+    assert "$output" matches "Switching pipenv: $(basename "$PWD")-[^\s]{8}  \[$PYTHON_VERSION\]"
107+
108+
109+}
110+
111+@test '_maybepipenv - switches virtualenv if current virtualenv is different' {
112+    VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
113+
114+    run _maybepipenv "$(pipenv --venv)"
115+
116+    assert $state equals 0
117+    assert "$output" matches "Switching pipenv: $(basename "$PWD")-[^\s]{8}  \[$PYTHON_VERSION\]"
118+}
119+
120+@test '_maybepipenv - switches virtualenv if current virtualenv is different (silent)' {
121+    VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
122+    AUTOSWITCH_SILENT="1"
123+
124+    run _maybepipenv "$(pipenv --venv)"
125+
126+    assert $state equals 0
127+    assert "$output" is_empty
128+}
129+
130+@test '_maybepipenv - does not switch to already activated virtualenv' {
131+    VIRTUAL_ENV="$(pipenv --venv)"
132+
133+    run _maybepipenv "$(pipenv --venv)"
134+
135+    assert $state equals 0
136+    assert "$output" is_empty
137+}