Skip to content

Commit 954f922

Browse files
committed
test: Factor out a fallible command runner
This pattern is currently used by the `freebsd-version` check but will be used by more in the future. (backport <#4782>) (cherry picked from commit 91f208c)
1 parent 2852a12 commit 954f922

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

libc-test/build.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,7 +2286,21 @@ fn test_freebsd(target: &str) {
22862286
assert!(target.contains("freebsd"));
22872287
let mut cfg = ctest_cfg();
22882288

2289-
let freebsd_ver = which_freebsd();
2289+
let freebsd_ver = if let Ok(version) = env::var("RUST_LIBC_UNSTABLE_FREEBSD_VERSION") {
2290+
let vers = version.parse().unwrap();
2291+
println!("cargo:warning=setting FreeBSD version to {vers}");
2292+
Some(vers)
2293+
} else {
2294+
match &try_command_output("freebsd-version", &[]) {
2295+
Some(s) if s.starts_with("10") => Some(10),
2296+
Some(s) if s.starts_with("11") => Some(11),
2297+
Some(s) if s.starts_with("12") => Some(12),
2298+
Some(s) if s.starts_with("13") => Some(13),
2299+
Some(s) if s.starts_with("14") => Some(14),
2300+
Some(s) if s.starts_with("15") => Some(15),
2301+
Some(_) | None => None,
2302+
}
2303+
};
22902304

22912305
match freebsd_ver {
22922306
Some(12) => cfg.cfg("freebsd12", None),
@@ -4905,34 +4919,6 @@ fn test_linux_like_apis(target: &str) {
49054919
}
49064920
}
49074921

4908-
fn which_freebsd() -> Option<i32> {
4909-
if let Ok(version) = env::var("RUST_LIBC_UNSTABLE_FREEBSD_VERSION") {
4910-
let vers = version.parse().unwrap();
4911-
println!("cargo:warning=setting FreeBSD version to {vers}");
4912-
return Some(vers);
4913-
}
4914-
4915-
let output = std::process::Command::new("freebsd-version")
4916-
.output()
4917-
.ok()?;
4918-
4919-
if !output.status.success() {
4920-
return None;
4921-
}
4922-
4923-
let stdout = String::from_utf8(output.stdout).ok()?;
4924-
4925-
match &stdout {
4926-
s if s.starts_with("10") => Some(10),
4927-
s if s.starts_with("11") => Some(11),
4928-
s if s.starts_with("12") => Some(12),
4929-
s if s.starts_with("13") => Some(13),
4930-
s if s.starts_with("14") => Some(14),
4931-
s if s.starts_with("15") => Some(15),
4932-
_ => None,
4933-
}
4934-
}
4935-
49364922
fn test_haiku(target: &str) {
49374923
assert!(target.contains("haiku"));
49384924

@@ -5573,3 +5559,17 @@ fn test_aix(target: &str) {
55735559

55745560
ctest::generate_test(&mut cfg, "../src/lib.rs", "ctest_output.rs").unwrap();
55755561
}
5562+
5563+
/// Attempt to execute a command and collect its output, If the command fails for whatever
5564+
/// reason, return `None`.
5565+
fn try_command_output(cmd: &str, args: &[&str]) -> Option<String> {
5566+
let output = std::process::Command::new(cmd).args(args).output().ok()?;
5567+
5568+
if !output.status.success() {
5569+
return None;
5570+
}
5571+
5572+
let res = String::from_utf8(output.stdout)
5573+
.unwrap_or_else(|e| panic!("command {cmd} returned non-UTF-8 output: {e}"));
5574+
Some(res)
5575+
}

0 commit comments

Comments
 (0)