Skip to content

Commit

Permalink
move all examples to example_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 2, 2023
1 parent 0f7cd69 commit 5c1678e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 87 deletions.
30 changes: 22 additions & 8 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
46 changes: 0 additions & 46 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"io"
"math"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}
112 changes: 112 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
33 changes: 0 additions & 33 deletions linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 5c1678e

Please sign in to comment.