@@ -458,12 +458,86 @@ func TestLinterPathsNotFound(t *testing.T) {
458
458
}
459
459
}
460
460
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
+
461
534
func TestLinterRemoveRuleOnRulesCreatedHook (t * testing.T ) {
462
535
o := & LinterOptions {
463
536
OnRulesCreated : func (rules []Rule ) []Rule {
464
537
for i , r := range rules {
465
538
if r .Name () == "runner-label" {
466
539
rules = append (rules [:i ], rules [i + 1 :]... )
540
+ break
467
541
}
468
542
}
469
543
return rules
@@ -474,6 +548,7 @@ func TestLinterRemoveRuleOnRulesCreatedHook(t *testing.T) {
474
548
if err != nil {
475
549
t .Fatal (err )
476
550
}
551
+ l .defaultConfig = & Config {}
477
552
478
553
f := filepath .Join ("testdata" , "err" , "invalid_runner_labels.yaml" )
479
554
errs , err := l .LintFile (f , nil )
0 commit comments