Skip to content

Commit

Permalink
improvement(hadolint): log one-line warning message when applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Dec 18, 2019
1 parent 600bedf commit 08b8696
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
30 changes: 22 additions & 8 deletions garden-service/src/plugins/hadolint/hadolint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { pathExists, readFile } from "fs-extra"
import { createGardenPlugin } from "../../types/plugin/plugin"
import { providerConfigBaseSchema, ProviderConfig, Provider } from "../../config/provider"
import { joi } from "../../config/common"
import { dedent, splitLines } from "../../util/string"
import { dedent, splitLines, naturalList } from "../../util/string"
import { TestModuleParams } from "../../types/plugin/module/testModule"
import { Module } from "../../types/module"
import { BinaryCmd } from "../../util/ext-tools"
Expand Down Expand Up @@ -183,21 +183,25 @@ export const gardenPlugin = createGardenPlugin({
const errors = parsed.filter((p: any) => p.level === "error")
const warnings = parsed.filter((p: any) => p.level === "warning")
const provider = ctx.provider as HadolintProvider
const threshold = provider.config.testFailureThreshold

if (warnings.length > 0 && threshold === "warning") {
success = false
} else if (errors.length > 0 && threshold !== "none") {
success = false
const resultCategories: string[] = []
let formattedResult = "OK"

if (errors.length > 0) {
resultCategories.push(`${errors.length} error(s)`)
}

let formattedResult = "OK"
if (warnings.length > 0) {
resultCategories.push(`${warnings.length} warning(s)`)
}

let formattedHeader = `hadolint reported ${naturalList(resultCategories)}`

if (parsed.length > 0) {
const dockerfileLines = splitLines(dockerfile)

formattedResult =
`hadolint reported ${errors.length} error(s) and ${warnings.length} warning(s):\n\n` +
`${formattedHeader}:\n\n` +
parsed
.map((msg: any) => {
const color = msg.level === "error" ? chalk.bold.red : chalk.bold.yellow
Expand All @@ -214,6 +218,16 @@ export const gardenPlugin = createGardenPlugin({
.join("\n")
}

const threshold = provider.config.testFailureThreshold

if (warnings.length > 0 && threshold === "warning") {
success = false
} else if (errors.length > 0 && threshold !== "none") {
success = false
} else if (warnings.length > 0) {
log.warn(chalk.yellow(formattedHeader))
}

return {
testName: testConfig.name,
moduleName: module.name,
Expand Down
4 changes: 2 additions & 2 deletions garden-service/test/integ/src/plugins/hadolint/hadolint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe("hadolint provider", () => {
expect(result).to.exist
expect(result!.error).to.exist
expect(stripAnsi(result!.error!.message)).to.equal(dedent`
hadolint reported 1 error(s) and 0 warning(s):
hadolint reported 1 error(s):
DL4000: MAINTAINER is deprecated
2: MAINTAINER foo
Expand Down Expand Up @@ -311,7 +311,7 @@ describe("hadolint provider", () => {
expect(result).to.exist
expect(result!.error).to.exist
expect(stripAnsi(result!.error!.message)).to.equal(dedent`
hadolint reported 1 error(s) and 0 warning(s):
hadolint reported 1 error(s):
DL4000: MAINTAINER is deprecated
2: MAINTAINER foo
Expand Down

0 comments on commit 08b8696

Please sign in to comment.