-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add test codebase for shell completions #14681
Open
shannmu
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
shannmu:test_codebase
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1e43f4
refactor: Make custom completers testable and reuse `new_gctx_for_com…
shannmu ae0363c
test: Add a test codebase for shell completions
shannmu b9d09f0
test: Add test case for `get_registry_candidates`
shannmu 64b9b0d
test: Add test case for `get_example_candidates`
shannmu 95d9be2
test: Add test case for `get_test_candidates`
shannmu 9ebdd49
test: Add test code for `get_bench_candidates`
shannmu 1ba0169
test: Add test case for `get_bin_candidates`
shannmu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ use cargo_util_schemas::manifest::RegistryName; | |
use cargo_util_schemas::manifest::StringOrVec; | ||
use clap::builder::UnknownArgumentValueParser; | ||
use home::cargo_home_with_cwd; | ||
use itertools::Itertools; | ||
use semver::Version; | ||
use std::collections::HashMap; | ||
use std::ffi::{OsStr, OsString}; | ||
|
@@ -163,13 +164,19 @@ pub trait CommandExt: Sized { | |
._arg( | ||
optional_multi_opt("test", "NAME", test) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new(get_test_candidates)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_test_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg(flag("benches", benches).help_heading(heading::TARGET_SELECTION)) | ||
._arg( | ||
optional_multi_opt("bench", "NAME", bench) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new(get_bench_candidates)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_bench_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg(flag("all-targets", all).help_heading(heading::TARGET_SELECTION)) | ||
} | ||
|
@@ -187,15 +194,19 @@ pub trait CommandExt: Sized { | |
._arg( | ||
optional_multi_opt("bin", "NAME", bin) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_bin_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION)) | ||
._arg( | ||
optional_multi_opt("example", "NAME", example) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new( | ||
get_example_candidates, | ||
)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_example_candidates(cwd.ok()) | ||
})), | ||
) | ||
} | ||
|
||
|
@@ -209,15 +220,19 @@ pub trait CommandExt: Sized { | |
self._arg( | ||
optional_multi_opt("bin", "NAME", bin) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_bin_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg(flag("bins", bins).help_heading(heading::TARGET_SELECTION)) | ||
._arg( | ||
optional_multi_opt("example", "NAME", example) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new( | ||
get_example_candidates, | ||
)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_example_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg(flag("examples", examples).help_heading(heading::TARGET_SELECTION)) | ||
} | ||
|
@@ -226,14 +241,18 @@ pub trait CommandExt: Sized { | |
self._arg( | ||
optional_multi_opt("bin", "NAME", bin) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_bin_candidates(cwd.ok()) | ||
})), | ||
) | ||
._arg( | ||
optional_multi_opt("example", "NAME", example) | ||
.help_heading(heading::TARGET_SELECTION) | ||
.add(clap_complete::ArgValueCandidates::new( | ||
get_example_candidates, | ||
)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_example_candidates(cwd.ok()) | ||
})), | ||
) | ||
} | ||
|
||
|
@@ -294,7 +313,10 @@ pub trait CommandExt: Sized { | |
self._arg( | ||
optional_multi_opt("target", "TRIPLE", target) | ||
.help_heading(heading::COMPILATION_OPTIONS) | ||
.add(clap_complete::ArgValueCandidates::new(get_target_triples)), | ||
.add(clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_target_triples(cwd.ok()) | ||
})), | ||
) | ||
._arg(unsupported_short_arg) | ||
} | ||
|
@@ -367,8 +389,12 @@ pub trait CommandExt: Sized { | |
.value_parser(["git", "hg", "pijul", "fossil", "none"]), | ||
) | ||
._arg( | ||
flag("bin", "Use a binary (application) template [default]") | ||
.add(clap_complete::ArgValueCandidates::new(get_bin_candidates)), | ||
flag("bin", "Use a binary (application) template [default]").add( | ||
clap_complete::ArgValueCandidates::new(|| { | ||
let cwd = std::env::current_dir(); | ||
get_bin_candidates(cwd.ok()) | ||
}), | ||
), | ||
) | ||
._arg(flag("lib", "Use a library template")) | ||
._arg( | ||
|
@@ -388,7 +414,8 @@ pub trait CommandExt: Sized { | |
fn arg_registry(self, help: &'static str) -> Self { | ||
self._arg(opt("registry", help).value_name("REGISTRY").add( | ||
clap_complete::ArgValueCandidates::new(|| { | ||
let candidates = get_registry_candidates(); | ||
let cwd = std::env::current_dir(); | ||
let candidates = get_registry_candidates(cwd.ok()); | ||
candidates.unwrap_or_default() | ||
}), | ||
)) | ||
|
@@ -1068,23 +1095,26 @@ pub fn lockfile_path( | |
return Ok(Some(path)); | ||
} | ||
|
||
pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> { | ||
let gctx = new_gctx_for_completions()?; | ||
pub fn get_registry_candidates( | ||
cwd: Option<PathBuf>, | ||
) -> CargoResult<Vec<clap_complete::CompletionCandidate>> { | ||
let gctx = new_gctx_for_completions(cwd)?; | ||
|
||
if let Ok(Some(registries)) = | ||
gctx.get::<Option<HashMap<String, HashMap<String, String>>>>("registries") | ||
{ | ||
Ok(registries | ||
.keys() | ||
.sorted() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.map(|name| clap_complete::CompletionCandidate::new(name.to_owned())) | ||
.collect()) | ||
} else { | ||
Ok(vec![]) | ||
} | ||
} | ||
|
||
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata() | ||
pub fn get_example_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata(cwd) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.filter_map(|target| match target.kind() { | ||
|
@@ -1094,8 +1124,8 @@ fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> { | |
.collect::<Vec<_>>() | ||
} | ||
|
||
fn get_bench_candidates() -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata() | ||
pub fn get_bench_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata(cwd) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.filter_map(|target| match target.kind() { | ||
|
@@ -1105,8 +1135,8 @@ fn get_bench_candidates() -> Vec<clap_complete::CompletionCandidate> { | |
.collect::<Vec<_>>() | ||
} | ||
|
||
fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata() | ||
pub fn get_test_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata(cwd) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.filter_map(|target| match target.kind() { | ||
|
@@ -1116,8 +1146,8 @@ fn get_test_candidates() -> Vec<clap_complete::CompletionCandidate> { | |
.collect::<Vec<_>>() | ||
} | ||
|
||
fn get_bin_candidates() -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata() | ||
pub fn get_bin_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
get_targets_from_metadata(cwd) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.filter_map(|target| match target.kind() { | ||
|
@@ -1127,10 +1157,9 @@ fn get_bin_candidates() -> Vec<clap_complete::CompletionCandidate> { | |
.collect::<Vec<_>>() | ||
} | ||
|
||
fn get_targets_from_metadata() -> CargoResult<Vec<Target>> { | ||
let cwd = std::env::current_dir()?; | ||
let gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?); | ||
let ws = Workspace::new(&find_root_manifest_for_wd(&cwd)?, &gctx)?; | ||
fn get_targets_from_metadata(cwd: Option<PathBuf>) -> CargoResult<Vec<Target>> { | ||
let gctx = new_gctx_for_completions(cwd)?; | ||
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?; | ||
|
||
let packages = ws.members().collect::<Vec<_>>(); | ||
|
||
|
@@ -1142,15 +1171,15 @@ fn get_targets_from_metadata() -> CargoResult<Vec<Target>> { | |
Ok(targets) | ||
} | ||
|
||
fn get_target_triples() -> Vec<clap_complete::CompletionCandidate> { | ||
pub fn get_target_triples(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
let mut candidates = Vec::new(); | ||
|
||
if let Ok(targets) = get_target_triples_from_rustup() { | ||
candidates = targets; | ||
} | ||
|
||
if candidates.is_empty() { | ||
if let Ok(targets) = get_target_triples_from_rustc() { | ||
if let Ok(targets) = get_target_triples_from_rustc(cwd) { | ||
candidates = targets; | ||
} | ||
} | ||
|
@@ -1182,10 +1211,11 @@ fn get_target_triples_from_rustup() -> CargoResult<Vec<clap_complete::Completion | |
.collect()) | ||
} | ||
|
||
fn get_target_triples_from_rustc() -> CargoResult<Vec<clap_complete::CompletionCandidate>> { | ||
let cwd = std::env::current_dir()?; | ||
let gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?); | ||
let ws = Workspace::new(&find_root_manifest_for_wd(&PathBuf::from(&cwd))?, &gctx); | ||
fn get_target_triples_from_rustc( | ||
cwd: Option<PathBuf>, | ||
) -> CargoResult<Vec<clap_complete::CompletionCandidate>> { | ||
let gctx = new_gctx_for_completions(cwd)?; | ||
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx); | ||
|
||
let rustc = gctx.load_global_rustc(ws.as_ref().ok())?; | ||
|
||
|
@@ -1198,12 +1228,12 @@ fn get_target_triples_from_rustc() -> CargoResult<Vec<clap_complete::CompletionC | |
.collect()) | ||
} | ||
|
||
pub fn get_pkg_id_spec_candidates() -> Vec<clap_complete::CompletionCandidate> { | ||
pub fn get_pkg_id_spec_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> { | ||
let mut candidates = vec![]; | ||
|
||
let package_map = HashMap::<&str, Vec<Package>>::new(); | ||
let package_map = | ||
get_packages() | ||
get_packages(cwd) | ||
.unwrap_or_default() | ||
.into_iter() | ||
.fold(package_map, |mut map, package| { | ||
|
@@ -1285,8 +1315,8 @@ pub fn get_pkg_id_spec_candidates() -> Vec<clap_complete::CompletionCandidate> { | |
candidates | ||
} | ||
|
||
fn get_packages() -> CargoResult<Vec<Package>> { | ||
let gctx = new_gctx_for_completions()?; | ||
fn get_packages(cwd: Option<PathBuf>) -> CargoResult<Vec<Package>> { | ||
let gctx = new_gctx_for_completions(cwd)?; | ||
|
||
let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?; | ||
|
||
|
@@ -1318,8 +1348,8 @@ fn get_packages() -> CargoResult<Vec<Package>> { | |
Ok(packages) | ||
} | ||
|
||
fn new_gctx_for_completions() -> CargoResult<GlobalContext> { | ||
let cwd = std::env::current_dir()?; | ||
pub fn new_gctx_for_completions(cwd: Option<PathBuf>) -> CargoResult<GlobalContext> { | ||
let cwd = cwd.unwrap_or(std::env::current_dir()?); | ||
let mut gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?); | ||
|
||
let verbose = 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This being
pub
doesn't make a difference as this only exists in thebin