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" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"testing" "testing"
"github.com/rogpeppe/go-internal/testscript" "github.com/rogpeppe/go-internal/testscript"
@ -48,6 +49,26 @@ func TestScripts(t *testing.T) {
} }
return nil return nil
}, },
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"bingrep": bingrep,
},
UpdateScripts: *update, 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 go build main.go
exec ./main exec ./main
cmp stderr main.stderr cmp stderr main.stderr
grep $WORK main
# The default build includes DWARF and the symbol table. # The default build includes DWARF and the symbol table.
exec readelf --section-headers main exec readelf --section-headers main
stdout 'debug_info' stdout 'debug_info'
stdout '\.symtab' stdout '\.symtab'
# The default build includes full non-trimmed paths. # The default build includes full non-trimmed paths, as well as our names.
grep $WORK main bingrep main$exe ${WORK@R} 'globalVar' 'globalFunc'
# Check that we fail if the user forgot -trimpath. # Check that we fail if the user forgot -trimpath.
! exec go build -a -toolexec=garble main.go ! exec go build -a -toolexec=garble main.go
@ -27,15 +26,13 @@ exec readelf --section-headers main
! stdout 'debug_info' ! stdout 'debug_info'
! stdout '\.symtab' ! stdout '\.symtab'
! grep $WORK main ! bingrep main$exe ${WORK@R} 'globalVar' 'globalFunc'
! grep 'globalVar' main
! grep 'globalFunc' main
# Finally, check that the 'garble build' shortcut works. # Finally, check that the 'garble build' shortcut works.
# cp main main_old # cp main main_old
garble build main.go garble build main.go
! grep 'globalVar' main ! bingrep main$exe 'globalVar'
# cmp main main_old # bincmp main main_old
-- main.go -- -- main.go --
package main package main

@ -2,7 +2,7 @@ garble build main.go
exec ./main exec ./main
cmp stdout main.stdout cmp stdout main.stdout
! grep 'unexportedMethod' main ! bingrep main$exe 'unexportedMethod'
-- main.go -- -- main.go --
package main package main

@ -2,10 +2,7 @@ garble build
exec ./main exec ./main
cmp stdout main.stdout cmp stdout main.stdout
! grep 'ImportedVar' main ! bingrep main$exe 'ImportedVar' 'ImportedConst' 'ImportedFunc' 'ImportedType'
! grep 'ImportedConst' main
! grep 'ImportedFunc' main
! grep 'ImportedType' main
-- go.mod -- -- go.mod --
module foo.com/main module foo.com/main

@ -1,11 +1,12 @@
exec go build exec go build
exec ./main exec ./main
cmp stdout main.stdout-orig cmp stdout main.stdout-orig
bingrep main$exe '\(devel\)'
garble build garble build
exec ./main exec ./main
cmp stdout main.stdout cmp stdout main.stdout
! grep '\(devel\)' main ! bingrep main$exe '\(devel\)'
-- go.mod -- -- go.mod --
module foo.com/main module foo.com/main

Loading…
Cancel
Save