|
| 1 | +use assert_cmd::Command as AssertCommand; |
| 2 | + |
| 3 | +// Note: use `cargo::cargo_bin_cmd!` macro to obtain the built binary to run. |
| 4 | +// We avoid `Command::cargo_bin` (deprecated) and prefer the macro which is |
| 5 | +// compatible with custom Cargo build directories. |
| 6 | +fn run_output_from_cmd(cmd: &mut AssertCommand) -> (i32, String, String) { |
| 7 | + let output = cmd.output().unwrap(); |
| 8 | + ( |
| 9 | + output.status.code().unwrap_or(-1), |
| 10 | + String::from_utf8_lossy(&output.stdout).to_string(), |
| 11 | + String::from_utf8_lossy(&output.stderr).to_string(), |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +fn normalize_output(output: &(i32, String, String)) -> (i32, String, String) { |
| 16 | + use regex::Regex; |
| 17 | + let re = Regex::new(r"/.*/target/debug/[^\s]+").unwrap_or_else(|_| Regex::new(r"invalid").unwrap()); |
| 18 | + let stdout = re.replace_all(&output.1, "hostname").to_string(); |
| 19 | + let stderr = re.replace_all(&output.2, "hostname").to_string(); |
| 20 | + (output.0, stdout, stderr) |
| 21 | +} |
| 22 | + |
| 23 | +#[test] |
| 24 | +fn dnsdomainname_matches_hostname_d() { |
| 25 | + let a = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("dnsdomainname").arg("--help"))); |
| 26 | + let b = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("hostname").arg("-d").arg("--help"))); |
| 27 | + assert_eq!(a, b); |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn domainname_matches_hostname_y() { |
| 32 | + let a = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("domainname").arg("--help"))); |
| 33 | + let b = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("hostname").arg("-y").arg("--help"))); |
| 34 | + assert_eq!(a, b); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn nisdomainname_matches_hostname_y() { |
| 39 | + let a = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("nisdomainname").arg("--help"))); |
| 40 | + let b = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("hostname").arg("-y").arg("--help"))); |
| 41 | + assert_eq!(a, b); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn ypdomainname_matches_hostname_y() { |
| 46 | + let a = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("ypdomainname").arg("--help"))); |
| 47 | + let b = normalize_output(&run_output_from_cmd(&mut assert_cmd::cargo::cargo_bin_cmd!("hostname").arg("-y").arg("--help"))); |
| 48 | + assert_eq!(a, b); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn ping_wrapper_tests() { |
| 53 | + // sanity test to ensure this test file is discovered by the harness |
| 54 | + assert!(true); |
| 55 | +} |
| 56 | + |
| 57 | + |
0 commit comments