Skip to content

Commit 84e4639

Browse files
authored
Merge pull request #2952 from hi-rustin/rustin-patch-tests
Add tests for proxyable tools
2 parents 1cf3d02 + 4942aec commit 84e4639

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/bin/rustup-init.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use rustup::cli::self_update;
2525
use rustup::cli::setup_mode;
2626
use rustup::currentprocess::{process, with, OSProcess};
2727
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
28+
use rustup::is_proxyable_tools;
2829
use rustup::utils::utils;
29-
use rustup::{DUP_TOOLS, TOOLS};
3030

3131
fn main() {
3232
let process = OSProcess::default();
@@ -81,20 +81,8 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
8181
}
8282
}
8383
Some(n) => {
84-
if TOOLS.iter().chain(DUP_TOOLS.iter()).any(|&name| name == n) {
85-
proxy_mode::main(n)
86-
} else {
87-
Err(anyhow!(format!(
88-
"unknown proxy name: '{}'; valid proxy names are {}",
89-
n,
90-
TOOLS
91-
.iter()
92-
.chain(DUP_TOOLS.iter())
93-
.map(|s| format!("'{}'", s))
94-
.collect::<Vec<_>>()
95-
.join(", ")
96-
)))
97-
}
84+
is_proxyable_tools(n)?;
85+
proxy_mode::main(n)
9886
}
9987
None => {
10088
// Weird case. No arg0, or it's unparsable.

src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use crate::errors::*;
1313
pub use crate::notifications::*;
1414
use crate::toolchain::*;
1515
pub(crate) use crate::utils::toml_utils;
16+
use anyhow::{anyhow, Result};
1617

1718
#[macro_use]
1819
extern crate rs_tracing;
@@ -36,6 +37,28 @@ pub static TOOLS: &[&str] = &[
3637
// installation.
3738
pub static DUP_TOOLS: &[&str] = &["rustfmt", "cargo-fmt"];
3839

40+
// If the given name is one of the tools we proxy.
41+
pub fn is_proxyable_tools(tool: &str) -> Result<()> {
42+
if TOOLS
43+
.iter()
44+
.chain(DUP_TOOLS.iter())
45+
.any(|&name| name == tool)
46+
{
47+
Ok(())
48+
} else {
49+
Err(anyhow!(format!(
50+
"unknown proxy name: '{}'; valid proxy names are {}",
51+
tool,
52+
TOOLS
53+
.iter()
54+
.chain(DUP_TOOLS.iter())
55+
.map(|s| format!("'{}'", s))
56+
.collect::<Vec<_>>()
57+
.join(", ")
58+
)))
59+
}
60+
}
61+
3962
fn component_for_bin(binary: &str) -> Option<&'static str> {
4063
use std::env::consts::EXE_SUFFIX;
4164

@@ -74,3 +97,25 @@ mod settings;
7497
pub mod test;
7598
mod toolchain;
7699
pub mod utils;
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use crate::{is_proxyable_tools, DUP_TOOLS, TOOLS};
104+
105+
#[test]
106+
fn test_is_proxyable_tools() {
107+
for tool in TOOLS {
108+
assert!(is_proxyable_tools(tool).is_ok());
109+
}
110+
for tool in DUP_TOOLS {
111+
assert!(is_proxyable_tools(tool).is_ok());
112+
}
113+
let message = &"unknown proxy name: 'unknown-tool'; valid proxy names are 'rustc', \
114+
'rustdoc', 'cargo', 'rust-lldb', 'rust-gdb', 'rust-gdbgui', 'rls', 'cargo-clippy', \
115+
'clippy-driver', 'cargo-miri', 'rustfmt', 'cargo-fmt'";
116+
assert!(is_proxyable_tools("unknown-tool")
117+
.unwrap_err()
118+
.to_string()
119+
.eq(message));
120+
}
121+
}

0 commit comments

Comments
 (0)