diff options
author | 2020-01-28 11:13:11 +0000 | |
---|---|---|
committer | 2020-01-28 11:13:11 +0000 | |
commit | 04292f1375f5d22abdfbe9fec962e3357ea416ac (patch) | |
tree | f70b8c3e8addcbddc8205545b2d6f2ec2d9992d9 /test | |
parent | 7ebc8ff5fe3528fed24171261834e60aed101e12 (diff) | |
download | coredns-04292f1375f5d22abdfbe9fec962e3357ea416ac.tar.gz coredns-04292f1375f5d22abdfbe9fec962e3357ea416ac.tar.zst coredns-04292f1375f5d22abdfbe9fec962e3357ea416ac.zip |
presubmit: add whitespace test in go (#3629)
Automatically submitted.
Diffstat (limited to 'test')
-rw-r--r-- | test/trailing_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/trailing_test.go b/test/trailing_test.go new file mode 100644 index 000000000..e4490df23 --- /dev/null +++ b/test/trailing_test.go @@ -0,0 +1,50 @@ +package test + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + "unicode" +) + +func TestTrailingWhitespace(t *testing.T) { + err := filepath.Walk("..", hasTrailingWhitespace) + if err != nil { + t.Fatal(err) + } +} + +func hasTrailingWhitespace(path string, info os.FileInfo, _ error) error { + // Only handle regular files, skip files that are executable and skip file in the + // root that start with a . + if !info.Mode().IsRegular() { + return nil + } + if info.Mode().Perm()&0111 != 0 { + return nil + } + if strings.HasPrefix(path, "../.") { + return nil + } + + println("looking at", path) + file, err := os.Open(path) + if err != nil { + return nil + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + text := scanner.Text() + trimmed := strings.TrimRightFunc(text, unicode.IsSpace) + if len(text) != len(trimmed) { + return fmt.Errorf("file %q has trailing whitespace, text: %q", path, text) + } + } + + return scanner.Err() +} |