-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-find-venv.sh
More file actions
executable file
·343 lines (284 loc) · 8.86 KB
/
test-find-venv.sh
File metadata and controls
executable file
·343 lines (284 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
# Focused test for the find_venv function only
# This extracts and tests just the core functionality
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Test directory
TEST_DIR="/tmp/zsh-uv-env-test-$$"
# Cleanup function
cleanup() {
if [[ -d "$TEST_DIR" ]]; then
rm -rf "$TEST_DIR"
fi
}
trap cleanup EXIT
# Helper functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[PASS]${NC} $1"
TESTS_PASSED=$((TESTS_PASSED + 1))
}
log_fail() {
echo -e "${RED}[FAIL]${NC} $1"
TESTS_FAILED=$((TESTS_FAILED + 1))
}
run_test() {
local test_name="$1"
local test_function="$2"
echo
log_info "Running test: $test_name"
TESTS_RUN=$((TESTS_RUN + 1))
# Run test in a way that doesn't exit the script on failure
set +e
$test_function
local result=$?
set -e
if [[ $result -eq 0 ]]; then
log_success "$test_name"
else
log_fail "$test_name"
fi
}
# Extract the find_venv function from the plugin
create_find_venv_function() {
cat << 'EOF'
# Function to find nearest .venv or venv directory
find_venv() {
local current_dir="$PWD"
local home_dir="$HOME"
local root_dir="/"
local stop_dir="$root_dir"
# If we're under home directory, stop at home
if [[ "$current_dir" == "$home_dir"* ]]; then
stop_dir="$home_dir"
fi
while [[ "$current_dir" != "$stop_dir" ]]; do
for _v in .venv venv; do
if [[ -d "$current_dir/$_v" && -r "$current_dir/$_v/bin/activate" ]]; then
echo "$current_dir/$_v"
return 0
fi
done
current_dir="$(dirname "$current_dir")"
done
# Check stop_dir itself
for _v in .venv venv; do
if [[ -d "$stop_dir/$_v" && -r "$stop_dir/$_v/bin/activate" ]]; then
echo "$stop_dir/$_v"
return 0
fi
done
return 1
}
EOF
}
# Test: find_venv function with .venv directory
test_dotenv_directory() {
local test_dir="$TEST_DIR/dotenv-test"
mkdir -p "$test_dir"
# Create .venv with activate script
mkdir -p "$test_dir/.venv/bin"
touch "$test_dir/.venv/bin/activate"
chmod +r "$test_dir/.venv/bin/activate"
# Test find_venv function
cd "$test_dir"
local result
result=$(bash -c "$(create_find_venv_function); find_venv")
local expected="$test_dir/.venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Expected '$expected', got '$result'"
return 1
fi
}
# Test: find_venv function with venv directory
test_venv_directory() {
local test_dir="$TEST_DIR/venv-test"
mkdir -p "$test_dir"
# Create venv with activate script
mkdir -p "$test_dir/venv/bin"
touch "$test_dir/venv/bin/activate"
chmod +r "$test_dir/venv/bin/activate"
# Test find_venv function
cd "$test_dir"
local result
result=$(bash -c "$(create_find_venv_function); find_venv")
local expected="$test_dir/venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Expected '$expected', got '$result'"
return 1
fi
}
# Test: Priority order when both .venv and venv exist
test_priority_order() {
local test_dir="$TEST_DIR/priority-test"
mkdir -p "$test_dir"
# Create both .venv and venv with activate scripts
mkdir -p "$test_dir/.venv/bin"
touch "$test_dir/.venv/bin/activate"
chmod +r "$test_dir/.venv/bin/activate"
mkdir -p "$test_dir/venv/bin"
touch "$test_dir/venv/bin/activate"
chmod +r "$test_dir/venv/bin/activate"
# Test find_venv function - should prefer .venv
cd "$test_dir"
local result
result=$(bash -c "$(create_find_venv_function); find_venv")
local expected="$test_dir/.venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Expected '.venv' to be preferred, but got '$result'"
return 1
fi
}
# Test: Validation of activate script existence
test_activate_script_validation() {
local test_dir="$TEST_DIR/broken-test"
mkdir -p "$test_dir"
# Create venv directory without activate script
mkdir -p "$test_dir/venv/bin"
# Note: No activate script created
# Test find_venv function - should fail
cd "$test_dir"
local result
local exit_code
result=$(bash -c "$(create_find_venv_function); find_venv" 2>/dev/null)
exit_code=$?
if [[ $exit_code -ne 0 && -z "$result" ]]; then
return 0
else
log_fail "Expected find_venv to fail for directory without activate script, but it returned: '$result'"
return 1
fi
}
# Test: No virtual environment found
test_no_venv_found() {
local test_dir="$TEST_DIR/no-venv-test"
mkdir -p "$test_dir"
# Test find_venv function in directory with no venv
cd "$test_dir"
local result
local exit_code
result=$(bash -c "$(create_find_venv_function); find_venv" 2>/dev/null)
exit_code=$?
if [[ $exit_code -ne 0 && -z "$result" ]]; then
return 0
else
log_fail "Expected find_venv to fail when no venv exists, but it returned: '$result'"
return 1
fi
}
# Test: Nested directory search
test_nested_directory_search() {
local test_dir="$TEST_DIR/nested-test"
mkdir -p "$test_dir/deep/nested/directory"
# Create .venv in parent directory
mkdir -p "$test_dir/.venv/bin"
touch "$test_dir/.venv/bin/activate"
chmod +r "$test_dir/.venv/bin/activate"
# Test find_venv function from nested directory
cd "$test_dir/deep/nested/directory"
local result
result=$(bash -c "$(create_find_venv_function); find_venv")
local expected="$test_dir/.venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Expected to find parent .venv from nested directory. Expected '$expected', got '$result'"
return 1
fi
}
# Test: Home directory boundary
test_home_directory_boundary() {
local test_dir="$TEST_DIR/home-boundary-test"
mkdir -p "$test_dir"
# Create a fake home directory structure
local fake_home="$test_dir/fake-home"
local project_dir="$fake_home/project"
mkdir -p "$project_dir"
# Create .venv in fake home
mkdir -p "$fake_home/.venv/bin"
touch "$fake_home/.venv/bin/activate"
chmod +r "$fake_home/.venv/bin/activate"
# Test from project directory with HOME set to fake home
cd "$project_dir"
local result
result=$(bash -c "HOME='$fake_home'; $(create_find_venv_function); find_venv")
local expected="$fake_home/.venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Failed to respect home directory boundary. Expected '$expected', got '$result'"
return 1
fi
}
# Test with actual uv environments
test_uv_integration() {
local test_dir="$TEST_DIR/uv-integration-test"
mkdir -p "$test_dir"
cd "$test_dir"
# Check if uv is available
if ! command -v uv >/dev/null 2>&1; then
echo -e "${YELLOW}[SKIP]${NC} uv not available, skipping uv integration test"
return 0
fi
# Create actual uv environment
uv venv .venv >/dev/null 2>&1
# Test find_venv function with real uv environment
local result
result=$(bash -c "$(create_find_venv_function); find_venv")
local expected="$test_dir/.venv"
if [[ "$result" == "$expected" ]]; then
return 0
else
log_fail "Failed to detect real uv environment. Expected '$expected', got '$result'"
return 1
fi
}
# Main test execution
main() {
echo "Testing find_venv function for zsh-uv-env plugin"
echo "==============================================="
# Setup
mkdir -p "$TEST_DIR"
# Run all tests
run_test "Test .venv directory detection" test_dotenv_directory
run_test "Test venv directory detection" test_venv_directory
run_test "Test priority order (.venv preferred)" test_priority_order
run_test "Test activate script validation" test_activate_script_validation
run_test "Test no virtual environment found" test_no_venv_found
run_test "Test nested directory search" test_nested_directory_search
run_test "Test home directory boundary" test_home_directory_boundary
run_test "Test uv integration" test_uv_integration
# Summary
echo
echo "Test Results Summary"
echo "==================="
echo "Tests run: $TESTS_RUN"
echo -e "Tests passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Tests failed: ${RED}$TESTS_FAILED${NC}"
if [[ $TESTS_FAILED -eq 0 ]]; then
echo -e "\n${GREEN}All tests passed! ✅${NC}"
exit 0
else
echo -e "\n${RED}Some tests failed! ❌${NC}"
exit 1
fi
}
# Run main function
main "$@"