Skip to content

Commit b2d64ec

Browse files
KSXGitHubclaudeCopilot
authored
test: refuse to test fs_errors on root environments (#364)
* fix(test): skip fs_errors test when running as root Root (uid=0) bypasses Unix discretionary access controls, so removing read permissions with chmod has no effect. This causes the fs_errors test to fail because read_dir() succeeds instead of returning Permission denied. Skip the test with an explanatory message when euid is 0. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * refactor(test): probe permissions instead of checking euid Replace the `unsafe { libc::geteuid() }` check with a direct probe: after chmod -r, try read_dir and skip if it still succeeds. This removes the `unsafe` block and the `libc` dev-dependency while being more semantically correct — it tests the actual condition we care about rather than a proxy. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * Revert "refactor(test): probe permissions instead of checking euid" This reverts commit bb8e8cc. * fix(test): fail loudly on root with cfg escape hatch for fs_errors Instead of silently skipping, panic with an actionable error message when running as root. The test can be excluded entirely via `RUSTFLAGS='--cfg pdu_test_skip_fs_errors'` for environments like Claude Code Web where root is unavoidable. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * docs: improve error message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(test): allow unexpected_cfgs for pdu_test_skip_fs_errors Suppress the -D unexpected-cfgs error by adding #[allow(unexpected_cfgs)] before the custom cfg gate. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * fix(test): register pdu_test_skip_fs_errors in check-cfg The #[allow(unexpected_cfgs)] approach doesn't override -D warnings used in CI release builds. Register the custom cfg in Cargo.toml's [lints.rust] section instead, which properly declares it as a known cfg to the compiler. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * style(cargo): move [lints.rust] section after [dev-dependencies] https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * fix(test): gate fs_errors imports and helper with pdu_test_skip_fs_errors Gate the Unix-specific imports and fs_permission helper with cfg(all(unix, not(pdu_test_skip_fs_errors))) so they don't produce dead code warnings when the test is skipped. https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * style(test): use separate cfg attributes instead of cfg(all(...)) https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ * fix(cargo): pin libc to exact patch version for consistency https://claude.ai/code/session_01Ki7wbrgQXWs2GXf8h2hybQ --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d27c20b commit b2d64ec

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ zero-copy-pads = "0.2.0"
8686
[dev-dependencies]
8787
build-fs-tree = "0.8.1"
8888
command-extra = "1.0.0"
89+
libc = "0.2.182"
8990
maplit = "1.0.2"
9091
normalize-path = "0.2.1"
9192
pretty_assertions = "1.4.1"
9293
rand = "0.10.0"
94+
95+
[lints.rust]
96+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(pdu_test_skip_fs_errors)'] }

tests/cli_errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use std::process::{Command, Output, Stdio};
1010
use text_block_macros::text_block;
1111

1212
#[cfg(unix)]
13+
#[cfg(not(pdu_test_skip_fs_errors))]
1314
use maplit::btreeset;
1415
#[cfg(unix)]
16+
#[cfg(not(pdu_test_skip_fs_errors))]
1517
use parallel_disk_usage::{
1618
bytes_format::BytesFormat,
1719
data_tree::DataTree,
@@ -23,6 +25,7 @@ use parallel_disk_usage::{
2325
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
2426
};
2527
#[cfg(unix)]
28+
#[cfg(not(pdu_test_skip_fs_errors))]
2629
use std::{collections::BTreeSet, path::Path};
2730

2831
fn stdio(command: Command) -> Command {
@@ -33,6 +36,7 @@ fn stdio(command: Command) -> Command {
3336
}
3437

3538
#[cfg(unix)]
39+
#[cfg(not(pdu_test_skip_fs_errors))]
3640
fn fs_permission(path: impl AsRef<Path>, permission: &'static str, recursive: bool) {
3741
let Output { status, stderr, .. } = Command::new("chmod")
3842
.pipe(|cmd| if recursive { cmd.with_arg("-R") } else { cmd })
@@ -106,8 +110,17 @@ fn max_depth_0() {
106110
}
107111

108112
#[cfg(unix)]
113+
#[cfg(not(pdu_test_skip_fs_errors))]
109114
#[test]
110115
fn fs_errors() {
116+
if unsafe { libc::geteuid() } == 0 {
117+
panic!(
118+
"{}\n{}",
119+
"error: This test must not be run as root because running with elevated privileges would affect its accuracy.",
120+
"hint: Either run this test as a non-root user or set `RUSTFLAGS='--cfg pdu_test_skip_fs_errors'` to skip this test.",
121+
);
122+
}
123+
111124
let workspace = SampleWorkspace::default();
112125
fs_permission(workspace.join("empty-dir"), "-r", false);
113126
fs_permission(workspace.join("nested").join("0"), "-r", false);

0 commit comments

Comments
 (0)