Skip to content

Commit b5be7c6

Browse files
MatthewMckee4j178
andauthored
Provide more information when validating configs and manifests (#1182)
* Provide more information when validating configs and manifests * Fix tests * Fix other snapshot * Write to printer to allow hide output with `-q` --------- Co-authored-by: Jo <[email protected]>
1 parent dad650b commit b5be7c6

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

src/cli/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) use self_update::self_update;
4242
pub(crate) use try_repo::try_repo;
4343
pub(crate) use validate::{validate_configs, validate_manifest};
4444

45-
#[derive(Copy, Clone)]
45+
#[derive(Copy, Clone, PartialEq, Eq)]
4646
pub(crate) enum ExitStatus {
4747
/// The command succeeded.
4848
Success,

src/cli/validate.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,81 @@
11
use std::error::Error;
2+
use std::fmt::Write;
23
use std::iter;
34
use std::path::PathBuf;
45

56
use anstream::eprintln;
7+
use anyhow::Result;
68
use owo_colors::OwoColorize;
79

810
use crate::cli::ExitStatus;
911
use crate::config::{read_config, read_manifest};
12+
use crate::printer::Printer;
13+
use crate::warn_user;
1014

11-
pub(crate) fn validate_configs(configs: Vec<PathBuf>) -> ExitStatus {
15+
pub(crate) fn validate_configs(configs: Vec<PathBuf>, printer: Printer) -> Result<ExitStatus> {
1216
let mut status = ExitStatus::Success;
1317

18+
if configs.is_empty() {
19+
warn_user!("No configs to check");
20+
return Ok(ExitStatus::Success);
21+
}
22+
1423
for config in configs {
1524
if let Err(err) = read_config(&config) {
16-
eprintln!("{}: {}", "error".red().bold(), err);
25+
writeln!(printer.stderr(), "{}: {}", "error".red().bold(), err)?;
1726
for source in iter::successors(err.source(), |&err| err.source()) {
18-
eprintln!(" {}: {}", "caused by".red().bold(), source);
27+
writeln!(
28+
printer.stderr(),
29+
" {}: {}",
30+
"caused by".red().bold(),
31+
source
32+
)?;
1933
}
2034
status = ExitStatus::Failure;
2135
}
2236
}
2337

24-
status
38+
if status == ExitStatus::Success {
39+
writeln!(
40+
printer.stderr(),
41+
"{}: All configs are valid",
42+
"success".green().bold()
43+
)?;
44+
}
45+
46+
Ok(status)
2547
}
2648

27-
pub(crate) fn validate_manifest(configs: Vec<PathBuf>) -> ExitStatus {
49+
pub(crate) fn validate_manifest(manifests: Vec<PathBuf>, printer: Printer) -> Result<ExitStatus> {
2850
let mut status = ExitStatus::Success;
2951

30-
for config in configs {
31-
if let Err(err) = read_manifest(&config) {
32-
eprintln!("{}: {}", "error".red().bold(), err);
52+
if manifests.is_empty() {
53+
warn_user!("No manifests to check");
54+
return Ok(ExitStatus::Success);
55+
}
56+
57+
for manifest in manifests {
58+
if let Err(err) = read_manifest(&manifest) {
59+
writeln!(printer.stderr(), "{}: {}", "error".red().bold(), err)?;
3360
for source in iter::successors(err.source(), |&err| err.source()) {
34-
eprintln!(" {}: {}", "caused by".red().bold(), source);
61+
writeln!(
62+
printer.stderr(),
63+
" {}: {}",
64+
"caused by".red().bold(),
65+
source
66+
)?;
3567
}
3668
status = ExitStatus::Failure;
3769
}
3870
}
3971

40-
status
72+
if status == ExitStatus::Success {
73+
writeln!(
74+
printer.stderr(),
75+
"{}: All manifests are valid",
76+
"success".green().bold()
77+
)?;
78+
}
79+
80+
Ok(status)
4181
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
315315
Command::ValidateConfig(args) => {
316316
show_settings!(args);
317317

318-
Ok(cli::validate_configs(args.configs))
318+
cli::validate_configs(args.configs, printer)
319319
}
320320
Command::ValidateManifest(args) => {
321321
show_settings!(args);
322322

323-
Ok(cli::validate_manifest(args.manifests))
323+
cli::validate_manifest(args.manifests, printer)
324324
}
325325
Command::SampleConfig(args) => cli::sample_config(args.file, printer),
326326
Command::AutoUpdate(args) => {

tests/validate.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ fn validate_config() -> anyhow::Result<()> {
1010
let context = TestContext::new();
1111

1212
// No files to validate.
13-
cmd_snapshot!(context.filters(), context.validate_config(), @r#"
13+
cmd_snapshot!(context.filters(), context.validate_config(), @r"
1414
success: true
1515
exit_code: 0
1616
----- stdout -----
1717
1818
----- stderr -----
19-
"#);
19+
warning: No configs to check
20+
");
2021

2122
context.write_pre_commit_config(indoc::indoc! {r"
2223
repos:
@@ -28,13 +29,14 @@ fn validate_config() -> anyhow::Result<()> {
2829
- id: check-json
2930
"});
3031
// Validate one file.
31-
cmd_snapshot!(context.filters(), context.validate_config().arg(CONFIG_FILE), @r#"
32+
cmd_snapshot!(context.filters(), context.validate_config().arg(CONFIG_FILE), @r"
3233
success: true
3334
exit_code: 0
3435
----- stdout -----
3536
3637
----- stderr -----
37-
"#);
38+
success: All configs are valid
39+
");
3840

3941
context
4042
.work_dir()
@@ -63,13 +65,14 @@ fn validate_manifest() -> anyhow::Result<()> {
6365
let context = TestContext::new();
6466

6567
// No files to validate.
66-
cmd_snapshot!(context.filters(), context.validate_manifest(), @r#"
68+
cmd_snapshot!(context.filters(), context.validate_manifest(), @r"
6769
success: true
6870
exit_code: 0
6971
----- stdout -----
7072
7173
----- stderr -----
72-
"#);
74+
warning: No manifests to check
75+
");
7376

7477
context
7578
.work_dir()
@@ -84,13 +87,14 @@ fn validate_manifest() -> anyhow::Result<()> {
8487
minimum_pre_commit_version: 3.2.0
8588
"})?;
8689
// Validate one file.
87-
cmd_snapshot!(context.filters(), context.validate_manifest().arg(".pre-commit-hooks.yaml"), @r#"
90+
cmd_snapshot!(context.filters(), context.validate_manifest().arg(".pre-commit-hooks.yaml"), @r"
8891
success: true
8992
exit_code: 0
9093
----- stdout -----
9194
9295
----- stderr -----
93-
"#);
96+
success: All manifests are valid
97+
");
9498

9599
context
96100
.work_dir()
@@ -143,6 +147,7 @@ fn unexpected_keys_warning() {
143147
144148
----- stderr -----
145149
warning: Ignored unexpected keys in `.pre-commit-config.yaml`: `another_unknown`, `unexpected_top_level_key`, `repos[0].unexpected_repo_key`
150+
success: All configs are valid
146151
");
147152

148153
context.write_pre_commit_config(indoc::indoc! {r"
@@ -177,5 +182,6 @@ fn unexpected_keys_warning() {
177182
- `repos[0].hooks[0].unexpected_hook_key_2`
178183
- `repos[0].hooks[0].unexpected_hook_key_3`
179184
- `repos[0].hooks[0].unexpected_hook_key_4`
185+
success: All configs are valid
180186
");
181187
}

0 commit comments

Comments
 (0)