diff --git a/tests/common/util.rs b/tests/common/util.rs index d5c9c4c27..abae40826 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -31,6 +31,22 @@ static ALREADY_RUN: &'static str = testing();"; static MULTIPLE_STDIN_MEANINGLESS: &'static str = "Ucommand is designed around a typical use case of: provide args and input stream -> spawn process -> block until completion -> return output streams. For verifying that a particular section of the input stream is what causes a particular behavior, use the Command type directly."; +/// Test if the program is running under WSL +// ref: @@ +// ToDO: test on WSL2 which likely doesn't need special handling; plan change to `is_wsl_1()` if WSL2 is less needy +pub fn is_wsl() -> bool { + #[cfg(target_os = "linux")] + { + if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") { + if let Ok(s) = std::str::from_utf8(&b) { + let a = s.to_ascii_lowercase(); + return a.contains("microsoft") || a.contains("wsl"); + } + } + } + false +} + fn read_scenario_fixture>(tmpd: &Option>, file_rel_path: S) -> String { let tmpdir_path = tmpd.as_ref().unwrap().as_ref().path(); AtPath::new(tmpdir_path).read(file_rel_path.as_ref().to_str().unwrap()) diff --git a/tests/test_chgrp.rs b/tests/test_chgrp.rs index e5a974910..a2a2b107b 100644 --- a/tests/test_chgrp.rs +++ b/tests/test_chgrp.rs @@ -98,7 +98,10 @@ fn test_preserve_root_symlink() { #[test] #[cfg(target_os = "linux")] fn test_reference() { - if get_effective_gid() != 0 { + // skip for root or MS-WSL + // * MS-WSL is bugged (as of 2019-12-25), allowing non-root accounts su-level privileges for `chgrp` + // * for MS-WSL, succeeds and stdout == 'group of /etc retained as root' + if !(get_effective_gid() == 0 || is_wsl()) { new_ucmd!() .arg("-v") .arg("--reference=/etc/passwd") diff --git a/tests/test_du.rs b/tests/test_du.rs index 7ade82dd7..d63c70cc1 100644 --- a/tests/test_du.rs +++ b/tests/test_du.rs @@ -47,7 +47,12 @@ fn _du_basics_subdir(s: String) { } #[cfg(not(target_os = "macos"))] fn _du_basics_subdir(s: String) { - assert_eq!(s, "8\tsubdir/deeper\n"); + // MS-WSL linux has altered expected output + if !is_wsl() { + assert_eq!(s, "8\tsubdir/deeper\n"); + } else { + assert_eq!(s, "0\tsubdir/deeper\n"); + } } #[test] @@ -81,7 +86,12 @@ fn _du_soft_link(s: String) { } #[cfg(not(target_os = "macos"))] fn _du_soft_link(s: String) { - assert_eq!(s, "16\tsubdir/links\n"); + // MS-WSL linux has altered expected output + if !is_wsl() { + assert_eq!(s, "16\tsubdir/links\n"); + } else { + assert_eq!(s, "8\tsubdir/links\n"); + } } #[test] @@ -104,7 +114,12 @@ fn _du_hard_link(s: String) { } #[cfg(not(target_os = "macos"))] fn _du_hard_link(s: String) { - assert_eq!(s, "16\tsubdir/links\n"); + // MS-WSL linux has altered expected output + if !is_wsl() { + assert_eq!(s, "16\tsubdir/links\n"); + } else { + assert_eq!(s, "8\tsubdir/links\n"); + } } #[test] @@ -123,5 +138,10 @@ fn _du_d_flag(s: String) { } #[cfg(not(target_os = "macos"))] fn _du_d_flag(s: String) { - assert_eq!(s, "28\t./subdir\n36\t./\n"); + // MS-WSL linux has altered expected output + if !is_wsl() { + assert_eq!(s, "28\t./subdir\n36\t./\n"); + } else { + assert_eq!(s, "8\t./subdir\n8\t./\n"); + } }