Skip to content

Commit 0ca7a3d

Browse files
authored
Merge pull request #9058 from akretz/chown-wheel-group
Fix chown tests for FreeBSD and macOS
2 parents 67ef669 + 4a2890a commit 0ca7a3d

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

tests/by-util/test_chown.rs

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ fn skipping_test_is_okay(result: &CmdResult, needle: &str) -> bool {
3636
false
3737
}
3838

39+
#[cfg(any(target_os = "linux", target_os = "android", target_os = "windows"))]
40+
const ROOT_GROUP: &str = "root";
41+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "windows")))]
42+
const ROOT_GROUP: &str = "wheel";
43+
3944
#[cfg(test)]
4045
mod test_passgrp {
4146
use chown::entries::{gid2grp, grp2gid, uid2usr, usr2uid};
@@ -49,11 +54,7 @@ mod test_passgrp {
4954

5055
#[test]
5156
fn test_grp2gid() {
52-
if cfg!(target_os = "linux") || cfg!(target_os = "android") || cfg!(target_os = "windows") {
53-
assert_eq!(0, grp2gid("root").unwrap());
54-
} else {
55-
assert_eq!(0, grp2gid("wheel").unwrap());
56-
}
57+
assert_eq!(0, grp2gid(super::ROOT_GROUP).unwrap());
5758
assert!(grp2gid("88_888_888").is_err());
5859
assert!(grp2gid("agroupthatdoesntexist").is_err());
5960
}
@@ -66,11 +67,7 @@ mod test_passgrp {
6667

6768
#[test]
6869
fn test_gid2grp() {
69-
if cfg!(target_os = "linux") || cfg!(target_os = "android") || cfg!(target_os = "windows") {
70-
assert_eq!("root", gid2grp(0).unwrap());
71-
} else {
72-
assert_eq!("wheel", gid2grp(0).unwrap());
73-
}
70+
assert_eq!(super::ROOT_GROUP, gid2grp(0).unwrap());
7471
assert!(gid2grp(88_888_888).is_err());
7572
}
7673
}
@@ -218,8 +215,7 @@ fn test_chown_failed_stdout() {
218215
}
219216

220217
#[test]
221-
// FixME: Fails on freebsd because of chown: invalid group: 'root:root'
222-
#[cfg(all(not(target_os = "freebsd"), not(target_os = "openbsd")))]
218+
#[cfg(not(target_os = "openbsd"))]
223219
fn test_chown_owner_group() {
224220
// test chown username:group file.txt
225221

@@ -271,20 +267,17 @@ fn test_chown_owner_group() {
271267
.fails()
272268
.stderr_contains("invalid group");
273269

274-
// TODO: on macos group name is not recognized correctly: "chown: invalid group: 'root:root'
275-
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
276270
scene
277271
.ucmd()
278-
.arg("root:root")
272+
.arg(format!("root:{ROOT_GROUP}"))
279273
.arg("--verbose")
280274
.arg(file1)
281275
.fails()
282276
.stderr_contains("failed to change");
283277
}
284278

285279
#[test]
286-
// FixME: Fails on freebsd because of chown: invalid group: 'root:root'
287-
#[cfg(all(not(target_os = "freebsd"), not(target_os = "openbsd")))]
280+
#[cfg(not(target_os = "openbsd"))]
288281
fn test_chown_various_input() {
289282
// test chown username:group file.txt
290283

@@ -344,54 +337,42 @@ fn test_chown_various_input() {
344337
}
345338

346339
#[test]
347-
// FixME: on macos & freebsd group name is not recognized correctly: "chown: invalid group: ':groupname'
348-
#[cfg(any(
349-
windows,
350-
all(
351-
unix,
352-
not(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))
353-
)
354-
))]
340+
#[cfg(any(windows, all(unix, not(target_os = "openbsd"))))]
355341
fn test_chown_only_group() {
356342
// test chown :group file.txt
357343

358344
let scene = TestScenario::new(util_name!());
359345
let at = &scene.fixtures;
360346

361-
let result = scene.cmd("whoami").run();
362-
if skipping_test_is_okay(&result, "whoami: cannot find name for user ID") {
347+
let result = scene.cmd("id").arg("-gn").run();
348+
if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
363349
return;
364350
}
365-
let user_name = String::from(result.stdout_str().trim());
366-
assert!(!user_name.is_empty());
351+
let group_name = String::from(result.stdout_str().trim());
352+
assert!(!group_name.is_empty());
367353

368354
let file1 = "test_chown_file1";
369355
at.touch(file1);
370356

371357
let result = scene
372358
.ucmd()
373-
.arg(format!(":{user_name}"))
359+
.arg(format!(":{group_name}"))
374360
.arg("--verbose")
375361
.arg(file1)
376362
.run();
377-
if is_ci() && result.stderr_str().contains("Operation not permitted") {
378-
// With ubuntu with old Rust in the CI, we can get an error
379-
return;
380-
}
381-
if is_ci() && result.stderr_str().contains("chown: invalid group:") {
382-
// With mac into the CI, we can get this answer
383-
return;
384-
}
385363
result.stderr_contains("retained as");
386364
result.success();
387365

388-
scene
389-
.ucmd()
390-
.arg(":root")
391-
.arg("--verbose")
392-
.arg(file1)
393-
.fails()
394-
.stderr_contains("failed to change");
366+
// FreeBSD user on CI is part of wheel group
367+
if group_name != ROOT_GROUP {
368+
scene
369+
.ucmd()
370+
.arg(format!(":{ROOT_GROUP}"))
371+
.arg("--verbose")
372+
.arg(file1)
373+
.fails()
374+
.stderr_contains("failed to change");
375+
}
395376
}
396377

397378
#[test]
@@ -484,8 +465,7 @@ fn test_chown_only_user_id_nonexistent_user() {
484465
}
485466

486467
#[test]
487-
// FixME: stderr = chown: ownership of 'test_chown_file1' retained as cuuser:wheel
488-
#[cfg(all(not(target_os = "freebsd"), not(target_os = "openbsd")))]
468+
#[cfg(not(target_os = "openbsd"))]
489469
fn test_chown_only_group_id() {
490470
// test chown :1111 file.txt
491471

@@ -517,13 +497,16 @@ fn test_chown_only_group_id() {
517497
// Apparently on CI "macos-latest, x86_64-apple-darwin, feat_os_macos"
518498
// the process has the rights to change from runner:staff to runner:wheel
519499
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
520-
scene
521-
.ucmd()
522-
.arg(":0")
523-
.arg("--verbose")
524-
.arg(file1)
525-
.fails()
526-
.stderr_contains("failed to change");
500+
// FreeBSD user on CI is part of wheel group
501+
if group_id != "0" {
502+
scene
503+
.ucmd()
504+
.arg(":0")
505+
.arg("--verbose")
506+
.arg(file1)
507+
.fails()
508+
.stderr_contains("failed to change");
509+
}
527510
}
528511

529512
/// Test for setting the group to a group ID for a group that does not exist.
@@ -610,8 +593,7 @@ fn test_chown_owner_group_id() {
610593
}
611594

612595
#[test]
613-
// FixME: Fails on freebsd because of chown: invalid group: '0:root'
614-
#[cfg(all(not(target_os = "freebsd"), not(target_os = "openbsd")))]
596+
#[cfg(not(target_os = "openbsd"))]
615597
fn test_chown_owner_group_mix() {
616598
// test chown 1111:group file.txt
617599

@@ -643,11 +625,9 @@ fn test_chown_owner_group_mix() {
643625
.run();
644626
result.stderr_contains("retained as");
645627

646-
// TODO: on macos group name is not recognized correctly: "chown: invalid group: '0:root'
647-
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
648628
scene
649629
.ucmd()
650-
.arg("0:root")
630+
.arg(format!("0:{ROOT_GROUP}"))
651631
.arg("--verbose")
652632
.arg(file1)
653633
.fails()

0 commit comments

Comments
 (0)