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.
pull/22/head
Daniel Martí 5 years ago
parent c19a41c882
commit e08dd99c1e

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

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save