Parent directory

test_plugin.zunit

2326 bytes
 1#!/usr/bin/env zunit
 2
 3@setup {
 4    if [[ ! -d "$HOME/.virtualenvs/foobar/" ]]; then
 5        echo "Creating test virtualenv"
 6        run virtualenv "$HOME/.virtualenvs/foobar" --python=python3
 7    fi
 8    unset VIRTUAL_ENV
 9    unset AUTOSWITCH_MESSAGE_FORMAT
10    PYTHON_VERSION="$(python3 --version)"
11
12    LANG=".UTF-8"
13}
14
15@test 'enabled correctly' {
16  function type {
17    return 0
18  }
19
20  # use run the first time to capture output
21  run load ../autoswitch_virtualenv.plugin.zsh
22
23  assert "$output" is_empty
24
25  # use load the next time to check its loaded into hooks correctly
26  load ../autoswitch_virtualenv.plugin.zsh
27
28  assert $output is_empty
29  assert '_autoswitch_startup' in $precmd_functions
30}
31
32@test 'correct version exported' {
33    unset AUTOSWITCH_DEFAULTENV
34    load ../autoswitch_virtualenv.plugin.zsh
35
36    git_version="$(git tag --list | sort -V | tail -1)"
37    git tag --list
38
39    assert "$AUTOSWITCH_VERSION" is_not_empty
40    assert "$AUTOSWITCH_VERSION" same_as "$git_version"
41}
42
43
44@test 'plugin - does nothing if switching to directory without .venv' {
45    unset AUTOSWITCH_DEFAULTENV
46    load ../autoswitch_virtualenv.plugin.zsh
47
48    TEMP_DIR="$(mktemp -d)"
49
50    run cd "$TEMP_DIR"
51
52    assert $status equals 0
53    assert "$output" is_empty
54}
55
56@test 'plugin - switches env when .venv found' {
57    unset AUTOSWITCH_DEFAULTENV
58    load ../autoswitch_virtualenv.plugin.zsh
59    # zsh-hook precmd does not seem to work correctly in tests
60    # so we need to manually enable the plugin
61    enable_autoswitch_virtualenv
62
63    TEMP_DIR="$(mktemp -d)"
64    echo "foobar" > "$TEMP_DIR/.venv"
65    chmod 600 "$TEMP_DIR/.venv"
66
67    run cd "$TEMP_DIR"
68
69    assert $status equals 0
70    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
71
72    mkdir "$TEMP_DIR/foo"
73
74    # switches even in project subdirectories
75    run cd "$TEMP_DIR/foo"
76
77    assert $status equals 0
78    assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
79}
80
81
82@test 'plugin - check randstr' {
83    unset AUTOSWITCH_DEFAULTENV
84    load ../autoswitch_virtualenv.plugin.zsh
85
86    # check that $(randstr n) returns a string of length n
87    assert "${#$(randstr)}" equals 4
88
89    assert "$(randstr)" different_to "$(randstr)"
90}