Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions engine/crates/opensweep-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn main() -> ExitCode {
other_drives,
&store,
),
Cmd::Plan { rules, project } => plan(rules.as_deref(), &project),
Cmd::Plan { rules, project } => plan(rules.as_deref(), &absolute(&project)),
Cmd::Clean {
rules,
include_caution,
Expand All @@ -256,7 +256,7 @@ fn main() -> ExitCode {
projects,
} => clean(
rules.as_deref(),
&projects,
&projects.iter().map(|p| absolute(p)).collect::<Vec<_>>(),
Flags {
caution: include_caution,
active: include_active,
Expand Down Expand Up @@ -301,6 +301,16 @@ fn main() -> ExitCode {
})
}

/// A project path typed relative to the working folder (`plan .`), made
/// absolute: the guard refuses every relative candidate. Absolute paths and
/// folders that do not exist are left as given.
fn absolute(project: &Path) -> PathBuf {
if project.is_absolute() {
return project.to_path_buf();
}
std::fs::canonicalize(project).unwrap_or_else(|_| project.to_path_buf())
}

fn load_rules(dir: Option<&Path>) -> Result<Vec<RuleFile>, String> {
match dir {
Some(d) => rules::load_dir(d),
Expand Down
24 changes: 24 additions & 0 deletions engine/crates/opensweep-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ fn clean_is_a_dry_run_by_default() {
);
}

#[test]
fn a_project_given_relative_to_the_working_folder_is_checked() {
let e = env();
let run_in_project = |args: &[&str]| {
let out = Command::new(env!("CARGO_BIN_EXE_opensweep"))
.args(args)
.current_dir(&e.project)
.env("HOME", &e.home)
.env_remove("XDG_DATA_HOME")
.env_remove("XDG_CONFIG_HOME")
.output()
.unwrap();
assert!(out.status.success(), "{args:?}");
String::from_utf8(out.stdout).unwrap()
};
let plan = run_in_project(&["plan", "--rules", RULES, "."]);
assert!(plan.contains("allow .next"), "{plan}");
assert!(!plan.contains("not absolute"), "{plan}");
let store = e.store.to_str().unwrap();
let clean = run_in_project(&["clean", "--rules", RULES, "--store", store, "../site"]);
assert!(clean.contains("move .next"), "{clean}");
assert!(clean.contains("Dry run"), "{clean}");
}

#[test]
fn a_folder_two_rules_claim_is_offered_once() {
let e = env();
Expand Down
Loading