|
| 1 | +#!/usr/bin/env nu |
| 2 | +# Test suite for auto-venv module on nushell 0.108+ |
| 3 | + |
| 4 | +use path_extensions.nu |
| 5 | +use venv_helpers.nu |
| 6 | + |
| 7 | +def main [] { |
| 8 | + let test_root = (mktemp -d | path expand) |
| 9 | + mut results = [] |
| 10 | + |
| 11 | + print $"nushell version: (version | get version)" |
| 12 | + print $"test root: ($test_root)" |
| 13 | + print "---" |
| 14 | + |
| 15 | + # Setup: create mock project structure |
| 16 | + let trigger_name = ".__test-trigger.nu" |
| 17 | + let trigger_src = ($env.FILE_PWD | path join "venvs" "python-venv.nu") |
| 18 | + |
| 19 | + mkdir ($test_root | path join "project_a" ".venv" "bin") |
| 20 | + mkdir ($test_root | path join "project_a" "sub") |
| 21 | + mkdir ($test_root | path join "project_b") |
| 22 | + mkdir ($test_root | path join "nested" "outer" ".venv" "bin") |
| 23 | + mkdir ($test_root | path join "nested" "outer" "inner" ".venv" "bin") |
| 24 | + |
| 25 | + ln -sf $trigger_src ($test_root | path join "project_a" $trigger_name) |
| 26 | + ln -sf $trigger_src ($test_root | path join "project_b" $trigger_name) |
| 27 | + ln -sf $trigger_src ($test_root | path join "nested" "outer" $trigger_name) |
| 28 | + ln -sf $trigger_src ($test_root | path join "nested" "outer" "inner" $trigger_name) |
| 29 | + |
| 30 | + # Test 1: path find-sub locates trigger file |
| 31 | + let result = (path_extensions path find-sub ($test_root | path join "project_a") $trigger_name --type ["symlink", "file"]) |
| 32 | + let ok = ($result == ($test_root | path join "project_a" $trigger_name)) |
| 33 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): path find-sub finds trigger in project_a" |
| 34 | + $results = ($results | append $ok) |
| 35 | + |
| 36 | + # Test 2: path find-sub returns null when not found |
| 37 | + let result = (path_extensions path find-sub "/tmp" $trigger_name) |
| 38 | + let ok = ($result | is-empty) |
| 39 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): path find-sub returns null for missing trigger" |
| 40 | + $results = ($results | append $ok) |
| 41 | + |
| 42 | + # Test 3: path find-sub returns deepest match (last not first) |
| 43 | + let result = (path_extensions path find-sub ($test_root | path join "nested" "outer" "inner") $trigger_name --type ["symlink", "file"]) |
| 44 | + let ok = ($result == ($test_root | path join "nested" "outer" "inner" $trigger_name)) |
| 45 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): path find-sub returns deepest match" |
| 46 | + if not $ok { print $" got: ($result)" } |
| 47 | + $results = ($results | append $ok) |
| 48 | + |
| 49 | + # Test 4: path find-sub finds .venv |
| 50 | + let result = (path_extensions path find-sub ($test_root | path join "project_a") ".venv") |
| 51 | + let ok = ($result | is-empty | not $in) |
| 52 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): path find-sub locates .venv" |
| 53 | + $results = ($results | append $ok) |
| 54 | + |
| 55 | + # Test 5: path find-sub returns null for .venv in project_b (no .venv there) |
| 56 | + let result = (path_extensions path find-sub ($test_root | path join "project_b") ".venv") |
| 57 | + let ok = ($result | is-empty) |
| 58 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): path find-sub returns null for missing .venv" |
| 59 | + $results = ($results | append $ok) |
| 60 | + |
| 61 | + # Test 6: venv-is-active returns false when no overlay |
| 62 | + let ok = (not (venv_helpers venv-is-active)) |
| 63 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): venv-is-active false with no active overlay" |
| 64 | + $results = ($results | append $ok) |
| 65 | + |
| 66 | + # Test 7: has-entered-venv condition check |
| 67 | + $env.AUTO_VENV_TRIGGER = $trigger_name |
| 68 | + let ok = (venv_helpers has-entered-venv ($test_root | path join "project_a")) |
| 69 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): has-entered-venv detects trigger in project_a" |
| 70 | + $results = ($results | append $ok) |
| 71 | + |
| 72 | + # Test 8: has-entered-venv returns false for dir without trigger in ancestry |
| 73 | + let ok = (not (venv_helpers has-entered-venv $test_root)) |
| 74 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): has-entered-venv false for dir without trigger in ancestry" |
| 75 | + $results = ($results | append $ok) |
| 76 | + |
| 77 | + # Test 9: default-hooks does not wrap list in list |
| 78 | + $env.AUTO_VENV_TRIGGER = $trigger_name |
| 79 | + $env.config.hooks.env_change = { PWD: [{|| null}] } |
| 80 | + source-env auto-venv.nu |
| 81 | + let hook_types = ($env.config.hooks.env_change.PWD | each {|h| $h | describe }) |
| 82 | + let ok = (not ($hook_types | any {|t| $t starts-with "list<" })) |
| 83 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): no nested list types in PWD hooks" |
| 84 | + if not $ok { print $" types: ($hook_types)" } |
| 85 | + $results = ($results | append $ok) |
| 86 | + |
| 87 | + # Test 10: overlay use resolves via NU_LIB_DIRS |
| 88 | + $env.NU_LIB_DIRS = ($env.NU_LIB_DIRS | append ($env.FILE_PWD | path join "venvs")) |
| 89 | + overlay use python-venv.nu as __auto_venv_test |
| 90 | + let ok = ('__auto_venv_test' in (overlay list | where active | get name)) |
| 91 | + print $"(if $ok { 'PASS' } else { 'FAIL' }): overlay use resolves via NU_LIB_DIRS" |
| 92 | + overlay hide __auto_venv_test --keep-env [PWD] |
| 93 | + $results = ($results | append $ok) |
| 94 | + |
| 95 | + # Cleanup |
| 96 | + rm -rf $test_root |
| 97 | + |
| 98 | + # Summary |
| 99 | + let passed = ($results | where $it | length) |
| 100 | + let failed = ($results | where (not $it) | length) |
| 101 | + print "---" |
| 102 | + print $"Results: ($passed) passed, ($failed) failed" |
| 103 | + if $failed > 0 { exit 1 } |
| 104 | +} |
0 commit comments