Skip to content

Commit 2a54b2c

Browse files
committed
fix example test fails on Windows
1 parent 655d745 commit 2a54b2c

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

example_your_won_rule_test.go example_your_own_rule_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package actionlint_test
22

33
import (
44
"fmt"
5-
"os"
5+
"io"
66
"path/filepath"
77

88
"github.com/rhysd/actionlint"
@@ -41,7 +41,7 @@ func ExampleLinter_yourOwnRule() {
4141
},
4242
}
4343

44-
l, err := actionlint.NewLinter(os.Stdout, o)
44+
l, err := actionlint.NewLinter(io.Discard, o)
4545
if err != nil {
4646
panic(err)
4747
}
@@ -54,12 +54,12 @@ func ExampleLinter_yourOwnRule() {
5454
panic(err)
5555
}
5656

57+
// `errs` includes errors like below:
58+
//
59+
// testdata/examples/main.yaml:14:9: every step must have its name [step-name]
60+
// |
61+
// 14 | - uses: actions/checkout@v3
62+
// | ^~~~~
5763
fmt.Println(len(errs), "lint errors found by actionlint")
58-
59-
// Output:
60-
// testdata/ok/minimal.yaml:6:9: every step must have its name [step-name]
61-
// |
62-
// 6 | - run: echo
63-
// | ^~~~
64-
// 1 lint errors found by actionlint
64+
// Output: 1 lint errors found by actionlint
6565
}

linter_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,86 @@ func TestLinterPathsNotFound(t *testing.T) {
458458
}
459459
}
460460

461+
type customRuleForTest struct {
462+
RuleBase
463+
count int
464+
}
465+
466+
func (r *customRuleForTest) VisitStep(n *Step) error {
467+
r.count++
468+
if r.count > 1 {
469+
r.Errorf(n.Pos, "only single step is allowed but got %d steps", r.count)
470+
}
471+
return nil
472+
}
473+
474+
func TestLinterAddCustomRuleOnRulesCreatedHook(t *testing.T) {
475+
o := &LinterOptions{
476+
OnRulesCreated: func(rules []Rule) []Rule {
477+
r := &customRuleForTest{
478+
RuleBase: NewRuleBase("this-is-test", ""),
479+
}
480+
return append(rules, r)
481+
},
482+
}
483+
484+
l, err := NewLinter(io.Discard, o)
485+
if err != nil {
486+
t.Fatal(err)
487+
}
488+
l.defaultConfig = &Config{}
489+
490+
{
491+
w := `on: push
492+
jobs:
493+
test:
494+
runs-on: ubuntu-latest
495+
steps:
496+
- run: echo
497+
`
498+
errs, err := l.Lint("test.yaml", []byte(w), nil)
499+
if err != nil {
500+
t.Fatal(err)
501+
}
502+
if len(errs) != 0 {
503+
t.Fatal("wanted no error but have", errs)
504+
}
505+
}
506+
507+
{
508+
w := `on: push
509+
jobs:
510+
test:
511+
runs-on: ubuntu-latest
512+
steps:
513+
- run: echo
514+
- run: echo
515+
`
516+
errs, err := l.Lint("test.yaml", []byte(w), nil)
517+
if err != nil {
518+
t.Fatal(err)
519+
}
520+
if len(errs) != 1 {
521+
t.Fatal("wanted 1 error but have", errs)
522+
}
523+
524+
var b strings.Builder
525+
errs[0].PrettyPrint(&b, nil)
526+
have := b.String()
527+
want := "test.yaml:7:9: only single step is allowed but got 2 steps [this-is-test]\n"
528+
if have != want {
529+
t.Fatalf("wanted error message %q but have %q", want, have)
530+
}
531+
}
532+
}
533+
461534
func TestLinterRemoveRuleOnRulesCreatedHook(t *testing.T) {
462535
o := &LinterOptions{
463536
OnRulesCreated: func(rules []Rule) []Rule {
464537
for i, r := range rules {
465538
if r.Name() == "runner-label" {
466539
rules = append(rules[:i], rules[i+1:]...)
540+
break
467541
}
468542
}
469543
return rules
@@ -474,6 +548,7 @@ func TestLinterRemoveRuleOnRulesCreatedHook(t *testing.T) {
474548
if err != nil {
475549
t.Fatal(err)
476550
}
551+
l.defaultConfig = &Config{}
477552

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

0 commit comments

Comments
 (0)