all: use quicktest more consistently

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

@ -12,6 +12,7 @@ import (
"strconv"
"testing"
"github.com/go-quicktest/qt"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
@ -28,9 +29,7 @@ func Test_generateTrashBlock(t *testing.T) {
fset := token.NewFileSet()
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)
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.IsNil(err))
return ssaPkg
}

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

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

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

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

Loading…
Cancel
Save