Parent directory

test_mkvenv.zunit

4372 bytes
  1#!/usr/bin/env zunit
  2
  3@setup {
  4    function python3 {
  5        echo python3 $@
  6    }
  7
  8    function python_foo {
  9        echo python_foo $@
 10    }
 11
 12    export DISABLE_AUTOSWITCH_VENV="1"
 13    load "../autoswitch_virtualenv.plugin.zsh"
 14    unset AUTOSWITCH_DEFAULT_PYTHON
 15    TARGET="$(mktemp -d)"
 16    cd "$TARGET"
 17
 18    function randstr {
 19        printf test
 20    }
 21
 22    function _maybeworkon {
 23    }
 24
 25    function poetry {
 26        echo "poetry" $@
 27    }
 28
 29    function _activate_poetry {
 30        echo "activating poetry"
 31    }
 32
 33    function pipenv {
 34        echo "pipenv" $@
 35    }
 36
 37    function _activate_pipenv {
 38        echo "activating pipenv"
 39    }
 40}
 41
 42@teardown {
 43    rm -rf "$TARGET"
 44}
 45
 46@test 'mkvenv - does not create .venv if one exists' {
 47    touch .venv
 48
 49    run mkvenv
 50
 51    assert $status equals 0
 52    assert "$output" same_as ".venv file already exists. If this is a mistake use the rmvenv command"
 53}
 54
 55
 56@test 'mkvenv - creates .venv' {
 57    mkdir myproject
 58    cd myproject
 59
 60    run mkvenv
 61
 62    assert $status equals 0
 63    assert "$TARGET/myproject/.venv" exists
 64
 65    run cat "$TARGET/myproject/.venv"
 66
 67    assert $status equals 0
 68    assert "$output" matches "myproject-[a-z]{4}"
 69}
 70
 71@test 'mkvenv - uses default Python if set' {
 72    mkdir myproject
 73    cd myproject
 74    AUTOSWITCH_DEFAULT_PYTHON="python_foo"
 75
 76    run mkvenv --system-site-packages
 77
 78    assert $status equals 0
 79    assert "$lines[2]" contains 'Using $AUTOSWITCH_DEFAULT_PYTHON=python_foo'
 80    assert "$lines[3]" same_as "python_foo -m venv --system-site-packages $HOME/.virtualenvs/myproject-test"
 81}
 82
 83@test 'mkvenv - uses explicitly specified Python' {
 84    mkdir myproject
 85    cd myproject
 86
 87    run mkvenv --python python_foo --clear
 88
 89    assert $status equals 0
 90    assert "$lines[2]" same_as "python_foo -m venv --clear $HOME/.virtualenvs/myproject-test"
 91}
 92
 93@test 'mkvenv - (poetry project) runs correct command' {
 94    mkdir myproject
 95    cd myproject
 96    touch "poetry.lock"
 97
 98    run mkvenv
 99
100    assert $status equals 0
101    assert "$lines[1]" same_as "poetry install"
102    assert "$lines[2]" same_as "activating poetry"
103}
104
105@test 'mkvenv - (pipenv project) runs correct command' {
106    mkdir myproject
107    cd myproject
108    touch "Pipfile"
109
110    run mkvenv
111
112    assert $status equals 0
113    assert "$lines[1]" same_as "pipenv install --dev --editable ."
114    assert "$lines[2]" same_as "activating pipenv"
115}
116
117@test 'prints help message if Python is not setup' {
118    # Mock type to fail
119    function type() {
120        if [[ "$1" == "python3" ]]; then
121            return 1
122        fi
123        return 0
124    }
125
126    run mkvenv
127
128    assert $status equals 0
129    assert "$lines[1]" contains "zsh-autoswitch-virtualenv requires 'python3' to install this project!"
130    assert "$lines[2]" is_empty
131    assert "$lines[3]" contains "If this is already installed but you are still seeing this message,"
132    assert "$lines[4]" contains "then make sure the \e[1mpython3\e[0m command is in your PATH.\n"
133    assert "$lines[5]" is_empty
134    assert ${#lines} equals 4
135}
136
137@test 'prints help message and disables plugin if poetry not setup' {
138    touch "poetry.lock"
139
140    # Mock type to fail
141    function type() {
142        if [[ "$1" == "poetry" ]]; then
143            return 1
144        fi
145        return 0
146    }
147
148    run mkvenv
149
150    assert $status equals 0
151    assert "$lines[1]" contains "zsh-autoswitch-virtualenv requires 'poetry' to install this project!"
152    assert "$lines[2]" is_empty
153    assert "$lines[3]" contains "If this is already installed but you are still seeing this message,"
154    assert "$lines[4]" contains "then make sure the \e[1mpoetry\e[0m command is in your PATH.\n"
155    assert "$lines[5]" is_empty
156    assert ${#lines} equals 4
157}
158
159@test 'prints help message and disables plugin if pipenv not setup' {
160    touch "Pipfile"
161
162    # Mock type to fail
163    function type() {
164        if [[ "$1" == "pipenv" ]]; then
165            return 1
166        fi
167        return 0
168    }
169
170    run mkvenv
171
172    assert $status equals 0
173    assert "$lines[1]" contains "zsh-autoswitch-virtualenv requires 'pipenv' to install this project!"
174    assert "$lines[2]" is_empty
175    assert "$lines[3]" contains "If this is already installed but you are still seeing this message,"
176    assert "$lines[4]" contains "then make sure the \e[1mpipenv\e[0m command is in your PATH.\n"
177    assert "$lines[5]" is_empty
178    assert ${#lines} equals 4
179}