Skip to content

Commit

Permalink
Merge pull request #181 from mbyio/master
Browse files Browse the repository at this point in the history
Ignore comments and empty lines in excludes files
  • Loading branch information
kisielk authored Jul 14, 2020
2 parents d6d3f06 + 6b5513d commit 082960c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ An example of an exclude file is:
io/ioutil.ReadFile
io.Copy(*bytes.Buffer)
io.Copy(os.Stdout)

// Sometimes we don't care if a HTTP request fails.
(*net/http.Client).Do

The exclude list is combined with an internal list for functions in the Go standard library that
Expand All @@ -70,6 +72,7 @@ In this case, add this line to your exclude file:
example.com/yourpkg/vendor/example.net/fmt2.Println
```

Empty lines and lines starting with `//` are ignored.

### The deprecated method

Expand Down
49 changes: 34 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -152,26 +154,17 @@ func parseFlags(checker *errcheck.Checker, args []string) ([]string, int) {
}

if excludeFile != "" {
exclude := make(map[string]bool)
fh, err := os.Open(excludeFile)
excludes, err := readExcludes(excludeFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read exclude file: %s\n", err)
fmt.Fprintf(os.Stderr, "Could not read exclude file: %v\n", err)
return nil, exitFatalError
}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
name := scanner.Text()
exclude[name] = true

if checker.Verbose {
fmt.Printf("Excluding %s\n", name)
if checker.Verbose {
for _, exclude := range excludes {
fmt.Printf("Excluding %v\n", exclude)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Could not read exclude file: %s\n", err)
return nil, exitFatalError
}
checker.SetExclude(exclude)
checker.SetExclude(excludes)
}

checker.Tags = tags
Expand All @@ -189,6 +182,32 @@ func parseFlags(checker *errcheck.Checker, args []string) ([]string, int) {
return paths, exitCodeOk
}

// readExcludes reads an excludes file, a newline delimited file that lists
// patterns for which to allow unchecked errors.
func readExcludes(path string) (map[string]bool, error) {
excludes := map[string]bool{}

buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

scanner := bufio.NewScanner(bytes.NewReader(buf))
for scanner.Scan() {
name := scanner.Text()
// Skip comments and empty lines.
if strings.HasPrefix(name, "//") || name == "" {
continue
}
excludes[name] = true
}
if err := scanner.Err(); err != nil {
return nil, err
}

return excludes, nil
}

func main() {
os.Exit(mainCmd(os.Args))
}
34 changes: 34 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"os"
"reflect"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -222,3 +223,36 @@ func TestParseFlags(t *testing.T) {
}
}
}

func TestReadExcludes(t *testing.T) {
expectedExcludes := map[string]bool{
"hello()": true,
"world()": true,
}
t.Logf("expectedExcludes: %#v", expectedExcludes)
excludes, err := readExcludes("testdata/excludes.txt")
if err != nil {
t.Fatal(err)
}
t.Logf("excludes: %#v", excludes)
if !reflect.DeepEqual(expectedExcludes, excludes) {
t.Fatal("excludes did not match expectedExcludes")
}
}

func TestReadEmptyExcludes(t *testing.T) {
excludes, err := readExcludes("testdata/empty_excludes.txt")
if err != nil {
t.Fatal(err)
}
if len(excludes) != 0 {
t.Fatalf("expected empty excludes, got %#v", excludes)
}
}

func TestReadExcludesMissingFile(t *testing.T) {
_, err := readExcludes("testdata/missing_file")
if err == nil {
t.Fatal("expected non-nil err, got nil")
}
}
Empty file added testdata/empty_excludes.txt
Empty file.
5 changes: 5 additions & 0 deletions testdata/excludes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// comment
hello()

//trickycomment()
world()

0 comments on commit 082960c

Please sign in to comment.