Skip to content

Commit

Permalink
install: Add a test 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 2c0f15b commit 817f1fc
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");
}

#[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 warning on line 1629 in tests/by-util/test_install.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_install.rs#L1627-L1629

Added lines #L1627 - L1629 were not covered by tests

// 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 {

Check warning on line 1635 in tests/by-util/test_install.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_install.rs#L1632-L1635

Added lines #L1632 - L1635 were not covered by tests
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"));

Check warning on line 1655 in tests/by-util/test_install.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_install.rs#L1654-L1655

Added lines #L1654 - L1655 were not covered by tests

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

Check warning on line 1660 in tests/by-util/test_install.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_install.rs#L1657-L1660

Added lines #L1657 - L1660 were not covered by tests
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 817f1fc

Please sign in to comment.