presubmit: add whitespace test in go (#3629)

Automatically submitted.
This commit is contained in:
Miek Gieben 2020-01-28 11:13:11 +00:00 committed by corbot[bot]
parent 7ebc8ff5fe
commit 04292f1375
8 changed files with 66 additions and 50 deletions

View file

@ -1,19 +0,0 @@
#!/usr/bin/env bash
set -e +o pipefail
# bit too spammy
return
if [ "$TRAVIS_PULL_REQUEST" != "false" ] ; then
echo -e "NOTE: The CPU benchmarks are performed on Travis VMs and vary wildly between runs," > .benchmark.body
echo -e " you can't trust them. The memory benchmarks are OK\n\n" >> .benchmark.body
awk '/^benchmark.*old/ { printf "%s\n%s\n", "```", $0 };
/^$/ { print "```" };
/^Bench/ { print $0 };
END{ print "```" }' .benchmark.log >> .benchmark.body
jq -n --arg body "$(cat .benchmark.body)" '{body: $body}' > .benchmark.json
curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST \
--data-binary "@.benchmark.json" \
"https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments"
fi

View file

@ -1,8 +0,0 @@
#!/usr/bin/env bash
echo "** presubmit/$(basename $0)"
if grep -lr "golang.org/x/net/context" "$@"; then
echo "** presubmit/$(basename $0): please use std lib's 'context' instead"
exit 1
fi

View file

@ -1,7 +0,0 @@
#!/usr/bin/env bash
echo "** presubmit/$(basename $0)"
if grep -r '[[:blank:]]$' "$@"; then
echo "** presubmit/$(basename $0): please remove any trailing white space"
fi

50
test/trailing_test.go Normal file
View file

@ -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()
}