refactor(test): Move nushell tests to completest

This commit is contained in:
Ed Page 2023-07-21 14:15:26 -05:00
parent 053c778e98
commit abdb1d513e
9 changed files with 694 additions and 411 deletions

18
Cargo.lock generated
View file

@ -570,12 +570,7 @@ version = "4.3.1"
dependencies = [ dependencies = [
"clap 4.3.19", "clap 4.3.19",
"clap_complete", "clap_complete",
"nu-cli", "completest",
"nu-command",
"nu-parser",
"nu-protocol",
"nu-test-support",
"reedline",
"snapbox", "snapbox",
] ]
@ -646,11 +641,18 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "completest" name = "completest"
version = "0.0.14" version = "0.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa515ddcd88ba6e25ed72581b38284255c9126ff2ca4fd81525f72a59187a985" checksum = "426bfb709a07e01f5ee02dfccf42a73ef64a03ca6295a82e5c1c211a302a770e"
dependencies = [ dependencies = [
"dunce",
"nu-cli",
"nu-command",
"nu-parser",
"nu-protocol",
"nu-test-support",
"ptyprocess", "ptyprocess",
"reedline",
"vt100", "vt100",
] ]

View file

@ -43,7 +43,7 @@ unicode-xid = { version = "0.2.2", optional = true }
snapbox = { version = "0.4.11", features = ["diff", "path", "examples"] } snapbox = { version = "0.4.11", features = ["diff", "path", "examples"] }
# Cutting out `filesystem` feature # Cutting out `filesystem` feature
trycmd = { version = "0.14.16", default-features = false, features = ["color-auto", "diff", "examples"] } trycmd = { version = "0.14.16", default-features = false, features = ["color-auto", "diff", "examples"] }
completest = "0.0.14" completest = "0.0.16"
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] } clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] }
[[example]] [[example]]

View file

@ -296,6 +296,96 @@ pub fn assert_matches_path(
.matches_path(expected_path, buf); .matches_path(expected_path, buf);
} }
pub fn register_example(name: &str, shell: completest::Shell) {
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
let scratch_path = scratch.path().unwrap();
let shell_name = shell.name();
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();
let registration = std::process::Command::new(&bin_path)
.arg(format!("--generate={shell_name}"))
.output()
.unwrap();
assert!(
registration.status.success(),
"{}",
String::from_utf8_lossy(&registration.stderr)
);
let registration = std::str::from_utf8(&registration.stdout).unwrap();
assert!(!registration.is_empty());
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
runtime.register(name, registration).unwrap();
snapbox::assert_subset_eq(home, scratch_path);
scratch.close().unwrap();
}
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
let shell_name = shell.name();
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
let scratch = snapbox::path::PathFixture::mutable_temp()
.unwrap()
.with_template(&home)
.unwrap();
let home = scratch.path().unwrap().to_owned();
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();
let runtime = shell.with_home(bin_root, home).unwrap();
Box::new(ScratchRuntime {
_scratch: scratch,
runtime,
})
}
#[derive(Debug)]
struct ScratchRuntime {
_scratch: snapbox::path::PathFixture,
runtime: Box<dyn completest::Runtime>,
}
impl completest::Runtime for ScratchRuntime {
fn home(&self) -> &std::path::Path {
self.runtime.home()
}
fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
self.runtime.register(name, content)
}
fn complete(&mut self, input: &str, term: &completest::Term) -> std::io::Result<String> {
let output = self.runtime.complete(input, term)?;
// HACK: elvish prints and clears this message when a completer takes too long which is
// dependent on a lot of factors, making this show up or no sometimes (especially if we
// aren't clearing the screen properly for fish)
let output = output.replace("\nCOMPLETING argument\n", "\n");
Ok(output)
}
}
pub fn has_command(command: &str) -> bool { pub fn has_command(command: &str) -> bool {
let output = match std::process::Command::new(command) let output = match std::process::Command::new(command)
.arg("--version") .arg("--version")
@ -341,87 +431,6 @@ pub fn has_command(command: &str) -> bool {
true true
} }
#[cfg(unix)]
pub fn register_example(name: &str, shell: completest::Shell) {
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
let scratch_path = scratch.path().unwrap();
let shell_name = shell.name();
let home = std::path::Path::new("tests/snapshots/home")
.join(name)
.join(shell_name);
let bin_path = snapbox::cmd::compile_example(name, []).unwrap();
let bin_root = bin_path.parent().unwrap().to_owned();
let registration = std::process::Command::new(&bin_path)
.arg(format!("--generate={shell_name}"))
.output()
.unwrap();
assert!(
registration.status.success(),
"{}",
String::from_utf8_lossy(&registration.stderr)
);
let registration = std::str::from_utf8(&registration.stdout).unwrap();
assert!(!registration.is_empty());
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
runtime.register(name, registration).unwrap();
snapbox::assert_subset_eq(home, scratch_path);
scratch.close().unwrap();
}
#[cfg(unix)]
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
let shell_name = shell.name();
let home = std::path::Path::new("tests/snapshots/home")
.join(name)
.join(shell_name);
let scratch = snapbox::path::PathFixture::mutable_temp()
.unwrap()
.with_template(&home)
.unwrap();
let home = scratch.path().unwrap().to_owned();
let bin_path = snapbox::cmd::compile_example(name, []).unwrap();
let bin_root = bin_path.parent().unwrap().to_owned();
let runtime = shell.with_home(bin_root, home);
Box::new(ScratchRuntime {
_scratch: scratch,
runtime,
})
}
#[cfg(unix)]
struct ScratchRuntime {
_scratch: snapbox::path::PathFixture,
runtime: Box<dyn completest::Runtime>,
}
#[cfg(unix)]
impl completest::Runtime for ScratchRuntime {
fn home(&self) -> &std::path::Path {
self.runtime.home()
}
fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
self.runtime.register(name, content)
}
fn complete(&mut self, input: &str, term: &completest::Term) -> std::io::Result<String> {
let output = self.runtime.complete(input, term)?;
// HACK: elvish prints and clears this message when a completer takes too long which is
// dependent on a lot of factors, making this show up or no sometimes (especially if we
// aren't clearing the screen properly for fish)
let output = output.replace("\nCOMPLETING argument\n", "\n");
Ok(output)
}
}
/// Whether or not this running in a Continuous Integration environment. /// Whether or not this running in a Continuous Integration environment.
fn is_ci() -> bool { fn is_ci() -> bool {
// Consider using `tracked_env` instead of option_env! when it is stabilized. // Consider using `tracked_env` instead of option_env! when it is stabilized.

View file

@ -35,11 +35,6 @@ clap = { path = "../", version = "4.0.0", default-features = false, features = [
clap_complete = { path = "../clap_complete", version = "4.0.0" } clap_complete = { path = "../clap_complete", version = "4.0.0" }
[dev-dependencies] [dev-dependencies]
snapbox = { version = "0.4.11", features = ["diff"] } snapbox = { version = "0.4.11", features = ["diff", "examples", "path"] }
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] } clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
nu-cli = "0.78.0" completest = { version = "0.0.16", features = ["nu"] }
nu-command = "0.78.0"
nu-parser = "0.78.0"
nu-protocol = "0.78.0"
nu-test-support = "0.78.0"
reedline = "0.18.0"

View file

@ -0,0 +1,170 @@
use clap_complete::generate;
use clap_complete_nushell::Nushell;
fn main() {
let matches = cli().get_matches();
if matches.contains_id("generate") {
let mut cmd = cli();
generate(Nushell, &mut cmd, "test", &mut std::io::stdout());
} else {
println!("{:?}", matches);
}
}
fn cli() -> clap::Command {
clap::Command::new("test")
.version("3.0")
.propagate_version(true)
.args([
clap::Arg::new("global")
.long("global")
.global(true)
.action(clap::ArgAction::SetTrue)
.help("everywhere"),
clap::Arg::new("generate").long("generate").help("generate"),
])
.subcommands([
clap::Command::new("action").args([
clap::Arg::new("set-true")
.long("set-true")
.action(clap::ArgAction::SetTrue)
.help("bool"),
clap::Arg::new("set")
.long("set")
.action(clap::ArgAction::Set)
.help("value"),
clap::Arg::new("count")
.long("count")
.action(clap::ArgAction::Count)
.help("number"),
clap::Arg::new("choice")
.long("choice")
.value_parser(["first", "second"])
.help("enum"),
]),
clap::Command::new("quote")
.args([
clap::Arg::new("single-quotes")
.long("single-quotes")
.action(clap::ArgAction::SetTrue)
.help("Can be 'always', 'auto', or 'never'"),
clap::Arg::new("double-quotes")
.long("double-quotes")
.action(clap::ArgAction::SetTrue)
.help("Can be \"always\", \"auto\", or \"never\""),
clap::Arg::new("backticks")
.long("backticks")
.action(clap::ArgAction::SetTrue)
.help("For more information see `echo test`"),
clap::Arg::new("backslash")
.long("backslash")
.action(clap::ArgAction::SetTrue)
.help("Avoid '\\n'"),
clap::Arg::new("brackets")
.long("brackets")
.action(clap::ArgAction::SetTrue)
.help("List packages [filter]"),
clap::Arg::new("expansions")
.long("expansions")
.action(clap::ArgAction::SetTrue)
.help("Execute the shell command with $SHELL"),
])
.subcommands([
clap::Command::new("cmd-single-quotes")
.about("Can be 'always', 'auto', or 'never'"),
clap::Command::new("cmd-double-quotes")
.about("Can be \"always\", \"auto\", or \"never\""),
clap::Command::new("cmd-backticks")
.about("For more information see `echo test`"),
clap::Command::new("cmd-backslash").about("Avoid '\\n'"),
clap::Command::new("cmd-brackets").about("List packages [filter]"),
clap::Command::new("cmd-expansions")
.about("Execute the shell command with $SHELL"),
]),
clap::Command::new("value").args([
clap::Arg::new("delim").long("delim").value_delimiter(','),
clap::Arg::new("tuple").long("tuple").num_args(2),
clap::Arg::new("require-eq")
.long("require-eq")
.require_equals(true),
clap::Arg::new("term").num_args(1..).value_terminator(";"),
]),
clap::Command::new("pacman").subcommands([
clap::Command::new("one").long_flag("one").short_flag('o'),
clap::Command::new("two").long_flag("two").short_flag('t'),
]),
clap::Command::new("last")
.args([clap::Arg::new("first"), clap::Arg::new("free").last(true)]),
clap::Command::new("alias").args([
clap::Arg::new("flag")
.short('f')
.visible_short_alias('F')
.long("flag")
.action(clap::ArgAction::SetTrue)
.visible_alias("flg")
.help("cmd flag"),
clap::Arg::new("option")
.short('o')
.visible_short_alias('O')
.long("option")
.visible_alias("opt")
.help("cmd option")
.action(clap::ArgAction::Set),
clap::Arg::new("positional"),
]),
clap::Command::new("hint").args([
clap::Arg::new("choice")
.long("choice")
.action(clap::ArgAction::Set)
.value_parser(["bash", "fish", "zsh"]),
clap::Arg::new("unknown")
.long("unknown")
.value_hint(clap::ValueHint::Unknown),
clap::Arg::new("other")
.long("other")
.value_hint(clap::ValueHint::Other),
clap::Arg::new("path")
.long("path")
.short('p')
.value_hint(clap::ValueHint::AnyPath),
clap::Arg::new("file")
.long("file")
.short('f')
.value_hint(clap::ValueHint::FilePath),
clap::Arg::new("dir")
.long("dir")
.short('d')
.value_hint(clap::ValueHint::DirPath),
clap::Arg::new("exe")
.long("exe")
.short('e')
.value_hint(clap::ValueHint::ExecutablePath),
clap::Arg::new("cmd_name")
.long("cmd-name")
.value_hint(clap::ValueHint::CommandName),
clap::Arg::new("cmd")
.long("cmd")
.short('c')
.value_hint(clap::ValueHint::CommandString),
clap::Arg::new("command_with_args")
.action(clap::ArgAction::Set)
.num_args(1..)
.trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments),
clap::Arg::new("user")
.short('u')
.long("user")
.value_hint(clap::ValueHint::Username),
clap::Arg::new("host")
.short('H')
.long("host")
.value_hint(clap::ValueHint::Hostname),
clap::Arg::new("url")
.long("url")
.value_hint(clap::ValueHint::Url),
clap::Arg::new("email")
.long("email")
.value_hint(clap::ValueHint::EmailAddress),
]),
])
}

View file

@ -1,3 +1,5 @@
#![allow(dead_code)] // shared with other test modules
use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint}; use clap::{builder::PossibleValue, Arg, ArgAction, Command, ValueHint};
pub fn basic_command(name: &'static str) -> Command { pub fn basic_command(name: &'static str) -> Command {
@ -244,7 +246,7 @@ pub fn value_hint_command(name: &'static str) -> Command {
pub fn assert_matches_path( pub fn assert_matches_path(
expected_path: impl AsRef<std::path::Path>, expected_path: impl AsRef<std::path::Path>,
gen: impl clap_complete::Generator, gen: impl clap_complete::Generator,
mut cmd: Command, mut cmd: clap::Command,
name: &'static str, name: &'static str,
) { ) {
let mut buf = vec![]; let mut buf = vec![];
@ -255,3 +257,91 @@ pub fn assert_matches_path(
.normalize_paths(false) .normalize_paths(false)
.matches_path(expected_path, buf); .matches_path(expected_path, buf);
} }
pub fn register_example(name: &str, shell: completest::Shell) {
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
let scratch_path = scratch.path().unwrap();
let shell_name = shell.name();
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();
let registration = std::process::Command::new(&bin_path)
.arg(format!("--generate={shell_name}"))
.output()
.unwrap();
assert!(
registration.status.success(),
"{}",
String::from_utf8_lossy(&registration.stderr)
);
let registration = std::str::from_utf8(&registration.stdout).unwrap();
assert!(!registration.is_empty());
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
runtime.register(name, registration).unwrap();
snapbox::assert_subset_eq(home, scratch_path);
scratch.close().unwrap();
}
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
let shell_name = shell.name();
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
std::fs::create_dir_all(&home).unwrap();
let scratch = snapbox::path::PathFixture::immutable(&home);
let home = scratch.path().unwrap().to_owned();
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();
let runtime = shell.with_home(bin_root, home).unwrap();
Box::new(ScratchRuntime {
_scratch: scratch,
runtime,
})
}
#[derive(Debug)]
struct ScratchRuntime {
_scratch: snapbox::path::PathFixture,
runtime: Box<dyn completest::Runtime>,
}
impl completest::Runtime for ScratchRuntime {
fn home(&self) -> &std::path::Path {
self.runtime.home()
}
fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
self.runtime.register(name, content)
}
fn complete(&mut self, input: &str, term: &completest::Term) -> std::io::Result<String> {
let output = self.runtime.complete(input, term)?;
// HACK: elvish prints and clears this message when a completer takes too long which is
// dependent on a lot of factors, making this show up or no sometimes (especially if we
// aren't clearing the screen properly for fish)
let output = output.replace("\nCOMPLETING argument\n", "\n");
Ok(output)
}
}

View file

@ -1,326 +1,85 @@
use std::fs::File; mod common;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use nu_cli::NuCompleter; #[test]
use nu_command::create_default_context; fn register_completion() {
use nu_parser::parse; common::register_example("test", completest::Shell::Nu);
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Value,
};
use reedline::{Completer as _, Suggestion};
// creates a new engine with the current path into the completions fixtures folder
fn new_engine() -> (PathBuf, EngineState, Stack) {
// Target folder inside assets
let mut dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
dir.push("snapshots");
let mut dir_str = dir
.clone()
.into_os_string()
.into_string()
.unwrap_or_default();
dir_str.push(std::path::MAIN_SEPARATOR);
// Create a new engine with default context
let mut engine_state = create_default_context();
// New stack
let mut stack = Stack::new();
// Add pwd as env var
stack.add_env_var(
"PWD".to_string(),
Value::String {
val: dir_str.clone(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);
#[cfg(windows)]
stack.add_env_var(
"Path".to_string(),
Value::String {
val: "c:\\some\\path;c:\\some\\other\\path".to_string(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);
#[cfg(not(windows))]
stack.add_env_var(
"PATH".to_string(),
Value::String {
val: "/some/path:/some/other/path".to_string(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);
// Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok(), "{}", merge_result.unwrap_err());
(dir, engine_state, stack)
}
fn external_completion(file_name: &str) -> NuCompleter {
// Create a new engine
let (dir, mut engine_state, mut stack) = new_engine();
let path = dir.join(file_name);
let mut buf = Vec::new();
let mut file =
File::open(&path).unwrap_or_else(|_| panic!("Failed to open {}", path.display()));
file.read_to_end(&mut buf)
.unwrap_or_else(|_| panic!("Failed to open {}", path.display()));
let (_, delta) = {
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, &buf, false, &[]);
assert!(err.is_none(), "{:?}", err.unwrap());
(block, working_set.render())
};
assert!(engine_state.merge_delta(delta).is_ok());
// Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
let latest_block_id = engine_state.num_blocks() - 1;
// Change config adding the external completer
let mut config = engine_state.get_config().clone();
config.external_completer = Some(latest_block_id);
engine_state.set_config(&config);
// Instantiate a new completer
NuCompleter::new(Arc::new(engine_state), stack)
}
// match a list of suggestions with the expected values
#[track_caller]
fn assert_suggestions(expected: &[&str], suggestions: Vec<Suggestion>) {
let expected = expected
.iter()
.map(|s| (*s).to_owned())
.collect::<Vec<String>>();
let suggestions = suggestions
.into_iter()
.map(|s| s.value)
.collect::<Vec<String>>();
assert_eq!(expected, suggestions);
} }
#[test] #[test]
fn completion_basic() { fn completion() {
let mut completer = external_completion("basic.nu"); let term = completest::Term::new();
let mut runtime = common::load_runtime("test", completest::Shell::Nu);
let input = "my-app -"; let input = "test -\t";
let suggestions = completer.complete(input, input.len()); let expected = r#"% test -
let expected = &["--help", "-c", "-h", "-v"]; --generate generate
assert_suggestions(expected, suggestions); --global everywhere
--help Print help
--version Print version
-V Print version
-h Print help
"#;
let actual = runtime.complete(input, &term).unwrap();
snapbox::assert_eq(expected, actual);
let input = "my-app test -"; let input = "test action -\t";
let suggestions = completer.complete(input, input.len()); let expected = r#"% test action -
let expected = &["--help", "-c", "-d", "-h"]; --choice enum
assert_suggestions(expected, suggestions); --count number
} --global everywhere
--help Print help
#[test] --set value
fn completion_feature_sample() { --set-true bool
let mut completer = external_completion("feature_sample.nu"); --version Print version
-V Print version
let input = "my-app test --"; -h Print help
let suggestions = completer.complete(input, input.len()); "#;
let expected = &["--case", "--help", "--version"]; let actual = runtime.complete(input, &term).unwrap();
assert_suggestions(expected, suggestions); snapbox::assert_eq(expected, actual);
let input = "my-app choice ";
let suggestions = completer.complete(input, input.len());
let expected = &["first", "second"];
assert_suggestions(expected, suggestions);
let input = "my-app -";
let suggestions = completer.complete(input, input.len());
let expected = &[
"--conf",
"--config",
"--help",
"--version",
"-C",
"-V",
"-c",
"-h",
];
assert_suggestions(expected, suggestions);
let input = "my-app --";
let suggestions = completer.complete(input, input.len());
let expected = &["--conf", "--config", "--help", "--version"];
assert_suggestions(expected, suggestions);
}
#[test]
fn completion_special_commands() {
let mut completer = external_completion("special_commands.nu");
let input = "my-app some";
let suggestions = completer.complete(input, input.len());
let expected = &[
"my-app some_cmd",
"my-app some-hidden-cmd",
"my-app some-cmd-with-hyphens",
];
assert_suggestions(expected, suggestions);
let input = "my-app choice ";
let suggestions = completer.complete(input, input.len());
let expected = &["first", "second"];
assert_suggestions(expected, suggestions);
let input = "my-app -";
let suggestions = completer.complete(input, input.len());
let expected = &[
"--conf",
"--config",
"--help",
"--version",
"-C",
"-V",
"-c",
"-h",
];
assert_suggestions(expected, suggestions);
let input = "my-app --";
let suggestions = completer.complete(input, input.len());
let expected = &["--conf", "--config", "--help", "--version"];
assert_suggestions(expected, suggestions);
}
#[test]
fn completion_quoting() {
let mut completer = external_completion("quoting.nu");
let input = "my-app cmd-s";
let suggestions = completer.complete(input, input.len());
let expected = &["my-app cmd-single-quotes"];
assert_suggestions(expected, suggestions);
let input = "my-app --";
let suggestions = completer.complete(input, input.len());
let expected = &[
"--backslash",
"--backticks",
"--brackets",
"--double-quotes",
"--expansions",
"--help",
"--single-quotes",
"--version",
];
assert_suggestions(expected, suggestions);
}
#[test]
fn completion_aliases() {
let mut completer = external_completion("aliases.nu");
let input = "my-app -";
let suggestions = completer.complete(input, input.len());
let expected = &[
"--flag",
"--flg",
"--help",
"--opt",
"--option",
"--version",
"-F",
"-O",
"-V",
"-f",
"-h",
"-o",
];
assert_suggestions(expected, suggestions);
}
#[test]
fn completion_sub_subcommands() {
let mut completer = external_completion("sub_subcommands.nu");
let input = "my-app";
let mut suggestions = completer.complete(input, input.len());
suggestions.sort_by_key(|s| s.value.clone());
let expected = &[
"my-app",
"my-app help",
"my-app help help",
"my-app help some_cmd",
"my-app help some_cmd sub_cmd",
"my-app help test",
"my-app some_cmd",
"my-app some_cmd help",
"my-app some_cmd help help",
"my-app some_cmd help sub_cmd",
"my-app some_cmd sub_cmd",
"my-app test",
];
assert_suggestions(expected, suggestions);
let input = "my-app some_cmd sub_cmd -";
let suggestions = completer.complete(input, input.len());
let expected = &["--config", "--help", "--version", "-V", "-h"];
assert_suggestions(expected, suggestions);
let input = "my-app some_cmd sub_cmd --config ";
let suggestions = completer.complete(input, input.len());
let expected = &[
"\"Lest quotes, aren't escaped.\"",
"\"Second to trigger display of options\"",
];
assert_suggestions(expected, suggestions);
} }
#[test] #[test]
fn completion_value_hint() { fn completion_value_hint() {
let mut completer = external_completion("value_hint.nu"); let term = completest::Term::new();
let mut runtime = common::load_runtime("test", completest::Shell::Nu);
let input = "my-app -"; let input = "test hint -\t";
let suggestions = completer.complete(input, input.len()); let expected = r#"% test hint -
let expected = &[ --choice
"--choice", --cmd
"--cmd", --cmd-name
"--cmd-name", --dir
"--dir", --email
"--email", --exe
"--exe", --file
"--file", --global everywhere
"--help", --help Print help
"--host", --host
"--other", --other
"--path", --path
"--unknown", --unknown
"--url", --url
"--user", --user
"-H", --version Print version
"-c", -H
"-d", -V Print version
"-e", -c
"-f", -d
"-h", -e
"-p", -f
"-u", -h Print help
]; -p
assert_suggestions(expected, suggestions); -u
"#;
let actual = runtime.complete(input, &term).unwrap();
snapbox::assert_eq(expected, actual);
let input = "my-app --choice "; let input = "test hint --choice \t";
let suggestions = completer.complete(input, input.len()); let expected = r#"% test hint --choice
let expected = &["bash", "fish", "zsh"]; bash
assert_suggestions(expected, suggestions); fish
zsh
"#;
let actual = runtime.complete(input, &term).unwrap();
snapbox::assert_eq(expected, actual);
} }

View file

@ -0,0 +1,258 @@
module completions {
export extern test [
--global # everywhere
--generate: string # generate
--help(-h) # Print help
--version(-V) # Print version
]
def "nu-complete test action choice" [] {
[ "first" "second" ]
}
export extern "test action" [
--set-true # bool
--set: string # value
--count # number
--choice: string@"nu-complete test action choice" # enum
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
export extern "test quote" [
--single-quotes # Can be 'always', 'auto', or 'never'
--double-quotes # Can be "always", "auto", or "never"
--backticks # For more information see `echo test`
--backslash # Avoid '\n'
--brackets # List packages [filter]
--expansions # Execute the shell command with $SHELL
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Can be 'always', 'auto', or 'never'
export extern "test quote cmd-single-quotes" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Can be "always", "auto", or "never"
export extern "test quote cmd-double-quotes" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# For more information see `echo test`
export extern "test quote cmd-backticks" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Avoid '\n'
export extern "test quote cmd-backslash" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# List packages [filter]
export extern "test quote cmd-brackets" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Execute the shell command with $SHELL
export extern "test quote cmd-expansions" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Print this message or the help of the given subcommand(s)
export extern "test quote help" [
]
# Can be 'always', 'auto', or 'never'
export extern "test quote help cmd-single-quotes" [
]
# Can be "always", "auto", or "never"
export extern "test quote help cmd-double-quotes" [
]
# For more information see `echo test`
export extern "test quote help cmd-backticks" [
]
# Avoid '\n'
export extern "test quote help cmd-backslash" [
]
# List packages [filter]
export extern "test quote help cmd-brackets" [
]
# Execute the shell command with $SHELL
export extern "test quote help cmd-expansions" [
]
# Print this message or the help of the given subcommand(s)
export extern "test quote help help" [
]
export extern "test value" [
--delim: string
--tuple: string
--require-eq: string
...term: string
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
export extern "test pacman" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
export extern "test pacman one" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
export extern "test pacman two" [
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Print this message or the help of the given subcommand(s)
export extern "test pacman help" [
]
export extern "test pacman help one" [
]
export extern "test pacman help two" [
]
# Print this message or the help of the given subcommand(s)
export extern "test pacman help help" [
]
export extern "test last" [
first?: string
free?: string
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
export extern "test alias" [
--flag(-f) # cmd flag
--flg # cmd flag
-F # cmd flag
--option(-o): string # cmd option
--opt: string # cmd option
-O: string # cmd option
positional?: string
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
def "nu-complete test hint choice" [] {
[ "bash" "fish" "zsh" ]
}
export extern "test hint" [
--choice: string@"nu-complete test hint choice"
--unknown: string
--other: string
--path(-p): string
--file(-f): string
--dir(-d): string
--exe(-e): string
--cmd-name: string
--cmd(-c): string
command_with_args?: string
--user(-u): string
--host(-H): string
--url: string
--email: string
--global # everywhere
--help(-h) # Print help
--version(-V) # Print version
]
# Print this message or the help of the given subcommand(s)
export extern "test help" [
]
export extern "test help action" [
]
export extern "test help quote" [
]
# Can be 'always', 'auto', or 'never'
export extern "test help quote cmd-single-quotes" [
]
# Can be "always", "auto", or "never"
export extern "test help quote cmd-double-quotes" [
]
# For more information see `echo test`
export extern "test help quote cmd-backticks" [
]
# Avoid '\n'
export extern "test help quote cmd-backslash" [
]
# List packages [filter]
export extern "test help quote cmd-brackets" [
]
# Execute the shell command with $SHELL
export extern "test help quote cmd-expansions" [
]
export extern "test help value" [
]
export extern "test help pacman" [
]
export extern "test help pacman one" [
]
export extern "test help pacman two" [
]
export extern "test help last" [
]
export extern "test help alias" [
]
export extern "test help hint" [
]
# Print this message or the help of the given subcommand(s)
export extern "test help help" [
]
}
use completions *