Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 419f231

Browse files
committed
chore: Update completest
1 parent 6e862f5 commit 419f231

File tree

12 files changed

+142
-41
lines changed

12 files changed

+142
-41
lines changed

Cargo.lock

Lines changed: 21 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clap_complete/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ unicode-xid = { version = "0.2.2", optional = true }
4545
snapbox = { version = "0.4.15", features = ["diff", "path", "examples"] }
4646
# Cutting out `filesystem` feature
4747
trycmd = { version = "0.14.19", default-features = false, features = ["color-auto", "diff", "examples"] }
48-
completest = "0.2.0"
48+
completest = "0.4.0"
49+
completest-pty = "0.4.0"
4950
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] }
5051

5152
[[example]]

clap_complete/tests/testsuite/bash.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn subcommand_last() {
141141
#[test]
142142
#[cfg(unix)]
143143
fn register_completion() {
144-
common::register_example("static", "exhaustive", completest::Shell::Bash);
144+
common::register_example::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
145145
}
146146

147147
#[test]
@@ -152,7 +152,8 @@ fn complete() {
152152
}
153153

154154
let term = completest::Term::new();
155-
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Bash);
155+
let mut runtime =
156+
common::load_runtime::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
156157

157158
let input = "exhaustive \t\t";
158159
let expected = r#"%
@@ -165,5 +166,5 @@ fn complete() {
165166
#[test]
166167
#[cfg(unix)]
167168
fn register_dynamic_completion() {
168-
common::register_example("dynamic", "exhaustive", completest::Shell::Bash);
169+
common::register_example::<completest_pty::BashRuntimeBuilder>("dynamic", "exhaustive");
169170
}

clap_complete/tests/testsuite/common.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,13 @@ pub fn assert_matches_path(
296296
.matches_path(expected_path, buf);
297297
}
298298

299-
pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
299+
pub fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
300+
use completest::Runtime as _;
301+
300302
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
301303
let scratch_path = scratch.path().unwrap();
302304

303-
let shell_name = shell.name();
305+
let shell_name = R::name();
304306
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
305307
.join("tests/snapshots/home")
306308
.join(context)
@@ -341,7 +343,7 @@ pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
341343
let registration = std::str::from_utf8(&registration.stdout).unwrap();
342344
assert!(!registration.is_empty());
343345

344-
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
346+
let mut runtime = R::new(bin_root, scratch_path.to_owned()).unwrap();
345347

346348
runtime.register(name, registration).unwrap();
347349

@@ -350,12 +352,14 @@ pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
350352
scratch.close().unwrap();
351353
}
352354

353-
pub fn load_runtime(
355+
pub fn load_runtime<R: completest::RuntimeBuilder>(
354356
context: &str,
355357
name: &str,
356-
shell: completest::Shell,
357-
) -> Box<dyn completest::Runtime> {
358-
let shell_name = shell.name();
358+
) -> Box<dyn completest::Runtime>
359+
where
360+
<R as completest::RuntimeBuilder>::Runtime: 'static,
361+
{
362+
let shell_name = R::name();
359363
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
360364
.join("tests/snapshots/home")
361365
.join(context)
@@ -382,11 +386,11 @@ pub fn load_runtime(
382386
println!("Compiled");
383387
let bin_root = bin_path.parent().unwrap().to_owned();
384388

385-
let runtime = shell.with_home(bin_root, home).unwrap();
389+
let runtime = R::with_home(bin_root, home).unwrap();
386390

387391
Box::new(ScratchRuntime {
388392
_scratch: scratch,
389-
runtime,
393+
runtime: Box::new(runtime),
390394
})
391395
}
392396

clap_complete/tests/testsuite/elvish.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn subcommand_last() {
123123
#[test]
124124
#[cfg(unix)]
125125
fn register_completion() {
126-
common::register_example("static", "exhaustive", completest::Shell::Elvish);
126+
common::register_example::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
127127
}
128128

129129
#[test]
@@ -134,7 +134,8 @@ fn complete() {
134134
}
135135

136136
let term = completest::Term::new();
137-
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Elvish);
137+
let mut runtime =
138+
common::load_runtime::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
138139

139140
let input = "exhaustive \t";
140141
let expected = r#"% exhaustive --generate

clap_complete/tests/testsuite/fish.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn subcommand_last() {
123123
#[test]
124124
#[cfg(unix)]
125125
fn register_completion() {
126-
common::register_example("static", "exhaustive", completest::Shell::Fish);
126+
common::register_example::<completest_pty::FishRuntimeBuilder>("static", "exhaustive");
127127
}
128128

129129
#[test]
@@ -134,7 +134,8 @@ fn complete() {
134134
}
135135

136136
let term = completest::Term::new();
137-
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Fish);
137+
let mut runtime =
138+
common::load_runtime::<completest_pty::FishRuntimeBuilder>("static", "exhaustive");
138139

139140
let input = "exhaustive \t";
140141
let expected = r#"% exhaustive
@@ -153,7 +154,7 @@ bash (bash (shell)) fish (fish shell) zsh (zsh shell)"#;
153154
#[cfg(all(unix, feature = "unstable-dynamic"))]
154155
#[test]
155156
fn register_dynamic() {
156-
common::register_example("dynamic", "exhaustive", completest::Shell::Fish);
157+
common::register_example::<completest_pty::FishRuntimeBuilder>("dynamic", "exhaustive");
157158
}
158159

159160
#[test]
@@ -164,7 +165,8 @@ fn complete_dynamic() {
164165
}
165166

166167
let term = completest::Term::new();
167-
let mut runtime = common::load_runtime("dynamic", "exhaustive", completest::Shell::Fish);
168+
let mut runtime =
169+
common::load_runtime::<completest_pty::FishRuntimeBuilder>("dynamic", "exhaustive");
168170

169171
let input = "exhaustive \t";
170172
let expected = r#"% exhaustive

clap_complete/tests/testsuite/zsh.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn subcommand_last() {
123123
#[test]
124124
#[cfg(unix)]
125125
fn register_completion() {
126-
common::register_example("static", "exhaustive", completest::Shell::Zsh);
126+
common::register_example::<completest_pty::ZshRuntimeBuilder>("static", "exhaustive");
127127
}
128128

129129
#[test]
@@ -134,7 +134,8 @@ fn complete() {
134134
}
135135

136136
let term = completest::Term::new();
137-
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Zsh);
137+
let mut runtime =
138+
common::load_runtime::<completest_pty::ZshRuntimeBuilder>("static", "exhaustive");
138139

139140
let input = "exhaustive \t";
140141
let expected = r#"% exhaustive

clap_complete_nushell/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ clap_complete = { path = "../clap_complete", version = "4.0.0" }
3737
[dev-dependencies]
3838
snapbox = { version = "0.4.15", features = ["diff", "examples", "path"] }
3939
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
40-
completest = { version = "0.2.0", features = ["nu"] }
40+
completest = "0.4.0"
41+
completest-nu = "0.4.0"

clap_complete_nushell/tests/common.rs

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,16 @@ pub fn assert_matches_path(
258258
.matches_path(expected_path, buf);
259259
}
260260

261-
pub fn register_example(name: &str, shell: completest::Shell) {
261+
pub fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
262+
use completest::Runtime as _;
263+
262264
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
263265
let scratch_path = scratch.path().unwrap();
264266

265-
let shell_name = shell.name();
267+
let shell_name = R::name();
266268
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
267269
.join("tests/snapshots/home")
270+
.join(context)
268271
.join(name)
269272
.join(shell_name);
270273
println!("Compiling");
@@ -275,10 +278,17 @@ pub fn register_example(name: &str, shell: completest::Shell) {
275278
println!("Compiled");
276279
let bin_root = bin_path.parent().unwrap().to_owned();
277280

278-
let registration = std::process::Command::new(&bin_path)
279-
.arg(format!("--generate={shell_name}"))
280-
.output()
281-
.unwrap();
281+
let mut registration = std::process::Command::new(&bin_path);
282+
match context {
283+
"static" => registration.args([format!("--generate={shell_name}")]),
284+
"dynamic" => registration.args([
285+
"complete".to_owned(),
286+
"--register=-".to_owned(),
287+
format!("--shell={shell_name}"),
288+
]),
289+
_ => unreachable!("unsupported context {}", context),
290+
};
291+
let registration = registration.output().unwrap();
282292
assert!(
283293
registration.status.success(),
284294
"{}",
@@ -287,7 +297,7 @@ pub fn register_example(name: &str, shell: completest::Shell) {
287297
let registration = std::str::from_utf8(&registration.stdout).unwrap();
288298
assert!(!registration.is_empty());
289299

290-
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
300+
let mut runtime = R::new(bin_root, scratch_path.to_owned()).unwrap();
291301

292302
runtime.register(name, registration).unwrap();
293303

@@ -296,14 +306,23 @@ pub fn register_example(name: &str, shell: completest::Shell) {
296306
scratch.close().unwrap();
297307
}
298308

299-
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
300-
let shell_name = shell.name();
309+
pub fn load_runtime<R: completest::RuntimeBuilder>(
310+
context: &str,
311+
name: &str,
312+
) -> Box<dyn completest::Runtime>
313+
where
314+
<R as completest::RuntimeBuilder>::Runtime: 'static,
315+
{
316+
let shell_name = R::name();
301317
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
302318
.join("tests/snapshots/home")
319+
.join(context)
303320
.join(name)
304321
.join(shell_name);
305-
std::fs::create_dir_all(&home).unwrap();
306-
let scratch = snapbox::path::PathFixture::immutable(&home);
322+
let scratch = snapbox::path::PathFixture::mutable_temp()
323+
.unwrap()
324+
.with_template(&home)
325+
.unwrap();
307326
let home = scratch.path().unwrap().to_owned();
308327
println!("Compiling");
309328
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
@@ -313,11 +332,11 @@ pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest:
313332
println!("Compiled");
314333
let bin_root = bin_path.parent().unwrap().to_owned();
315334

316-
let runtime = shell.with_home(bin_root, home).unwrap();
335+
let runtime = R::with_home(bin_root, home).unwrap();
317336

318337
Box::new(ScratchRuntime {
319338
_scratch: scratch,
320-
runtime,
339+
runtime: Box::new(runtime),
321340
})
322341
}
323342

@@ -345,3 +364,56 @@ impl completest::Runtime for ScratchRuntime {
345364
Ok(output)
346365
}
347366
}
367+
368+
pub fn has_command(command: &str) -> bool {
369+
let output = match std::process::Command::new(command)
370+
.arg("--version")
371+
.output()
372+
{
373+
Ok(output) => output,
374+
Err(e) => {
375+
// CI is expected to support all of the commands
376+
if is_ci() && cfg!(linux) {
377+
panic!(
378+
"expected command `{}` to be somewhere in PATH: {}",
379+
command, e
380+
);
381+
}
382+
return false;
383+
}
384+
};
385+
if !output.status.success() {
386+
panic!(
387+
"expected command `{}` to be runnable, got error {}:\n\
388+
stderr:{}\n\
389+
stdout:{}\n",
390+
command,
391+
output.status,
392+
String::from_utf8_lossy(&output.stderr),
393+
String::from_utf8_lossy(&output.stdout)
394+
);
395+
}
396+
let stdout = String::from_utf8_lossy(&output.stdout);
397+
println!(
398+
"$ {command} --version
399+
{}",
400+
stdout
401+
);
402+
if cfg!(target_os = "macos") && stdout.starts_with("GNU bash, version 3") {
403+
return false;
404+
}
405+
if cfg!(target_os = "macos") && command == "zsh" {
406+
// HACK: At least on CI, the prompt override is not working
407+
return false;
408+
}
409+
410+
true
411+
}
412+
413+
/// Whether or not this running in a Continuous Integration environment.
414+
fn is_ci() -> bool {
415+
// Consider using `tracked_env` instead of option_env! when it is stabilized.
416+
// `tracked_env` will handle changes, but not require rebuilding the macro
417+
// itself like option_env does.
418+
option_env!("CI").is_some() || option_env!("TF_BUILD").is_some()
419+
}

0 commit comments

Comments
 (0)