Skip to content
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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1104,14 +1105,15 @@ pub fn get_registry_candidates(
{
Ok(registries
.keys()
.sorted()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This appears to be unrelated to the current commit
  • This should likely be a fix commit on its own

.map(|name| clap_complete::CompletionCandidate::new(name.to_owned()))
.collect())
} else {
Ok(vec![])
}
}

fn get_example_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> {
pub fn get_example_candidates(cwd: Option<PathBuf>) -> Vec<clap_complete::CompletionCandidate> {
get_targets_from_metadata(cwd)
.unwrap_or_default()
.into_iter()
Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/shell_completions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ use std::path::PathBuf;
use cargo::util::command_prelude::*;
use cargo_test_support::cargo_test;

#[cargo_test]
fn test_get_example_candidates() {
let current_dir = std::env::current_dir().expect("Failed to get current directory");
let cwd = PathBuf::from(file!()).parent().unwrap().join("template");
let cwd = current_dir.join(cwd);

let expected = snapbox::str![
"example1
example2"
];
let actual = print_candidates(get_example_candidates(Some(cwd)));
snapbox::assert_data_eq!(actual, expected);
}

#[cargo_test]
fn test_get_registry_candidates() {
let current_dir = std::env::current_dir().expect("Failed to get current directory");
Expand Down
16 changes: 16 additions & 0 deletions tests/testsuite/shell_completions/template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[workspace]
resolver = "2"

[workspace.package]
rust-version = "1.81"
edition = "2021"

[package]
name = "template"
version = "0.1.0"
edition.workspace = true
rust-version = "1.81"

[lib]
name = "template"
path = "src/lib.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/testsuite/shell_completions/template/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@