From e08dd99c1e6f41def8efb58cd719d83483663a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 9 Dec 2019 12:21:48 +0000 Subject: [PATCH] introduce a binary grep command for the tests The problem with the "grep" built-in command is that it prints the entire data if there is an error. We don't want megabytes of binary output for a test. --- main_test.go | 21 +++++++++++++++++++++ testdata/scripts/basic.txt | 13 +++++-------- testdata/scripts/implement.txt | 2 +- testdata/scripts/imports.txt | 5 +---- testdata/scripts/modinfo.txt | 3 ++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/main_test.go b/main_test.go index dec2bb8..a74456b 100644 --- a/main_test.go +++ b/main_test.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "testing" "github.com/rogpeppe/go-internal/testscript" @@ -48,6 +49,26 @@ func TestScripts(t *testing.T) { } return nil }, + Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ + "bingrep": bingrep, + }, UpdateScripts: *update, }) } + +func bingrep(ts *testscript.TestScript, neg bool, args []string) { + if len(args) < 2 { + ts.Fatalf("usage: bingrep file pattern...") + } + data := ts.ReadFile(args[0]) + for _, pattern := range args[1:] { + rx, err := regexp.Compile(pattern) + ts.Check(err) + match := rx.MatchString(data) + if match && neg { + ts.Fatalf("unexpected match for %q in %s", pattern, args[0]) + } else if !match && !neg { + ts.Fatalf("expected match for %q in %s", pattern, args[0]) + } + } +} diff --git a/testdata/scripts/basic.txt b/testdata/scripts/basic.txt index a5133a8..c527e1c 100644 --- a/testdata/scripts/basic.txt +++ b/testdata/scripts/basic.txt @@ -4,15 +4,14 @@ exec go build main.go exec ./main cmp stderr main.stderr -grep $WORK main # The default build includes DWARF and the symbol table. exec readelf --section-headers main stdout 'debug_info' stdout '\.symtab' -# The default build includes full non-trimmed paths. -grep $WORK main +# The default build includes full non-trimmed paths, as well as our names. +bingrep main$exe ${WORK@R} 'globalVar' 'globalFunc' # Check that we fail if the user forgot -trimpath. ! exec go build -a -toolexec=garble main.go @@ -27,15 +26,13 @@ exec readelf --section-headers main ! stdout 'debug_info' ! stdout '\.symtab' -! grep $WORK main -! grep 'globalVar' main -! grep 'globalFunc' main +! bingrep main$exe ${WORK@R} 'globalVar' 'globalFunc' # Finally, check that the 'garble build' shortcut works. # cp main main_old garble build main.go -! grep 'globalVar' main -# cmp main main_old +! bingrep main$exe 'globalVar' +# bincmp main main_old -- main.go -- package main diff --git a/testdata/scripts/implement.txt b/testdata/scripts/implement.txt index c6c6992..7a605a8 100644 --- a/testdata/scripts/implement.txt +++ b/testdata/scripts/implement.txt @@ -2,7 +2,7 @@ garble build main.go exec ./main cmp stdout main.stdout -! grep 'unexportedMethod' main +! bingrep main$exe 'unexportedMethod' -- main.go -- package main diff --git a/testdata/scripts/imports.txt b/testdata/scripts/imports.txt index 45962c9..aaef4c1 100644 --- a/testdata/scripts/imports.txt +++ b/testdata/scripts/imports.txt @@ -2,10 +2,7 @@ garble build exec ./main cmp stdout main.stdout -! grep 'ImportedVar' main -! grep 'ImportedConst' main -! grep 'ImportedFunc' main -! grep 'ImportedType' main +! bingrep main$exe 'ImportedVar' 'ImportedConst' 'ImportedFunc' 'ImportedType' -- go.mod -- module foo.com/main diff --git a/testdata/scripts/modinfo.txt b/testdata/scripts/modinfo.txt index f7a1abf..eab1305 100644 --- a/testdata/scripts/modinfo.txt +++ b/testdata/scripts/modinfo.txt @@ -1,11 +1,12 @@ exec go build exec ./main cmp stdout main.stdout-orig +bingrep main$exe '\(devel\)' garble build exec ./main cmp stdout main.stdout -! grep '\(devel\)' main +! bingrep main$exe '\(devel\)' -- go.mod -- module foo.com/main