nushell/scripts/test_virtualenv.nu
zc he 44b7cfd696
refactor: tree-sitter-nu friendly alternative expressions (#15301)
# Description

Choose more tree-sitter-nu-friendly (if not better) expressions in nu
scripts.
The changes made in this PR all come from known issues of
`tree-sitter-nu`.

1. nested single/double quotes:
https://github.com/nushell/tree-sitter-nu/issues/125
2. module path of `use` command:
https://github.com/nushell/tree-sitter-nu/issues/165
3. where predicates of boolean column:
https://github.com/nushell/tree-sitter-nu/issues/177
4. `error make` keyword:
https://github.com/nushell/tree-sitter-nu/issues/179

Those issues are either hard to fix or "not planned" for syntactical
precision considerations ATM.

# User-Facing Changes

Should be none

# Tests + Formatting

# After Submitting
2025-03-12 08:48:19 -05:00

58 lines
2.0 KiB
Plaintext

if $nu.os-info.family == 'windows' {
# fix encoding on Windows https://stackoverflow.com/a/63573649
load-env {
PYTHONIOENCODING: utf-8
PYTHONLEGACYWINDOWSSTDIO: utf-8
}
}
let env_name = 'e-$ èрт🚒♞中片-j'
let paths = if $nu.os-info.family == 'windows' {
['Scripts', 'python.exe']
} else {
['bin', 'python']
}
let subdir = $paths.0
let exe = $paths.1
let test_lines = [
"python -c 'import sys; print(sys.executable)'" # 1
`python -c 'import os; import sys; v = os.environ.get("VIRTUAL_ENV"); print(v)'` # 2
$"overlay use '([$env.PWD $env_name $subdir activate.nu] | path join)'"
"python -c 'import sys; print(sys.executable)'" # 3
`python -c 'import os; import sys; v = os.environ.get("VIRTUAL_ENV"); print(v)'` # 4
"print $env.VIRTUAL_ENV_PROMPT" # 5
"deactivate"
"python -c 'import sys; print(sys.executable)'" # 6
`python -c 'import os; import sys; v = os.environ.get("VIRTUAL_ENV"); print(v)'` # 7
]
def main [] {
let orig_python_interpreter = (python -c 'import sys; print(sys.executable)')
let expected = [
$orig_python_interpreter # 1
"None" # 2
([$env.PWD $env_name $subdir $exe] | path join) # 3
([$env.PWD $env_name] | path join) # 4
$env_name # 5
$orig_python_interpreter # 6
"None" # 7
]
virtualenv $env_name
$test_lines | save script.nu
let out = (nu script.nu | lines)
let o = ($out | str trim | str join (char nl))
let e = ($expected | str trim | str join (char nl))
if $o != $e {
let msg = $"OUTPUT:\n($o)\n\nEXPECTED:\n($e)"
error make {msg: $"Output does not match the expected value:\n($msg)"}
}
rm script.nu
}