Skip to content

Commit

Permalink
fix example test fails on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 1, 2023
1 parent 655d745 commit 2a54b2c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
18 changes: 9 additions & 9 deletions example_your_won_rule_test.go → example_your_own_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package actionlint_test

import (
"fmt"
"os"
"io"
"path/filepath"

"github.com/rhysd/actionlint"
Expand Down Expand Up @@ -41,7 +41,7 @@ func ExampleLinter_yourOwnRule() {
},
}

l, err := actionlint.NewLinter(os.Stdout, o)
l, err := actionlint.NewLinter(io.Discard, o)
if err != nil {
panic(err)
}
Expand All @@ -54,12 +54,12 @@ func ExampleLinter_yourOwnRule() {
panic(err)
}

// `errs` includes errors like below:
//
// testdata/examples/main.yaml:14:9: every step must have its name [step-name]
// |
// 14 | - uses: actions/checkout@v3
// | ^~~~~
fmt.Println(len(errs), "lint errors found by actionlint")

// Output:
// testdata/ok/minimal.yaml:6:9: every step must have its name [step-name]
// |
// 6 | - run: echo
// | ^~~~
// 1 lint errors found by actionlint
// Output: 1 lint errors found by actionlint
}
75 changes: 75 additions & 0 deletions linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,86 @@ func TestLinterPathsNotFound(t *testing.T) {
}
}

type customRuleForTest struct {
RuleBase
count int
}

func (r *customRuleForTest) VisitStep(n *Step) error {
r.count++
if r.count > 1 {
r.Errorf(n.Pos, "only single step is allowed but got %d steps", r.count)
}
return nil
}

func TestLinterAddCustomRuleOnRulesCreatedHook(t *testing.T) {
o := &LinterOptions{
OnRulesCreated: func(rules []Rule) []Rule {
r := &customRuleForTest{
RuleBase: NewRuleBase("this-is-test", ""),
}
return append(rules, r)
},
}

l, err := NewLinter(io.Discard, o)
if err != nil {
t.Fatal(err)
}
l.defaultConfig = &Config{}

{
w := `on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo
`
errs, err := l.Lint("test.yaml", []byte(w), nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatal("wanted no error but have", errs)
}
}

{
w := `on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo
- run: echo
`
errs, err := l.Lint("test.yaml", []byte(w), nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatal("wanted 1 error but have", errs)
}

var b strings.Builder
errs[0].PrettyPrint(&b, nil)
have := b.String()
want := "test.yaml:7:9: only single step is allowed but got 2 steps [this-is-test]\n"
if have != want {
t.Fatalf("wanted error message %q but have %q", want, have)
}
}
}

func TestLinterRemoveRuleOnRulesCreatedHook(t *testing.T) {
o := &LinterOptions{
OnRulesCreated: func(rules []Rule) []Rule {
for i, r := range rules {
if r.Name() == "runner-label" {
rules = append(rules[:i], rules[i+1:]...)
break
}
}
return rules
Expand All @@ -474,6 +548,7 @@ func TestLinterRemoveRuleOnRulesCreatedHook(t *testing.T) {
if err != nil {
t.Fatal(err)
}
l.defaultConfig = &Config{}

f := filepath.Join("testdata", "err", "invalid_runner_labels.yaml")
errs, err := l.LintFile(f, nil)
Expand Down

0 comments on commit 2a54b2c

Please sign in to comment.