Skip to content

Commit

Permalink
Ensure lint warnings are printed (getporter#2534)
Browse files Browse the repository at this point in the history
* Refactor PrintLintResults() to ensure lint warnings are printed.

Signed-off-by: James Blair <mail@jamesblair.net>

* Bump github.com/moby/buildkit from 0.11.0 to 0.11.1 (getporter#2528)

Bumps [github.com/moby/buildkit](https://github.com/moby/buildkit) from 0.11.0 to 0.11.1.

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* check for plugins file schema version and type (getporter#2532)

* check for plugins file schema version and type

Signed-off-by: Yingrong Zhao <yingrong.zhao@gmail.com>

* Update contributors list.

Signed-off-by: James Blair <mail@jamesblair.net>

* Add unit tests for ensuring lint warnings are printed.

Signed-off-by: James Blair <mail@jamesblair.net>

---------

Signed-off-by: James Blair <mail@jamesblair.net>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Yingrong Zhao <yingrong.zhao@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yingrong Zhao <yingrong.zhao@gmail.com>
  • Loading branch information
3 people authored and bdegeeter committed May 11, 2023
1 parent f3873b2 commit c8ef12d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ and we will add you. **All** contributors belong here. 💯
* [Madhu M Pandurangi](https://github.com/MadhuMPandurangi)
* [Karanjot Singh](https://github.com/0xquark)
* [Omar Kohl](https://github.com/omarkohl)
* [James Blair](https://github.com/jmhbnz)
24 changes: 13 additions & 11 deletions pkg/porter/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@ func (p *Porter) PrintLintResults(ctx context.Context, opts LintOptions) error {
return err
}

if !results.HasError() {
fmt.Fprintln(p.Out, "✨ Bundle validation was successful!")
return nil
if results.String() != "" {
switch opts.Format {
case printer.FormatPlaintext:
fmt.Fprintln(p.Out, results.String())
case printer.FormatJson:
printer.PrintJson(p.Out, results)
default:
return fmt.Errorf("invalid format: %s", opts.Format)
}
}

switch opts.Format {
case printer.FormatPlaintext:
fmt.Fprintln(p.Out, results.String())
return nil
case printer.FormatJson:
return printer.PrintJson(p.Out, results)
default:
return fmt.Errorf("invalid format: %s", opts.Format)
if !results.HasError() && opts.Format == printer.FormatPlaintext {
fmt.Fprintln(p.Out, "✨ Bundle validation was successful!")
}

return nil
}
58 changes: 58 additions & 0 deletions pkg/porter/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,61 @@ exec:
})
}
}

func TestPorter_PrintLintResults_Warning(t *testing.T) {
lintResults := linter.Results{
{
Level: linter.LevelWarning,
Location: linter.Location{
Action: "install",
Mixin: "exec",
StepNumber: 2,
StepDescription: "Install Hello World",
},
Code: "exec-100",
Title: "bash -c argument missing wrapping quotes",
Message: `The bash -c flag argument must be wrapped in quotes, for example
exec:
description: Say Hello
command: bash
flags:
c: '"echo Hello World"'
`,
URL: "https://getporter.org/best-practices/exec-mixin/#quoting-escaping-bash-and-yaml",
},
}

testcases := []struct {
format string
wantOutputFile string
linterResults linter.Results
}{
{"plaintext", "testdata/lint/results_warning.txt", lintResults},
{"json", "testdata/lint/results_warning.json", lintResults},
{"plaintext", "testdata/lint/success.txt", linter.Results{}},
}
for _, tc := range testcases {
t.Run(tc.format, func(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

p.TestConfig.TestContext.AddTestFile("testdata/porter.yaml", "porter.yaml")

mixins := p.Mixins.(*mixin.TestMixinProvider)
mixins.LintResults = tc.linterResults

var opts LintOptions
opts.RawFormat = tc.format
err := opts.Validate(p.Context)
require.NoError(t, err, "Validate failed")

err = p.PrintLintResults(context.Background(), opts)
require.NoError(t, err, "PrintLintResults failed")

wantOutputB, err := os.ReadFile(tc.wantOutputFile)
require.NoError(t, err, "Reading output file failed")
gotOutput := p.TestConfig.TestContext.GetOutput()
assert.Equal(t, string(wantOutputB), gotOutput, "unexpected output printed")
})
}
}
15 changes: 15 additions & 0 deletions pkg/porter/testdata/lint/results_warning.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"Level": 2,
"Location": {
"Action": "install",
"Mixin": "exec",
"StepNumber": 2,
"StepDescription": "Install Hello World"
},
"Code": "exec-100",
"Title": "bash -c argument missing wrapping quotes",
"Message": "The bash -c flag argument must be wrapped in quotes, for example\nexec:\n description: Say Hello\n command: bash\n flags:\n c: '\"echo Hello World\"'\n",
"URL": "https://getporter.org/best-practices/exec-mixin/#quoting-escaping-bash-and-yaml"
}
]
13 changes: 13 additions & 0 deletions pkg/porter/testdata/lint/results_warning.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
warning(exec-100) - bash -c argument missing wrapping quotes
install: 2nd step in the exec mixin (Install Hello World)
The bash -c flag argument must be wrapped in quotes, for example
exec:
description: Say Hello
command: bash
flags:
c: '"echo Hello World"'

See https://getporter.org/best-practices/exec-mixin/#quoting-escaping-bash-and-yaml for more information
---

✨ Bundle validation was successful!

0 comments on commit c8ef12d

Please sign in to comment.