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

Display alias target on 'cargo help <alias>` #10193

Merged
merged 6 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
42 changes: 25 additions & 17 deletions src/bin/cargo/commands/help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::aliased_command;
use cargo::util::errors::CargoResult;
use cargo::Config;
use cargo::{drop_println, Config};
use cargo_util::paths::resolve_executable;
use flate2::read::GzDecoder;
use std::ffi::OsString;
Expand Down Expand Up @@ -46,8 +46,16 @@ fn try_help(config: &Config) -> CargoResult<bool> {
Some(s) => s,
None => return Ok(false),
};
// Check if this is a built-in command (or alias);
let subcommand = match check_alias(config, subcommand) {

// Check if this is an alias. If so, just display the alias information.
if let Some(argv) = check_alias(config, subcommand) {
let alias = argv.join(" ");
drop_println!(config, "'{}' is aliased to '{}'", subcommand, alias);
sstangl marked this conversation as resolved.
Show resolved Hide resolved
return Ok(true);
}

// If not an alias, this should be a built-in subcommand.
let subcommand = match check_builtin(subcommand) {
Some(s) => s,
None => return Ok(false),
};
Expand All @@ -73,26 +81,26 @@ fn try_help(config: &Config) -> CargoResult<bool> {
Ok(true)
}

/// Checks if the given subcommand is a built-in command (possibly via an alias).
/// Checks if the given subcommand is an alias.
///
/// Returns None if it is not a built-in command.
fn check_alias(config: &Config, subcommand: &str) -> Option<String> {
if super::builtin_exec(subcommand).is_some() {
return Some(subcommand.to_string());
}
/// Returns None if it is not an alias.
fn check_alias(config: &Config, subcommand: &str) -> Option<Vec<String>> {
sstangl marked this conversation as resolved.
Show resolved Hide resolved
match aliased_command(config, subcommand) {
Ok(Some(alias)) => {
let alias = alias.into_iter().next()?;
if super::builtin_exec(&alias).is_some() {
Some(alias)
} else {
None
}
}
Ok(Some(alias)) => Some(alias),
_ => None,
}
}

/// Checks if the given subcommand is a built-in command (not via an alias).
///
/// Returns None if it is not a built-in command.
fn check_builtin(subcommand: &str) -> Option<String> {
match super::builtin_exec(subcommand) {
Some(_) => Some(subcommand.to_string()),
None => None,
}
}
sstangl marked this conversation as resolved.
Show resolved Hide resolved

/// Extracts the given man page from the compressed archive.
///
/// Returns None if the command wasn't found.
Expand Down
20 changes: 18 additions & 2 deletions tests/testsuite/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ fn help_with_man_and_path(
assert_eq!(stdout, contents);
}

fn help_with_stdout_and_path(subcommand: &str, path: &Path) -> String {
let output = process(&cargo_exe())
.arg("help")
.arg(subcommand)
.env("PATH", path)
.exec_with_output()
.unwrap();
assert!(output.status.success());
let stderr = from_utf8(&output.stderr).unwrap();
assert_eq!(stderr, "");
let stdout = from_utf8(&output.stdout).unwrap();
stdout.to_string()
}

#[cargo_test]
fn help_man() {
// Checks that `help command` displays the man page using the given command.
Expand All @@ -124,7 +138,8 @@ fn help_man() {
#[cargo_test]
fn help_alias() {
// Check that `help some_alias` will resolve.
help_with_man_and_path("", "b", "build", Path::new(""));
let out = help_with_stdout_and_path("b", Path::new(""));
assert_eq!(out, "'b' is aliased to 'build'\n");

let config = paths::root().join(".cargo/config");
fs::create_dir_all(config.parent().unwrap()).unwrap();
Expand All @@ -136,7 +151,8 @@ fn help_alias() {
"#,
)
.unwrap();
help_with_man_and_path("", "my-alias", "build", Path::new(""));
let out = help_with_stdout_and_path("my-alias", Path::new(""));
assert_eq!(out, "'my-alias' is aliased to 'build --release'\n");
}

#[cargo_test]
Expand Down