From f3c13069746249e9456c4c96bae4c71151899204 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Tue, 21 Jan 2025 20:44:05 -0500 Subject: [PATCH] feat: improve parse error slightly (#482) --- docs/release-notes.md | 5 +++++ src/registry.rs | 4 ++-- tests/snapshot.rs | 11 +++++++++++ tests/snapshots/snapshot__invalid_inputs.snap | 17 +++++++++++++++++ tests/test-data/invalid/invalid-workflow.yml | 14 ++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/snapshots/snapshot__invalid_inputs.snap create mode 100644 tests/test-data/invalid/invalid-workflow.yml diff --git a/docs/release-notes.md b/docs/release-notes.md index 47d59a2..b5ed9d7 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -11,6 +11,11 @@ of `zizmor`. Nothing to see here (yet!) +### Improvements 🌱 + +* `zizmor` produces slightly more informative error messages when given + an invalid input file (#482) + ### Bug Fixes 🐛 * Fixed a bug where `zizmor` would fail to discover actions within diff --git a/src/registry.rs b/src/registry.rs index d0eda40..833a8cd 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -159,8 +159,8 @@ impl InputRegistry { Err(we) => match Action::from_file(path, prefix) { Ok(action) => self.register_input(action.into()), Err(ae) => Err(anyhow!("failed to register input as workflow or action")) - .with_context(|| we) - .with_context(|| ae), + .with_context(|| format!("{ae:?}")) + .with_context(|| format!("{we:?}")), }, } } diff --git a/tests/snapshot.rs b/tests/snapshot.rs index 632c29d..d7853ec 100644 --- a/tests/snapshot.rs +++ b/tests/snapshot.rs @@ -127,6 +127,17 @@ fn test_conflicting_online_options() -> Result<()> { Ok(()) } +#[test] +fn test_invalid_inputs() -> Result<()> { + insta::assert_snapshot!(zizmor() + .output(OutputMode::Stderr) + .offline(true) + .workflow(workflow_under_test("invalid/invalid-workflow.yml")) + .run()?); + + Ok(()) +} + #[test] fn artipacked() -> Result<()> { insta::assert_snapshot!(zizmor() diff --git a/tests/snapshots/snapshot__invalid_inputs.snap b/tests/snapshots/snapshot__invalid_inputs.snap new file mode 100644 index 0000000..6d94fe1 --- /dev/null +++ b/tests/snapshots/snapshot__invalid_inputs.snap @@ -0,0 +1,17 @@ +--- +source: tests/snapshot.rs +expression: "zizmor().output(OutputMode::Stderr).offline(true).workflow(workflow_under_test(\"invalid/invalid-workflow.yml\")).run()?" +snapshot_kind: text +--- +failed to register input: @@INPUT@@ + +Caused by: + 0: invalid GitHub Actions workflow: file://@@INPUT@@ + + Caused by: + jobs: data did not match any variant of untagged enum Job at line 10 column 3 + 1: invalid GitHub Actions definition: file://@@INPUT@@ + + Caused by: + missing field `runs` + 2: failed to register input as workflow or action diff --git a/tests/test-data/invalid/invalid-workflow.yml b/tests/test-data/invalid/invalid-workflow.yml new file mode 100644 index 0000000..0a154ff --- /dev/null +++ b/tests/test-data/invalid/invalid-workflow.yml @@ -0,0 +1,14 @@ +name: "invalid-workflow" + +on: + repository_dispatch: + +permissions: + contents: read + +jobs: + invalid: + name: "invalid" + + steps: + - run: echo hello