all: use quicktest more consistently

pull/890/head
Daniel Martí 1 year ago committed by Paul Scheduikat
parent 21c70f2502
commit b0d3563fef

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"github.com/go-quicktest/qt"
"golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil" "golang.org/x/tools/go/ssa/ssautil"
@ -28,9 +29,7 @@ func Test_generateTrashBlock(t *testing.T) {
fset := token.NewFileSet() fset := token.NewFileSet()
buildPkg := func(f *ast.File) *ssa.Package { buildPkg := func(f *ast.File) *ssa.Package {
ssaPkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, types.NewPackage("test/main", ""), []*ast.File{f}, 0) ssaPkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, types.NewPackage("test/main", ""), []*ast.File{f}, 0)
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
return ssaPkg return ssaPkg
} }

@ -16,6 +16,7 @@ import (
"sync/atomic" "sync/atomic"
"testing" "testing"
"github.com/go-quicktest/qt"
"mvdan.cc/garble/internal/literals" "mvdan.cc/garble/internal/literals"
) )
@ -59,35 +60,29 @@ func FuzzObfuscate(f *testing.F) {
t.Log(srcText) // shown on failures t.Log(srcText) // shown on failures
fset := token.NewFileSet() fset := token.NewFileSet()
srcSyntax, err := parser.ParseFile(fset, "", srcText, parser.SkipObjectResolution) srcSyntax, err := parser.ParseFile(fset, "", srcText, parser.SkipObjectResolution)
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
info := types.Info{ info := types.Info{
Types: make(map[ast.Expr]types.TypeAndValue), Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object), Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object), Uses: make(map[*ast.Ident]types.Object),
} }
var conf types.Config var conf types.Config
if _, err := conf.Check("p", fset, []*ast.File{srcSyntax}, &info); err != nil { _, err = conf.Check("p", fset, []*ast.File{srcSyntax}, &info)
t.Fatal(err) qt.Assert(t, qt.IsNil(err))
}
// Obfuscate the literals and print the source back. // Obfuscate the literals and print the source back.
rand := mathrand.New(mathrand.NewSource(randSeed)) rand := mathrand.New(mathrand.NewSource(randSeed))
srcSyntax = literals.Obfuscate(rand, srcSyntax, &info, nil) srcSyntax = literals.Obfuscate(rand, srcSyntax, &info, nil)
count := tdirCounter.Add(1) count := tdirCounter.Add(1)
f, err := os.Create(filepath.Join(tdir, fmt.Sprintf("src_%d.go", count))) f, err := os.Create(filepath.Join(tdir, fmt.Sprintf("src_%d.go", count)))
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
srcPath := f.Name() srcPath := f.Name()
t.Cleanup(func() { t.Cleanup(func() {
f.Close() f.Close()
os.Remove(srcPath) os.Remove(srcPath)
}) })
if err := printer.Fprint(f, fset, srcSyntax); err != nil { err = printer.Fprint(f, fset, srcSyntax)
t.Fatal(err) qt.Assert(t, qt.IsNil(err))
}
// Build the main package. Use some flags to avoid work. // Build the main package. Use some flags to avoid work.
binPath := strings.TrimSuffix(srcPath, ".go") binPath := strings.TrimSuffix(srcPath, ".go")
@ -104,12 +99,8 @@ func FuzzObfuscate(f *testing.F) {
// Run the binary, expecting the output to match. // Run the binary, expecting the output to match.
out, err := exec.Command(binPath).CombinedOutput() out, err := exec.Command(binPath).CombinedOutput()
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatalf("%v: %s", err, out)
}
want := fmt.Sprintf("%[1]s\nx%[1]sy\n--\n%[1]s\n%[1]s\n", in) want := fmt.Sprintf("%[1]s\nx%[1]sy\n--\n%[1]s\n%[1]s\n", in)
if got := string(out); got != want { qt.Assert(t, qt.Equals(string(out), want))
t.Fatalf("got: %q\nwant: %q", got, want)
}
}) })
} }

@ -10,10 +10,10 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/go-quicktest/qt"
"golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/go/ssa/ssautil" "golang.org/x/tools/go/ssa/ssautil"
) )
@ -60,12 +60,8 @@ func TestConvertSignature(t *testing.T) {
funcObj := info.Defs[funcDecl.Name].(*types.Func) funcObj := info.Defs[funcDecl.Name].(*types.Func)
funcDeclConverted, err := conv.convertSignatureToFuncDecl(funcObj.Name(), funcObj.Type().(*types.Signature)) funcDeclConverted, err := conv.convertSignatureToFuncDecl(funcObj.Name(), funcObj.Type().(*types.Signature))
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err) qt.Assert(t, qt.CmpEquals(funcDeclConverted, funcDecl, astCmpOpt))
}
if structDiff := cmp.Diff(funcDecl, funcDeclConverted, astCmpOpt); structDiff != "" {
t.Fatalf("method decl not equals: %s", structDiff)
}
} }
} }
@ -372,23 +368,18 @@ func TestConvert(t *testing.T) {
runGoFile := func(f string) string { runGoFile := func(f string) string {
cmd := exec.Command("go", "run", f) cmd := exec.Command("go", "run", f)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatalf("compile failed: %v\n%s", err, string(out))
}
return string(out) return string(out)
} }
testFile := filepath.Join(t.TempDir(), "convert.go") testFile := filepath.Join(t.TempDir(), "convert.go")
if err := os.WriteFile(testFile, []byte(mainSrc), 0o777); err != nil { err := os.WriteFile(testFile, []byte(mainSrc), 0o777)
t.Fatal(err) qt.Assert(t, qt.IsNil(err))
}
originalOut := runGoFile(testFile) originalOut := runGoFile(testFile)
file, fset, _, _ := mustParseAndTypeCheckFile(mainSrc) file, fset, _, _ := mustParseAndTypeCheckFile(mainSrc)
ssaPkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, types.NewPackage("test/main", ""), []*ast.File{file}, 0) ssaPkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, types.NewPackage("test/main", ""), []*ast.File{file}, 0)
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
for fIdx, decl := range file.Decls { for fIdx, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl) funcDecl, ok := decl.(*ast.FuncDecl)
@ -400,25 +391,18 @@ func TestConvert(t *testing.T) {
ssaFunc := ssa.EnclosingFunction(ssaPkg, path) ssaFunc := ssa.EnclosingFunction(ssaPkg, path)
astFunc, err := Convert(ssaFunc, DefaultConfig()) astFunc, err := Convert(ssaFunc, DefaultConfig())
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
file.Decls[fIdx] = astFunc file.Decls[fIdx] = astFunc
} }
convertedFile := filepath.Join(t.TempDir(), "main.go") convertedFile := filepath.Join(t.TempDir(), "main.go")
f, err := os.Create(convertedFile) f, err := os.Create(convertedFile)
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err) err = printer.Fprint(f, fset, file)
} qt.Assert(t, qt.IsNil(err))
if err := printer.Fprint(f, fset, file); err != nil {
t.Fatal(err)
}
_ = f.Close() _ = f.Close()
convertedOut := runGoFile(convertedFile) convertedOut := runGoFile(convertedFile)
if convertedOut != originalOut { qt.Assert(t, qt.Equals(convertedOut, originalOut))
t.Fatalf("Output not equals:\n\n%s\n\n%s", originalOut, convertedOut)
}
} }

@ -4,7 +4,7 @@ import (
"go/ast" "go/ast"
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/go-quicktest/qt"
) )
const typesSrc = `package main const typesSrc = `package main
@ -97,12 +97,8 @@ func TestTypeToExpr(t *testing.T) {
obj := info.Defs[name] obj := info.Defs[name]
fc := &TypeConverter{resolver: defaultImportNameResolver} fc := &TypeConverter{resolver: defaultImportNameResolver}
convAst, err := fc.Convert(obj.Type().Underlying()) convAst, err := fc.Convert(obj.Type().Underlying())
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
structConvAst := convAst.(*ast.StructType) structConvAst := convAst.(*ast.StructType)
if structDiff := cmp.Diff(structAst, structConvAst, astCmpOpt); structDiff != "" { qt.Assert(t, qt.CmpEquals(structConvAst, structAst, astCmpOpt))
t.Fatalf("struct not equals: %s", structDiff)
}
} }

@ -20,7 +20,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/go-quicktest/qt"
"github.com/rogpeppe/go-internal/goproxytest" "github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/gotooltest" "github.com/rogpeppe/go-internal/gotooltest"
"github.com/rogpeppe/go-internal/testscript" "github.com/rogpeppe/go-internal/testscript"
@ -69,16 +69,12 @@ func TestScript(t *testing.T) {
t.Parallel() t.Parallel()
execPath, err := os.Executable() execPath, err := os.Executable()
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
tempCacheDir := t.TempDir() tempCacheDir := t.TempDir()
hostCacheDir, err := os.UserCacheDir() hostCacheDir, err := os.UserCacheDir()
if err != nil { qt.Assert(t, qt.IsNil(err))
t.Fatal(err)
}
p := testscript.Params{ p := testscript.Params{
Dir: filepath.Join("testdata", "script"), Dir: filepath.Join("testdata", "script"),
@ -426,9 +422,7 @@ func TestSplitFlagsFromArgs(t *testing.T) {
flags, args := splitFlagsFromArgs(test.args) flags, args := splitFlagsFromArgs(test.args)
got := [2][]string{flags, args} got := [2][]string{flags, args}
if diff := cmp.Diff(test.want, got); diff != "" { qt.Assert(t, qt.DeepEquals(got, test.want))
t.Fatalf("splitFlagsFromArgs(%q) mismatch (-want +got):\n%s", test.args, diff)
}
}) })
} }
} }
@ -461,10 +455,7 @@ func TestFilterForwardBuildFlags(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
t.Parallel() t.Parallel()
got, _ := filterForwardBuildFlags(test.flags) got, _ := filterForwardBuildFlags(test.flags)
qt.Assert(t, qt.DeepEquals(got, test.want))
if diff := cmp.Diff(test.want, got); diff != "" {
t.Fatalf("filterForwardBuildFlags(%q) mismatch (-want +got):\n%s", test.flags, diff)
}
}) })
} }
} }
@ -489,10 +480,7 @@ func TestFlagValue(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
t.Parallel() t.Parallel()
got := flagValue(test.flags, test.flagName) got := flagValue(test.flags, test.flagName)
if got != test.want { qt.Assert(t, qt.DeepEquals(got, test.want))
t.Fatalf("flagValue(%q, %q) got %q, want %q",
test.flags, test.flagName, got, test.want)
}
}) })
} }
} }

Loading…
Cancel
Save