a9e8b6415665bf27a225ad4e725eb4300f6a547b

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

Message

Merge pull request #68 from MichaelAquilina/next

Detect python projects when setup.py file found

Diff

  1diff --git a/CHANGELOG.rst b/CHANGELOG.rst
  2index 07b954e22b064262e245ba4c4ba6408aeff4b347..e0e090fa3d8677d95b770b9fd1d4a151feb7abfa 100644
  3--- a/CHANGELOG.rst
  4+++ b/CHANGELOG.rst
  5@@ -1,6 +1,11 @@
  6 Changelog
  7 =========
  8 
  9+1.5.0
 10+-----
 11+* Detect python projects with `setup.py`
 12+
 13+
 14 1.4.1
 15 -----
 16 * Show helpful error message when target virtualenv is not found
 17diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 18index f01a730b8596473dc8c91f22b7be396db19bf06e..f3ad8c510322b4124e0c00bba881694c529d0d4b 100644
 19--- a/autoswitch_virtualenv.plugin.zsh
 20+++ b/autoswitch_virtualenv.plugin.zsh
 21@@ -1,4 +1,4 @@
 22-export AUTOSWITCH_VERSION='1.4.1'
 23+export AUTOSWITCH_VERSION='1.5.0'
 24 
 25 RED="\e[31m"
 26 GREEN="\e[32m"
 27@@ -114,7 +114,7 @@ function check_venv()
 28         else
 29             SWITCH_TO="$(<"$venv_path")"
 30         fi
 31-    elif [[ -f "$PWD/requirements.txt" ]]; then
 32+    elif [[ -f "$PWD/requirements.txt" || -f "$PWD/setup.py" ]]; then
 33         printf "Python project detected. "
 34         printf "Run ${PURPLE}mkvenv${NORMAL} to setup autoswitching\n"
 35     fi
 36@@ -194,6 +194,17 @@ function mkvenv()
 37 
 38 function install_requirements() {
 39     setopt nullglob
 40+    if [[ -f "$PWD/setup.py" ]]; then
 41+        printf "Found a ${PURPLE}setup.py${NORMAL} file. Install dependencies? [y/N]: "
 42+        read ans
 43+
 44+        if [[ "$ans" = "y" || "$ans" = "Y" ]]; then
 45+            pip install .
 46+            # We probably don't want to ALSO install requirements.txt
 47+            return
 48+        fi
 49+    fi
 50+
 51     for requirements in *requirements.txt
 52     do
 53         printf "Found a ${PURPLE}%s${NORMAL} file. Install? [y/N]: " "$requirements"
 54diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
 55index cab07d2dd4be4a56c416c2677685454327ea6eaf..a90d37a38438514cb1096c322cf20b073857b78d 100644
 56--- a/tests/test_check_venv.zunit
 57+++ b/tests/test_check_venv.zunit
 58@@ -25,7 +25,7 @@
 59     rm -rf Pipfile
 60 }
 61 
 62-@test 'check_venv - Displays message on project detection' {
 63+@test 'check_venv - Displays message on project detection (requirements.txt)' {
 64     PWD="$TARGET"
 65     touch "$TARGET/requirements.txt"
 66 
 67@@ -35,6 +35,26 @@
 68     assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
 69 }
 70 
 71+@test 'check_venv - Displays message on project detection (setup.py)' {
 72+    PWD="$TARGET"
 73+    touch "$TARGET/setup.py"
 74+
 75+    run check_venv
 76+
 77+    assert $status equals 0
 78+    assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
 79+}
 80+
 81+@test 'check_venv - Displays message on project detection (setup.py + requirements.txt)' {
 82+    PWD="$TARGET"
 83+    touch "$TARGET/setup.py"
 84+    touch "$TARGET/requirements.txt"
 85+
 86+    run check_venv
 87+
 88+    assert $status equals 0
 89+    assert "$output" same_as "Python project detected. Run \e[35mmkvenv\e[0m to setup autoswitching"
 90+}
 91 
 92 @test 'check_venv - Security warning for weak writeable by group permissions' {
 93     PWD="$TARGET"
 94diff --git a/tests/test_install_requirements.txt b/tests/test_install_requirements.txt
 95new file mode 100644
 96index 0000000000000000000000000000000000000000..3ce2192e67ddb2cf63bcdab3dac37c0fc26efafb
 97--- /dev/null
 98+++ b/tests/test_install_requirements.txt
 99@@ -0,0 +1,85 @@
100+#!/usr/bin/env zunit
101+
102+@setup {
103+    export DISABLE_AUTOSWITCH_VENV="1"
104+    load "../autoswitch_virtualenv.plugin.zsh"
105+
106+    function read {
107+        eval $1=\"y\"
108+        echo y
109+    }
110+
111+    TARGET="$(mktemp -d)"
112+    cd "$TARGET"
113+}
114+
115+@teardown {
116+    rm -rf "$TARGET"
117+}
118+
119+@test 'install_requirements - does nothing if no files found' {
120+    run install_requirements
121+
122+    assert $status equals 0
123+    assert "$output" is_empty
124+}
125+
126+@test 'install_requirements - does not install with no input' {
127+    echo "django" > requirements.txt
128+    echo "pytest" > dev-requirements.txt
129+
130+    # override default mock to return nothing
131+    function read {
132+        eval $1=\"\"
133+        echo ""
134+    }
135+
136+    # mock pip to make it testable
137+    function pip {
138+        echo "pip $@"
139+        cat "$3"
140+    }
141+
142+    run install_requirements
143+
144+    assert $status equals 0
145+    assert "$lines[1]" same_as "Found a \e[35mdev-requirements.txt\e[0m file. Install? [y/N]: "
146+    assert "$lines[2]" same_as "Found a \e[35mrequirements.txt\e[0m file. Install? [y/N]: "
147+}
148+
149+@test 'install_requirements - installs *requirements.txt' {
150+    echo "django" > requirements.txt
151+    echo "pytest" > dev-requirements.txt
152+
153+    # mock pip to make it testable
154+    function pip {
155+        echo "pip $@"
156+        cat "$3"
157+    }
158+
159+    run install_requirements
160+
161+    assert $status equals 0
162+    assert "$lines[1]" same_as "Found a \e[35mdev-requirements.txt\e[0m file. Install? [y/N]: y"
163+    assert "$lines[2]" same_as "pip install -r dev-requirements.txt"
164+    assert "$lines[3]" same_as "pytest"
165+    assert "$lines[4]" same_as "Found a \e[35mrequirements.txt\e[0m file. Install? [y/N]: y"
166+    assert "$lines[5]" same_as "pip install -r requirements.txt"
167+    assert "$lines[6]" same_as "django"
168+}
169+
170+@test 'install_requirements - installs setup.py' {
171+    touch "setup.py"
172+    echo "django" > requirements.txt
173+
174+    # mock pip to make it testable
175+    function pip {
176+        echo "pip $@"
177+    }
178+
179+    run install_requirements
180+
181+    assert $status equals 0
182+    assert "$lines[1]" same_as "Found a \e[35msetup.py\e[0m file. Install dependencies? [y/N]: y"
183+    assert "$lines[2]" same_as "pip install ."
184+}