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
44 changes: 27 additions & 17 deletions engine/crates/opensweep-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn main() -> ExitCode {
other_drives,
&store,
),
Cmd::Plan { rules, project } => plan(rules.as_deref(), &absolute(&project)),
Cmd::Plan { rules, project } => absolute(&project).and_then(|p| plan(rules.as_deref(), &p)),
Cmd::Clean {
rules,
include_caution,
Expand All @@ -265,17 +265,23 @@ fn main() -> ExitCode {
execute,
store,
projects,
} => clean(
rules.as_deref(),
&projects.iter().map(|p| absolute(p)).collect::<Vec<_>>(),
Flags {
caution: include_caution,
active: include_active,
execute,
},
&only,
&store,
),
} => projects
.iter()
.map(|p| absolute(p))
.collect::<Result<Vec<_>, _>>()
.and_then(|projects| {
clean(
rules.as_deref(),
&projects,
Flags {
caution: include_caution,
active: include_active,
execute,
},
&only,
&store,
)
}),
Cmd::Pin {
folder,
days,
Expand Down Expand Up @@ -313,13 +319,17 @@ 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 {
/// absolute: the guard refuses every relative candidate. Absolute paths are
/// left as given. A path that is not a folder is an error, so a typo is not
/// reported as an idle project with nothing to clean.
fn absolute(project: &Path) -> Result<PathBuf, String> {
if !project.is_dir() {
return Err(format!("{} is not a folder", project.display()));
}
if project.is_absolute() {
return project.to_path_buf();
return Ok(project.to_path_buf());
}
std::fs::canonicalize(project).unwrap_or_else(|_| project.to_path_buf())
std::fs::canonicalize(project).map_err(|e| format!("{}: {e}", project.display()))
}

fn load_rules(dir: Option<&Path>) -> Result<Vec<RuleFile>, String> {
Expand Down
19 changes: 19 additions & 0 deletions engine/crates/opensweep-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ fn a_project_given_relative_to_the_working_folder_is_checked() {
assert!(clean.contains("Dry run"), "{clean}");
}

#[test]
fn a_project_that_is_not_a_folder_is_an_error() {
let e = env();
let missing = e.project.with_file_name("sit");
let file = e.project.join("package.json");
for path in [&missing, &file] {
let p = path.to_str().unwrap();
for args in [
vec!["plan", "--rules", RULES, p],
vec!["clean", "--rules", RULES, p],
] {
let out = e.run(&args);
assert!(!out.status.success(), "{args:?}");
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("is not a folder"), "{args:?}: {err}");
}
}
}

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