Skip to content

Commit

Permalink
install: Add tests to cover recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 28, 2023
1 parent e23dd31 commit d21a113
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
// file that was distributed with this source code.
// spell-checker:ignore (words) helloworld nodir objdump n'source

use crate::common::util::{is_ci, TestScenario};
use crate::common::util::{is_ci, run_ucmd_as_root, TestScenario};
use filetime::FileTime;
use std::os::unix::fs::PermissionsExt;
use std::fs;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
#[cfg(not(any(windows, target_os = "freebsd")))]
use std::process::Command;
#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down Expand Up @@ -1613,3 +1614,55 @@ fn test_target_file_ends_with_slash() {
.fails()
.stderr_contains("failed to access 'dir/target_file/': Not a directory");

Check failure on line 1615 in tests/by-util/test_install.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'tests/by-util/test_install.rs', line:1615; use `cargo fmt -- "tests/by-util/test_install.rs"`)
}

#[test]
// Matches part of tests/install/install-C-root.sh
fn test_install_root() {
let ts = TestScenario::new(util_name!());
let at = ts.fixtures.clone();
at.touch("a");

let run_and_check = |args: &[&str], expected_uid: u32, expected_gid: u32| {
if let Ok(result) = run_ucmd_as_root(&ts, args) {
result.success();
assert!(at.file_exists("b"));
assert!(result.stdout_str().contains("'a' -> 'b'\n"));

// Check the UID and GID of the file
let metadata = fs::metadata(at.plus("b")).unwrap();
assert_eq!(metadata.uid(), expected_uid);
assert_eq!(metadata.gid(), expected_gid);
} else {
print!("Test skipped; requires root user");
}
};

run_and_check(&["-Cv", "-o1", "-g1", "a", "b"], 1, 1);
run_and_check(&["-Cv", "-o2", "-g1", "a", "b"], 2, 1);
run_and_check(&["-Cv", "-o2", "-g2", "a", "b"], 2, 2);
}

#[test]
fn test_install_as_root() {
// Matches part of tests/install/install-C-root.sh
let ts = TestScenario::new(util_name!());
let at = ts.fixtures.clone();
at.touch("a");

let run_and_check_uid_gid = |args: &[&str], expected_uid: u32, expected_gid: u32| {
if let Ok(result) = run_ucmd_as_root(&ts, args) {
result.success();
assert!(at.file_exists("b"));

let metadata = fs::metadata(at.plus("b")).unwrap();
assert_eq!(metadata.uid(), expected_uid);
assert_eq!(metadata.gid(), expected_gid);
} else {
print!("Test skipped; requires root user");
}
};

run_and_check_uid_gid(&["-Cv", "-o2", "a", "b"], 2, 0);
run_and_check_uid_gid(&["-Cv", "a", "b"], 0, 0);
run_and_check_uid_gid(&["-Cv", "a", "b"], 0, 0);
}

0 comments on commit d21a113

Please sign in to comment.