From 5c1678e7755886bd1f1cc1c5cab4fb921716f761 Mon Sep 17 00:00:00 2001 From: rhysd Date: Wed, 2 Aug 2023 13:42:54 +0900 Subject: [PATCH] move all examples to `example_test.go` --- command_test.go | 30 +++++++++---- error_test.go | 46 -------------------- example_test.go | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ linter_test.go | 33 -------------- 4 files changed, 134 insertions(+), 87 deletions(-) create mode 100644 example_test.go diff --git a/command_test.go b/command_test.go index d91a81838..8f0a7940d 100644 --- a/command_test.go +++ b/command_test.go @@ -2,12 +2,13 @@ package actionlint import ( "bytes" - "fmt" "os" "path/filepath" + "strings" + "testing" ) -func ExampleCommand() { +func TestCommandMain(t *testing.T) { var output bytes.Buffer // Create command instance populating stdin/stdout/stderr @@ -18,13 +19,26 @@ func ExampleCommand() { } // Run the command end-to-end. Note that given args should contain program name - workflow := filepath.Join(".github", "workflows", "release.yaml") - status := cmd.Main([]string{"actionlint", "-shellcheck=", "-pyflakes=", workflow}) + workflow := filepath.Join("testdata", "examples", "main.yaml") + status := cmd.Main([]string{"actionlint", "-shellcheck=", "-pyflakes=", "-ignore", `label .+ is unknown\.`, workflow}) - fmt.Println("Exited with status", status) - // Output: Exited with status 0 + if status != 1 { + t.Fatal("exit status should be 1 but got", status) + } + + out := output.String() + + for _, s := range []string{ + "main.yaml:3:5:", + "unexpected key \"branch\" for \"push\" section", + "^~~~~~~~~~~~~~~", + } { + if !strings.Contains(out, s) { + t.Errorf("output should contain %q: %q", s, out) + } + } - if status != 0 { - panic("actionlint command failed: " + output.String()) + if strings.Contains(out, "[runner-label]") { + t.Errorf("runner-label rule should be ignored by -ignore but it is included in output: %q", out) } } diff --git a/error_test.go b/error_test.go index eca58928a..3e5edbbcd 100644 --- a/error_test.go +++ b/error_test.go @@ -6,7 +6,6 @@ import ( "errors" "io" "math" - "os" "sort" "strconv" "strings" @@ -648,48 +647,3 @@ func TestErrorString(t *testing.T) { t.Fatalf("wanted %q but have %q", want, have) } } - -func ExampleErrorFormatter() { - // Errors returned from Linter methods - errs := []*Error{ - { - Message: "error message 1", - Filepath: "foo.yaml", - Line: 1, - Column: 4, - Kind: "rule1", - }, - { - Message: "error message 2", - Filepath: "foo.yaml", - Line: 3, - Column: 1, - Kind: "rule2", - }, - } - - // Create ErrorFormatter instance with template - f, err := NewErrorFormatter(`{{range $ := .}}{{$.Filepath}}:{{$.Line}}:{{$.Column}}: {{$.Message}}\n{{end}}`) - if err != nil { - // Some error happened while creating the formatter (e.g. syntax error) - panic(err) - } - - src := `on: push - -jobs: - test: - runs-on: ubuntu-latest - steps: - - run: echo -` - - // Prints all errors to stdout following the template - if err := f.PrintErrors(os.Stdout, errs, []byte(src)); err != nil { - panic(err) - } - - // Output: - // foo.yaml:1:4: error message 1 - // foo.yaml:3:1: error message 2 -} diff --git a/example_test.go b/example_test.go new file mode 100644 index 000000000..b1ac80add --- /dev/null +++ b/example_test.go @@ -0,0 +1,112 @@ +package actionlint_test + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + + "github.com/rhysd/actionlint" +) + +func ExampleLinter() { + // Specify linter options + o := &actionlint.LinterOptions{ + IgnorePatterns: []string{`'label ".+" is unknown'`}, + // Other options... + } + + // Create Linter instance which outputs errors to stdout + l, err := actionlint.NewLinter(os.Stdout, o) + if err != nil { + panic(err) + } + + // File to check + f := filepath.Join("testdata", "examples", "main.yaml") + + // First return value is an array of lint errors found in the workflow files. The second return + // value is an error of actionlint itself. This call outputs the lint errors to stdout. Use + // io.Discard to prevent the output. + // + // There are several methods to run linter. + // - LintFile: Check the given single file + // - LintFiles: Check the given multiple files + // - LintDir: Check all workflow files in the given single directory recursively + // - LintRepository: Check all workflow files under .github/workflows in the given repository + // - Lint: Check the given workflow content assuming the given file path + errs, err := l.LintFile(f, nil) + if err != nil { + panic(err) + } + + fmt.Println(len(errs), "lint errors found by actionlint") +} + +func ExampleErrorFormatter() { + // Errors returned from Linter methods + errs := []*actionlint.Error{ + { + Message: "error message 1", + Filepath: "foo.yaml", + Line: 1, + Column: 4, + Kind: "rule1", + }, + { + Message: "error message 2", + Filepath: "foo.yaml", + Line: 3, + Column: 1, + Kind: "rule2", + }, + } + + // Create ErrorFormatter instance with template + f, err := actionlint.NewErrorFormatter(`{{range $ := .}}{{$.Filepath}}:{{$.Line}}:{{$.Column}}: {{$.Message}}\n{{end}}`) + if err != nil { + // Some error happened while creating the formatter (e.g. syntax error) + panic(err) + } + + src := `on: push + +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo +` + + // Prints all errors to stdout following the template + if err := f.PrintErrors(os.Stdout, errs, []byte(src)); err != nil { + panic(err) + } + + // Output: + // foo.yaml:1:4: error message 1 + // foo.yaml:3:1: error message 2 +} + +func ExampleCommand() { + // Write command output to this buffer + var output bytes.Buffer + + // Create command instance populating stdin/stdout/stderr + cmd := actionlint.Command{ + Stdin: os.Stdin, + Stdout: &output, + Stderr: &output, + } + + // Run the command end-to-end. Note that given args should contain program name + workflow := filepath.Join(".github", "workflows", "release.yaml") + status := cmd.Main([]string{"actionlint", "-shellcheck=", "-pyflakes=", workflow}) + + fmt.Println("Exited with status", status) + // Output: Exited with status 0 + + if status != 0 { + panic("actionlint command failed: " + output.String()) + } +} diff --git a/linter_test.go b/linter_test.go index a290da215..a91addcc5 100644 --- a/linter_test.go +++ b/linter_test.go @@ -17,39 +17,6 @@ import ( "golang.org/x/sys/execabs" ) -func ExampleLinter() { - // Specify linter options - o := &LinterOptions{ - IgnorePatterns: []string{`'label ".+" is unknown'`}, - // Other options... - } - - // Create Linter instance which outputs errors to stdout - l, err := NewLinter(os.Stdout, o) - if err != nil { - panic(err) - } - - f := filepath.Join("testdata", "examples", "main.yaml") - - // First return value is an array of lint errors found in the workflow files. The second return - // value is an error of actionlint itself. This call outputs the lint errors to stdout. Use - // io.Discard to prevent the output. - // - // There are several methods to run linter. - // - LintFile: Check the given single file - // - LintFiles: Check the given multiple files - // - LintDir: Check all workflow files in the given single directory recursively - // - LintRepository: Check all workflow files under .github/workflows in the given repository - // - Lint: Check the given workflow content assuming the given file path - errs, err := l.LintFile(f, nil) - if err != nil { - panic(err) - } - - fmt.Println(len(errs), "lint errors found by actionlint") -} - func TestLinterLintOK(t *testing.T) { dir := filepath.Join("testdata", "ok")