Skip to content

Commit bda492f

Browse files
ClSlaidsmoelius
authored andcommitted
fix: parse active toolchain error
Signed-off-by: cl <[email protected]>
1 parent f4cdda1 commit bda492f

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

internal/src/rustup.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ pub fn active_toolchain(path: &Path) -> Result<String> {
2929
.args(["show", "active-toolchain"])
3030
.logged_output(true)?;
3131
let stdout = std::str::from_utf8(&output.stdout)?;
32-
stdout
33-
.split_once(' ')
34-
.map(|(s, _)| s.to_owned())
32+
33+
// split at the first whitespace character
34+
parse_active_toolchain(stdout)
35+
}
36+
37+
fn parse_active_toolchain(active: &str) -> Result<String> {
38+
active
39+
.split_ascii_whitespace()
40+
.take(1)
41+
.map(|s| s.to_owned())
42+
.next()
3543
.ok_or_else(|| anyhow!("Could not determine active toolchain"))
3644
}
3745

@@ -54,7 +62,24 @@ pub fn is_rustc<T: AsRef<OsStr> + ?Sized>(arg: &T) -> bool {
5462
Path::new(arg).file_stem() == Some(OsStr::new("rustc"))
5563
}
5664

57-
#[test]
58-
fn rustc_is_rustc() {
59-
assert!(is_rustc("rustc"));
65+
#[cfg(test)]
66+
mod rustup_test {
67+
68+
use crate::rustup::{is_rustc, parse_active_toolchain};
69+
70+
#[test]
71+
fn rustc_is_rustc() {
72+
assert!(is_rustc("rustc"));
73+
}
74+
75+
#[test]
76+
fn test_parse_active_toolchain() {
77+
let output = r#"nightly-aarch64-apple-darwin
78+
active because: it's the default toolchain
79+
"#;
80+
assert_eq!(
81+
parse_active_toolchain(output).unwrap(),
82+
"nightly-aarch64-apple-darwin".to_owned()
83+
);
84+
}
6085
}

0 commit comments

Comments
 (0)