test_maybeworkon.zunit
3301 bytes
1#!/usr/bin/env zunit
2
3
4@setup {
5 TEST_VIRTUALENV="$HOME/.virtualenvs/foobar/"
6 PYTHON_VERSION="$(python3 --version 2>&1)"
7
8 if [[ ! -d "$TEST_VIRTUALENV" ]]; then
9 printf "Creating test virtualenv: %s" % "$PYTHON_VERSION"
10 run python -m virtualenv "$TEST_VIRTUALENV" --python=python3
11 fi
12
13 unset AUTOSWITCH_MESSAGE_FORMAT
14
15 export DISABLE_AUTOSWITCH_VENV="1"
16 load "../autoswitch_virtualenv.plugin.zsh"
17 TARGET="$(mktemp -d)"
18
19 LANG=".UTF-8"
20}
21
22@teardown {
23 rm -rf "$TARGET"
24}
25
26@test '_maybeworkon - do not activate paths which are potentially insecure' {
27 VIRTUAL_ENV=""
28
29 run _maybeworkon "$TARGET/../../../" virtualenv
30
31 assert $state equals 0
32
33 # first line would be the "switching virtualenv: ...."
34 assert "${lines[2]}" same_as "AUTOSWITCH WARNING: target virtualenv contains invalid characters"
35 assert "${lines[3]}" same_as "virtualenv activation cancelled"
36}
37
38@test '_maybeworkon - error message if virtualenv can not be found' {
39 VIRTUAL_ENV=""
40
41 run _maybeworkon $HOME/.virtualenvs/idontexist virtualenv
42
43 assert $state equals 0
44 assert "${lines[1]}" same_as "Unable to find \e[35midontexist\e[0m virtualenv"
45 assert "${lines[2]}" same_as "If the issue persists run \e[35mrmvenv && mkvenv\e[0m in this directory"
46}
47
48
49@test '_maybeworkon - switches virtualenv if nothing is activated' {
50 VIRTUAL_ENV=""
51
52 run _maybeworkon "$TEST_VIRTUALENV" virtualenv
53
54 assert $state equals 0
55 assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
56}
57
58@test '_maybeworkon - custom message' {
59 VIRTUAL_ENV=""
60 AUTOSWITCH_MESSAGE_FORMAT="Hello %venv_type - this is %venv_name (%py_version)"
61
62 run _maybeworkon "$TEST_VIRTUALENV" cheese
63
64 assert $state equals 0
65 assert "$output" same_as "Hello cheese - this is foobar ($PYTHON_VERSION)"
66}
67
68@test '_maybeworkon - no emoji on non utf-8 LANG' {
69 VIRTUAL_ENV=""
70 LANG="ASCII"
71
72 run _maybeworkon "$TEST_VIRTUALENV" virtualenv
73
74 assert $state equals 0
75 assert "$output" same_as "Switching to \e[1mvirtualenv\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
76}
77
78@test '_maybeworkon - switches virtualenv if current virtualenv is different' {
79 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
80
81 run _maybeworkon "$TEST_VIRTUALENV" virtualfoo
82
83 assert $state equals 0
84 assert "$output" contains "Switching to \e[1mvirtualfoo\e[0m project: \e[1m\e[35mfoobar\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
85}
86
87@test '_maybeworkon - switches virtualenv if current virtualenv is different (silent)' {
88 VIRTUAL_ENV="$HOME/.virtualenvs/default_venv"
89 AUTOSWITCH_SILENT="1"
90
91 run _maybeworkon "$TEST_VIRTUALENV" virtualenv
92
93 assert $state equals 0
94 assert "$output" is_empty
95}
96
97@test '_maybeworkon - does not switch to already activated virtualenv' {
98 VIRTUAL_ENV="$HOME/.virtualenvs/bwaaah"
99
100 run _maybeworkon "$VIRTUAL_ENV" virtualenv
101
102 assert $state equals 0
103 assert "$output" is_empty
104}
105
106@test '_maybeworkon - pipenv name cleaned' [
107 run _maybeworkon hello-world-foo-de31f pipenv
108
109 assert $state equals 0
110 assert "$output" same_as "Switching to \e[1mpipenv\e[0m project: \e[1m\e[35mhello-world-foo\e[0m \e[32m[$PYTHON_VERSION]\e[0m"
111
112}