From f31ec636c912f3c9a53672e87a13ad724921b902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Wed, 1 Jun 2022 13:07:16 +0200 Subject: [PATCH] fix(cli): do not show 'or higher' if severity equals error (#2172) --- packages/cli/src/commands/lint.ts | 9 ++++-- .../scenarios/custom-ref-resolver.scenario | 2 +- .../ignored-unrecognized-format.scenario | 2 +- .../scenarios/oas3.1/petstore.scenario | 2 +- .../operation-security-defined.oas3.scenario | 2 +- .../parameter-description-links.oas3.scenario | 2 +- .../severity/display-errors.scenario | 2 +- .../severity/fail-on-warn-no-warn.scenario | 28 +++++++++++++++++++ .../scenarios/valid-no-errors.oas2.scenario | 2 +- 9 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 test-harness/scenarios/severity/fail-on-warn-no-warn.scenario diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index 094991c17..1d9b35705 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -1,7 +1,7 @@ -import { Dictionary } from '@stoplight/types'; +import { DiagnosticSeverity, Dictionary } from '@stoplight/types'; import { isPlainObject } from '@stoplight/json'; import { getDiagnosticSeverity, IRuleResult } from '@stoplight/spectral-core'; -import { isError, difference, pick } from 'lodash'; +import { difference, isError, pick } from 'lodash'; import type { ReadStream } from 'tty'; import type { CommandModule } from 'yargs'; import * as process from 'process'; @@ -207,7 +207,10 @@ const lintCommand: CommandModule = { if (results.length > 0) { process.exit(severeEnoughToFail(results, failSeverity) ? 1 : 0); } else if (config.quiet !== true) { - process.stdout.write(`No results with a severity of '${failSeverity}' or higher found!`); + const isErrorSeverity = getDiagnosticSeverity(failSeverity) === DiagnosticSeverity.Error; + process.stdout.write( + `No results with a severity of '${failSeverity}' ${isErrorSeverity ? '' : 'or higher '}found!`, + ); } } catch (ex) { fail(isError(ex) ? ex : new Error(String(ex)), config.verbose === true); diff --git a/test-harness/scenarios/custom-ref-resolver.scenario b/test-harness/scenarios/custom-ref-resolver.scenario index aff4af70e..ba0f09e4c 100644 --- a/test-harness/scenarios/custom-ref-resolver.scenario +++ b/test-harness/scenarios/custom-ref-resolver.scenario @@ -31,4 +31,4 @@ module.exports = { ====command==== {bin} lint {document} --ruleset "{asset:ruleset}" --resolver "{asset:resolver.js}" --ignore-unknown-format ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/ignored-unrecognized-format.scenario b/test-harness/scenarios/ignored-unrecognized-format.scenario index 2905764b1..31d4bfbe0 100644 --- a/test-harness/scenarios/ignored-unrecognized-format.scenario +++ b/test-harness/scenarios/ignored-unrecognized-format.scenario @@ -11,4 +11,4 @@ module.exports = { ====command==== {bin} lint {document} --ruleset "{asset:ruleset}" --ignore-unknown-format ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/oas3.1/petstore.scenario b/test-harness/scenarios/oas3.1/petstore.scenario index 07d1b5da7..d5c776053 100644 --- a/test-harness/scenarios/oas3.1/petstore.scenario +++ b/test-harness/scenarios/oas3.1/petstore.scenario @@ -196,4 +196,4 @@ rules: ====command==== {bin} lint {document} --ruleset {asset:ruleset.yaml} ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/operation-security-defined.oas3.scenario b/test-harness/scenarios/operation-security-defined.oas3.scenario index fe982941d..87e0740d0 100644 --- a/test-harness/scenarios/operation-security-defined.oas3.scenario +++ b/test-harness/scenarios/operation-security-defined.oas3.scenario @@ -40,4 +40,4 @@ components: ====command==== {bin} lint {document} --ruleset "{asset:ruleset}" ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/parameter-description-links.oas3.scenario b/test-harness/scenarios/parameter-description-links.oas3.scenario index 757f7094b..7542ff7fb 100644 --- a/test-harness/scenarios/parameter-description-links.oas3.scenario +++ b/test-harness/scenarios/parameter-description-links.oas3.scenario @@ -33,4 +33,4 @@ module.exports = { ====command==== {bin} lint --ruleset "{asset:ruleset}" {document} ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/severity/display-errors.scenario b/test-harness/scenarios/severity/display-errors.scenario index 49fe6528a..b2b353ca0 100644 --- a/test-harness/scenarios/severity/display-errors.scenario +++ b/test-harness/scenarios/severity/display-errors.scenario @@ -43,4 +43,4 @@ servers: ====status==== 0 ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found! diff --git a/test-harness/scenarios/severity/fail-on-warn-no-warn.scenario b/test-harness/scenarios/severity/fail-on-warn-no-warn.scenario new file mode 100644 index 000000000..11a0e1ee5 --- /dev/null +++ b/test-harness/scenarios/severity/fail-on-warn-no-warn.scenario @@ -0,0 +1,28 @@ +====test==== +Will fail and return 1 as exit code because warnings exist +====document==== +- type: string +- type: array +====asset:ruleset.json==== +{ + "rules": { + "valid-type": { + "given": "$..type", + "severity": "hint", + "then": { + "function": "enumeration", + "functionOptions": { + "values": ["array", "string"] + } + } + } + } +} +====command-nix==== +{bin} lint {document} --ruleset "{asset:ruleset.json}" --fail-severity=warn +====command-win==== +{bin} lint {document} --ruleset "{asset:ruleset.json}" --fail-severity warn +====status==== +0 +====stdout==== +No results with a severity of 'warn' or higher found! diff --git a/test-harness/scenarios/valid-no-errors.oas2.scenario b/test-harness/scenarios/valid-no-errors.oas2.scenario index 9b4785d37..b671062f2 100644 --- a/test-harness/scenarios/valid-no-errors.oas2.scenario +++ b/test-harness/scenarios/valid-no-errors.oas2.scenario @@ -20,4 +20,4 @@ module.exports = oas; ====command==== {bin} lint {document} --ruleset "{asset:ruleset}" ====stdout==== -No results with a severity of 'error' or higher found! +No results with a severity of 'error' found!