Skip to content

Commit

Permalink
Make readme_linter have nonzero exit status if any file fails assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda committed May 20, 2022
1 parent 4340ab8 commit be1567c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions tools/readme_linter/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ func (t *T) assertHeadingLevel(expected int, n ast.Node) {

t.printFailedAssertf(n, "expected header level %d, have %d", expected, h.Level)
}

func (t *T) pass() bool {
return t.fails == 0
}
16 changes: 11 additions & 5 deletions tools/readme_linter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ func main() {
flag.Parse()

var err error
pass := true
for _, filename := range flag.Args() {
err = checkFile(filename, guessPluginType(filename), *sourceFlag)
var filePass bool
filePass, err = checkFile(filename, guessPluginType(filename), *sourceFlag)
if err != nil {
panic(err)
}
pass = pass && filePass
}
if !pass {
os.Exit(1)
}
}

Expand Down Expand Up @@ -57,10 +63,10 @@ func init() {
rules[pluginInput] = append(rules[pluginInput], inputRules...)
}

func checkFile(filename string, pluginType plugin, sourceFlag bool) error {
func checkFile(filename string, pluginType plugin, sourceFlag bool) (bool, error) {
md, err := os.ReadFile(filename)
if err != nil {
return err
return false, err
}

// Goldmark returns locations as offsets. We want line
Expand Down Expand Up @@ -107,10 +113,10 @@ func checkFile(filename string, pluginType plugin, sourceFlag bool) error {
for _, rule := range rules {
err = rule(&tester, root)
if err != nil {
return err
return false, err
}
}
tester.printPassFail()

return nil
return tester.pass(), nil
}

0 comments on commit be1567c

Please sign in to comment.