9b08064397dc18be99c2735df6cd44e726db6310

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

Message

Merge pull request #75 from MichaelAquilina/default_requirements

Allow default requirements to be requested to be installed

Diff

 1diff --git a/README.rst b/README.rst
 2index b694ba12fb85a6ff3d0a5251705a154fa2fd066b..6f8701a85ca3a009d05fd1fb52d798109883597c 100644
 3--- a/README.rst
 4+++ b/README.rst
 5@@ -247,6 +247,19 @@ by setting the value of ``AUTOSWITCH_DEFAULT_PYTHON``. For example:
 6 You may still override this default as usual by passing the --python parameter to
 7 the mkvenv command.
 8 
 9+**Default requirements file**
10+
11+You may specify a default requirements file to use when creating a virtualenv by
12+setting the value of ``AUTOSWTICH_DEFAULT_REQUIREMENTS``. For example:
13+
14+::
15+
16+    export AUTOSWITCH_DEFAULT_REQUIREMENTS="$HOME/.requirements.txt"
17+
18+If the value is set and the target file exists you will be prompted to install with that file
19+each time you create a new virtualenv.
20+
21+
22 **Set verbosity when changing environments**
23 
24 You can prevent verbose messages from being displayed when moving
25diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
26index 0424fb806acc3cbf9cb68c8622c526759cb281bd..070fda17d6d1d51db4cc7c4ce0877e25f9343439 100644
27--- a/autoswitch_virtualenv.plugin.zsh
28+++ b/autoswitch_virtualenv.plugin.zsh
29@@ -204,6 +204,15 @@ function mkvenv()
30 
31 
32 function install_requirements() {
33+    if [[ -f "$AUTOSWITCH_DEFAULT_REQUIREMENTS" ]]; then
34+        printf "Install default requirements? (${PURPLE}$AUTOSWITCH_DEFAULT_REQUIREMENTS${NORMAL}) [y/N]: "
35+        read ans
36+
37+        if [[ "$ans" = "y" || "$ans" == "Y" ]]; then
38+            pip install -r "$AUTOSWITCH_DEFAULT_REQUIREMENTS"
39+        fi
40+    fi
41+
42     if [[ -f "$PWD/setup.py" ]]; then
43         printf "Found a ${PURPLE}setup.py${NORMAL} file. Install dependencies? [y/N]: "
44         read ans
45diff --git a/tests/test_install_requirements.txt b/tests/test_install_requirements.txt
46index 3ce2192e67ddb2cf63bcdab3dac37c0fc26efafb..9513251da44328e995813219a3794a5b23e34d87 100644
47--- a/tests/test_install_requirements.txt
48+++ b/tests/test_install_requirements.txt
49@@ -47,6 +47,24 @@
50     assert "$lines[2]" same_as "Found a \e[35mrequirements.txt\e[0m file. Install? [y/N]: "
51 }
52 
53+@test 'install_requirements - installs default requirements' {
54+    AUTOSWITCH_DEFAULT_REQUIREMENTS="$(mktemp -d)/default-requirements.txt"
55+    echo "django" > "$AUTOSWITCH_DEFAULT_REQUIREMENTS"
56+
57+    # mock pip to make it testable
58+    function pip {
59+        echo "pip $@"
60+        cat "$3"
61+    }
62+
63+    run install_requirements
64+
65+    assert $status equals 0
66+    assert "$lines[1]" same_as "Install default requirements? (\e[35m$AUTOSWITCH_DEFAULT_REQUIREMENTS\e[0m) [y/N]: y"
67+    assert "$lines[2]" same_as "pip install -r $AUTOSWITCH_DEFAULT_REQUIREMENTS"
68+    assert "$lines[3]" same_as "django"
69+}
70+
71 @test 'install_requirements - installs *requirements.txt' {
72     echo "django" > requirements.txt
73     echo "pytest" > dev-requirements.txt