mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
refactor(complete): Assert on home change
This commit is contained in:
parent
1d0fa2c6ce
commit
34a7973e53
3 changed files with 61 additions and 3 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -687,6 +687,15 @@ dependencies = [
|
|||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "content_inspector"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation"
|
||||
version = "0.9.3"
|
||||
|
@ -926,6 +935,12 @@ dependencies = [
|
|||
"rust_decimal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dunce"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
|
@ -3245,13 +3260,18 @@ checksum = "f6bccd62078347f89a914e3004d94582e13824d4e3d8a816317862884c423835"
|
|||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"content_inspector",
|
||||
"dunce",
|
||||
"escargot",
|
||||
"filetime",
|
||||
"libc",
|
||||
"normalize-line-endings",
|
||||
"os_pipe",
|
||||
"similar",
|
||||
"snapbox-macros",
|
||||
"tempfile",
|
||||
"wait-timeout",
|
||||
"walkdir",
|
||||
"windows-sys 0.45.0",
|
||||
]
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ shlex = { version = "1.1.0", optional = true }
|
|||
unicode-xid = { version = "0.2.2", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
snapbox = { version = "0.4.11", features = ["diff", "examples"] }
|
||||
snapbox = { version = "0.4.11", features = ["diff", "path", "examples"] }
|
||||
# Cutting out `filesystem` feature
|
||||
trycmd = { version = "0.14.16", default-features = false, features = ["color-auto", "diff", "examples"] }
|
||||
completest = "0.0.4"
|
||||
|
|
|
@ -339,6 +339,9 @@ pub fn has_command(command: &str) -> bool {
|
|||
|
||||
#[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)
|
||||
|
@ -358,9 +361,13 @@ pub fn register_example(name: &str, shell: completest::Shell) {
|
|||
let registration = std::str::from_utf8(®istration.stdout).unwrap();
|
||||
assert!(!registration.is_empty());
|
||||
|
||||
let runtime = shell.init(bin_root, home).unwrap();
|
||||
let 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)]
|
||||
|
@ -369,10 +376,41 @@ pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest:
|
|||
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();
|
||||
|
||||
shell.with_home(bin_root, home)
|
||||
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(&self, name: &str, content: &str) -> std::io::Result<()> {
|
||||
self.runtime.register(name, content)
|
||||
}
|
||||
|
||||
fn complete(&self, input: &str, term: &completest::Term) -> std::io::Result<String> {
|
||||
self.runtime.complete(input, term)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether or not this running in a Continuous Integration environment.
|
||||
|
|
Loading…
Reference in a new issue