Files
task/completion/tests/wrapper.nu
Valentin Maerten b782d18a45 feat(completion): make the engine the default behind --completion
The engine shipped opt-in behind --new-completion, with the old scripts
still on --completion. That split was never released, so flip it now
rather than carry a third flag through a deprecation later.

--completion serves the engine wrappers; the hand-written scripts move to
completion/legacy/ and stay reachable via --legacy-completion for a
release or two.

.goreleaser.yml needed to follow: it packages the static files from
completion/ into the deb/rpm/apk contents and the Homebrew cask, so
leaving it untouched would have shipped the engine to anyone running
`eval "$(task --completion zsh)"` and the old scripts to everyone
installing from a package. The paths it references are unchanged and now
resolve to the wrappers. Its archive glob is narrowed at the same time,
so the test harness under completion/tests/ stops being shipped in the
release archives.
2026-08-29 10:55:16 +02:00

95 lines
3.2 KiB
Nu

#!/usr/bin/env nu
# Smoke-tests how the Nushell wrapper routes each directive. External completers
# only run in the interactive REPL, so the closure is called directly.
# Set up by run.sh: $env.TASK_FIXTURE, and `task` on PATH = the binary under test.
# `source` needs a parse-time constant path.
const TASK_NU = (path self "../nu/task-completions.nu")
# Installed before the wrapper is sourced, to assert the delegation path.
$env.config.completions.external.completer = {|spans| [{ value: $"prev:($spans | first)" }] }
source $TASK_NU
cd $env.TASK_FIXTURE
let completer = $env.config.completions.external.completer
def cands [spans: list<string>] {
let out = (do $completer $spans)
if $out == null { [] } else { $out | get value }
}
def has [label: string, spans: list<string>, value: string] {
let values = (cands $spans)
if $value in $values {
print $" ok ($label)"
0
} else {
print $" FAIL ($label) — '($value)' missing from: ($values | str join ' ')"
1
}
}
def hasnot [label: string, spans: list<string>, value: string] {
if $value in (cands $spans) {
print $" FAIL ($label) — '($value)' should be absent"
1
} else {
print $" ok ($label)"
0
}
}
def check [label: string, ok: bool] {
if $ok {
print $" ok ($label)"
0
} else {
print $" FAIL ($label)"
1
}
}
mut fails = 0
print "nu: :4 (NoFileComp) forwards candidates, offers no files"
$fails += (has "candidate forwarded" [task ""] "build")
$fails += (hasnot "no file fallback" [task ""] "notes.txt")
print "nu: filters candidates by the current word"
$fails += (has "prefix keeps match" [task b] "build")
$fails += (hasnot "prefix drops others" [task b] "deploy")
print "nu: :16 (FilterDirs) offers directories only"
$fails += (has "dir offered" [task --dir ""] $"sub(char path_sep)")
$fails += (hasnot "no plain file" [task --dir ""] "notes.txt")
print "nu: :8 (FilterFileExt) filters by extension"
$fails += (has "matching file" [task --taskfile ""] "Taskfile.yml")
$fails += (hasnot "non-matching file" [task --taskfile ""] "notes.txt")
print "nu: nested path completion keeps the directory prefix"
$fails += (has "prefix kept" [task --taskfile $"sub(char path_sep)"] $"sub(char path_sep)nested.yml")
print "nu: inline --flag=path keeps the --flag= prefix"
$fails += (has "inline nested" [task $"--taskfile=sub(char path_sep)"] $"--taskfile=sub(char path_sep)nested.yml")
$fails += (hasnot "inline non-matching" [task "--taskfile="] "--taskfile=notes.txt")
print "nu: :2|:32 (NoSpace|KeepOrder) keep the order the engine emitted"
let vars = (cands [task deploy ""])
$fails += (has "required var offered" [task deploy ""] "ENV=dev")
$fails += (check "declaration order kept" (($vars | enumerate | where item == "ENV=dev" | get 0.index) < ($vars | enumerate | where item == "REGION=" | get 0.index)))
print "nu: :0 (Default) returns null so Nushell completes files itself"
$fails += (check "null returned" ((do $completer [task build "--" ""]) == null))
print "nu: other commands go to the previously installed completer"
$fails += (has "delegated" [git status ""] "prev:git")
if $fails != 0 {
print $"nu: ($fails) failure\(s\)"
exit 1
}
print "nu: all passed"