fa89c24b13e1aa20a4cfe59ebe3626c4a08bb214

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

Message

Fix detection of pipenv projects in subdirectories

Diff

  1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
  2index d91669ac9eb57b64ad974eea2a29b1bf18fb3136..4ca6e39030e2af0eddf794f425a2f0caecc20ea6 100644
  3--- a/autoswitch_virtualenv.plugin.zsh
  4+++ b/autoswitch_virtualenv.plugin.zsh
  5@@ -70,7 +70,8 @@ function _maybeworkon() {
  6         DEFAULT_MESSAGE_FORMAT="${DEFAULT_MESSAGE_FORMAT/🐍/}"
  7     fi
  8 
  9-    if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
 10+    # Don't reactivate an already activated virtual environment
 11+    if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(_get_venv_name $VIRTUAL_ENV $venv_type)" ]]; then
 12 
 13         if [[ ! -d "$venv_dir" ]]; then
 14             printf "Unable to find ${PURPLE}$venv_name${NORMAL} virtualenv\n"
 15@@ -97,20 +98,21 @@ function _maybeworkon() {
 16 }
 17 
 18 
 19-# Gives the path to the nearest parent $AUTOSWITCH_FILE or nothing if it gets to root
 20-function _check_venv_path()
 21+# Gives the path to the nearest target file
 22+function _check_path()
 23 {
 24     local check_dir="$1"
 25+    local check_file="$2"
 26 
 27-    if [[ -f "${check_dir}/$AUTOSWITCH_FILE" ]]; then
 28-        printf "${check_dir}/$AUTOSWITCH_FILE"
 29+    if [[ -f "${check_dir}/$check_file" ]]; then
 30+        printf "${check_dir}/$check_file"
 31         return
 32     else
 33         # Abort search at file system root or HOME directory (latter is a performance optimisation).
 34         if [[ "$check_dir" = "/" || "$check_dir" = "$HOME" ]]; then
 35             return
 36         fi
 37-        _check_venv_path "$(dirname "$check_dir")"
 38+        _check_path "$(dirname "$check_dir")" "$check_file"
 39     fi
 40 }
 41 
 42@@ -122,7 +124,7 @@ function check_venv()
 43     local file_permissions
 44 
 45     # Get the $AUTOSWITCH_FILE, scanning parent directories
 46-    local venv_path=$(_check_venv_path "$PWD")
 47+    local venv_path="$(_check_path "$PWD" "$AUTOSWITCH_FILE")"
 48 
 49     if [[ -n "$venv_path" ]]; then
 50 
 51@@ -151,7 +153,9 @@ function check_venv()
 52     fi
 53 
 54     # check if Pipfile exists rather than invoking pipenv as it is slow
 55-    if [[ -f "Pipfile" ]] && type "pipenv" > /dev/null; then
 56+    local pipfile_path="$(_check_path "$PWD" "Pipfile")"
 57+
 58+    if [[ -n "$pipfile_path" ]] && type "pipenv" > /dev/null; then
 59         if venv_path="$(PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv 2>/dev/null)"; then
 60             _maybeworkon "$venv_path" "pipenv"
 61             return
 62diff --git a/tests/test_check_venv_path.zunit b/tests/test_check_path.zunit
 63rename from tests/test_check_venv_path.zunit
 64rename to tests/test_check_path.zunit
 65index c844f40caa06de10a203ab1ff7c053bc0be6cf17..8ee46795e835de214d6b17df6e691c670d23661b 100644
 66--- a/tests/test_check_venv_path.zunit
 67+++ b/tests/test_check_path.zunit
 68@@ -11,34 +11,34 @@
 69     rm -rf "$TARGET"
 70 }
 71 
 72-@test '_check_venv_path - test finds in base directory' {
 73+@test '_check_path - test finds in base directory' {
 74     touch "$TARGET/.venv"
 75 
 76-    run _check_venv_path "$TARGET"
 77+    run _check_path "$TARGET" ".venv"
 78 
 79     assert $state equals 0
 80     assert "$output" same_as "$TARGET/.venv"
 81 }
 82 
 83-@test '_check_venv_path - returns nothing if not found' {
 84-   run _check_venv_path "$TARGET"
 85+@test '_check_path - returns nothing if not found' {
 86+   run _check_path "$TARGET" ".venv"
 87 
 88    assert $state equals 0
 89    assert "$output" is_empty
 90 }
 91 
 92-@test '_check_venv_path - finds .venv in parent directories' {
 93-    touch "$TARGET/.venv"
 94+@test '_check_path - finds in parent directories' {
 95+    touch "$TARGET/Pipfile"
 96     mkdir "$TARGET/foo"
 97 
 98-    run _check_venv_path "$TARGET/foo"
 99+    run _check_path "$TARGET/foo" "Pipfile"
100 
101     assert $state equals 0
102-    assert "$output" same_as "$TARGET/.venv"
103+    assert "$output" same_as "$TARGET/Pipfile"
104 }
105 
106-@test '_check_venv_path - returns nothing with root path' {
107-    run _check_venv_path '/'
108+@test '_check_path - returns nothing with root path' {
109+    run _check_path '/' ".venv"
110 
111     assert $state equals 0
112     assert "$output" is_empty
113diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
114index 058c815dddcf982768a170e03bc4b1cb2628f082..a1194b02108b45a8e3051f0d87059f805b94d5a7 100644
115--- a/tests/test_check_venv.zunit
116+++ b/tests/test_check_venv.zunit
117@@ -156,9 +156,8 @@
118 
119 
120 @test 'check_venv - activate if .venv unavailable but pipenv available' {
121-    PWD="$TARGET"
122-    VIRTUAL_ENV="foo"
123-    touch "Pipfile"
124+    PWD="$TARGET/sub-directory"  # Should also work in subdirectories where Pipfile is found
125+    touch "$TARGET/Pipfile"
126 
127     function pipenv {
128         echo "$HOME/.virtualenvs/foobar"