Skip to content

Commit

Permalink
Summarize paths affected by commands when the list of paths is long
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Dec 16, 2023
1 parent d53919c commit fbed20e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<!-- next-header -->

- When printing the results of running a command that was invoked with a long list of paths, the
output will now summarize the paths affected instead of always printing all of them. GH #60.

## 0.6.1 2023-11-06

- The `dev/bin/check-go-mod.sh` script created when running `precious config init --component go` is
Expand Down
51 changes: 43 additions & 8 deletions precious-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ impl LintOrTidyCommand {
Ok(cmd)
}

pub(crate) fn files_summary(&self, paths: &[&Path]) -> String {
pub(crate) fn paths_summary(&self, paths: &[&Path]) -> String {
let all = paths
.iter()
.sorted()
Expand All @@ -678,11 +678,11 @@ impl LintOrTidyCommand {
}

match self.invoke {
Invoke::Once => {
Invoke::Once | Invoke::PerDir => {
let initial = paths
.iter()
.take(2)
.sorted()
.take(2)
.map(|p| p.to_string_lossy())
.join(" ");
format!(
Expand All @@ -692,7 +692,6 @@ impl LintOrTidyCommand {
initial
)
}
Invoke::PerDir => format!("{} files in {}", paths.len(), all,),
Invoke::PerFile => format!("{} files: {}", paths.len(), all,),
}
}
Expand Down Expand Up @@ -1650,7 +1649,7 @@ mod tests {

#[test]
#[parallel]
fn files_summary() -> Result<()> {
fn paths_summary() -> Result<()> {
struct TestCase {
invoke: Invoke,
include: Vec<String>,
Expand Down Expand Up @@ -1704,14 +1703,50 @@ mod tests {
.collect::<Vec<_>>(),
expect: "4 files matching **/*.go !food.go, starting with bar.go foo.go",
},
TestCase {
invoke: Invoke::PerDir,
include: vec![String::from("**/*.go")],
paths: vec![Path::new("foo.go")],
expect: "foo.go",
},
TestCase {
invoke: Invoke::PerDir,
include: vec![String::from("**/*.go")],
paths: ["foo.go", "bar.go"]
.iter()
.map(Path::new)
.collect::<Vec<_>>(),
expect: "bar.go foo.go",
},
TestCase {
invoke: Invoke::PerDir,
include: vec![String::from("**/*.go")],
paths: ["foo.go", "bar.go", "baz.go"]
.iter()
.map(Path::new)
.collect::<Vec<_>>(),
expect: "bar.go baz.go foo.go",
},
TestCase {
invoke: Invoke::PerDir,
include: vec![String::from("**/*.go")],
paths: ["foo.go", "bar.go", "baz.go", "quux.go"]
.iter()
.map(Path::new)
.collect::<Vec<_>>(),
expect: "4 files matching **/*.go, starting with bar.go foo.go",
},
TestCase {
invoke: Invoke::PerDir,
include: ["**/*.go", "!food.go"]
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
paths: vec![Path::new("foo")],
expect: "foo",
paths: ["foo.go", "bar.go", "baz.go", "quux.go"]
.iter()
.map(Path::new)
.collect::<Vec<_>>(),
expect: "4 files matching **/*.go !food.go, starting with bar.go foo.go",
},
TestCase {
invoke: Invoke::PerFile,
Expand All @@ -1733,7 +1768,7 @@ mod tests {
};

{
assert_eq!(command.files_summary(&t.paths), t.expect);
assert_eq!(command.paths_summary(&t.paths), t.expect);
}
}

Expand Down
14 changes: 7 additions & 7 deletions precious-core/src/precious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ impl LintOrTidyRunner {
"{} Tidied by {}: {}",
s.chars.tidied,
t.name,
t.files_summary(files),
t.paths_summary(files),
);
}
Some(Ok(()))
Expand All @@ -775,7 +775,7 @@ impl LintOrTidyRunner {
"{} Unchanged by {}: {}",
s.chars.unchanged,
t.name,
t.files_summary(files),
t.paths_summary(files),
);
}
Some(Ok(()))
Expand All @@ -786,7 +786,7 @@ impl LintOrTidyRunner {
"{} Maybe changed by {}: {}",
s.chars.unknown,
t.name,
t.files_summary(files),
t.paths_summary(files),
);
}
Some(Ok(()))
Expand All @@ -797,7 +797,7 @@ impl LintOrTidyRunner {
"{} Error from {}: {}",
s.chars.execution_error,
t.name,
t.files_summary(files),
t.paths_summary(files),
);
Some(Err(ActionFailure {
error: format!("{e:#}"),
Expand Down Expand Up @@ -825,7 +825,7 @@ impl LintOrTidyRunner {
"{} Passed {}: {}",
s.chars.lint_free,
l.name,
l.files_summary(files),
l.paths_summary(files),
);
}
Some(Ok(()))
Expand All @@ -834,7 +834,7 @@ impl LintOrTidyRunner {
"{} Failed {}: {}",
s.chars.lint_dirty,
l.name,
l.files_summary(files),
l.paths_summary(files),
);
if let Some(s) = lo.stdout {
println!("{s}");
Expand Down Expand Up @@ -869,7 +869,7 @@ impl LintOrTidyRunner {
"{} error {}: {}",
s.chars.execution_error,
l.name,
l.files_summary(files),
l.paths_summary(files),
);
Some(Err(ActionFailure {
error: format!("{e:#}"),
Expand Down

0 comments on commit fbed20e

Please sign in to comment.