diff --git a/README.md b/README.md index adf5d73..d876691 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,8 @@ to keep the column out of the inventory afterwards too, name both rules: `-- cdclint:ignore schema-before-connector,source-column-not-captured: PII`. Files in, findings out, non-zero exit. No database, no daemon, no credentials. -Under a second on a laptop. `--fail-on warning` or `info` raises the bar; +Under a second on a laptop. `--fail-on warning` or `info` raises the bar +(any other value is an error, not the default); `--format json` is for anything that wants to post findings somewhere. A later live mode reads the deployed Postgres, Kafka Connect and sink to report drift against what is actually running. diff --git a/action.yml b/action.yml index f7cd0cf..743cc2c 100644 --- a/action.yml +++ b/action.yml @@ -133,7 +133,10 @@ runs: PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} run: | set -uo pipefail - args=(--migrations "$MIGRATIONS" --connector "$CONNECTOR" --fail-on "$FAIL_ON") + # An empty input (a workflow passing an unset variable) meant error + # before cdclint checked --fail-on, and still does; anything else + # that is not error, warning or info now fails the step. + args=(--migrations "$MIGRATIONS" --connector "$CONNECTOR" --fail-on "${FAIL_ON:-error}") if [ -n "$DISABLE" ]; then args+=(--disable "$DISABLE") fi diff --git a/cmd/cdclint/main.go b/cmd/cdclint/main.go index 054459c..14aca0b 100644 --- a/cmd/cdclint/main.go +++ b/cmd/cdclint/main.go @@ -70,6 +70,15 @@ func run(args []string, stdout, stderr io.Writer) int { fmt.Fprintln(stdout, "cdclint", version) return 0 } + // An unknown --fail-on used to fall through to error, so + // `--fail-on warnings` let warnings pass while the pipeline believed it + // blocked on them. It is checked before anything is read, like + // --disable, so a typo cannot look like a clean run. + threshold, ok := map[string]model.Severity{"error": model.Error, "warning": model.Warning, "info": model.Info}[*minSev] + if !ok { + fmt.Fprintf(stderr, "cdclint: --fail-on: unknown severity %q; the severities are error, warning, info\n", *minSev) + return 2 + } disabled := map[string]bool{} for _, v := range disable { for _, name := range strings.Split(v, ",") { @@ -139,13 +148,6 @@ func run(args []string, stdout, stderr io.Writer) int { default: fmt.Fprint(stdout, TextReport(findings, hidden, acked)) } - threshold := model.Error - switch *minSev { - case "warning": - threshold = model.Warning - case "info": - threshold = model.Info - } for _, f := range findings { if f.Severity >= threshold { return 1 diff --git a/cmd/cdclint/main_test.go b/cmd/cdclint/main_test.go index d7055b3..fdda858 100644 --- a/cmd/cdclint/main_test.go +++ b/cmd/cdclint/main_test.go @@ -88,3 +88,32 @@ func TestJSONKeepsItsShapeAndNotesOnStderr(t *testing.T) { t.Errorf("stderr %q", errOut) } } + +func TestAnUnknownFailOnIsAnErrorNotTheDefault(t *testing.T) { + // "warnings" used to fall through to the default, error: a pipeline + // that asked to block on warnings passed them silently. include-list-typo + // has a warning and an error, so any lint that ran would exit 1. + // An explicit empty value is rejected too; action.yml maps an empty + // input to error itself, so only the CLI sees "" from a person. + for _, bad := range []string{"warnings", "WARNING", "err", ""} { + code, out, errOut := cli(t, append(entry("include-list-typo"), "--fail-on", bad)...) + if code != 2 || out != "" { + t.Errorf("--fail-on %q: exit %d, stdout %q; want exit 2 before linting", bad, code, out) + } + if !strings.Contains(errOut, "--fail-on") || !strings.Contains(errOut, "error, warning, info") { + t.Errorf("--fail-on %q: stderr %q", bad, errOut) + } + } + // The three that exist still work; info and warning reach the warning. + for sev, want := range map[string]int{"error": 1, "warning": 1, "info": 1} { + if code, _, _ := cli(t, append(entry("include-list-typo"), "--fail-on", sev)...); code != want { + t.Errorf("--fail-on %s: exit %d, want %d", sev, code, want) + } + } + if code, _, _ := cli(t, append(entry("include-list-typo"), "--fail-on", "warning", "--disable", "sink-column-not-captured")...); code != 1 { + t.Errorf("--fail-on warning with only the warning left: exit %d, want 1", code) + } + if code, _, _ := cli(t, append(entry("include-list-typo"), "--disable", "sink-column-not-captured")...); code != 0 { + t.Errorf("default --fail-on error with only the warning left: exit %d, want 0", code) + } +}