use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
#[test]
fn user_rule_matches_custom_artifact_directory() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("Makefile"), "all:\n\techo build\n").unwrap();
std::fs::create_dir(temp.path().join("my_build_x86")).unwrap();
std::fs::write(temp.path().join("my_build_x86").join("a.o"), b"x").unwrap();
std::fs::write(
temp.path().join(".rclean.toml"),
r#"
[[rule]]
id = "user.makefile_build"
name_glob = "my_build_*"
parent_markers = ["Makefile"]
category = "build"
safety = "safe"
why = "Custom Makefile build dir"
restore_hint = "make build"
"#,
)
.unwrap();
Command::cargo_bin("rclean")
.unwrap()
.args([
"scan",
temp.path().to_str().unwrap(),
"--json",
"--min-size",
"0",
])
.assert()
.success()
.stdout(predicate::str::contains(
"\"ruleId\": \"user.makefile_build\"",
))
.stdout(predicate::str::contains("\"safety\": \"safe\""));
}
#[test]
fn user_rule_action_plan_replays_successfully() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("Makefile"), "all:\n\techo build\n").unwrap();
std::fs::create_dir(temp.path().join("my_build_x86")).unwrap();
std::fs::write(temp.path().join("my_build_x86").join("a.o"), b"x").unwrap();
std::fs::write(
temp.path().join(".rclean.toml"),
r#"
[[rule]]
id = "user.makefile_build"
name_glob = "my_build_*"
parent_markers = ["Makefile"]
category = "build"
safety = "safe"
"#,
)
.unwrap();
let plan = temp.path().join("plan.json");
Command::cargo_bin("rclean")
.unwrap()
.args([
"scan",
temp.path().to_str().unwrap(),
"--write-plan",
plan.to_str().unwrap(),
"--min-size",
"0",
])
.assert()
.success();
Command::cargo_bin("rclean")
.unwrap()
.args(["clean", "--plan", plan.to_str().unwrap(), "--dry-run"])
.assert()
.success()
.stdout(predicate::str::contains("Plan: 1 candidates"))
.stdout(predicate::str::contains("user.makefile_build"));
assert!(temp.path().join("my_build_x86").exists());
}
#[test]
fn user_rule_skipped_when_parent_marker_missing() {
let temp = TempDir::new().unwrap();
std::fs::create_dir(temp.path().join("my_build_x86")).unwrap();
std::fs::write(temp.path().join("my_build_x86").join("a.o"), b"x").unwrap();
std::fs::write(
temp.path().join(".rclean.toml"),
r#"
[[rule]]
id = "user.makefile_build"
name_glob = "my_build_*"
parent_markers = ["Makefile"]
category = "build"
safety = "safe"
"#,
)
.unwrap();
Command::cargo_bin("rclean")
.unwrap()
.args([
"scan",
temp.path().to_str().unwrap(),
"--json",
"--min-size",
"0",
])
.assert()
.code(3)
.stdout(predicate::str::contains("\"ruleId\":").not());
}
#[test]
fn user_rule_with_blocked_safety_is_rejected_and_warned() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("Makefile"), "all:\n").unwrap();
std::fs::create_dir(temp.path().join("bad_dir")).unwrap();
std::fs::write(temp.path().join("bad_dir").join("a"), b"x").unwrap();
std::fs::write(
temp.path().join(".rclean.toml"),
r#"
[[rule]]
id = "user.evil"
name_glob = "bad_*"
parent_markers = ["Makefile"]
category = "build"
safety = "blocked"
"#,
)
.unwrap();
Command::cargo_bin("rclean")
.unwrap()
.args([
"scan",
temp.path().to_str().unwrap(),
"--json",
"--min-size",
"0",
])
.assert()
.code(3)
.stderr(predicate::str::contains("safety=blocked"))
.stdout(predicate::str::contains("\"ruleId\":").not());
}