diff --git a/gnovm/cmd/gno/main_test.go b/gnovm/cmd/gno/main_test.go index bc4fd8eb4b8..8d19a50e814 100644 --- a/gnovm/cmd/gno/main_test.go +++ b/gnovm/cmd/gno/main_test.go @@ -5,12 +5,15 @@ import ( "context" "fmt" "os" + "os/exec" "path/filepath" "strings" "testing" - "github.com/gnolang/gno/tm2/pkg/commands" + "github.com/rogpeppe/go-internal/testscript" "github.com/stretchr/testify/require" + + "github.com/gnolang/gno/tm2/pkg/commands" ) func TestMain_Gnodev(t *testing.T) { @@ -141,3 +144,45 @@ func testMainCaseRun(t *testing.T, tc []testMainCase) { }) } } + +func setupTestScript(t *testing.T, txtarDir string) testscript.Params { + t.Helper() + // Get root location of github.com/gnolang/gno + goModPath, err := exec.Command("go", "env", "GOMOD").CombinedOutput() + require.NoError(t, err) + rootDir := filepath.Dir(string(goModPath)) + // Build a fresh gno binary in a temp directory + gnoBin := filepath.Join(t.TempDir(), "gno") + err = exec.Command("go", "build", "-o", gnoBin, filepath.Join(rootDir, "gnovm", "cmd", "gno")).Run() + require.NoError(t, err) + // Define script params + return testscript.Params{ + Setup: func(env *testscript.Env) error { + env.Vars = append(env.Vars, + "GNOROOT="+rootDir, // thx PR 1014 :) + // by default, $HOME=/no-home, but we need an existing $HOME directory + // because some commands needs to access $HOME/.cache/go-build + "HOME="+t.TempDir(), + ) + return nil + }, + Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ + // add a custom "gno" command so txtar files can easily execute "gno" + // without knowing where is the binary or how it is executed. + "gno": func(ts *testscript.TestScript, neg bool, args []string) { + err := ts.Exec(gnoBin, args...) + if err != nil { + ts.Logf("[%v]\n", err) + if !neg { + ts.Fatalf("unexpected gno command failure") + } + } else { + if neg { + ts.Fatalf("unexpected gno command success") + } + } + }, + }, + Dir: txtarDir, + } +} diff --git a/gnovm/cmd/gno/test_test.go b/gnovm/cmd/gno/test_test.go index 856f63a9053..f5b069a5f03 100644 --- a/gnovm/cmd/gno/test_test.go +++ b/gnovm/cmd/gno/test_test.go @@ -1,196 +1,11 @@ package main -import "testing" +import ( + "testing" -func TestTest(t *testing.T) { - tc := []testMainCase{ - { - args: []string{"test"}, - errShouldBe: "flag: help requested", - }, - { - args: []string{"test", "../../../examples/gno.land/p/demo/rand"}, - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/rand \t", - }, - { - args: []string{"test", "../../tests/integ/no-such-dir"}, - errShouldContain: "no such file or directory", - }, - { - args: []string{"test", "../../tests/integ/empty-dir"}, - stderrShouldContain: "no packages to test", - }, - { - // FIXME: should have an output - args: []string{"test", "../../tests/integ/minimalist-gno1"}, - stderrShouldBe: "? ./../../tests/integ/minimalist-gno1 \t[no test files]\n", - }, - { - args: []string{"test", "../../tests/integ/minimalist-gno2"}, - stderrShouldContain: "ok ", - }, - { - args: []string{"test", "../../tests/integ/minimalist-gno3"}, - stderrShouldContain: "ok ", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/valid1"}, - stderrShouldContain: "ok ", - }, - { - args: []string{"test", "../../tests/integ/valid2"}, - stderrShouldContain: "ok ", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/valid2"}, - stderrShouldContain: "ok ", - }, - { - args: []string{"test", "../../tests/integ/empty-gno1"}, - stderrShouldBe: "? ./../../tests/integ/empty-gno1 \t[no test files]\n", - }, - { - args: []string{"test", "--precompile", "../../tests/integ/empty-gno1"}, - errShouldBe: "FAIL: 1 build errors, 0 test errors", - stderrShouldContain: "../../tests/integ/empty-gno1/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'", - }, - { - args: []string{"test", "../../tests/integ/empty-gno2"}, - recoverShouldBe: "empty.gno:1:1: expected 'package', found 'EOF'", - }, - { - // FIXME: better error handling + rename dontcare.gno with actual test file - args: []string{"test", "--precompile", "../../tests/integ/empty-gno2"}, - errShouldContain: "FAIL: 1 build errors, 0 test errors", - stderrShouldContain: "../../tests/integ/empty-gno2/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'", - }, - { - args: []string{"test", "../../tests/integ/empty-gno3"}, - recoverShouldBe: "../../tests/integ/empty-gno3/empty_filetest.gno:1:1: expected 'package', found 'EOF'", - }, - { - // FIXME: better error handling - args: []string{"test", "--precompile", "../../tests/integ/empty-gno3"}, - errShouldContain: "FAIL: 1 build errors, 0 test errors", - stderrShouldContain: "../../tests/integ/empty-gno3/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/failing1"}, - errShouldBe: "FAIL: 0 build errors, 1 test errors", - stderrShouldContain: "FAIL: TestAlwaysFailing", - }, - { - args: []string{"test", "--verbose", "--precompile", "../../tests/integ/failing1"}, - errShouldBe: "FAIL: 0 build errors, 1 test errors", - stderrShouldContain: "FAIL: TestAlwaysFailing", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/failing2"}, - recoverShouldBe: "fail on ../../tests/integ/failing2/failing_filetest.gno: got unexpected error: beep boop", - stderrShouldContain: "== RUN file/failing_filetest.gno", - }, - { - args: []string{"test", "--verbose", "--precompile", "../../tests/integ/failing2"}, - stderrShouldBe: "=== PREC ./../../tests/integ/failing2\n=== BUILD ./../../tests/integ/failing2\n=== RUN file/failing_filetest.gno\n", - recoverShouldBe: "fail on ../../tests/integ/failing2/failing_filetest.gno: got unexpected error: beep boop", - }, - { - args: []string{"test", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "../../../examples/gno.land/p/demo/ufmt/ufmt_test.gno"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", ".*", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", "NoExists", "../../../examples/gno.land/p/demo/ufmt"}, - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", ".*/hello", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", ".*/hi", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", ".*/NoExists", "../../../examples/gno.land/p/demo/ufmt"}, - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", ".*/hello/NoExists", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", "Sprintf/", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", "Sprintf/.*", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--run", "Sprintf/hello", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "--timeout", "1000000s", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "ok ./../../../examples/gno.land/p/demo/ufmt", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/native-lib"}, - recoverShouldContain: "gno.land/r/\\w{8}/contract.gno:1: unknown import path net", - }, - { - args: []string{"test", "--verbose", "--with-native-fallback", "../../tests/integ/native-lib"}, - stderrShouldContain: "ok ./../../tests/integ/native-lib", - }, - { - args: []string{"test", "--verbose", "../../tests/integ/unknown-lib"}, - recoverShouldContain: "gno.land/r/\\w{8}/contract.gno:1: unknown import path foobarbaz", - }, - { - args: []string{"test", "--verbose", "--with-native-fallback", "../../tests/integ/unknown-lib"}, - recoverShouldContain: "gno.land/r/\\w{8}/contract.gno:1: unknown import path foobarbaz", - }, - { - args: []string{"test", "--verbose", "--print-runtime-metrics", "../../../examples/gno.land/p/demo/ufmt"}, - stdoutShouldContain: "RUN TestSprintf", - stderrShouldContain: "cycle=", - }, + "github.com/rogpeppe/go-internal/testscript" +) - // TODO: when 'gnodev test' will by default imply running precompile, we should use the following tests. - // {args: []string{"test", "../../tests/integ/empty-gno1", "--no-precompile"}, stderrShouldBe: "? ./../../tests/integ/empty-gno1 \t[no test files]\n"}, - // {args: []string{"test", "../../tests/integ/empty-gno1"}, errShouldBe: "FAIL: 1 build errors, 0 test errors", stderrShouldContain: "../../tests/integ/empty-gno1/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'"}, - // {args: []string{"test", "../../tests/integ/empty-gno2", "--no-precompile"}, recoverShouldBe: "empty.gno:1:1: expected 'package', found 'EOF'"}, // FIXME: better error handling + rename dontcare.gno with actual test file - // {args: []string{"test", "../../tests/integ/empty-gno2"}, errShouldContain: "FAIL: 1 build errors, 0 test errors", stderrShouldContain: "../../tests/integ/empty-gno2/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'"}, - // {args: []string{"test", "../../tests/integ/empty-gno3", "--no-precompile"}, recoverShouldBe: "../../tests/integ/empty-gno3/empty_filetest.gno:1:1: expected 'package', found 'EOF'"}, // FIXME: better error handling - // {args: []string{"test", "../../tests/integ/empty-gno3"}, errShouldContain: "FAIL: 1 build errors, 0 test errors", stderrShouldContain: "../../tests/integ/empty-gno3/empty.gno: parse: tmp.gno:1:1: expected 'package', found 'EOF'"}, - // {args: []string{"test", "../../tests/integ/failing1", "--verbose", "--no-precompile"}, errShouldBe: "FAIL: 0 build errors, 1 test errors", stderrShouldContain: "FAIL: TestAlwaysFailing"}, - // {args: []string{"test", "../../tests/integ/failing1", "--verbose"}, errShouldBe: "FAIL: 0 build errors, 1 test errors", stderrShouldContain: "FAIL: TestAlwaysFailing"}, - // {args: []string{"test", "../../tests/integ/failing2", "--verbose", "--no-precompile"}, recoverShouldBe: "fail on ../../tests/integ/failing2/failing_filetest.gno: got unexpected error: beep boop", stderrShouldContain: "== RUN file/failing_filetest.gno"}, - // {args: []string{"test", "../../tests/integ/failing2", "--verbose"}, stderrShouldBe: "=== PREC ./../../tests/integ/failing2\n=== BUILD ./../../tests/integ/failing2\n=== RUN file/failing_filetest.gno\n", recoverShouldBe: "fail on ../../tests/integ/failing2/failing_filetest.gno: got unexpected error: beep boop"}, - // {args: []string{"test", "../../../examples/gno.land/p/demo/ufmt", "--verbose", "--timeout", "10000" /* 10µs */}, recoverShouldContain: "test timed out after"}, // FIXME: should be testable - } - testMainCaseRun(t, tc) +func TestTest(t *testing.T) { + testscript.Run(t, setupTestScript(t, "testdata/gno_test")) } diff --git a/gnovm/cmd/gno/testdata/gno_test/dir_not_exist.txtar b/gnovm/cmd/gno/testdata/gno_test/dir_not_exist.txtar new file mode 100644 index 00000000000..8cd0e06eb8c --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/dir_not_exist.txtar @@ -0,0 +1,6 @@ +# Run gno test with a dir that doesn't exists + +! gno test no-such-dir + +! stdout .+ +stderr 'no such file or directory' diff --git a/gnovm/cmd/gno/testdata/gno_test/empty_dir.txtar b/gnovm/cmd/gno/testdata/gno_test/empty_dir.txtar new file mode 100644 index 00000000000..73f0da72dfe --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/empty_dir.txtar @@ -0,0 +1,6 @@ +# Run gno test on an empty dir + +gno test . + +! stdout .+ +stderr 'no packages to test' diff --git a/gnovm/cmd/gno/testdata/gno_test/empty_gno1.txtar b/gnovm/cmd/gno/testdata/gno_test/empty_gno1.txtar new file mode 100644 index 00000000000..ae73b3ab275 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/empty_gno1.txtar @@ -0,0 +1,13 @@ +# Test empty gno1 + +gno test . + +! stdout .+ +stderr '\? \./\. \[no test files\]' + +! gno test --precompile . + +! stdout .+ +stderr 'expected ''package'', found ''EOF''' + +-- empty.gno -- diff --git a/gnovm/cmd/gno/testdata/gno_test/empty_gno2.txtar b/gnovm/cmd/gno/testdata/gno_test/empty_gno2.txtar new file mode 100644 index 00000000000..0ea13294a1b --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/empty_gno2.txtar @@ -0,0 +1,14 @@ +# Test empty gno2 + +! gno test . + +! stdout .+ +stderr 'expected ''package'', found ''EOF''' + +! gno test --precompile . + +! stdout .+ +stderr 'expected ''package'', found ''EOF''' + +-- empty.gno -- +-- empty_test.gno -- diff --git a/gnovm/cmd/gno/testdata/gno_test/empty_gno3.txtar b/gnovm/cmd/gno/testdata/gno_test/empty_gno3.txtar new file mode 100644 index 00000000000..c095de6c358 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/empty_gno3.txtar @@ -0,0 +1,14 @@ +# Test empty gno3 + +! gno test . + +! stdout .+ +stderr 'expected ''package'', found ''EOF''' + +! gno test --precompile . + +! stdout .+ +stderr 'expected ''package'', found ''EOF''' + +-- empty.gno -- +-- empty_filetest.gno -- diff --git a/gnovm/cmd/gno/testdata/gno_test/error_correct.txtar b/gnovm/cmd/gno/testdata/gno_test/error_correct.txtar new file mode 100644 index 00000000000..c7d3187424c --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/error_correct.txtar @@ -0,0 +1,18 @@ +# Test Error instruction correct + +gno test -verbose . + +stdout 'Machine\.RunMain\(\) panic: oups' +stderr '=== RUN file/x_filetest.gno' +stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +-- x_filetest.gno -- +package main + +func main() { + panic("oups") +} + +// Error: +// oups diff --git a/gnovm/cmd/gno/testdata/gno_test/error_incorrect.txtar b/gnovm/cmd/gno/testdata/gno_test/error_incorrect.txtar new file mode 100644 index 00000000000..10e8c09e6cb --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/error_incorrect.txtar @@ -0,0 +1,17 @@ +# Test Error instruction incorrect + +! gno test -verbose . + +stdout 'Machine\.RunMain\(\) panic: oups' +stderr '=== RUN file/x_filetest.gno' +stderr 'panic: fail on x_filetest.gno: got "oups", want: "xxx"' + +-- x_filetest.gno -- +package main + +func main() { + panic("oups") +} + +// Error: +// xxx diff --git a/gnovm/cmd/gno/testdata/gno_test/error_sync.txtar b/gnovm/cmd/gno/testdata/gno_test/error_sync.txtar new file mode 100644 index 00000000000..e8d25a09b9b --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/error_sync.txtar @@ -0,0 +1,32 @@ +# Test Error instruction updated +# NOTE: unlike Output and Realm instruction updates, Error update is not driven +# by the '-update-golden-tests' flag. The Error is only updated when it is +# empty. + +! gno test -verbose . + +stdout 'Machine\.RunMain\(\) panic: oups' +stderr '=== RUN file/x_filetest.gno' + +cmp x_filetest.gno x_filetest.gno.golden + +-- x_filetest.gno -- +package main + +func main() { + panic("oups") +} + +// Error: + +-- x_filetest.gno.golden -- +package main + +func main() { + panic("oups") +} + +// Error: +// oups +// *** CHECK THE ERR MESSAGES ABOVE, MAKE SURE IT'S WHAT YOU EXPECTED, DELETE THIS LINE AND RUN TEST AGAIN *** + diff --git a/gnovm/cmd/gno/testdata/gno_test/failing_filetest.txtar b/gnovm/cmd/gno/testdata/gno_test/failing_filetest.txtar new file mode 100644 index 00000000000..bc3efc1a8c9 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/failing_filetest.txtar @@ -0,0 +1,28 @@ +# Test with a failing _filetest.gno file + +! gno test -verbose . + +stdout 'Machine.RunMain\(\) panic: beep boop' +stderr '=== RUN file/failing_filetest.gno' +stderr 'panic: fail on failing_filetest.gno: got unexpected error: beep boop' + +! gno test -verbose --precompile . + +stdout 'Machine.RunMain\(\) panic: beep boop' +stderr '=== PREC \./\.' +stderr '=== BUILD \./\.' +stderr '=== RUN file/failing_filetest.gno' +stderr 'panic: fail on failing_filetest.gno: got unexpected error: beep boop' + +-- failing.gno -- +package failing + +-- failing_filetest.gno -- +package main + +func main() { + panic("beep boop") +} + +// Output: +// blah diff --git a/gnovm/cmd/gno/testdata/gno_test/failing_test.txtar b/gnovm/cmd/gno/testdata/gno_test/failing_test.txtar new file mode 100644 index 00000000000..0bf4aab21ff --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/failing_test.txtar @@ -0,0 +1,27 @@ +# Test with a failing _test.gno file + +! gno test -verbose . + +! stdout .+ +stderr '=== RUN TestAlwaysFailing' +stderr '--- FAIL: TestAlwaysFailing' +stderr 'FAIL: 0 build errors, 1 test errors' + +! gno test -verbose --precompile . + +! stdout .+ +stderr '=== RUN TestAlwaysFailing' +stderr '--- FAIL: TestAlwaysFailing' +stderr 'FAIL: 0 build errors, 1 test errors' + +-- failing.gno -- +package failing + +-- failing_test.gno -- +package failing + +import "testing" + +func TestAlwaysFailing(t *testing.T) { + t.Fatal("failed") +} diff --git a/gnovm/cmd/gno/testdata/gno_test/flag_print-runtime-metrics.txtar b/gnovm/cmd/gno/testdata/gno_test/flag_print-runtime-metrics.txtar new file mode 100644 index 00000000000..e065d00d55a --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/flag_print-runtime-metrics.txtar @@ -0,0 +1,23 @@ +# Test --print-runtime-metrics flag + +gno test --print-runtime-metrics . + +! stdout .+ +stderr '--- runtime: cycle=[\d\.kM]+ imports=\d+ allocs=[\d\.kM]+\(\d\.\d\d%\)' + +-- metrics.gno -- +package metrics + +-- metrics_test.gno -- +package metrics + +import ( + "testing" +) + +func TestTimeout(t *testing.T) { + for i := 0; i < 2; i++ { + println("plop") + } +} + diff --git a/gnovm/cmd/gno/testdata/gno_test/flag_run.txtar b/gnovm/cmd/gno/testdata/gno_test/flag_run.txtar new file mode 100644 index 00000000000..838206d15f9 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/flag_run.txtar @@ -0,0 +1,103 @@ +# Run test on gno.land/p/demo/ufmt + +gno test . + +stdout '=== RUN TestRun/hello' # show that t.Run doesn't care about -verbose +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' + +gno test ./run_test.gno + +stdout '=== RUN TestRun/hello' # show that t.Run doesn't care about -verbose +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' + +gno test -verbose . + +stdout '=== RUN TestRun/hello' +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run .* . + +stdout '=== RUN TestRun/hello' +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run NotExists . + +! stdout .+ +stderr '=== RUN TestRun' +stderr '--- FILT: TestRun' + +gno test -verbose -run .*/hello . + +stdout '=== RUN TestRun/hello' +! stdout '=== RUN TestRun/hi_you' +! stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run .*/hi . + +! stdout '=== RUN TestRun/hello' +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run .*/NotExists . + +! stdout .+ +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run Run/.* . + +stdout '=== RUN TestRun/hello' +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run Run/ . + +stdout '=== RUN TestRun/hello' +stdout '=== RUN TestRun/hi_you' +stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +gno test -verbose -run Run/hello . + +stdout '=== RUN TestRun/hello' +! stdout '=== RUN TestRun/hi_you' +! stdout '=== RUN TestRun/hi_me' +stderr '=== RUN TestRun' +stderr '--- PASS: TestRun' + +-- run.gno -- +package run + +-- run_test.gno -- +package run + +import ( + "fmt" + "testing" +) + +func TestRun(t *testing.T) { + cases := []string { + "hello", + "hi you", + "hi me", + } + for _, tc := range cases { + t.Run(tc, func(t *testing.T) {}) + } +} diff --git a/gnovm/cmd/gno/testdata/gno_test/flag_timeout.txtar b/gnovm/cmd/gno/testdata/gno_test/flag_timeout.txtar new file mode 100644 index 00000000000..1e9171164e8 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/flag_timeout.txtar @@ -0,0 +1,23 @@ +# Test -timeout flag + +! gno test --timeout 1ms . + +! stdout .+ +stderr 'panic: test timed out after 1ms' + +-- timeout.gno -- +package timeout + +-- timeout_test.gno -- +package timeout + +import ( + "testing" +) + +func TestTimeout(t *testing.T) { + for i := 0; i < 10_000; i++ { + println("plop") + } +} + diff --git a/gnovm/cmd/gno/testdata/gno_test/minim1.txtar b/gnovm/cmd/gno/testdata/gno_test/minim1.txtar new file mode 100644 index 00000000000..231ef4a270c --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/minim1.txtar @@ -0,0 +1,9 @@ +# Test minimalist 1 + +gno test . + +! stdout .+ +stderr '\? \./\. \[no test files\]' + +-- minim.gno -- +package minim diff --git a/gnovm/cmd/gno/testdata/gno_test/minim2.txtar b/gnovm/cmd/gno/testdata/gno_test/minim2.txtar new file mode 100644 index 00000000000..038dfd19289 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/minim2.txtar @@ -0,0 +1,12 @@ +# Test minimalist 2 + +gno test . + +! stdout .+ +stderr 'ok \./\. \d\.\d\ds' + +-- minim.gno -- +package minim + +-- minim_test.gno -- +package minim diff --git a/gnovm/cmd/gno/testdata/gno_test/minim3.txtar b/gnovm/cmd/gno/testdata/gno_test/minim3.txtar new file mode 100644 index 00000000000..8e657104801 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/minim3.txtar @@ -0,0 +1,14 @@ +# Test minimalist 3 + +gno test . + +! stdout .+ +stderr 'ok \./\. \d\.\d\ds' + +-- minim.gno -- +package minim + +-- minim_filetest.gno -- +package main + +func main() {} diff --git a/gnovm/cmd/gno/testdata/gno_test/no_args.txtar b/gnovm/cmd/gno/testdata/gno_test/no_args.txtar new file mode 100644 index 00000000000..e69d0994fdc --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/no_args.txtar @@ -0,0 +1,6 @@ +# Run gno test without args + +! gno test + +! stdout .+ +stderr 'flag: help requested' diff --git a/gnovm/cmd/gno/testdata/gno_test/output_correct.txtar b/gnovm/cmd/gno/testdata/gno_test/output_correct.txtar new file mode 100644 index 00000000000..ce12874f669 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/output_correct.txtar @@ -0,0 +1,20 @@ +# Test Output instruction correct + +gno test -verbose . + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +-- x_filetest.gno -- +package main + +func main() { + println("hey") + println("hru?") +} + +// Output: +// hey +// hru? diff --git a/gnovm/cmd/gno/testdata/gno_test/output_incorrect.txtar b/gnovm/cmd/gno/testdata/gno_test/output_incorrect.txtar new file mode 100644 index 00000000000..d5528a7abb9 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/output_incorrect.txtar @@ -0,0 +1,23 @@ +# Test Output instruction incorrect + +! gno test -verbose . + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr 'panic: fail on x_filetest.gno: diff:' +stderr '--- Expected' +stderr '\+\+\+ Actual' +stderr '@@ -1,2 \+1 @@' +stderr 'hey' +stderr '-hru?' + +-- x_filetest.gno -- +package main + +func main() { + println("hey") +} + +// Output: +// hey +// hru? diff --git a/gnovm/cmd/gno/testdata/gno_test/output_sync.txtar b/gnovm/cmd/gno/testdata/gno_test/output_sync.txtar new file mode 100644 index 00000000000..85fec4ab316 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/output_sync.txtar @@ -0,0 +1,34 @@ +# Test Output instruction updated + +gno test -verbose . -update-golden-tests + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +cmp x_filetest.gno x_filetest.gno.golden + +-- x_filetest.gno -- +package main + +func main() { + println("hey") + println("hru?") +} + +// Output: +// hey + +-- x_filetest.gno.golden -- +package main + +func main() { + println("hey") + println("hru?") +} + +// Output: +// hey +// hru? + diff --git a/gnovm/cmd/gno/testdata/gno_test/realm_correct.txtar b/gnovm/cmd/gno/testdata/gno_test/realm_correct.txtar new file mode 100644 index 00000000000..f376e61d9a4 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/realm_correct.txtar @@ -0,0 +1,85 @@ +# Test Realm instruction correct + +gno test -verbose . + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +-- x_filetest.gno -- +// PKGPATH: gno.land/r/x +package x + +var x int + +func main() { + x = 1 +} + +// Realm: +// switchrealm["gno.land/r/x"] +// u[58cde29876a8d185e30c727361981efb068f4726:2]={ +// "Blank": {}, +// "ObjectInfo": { +// "ID": "58cde29876a8d185e30c727361981efb068f4726:2", +// "IsEscaped": true, +// "ModTime": "3", +// "RefCount": "2" +// }, +// "Parent": null, +// "Source": { +// "@type": "/gno.RefNode", +// "BlockNode": null, +// "Location": { +// "File": "", +// "Line": "0", +// "Nonce": "0", +// "PkgPath": "gno.land/r/x" +// } +// }, +// "Values": [ +// { +// "T": { +// "@type": "/gno.FuncType", +// "Params": [], +// "Results": [] +// }, +// "V": { +// "@type": "/gno.FuncValue", +// "Closure": { +// "@type": "/gno.RefValue", +// "Escaped": true, +// "ObjectID": "58cde29876a8d185e30c727361981efb068f4726:3" +// }, +// "FileName": "main.gno", +// "IsMethod": false, +// "Name": "main", +// "PkgPath": "gno.land/r/x", +// "Source": { +// "@type": "/gno.RefNode", +// "BlockNode": null, +// "Location": { +// "File": "main.gno", +// "Line": "6", +// "Nonce": "0", +// "PkgPath": "gno.land/r/x" +// } +// }, +// "Type": { +// "@type": "/gno.FuncType", +// "Params": [], +// "Results": [] +// } +// } +// }, +// { +// "N": "AQAAAAAAAAA=", +// "T": { +// "@type": "/gno.PrimitiveType", +// "value": "32" +// } +// } +// ] +// } + diff --git a/gnovm/cmd/gno/testdata/gno_test/realm_incorrect.txtar b/gnovm/cmd/gno/testdata/gno_test/realm_incorrect.txtar new file mode 100644 index 00000000000..3922c34fec8 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/realm_incorrect.txtar @@ -0,0 +1,26 @@ +# Test Realm instruction incorrect + +! gno test -verbose . + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr 'panic: fail on x_filetest.gno: diff:' +stderr '--- Expected' +stderr '\+\+\+ Actual' +stderr '@@ -1 \+1,64 @@' +stderr '-xxx' +stderr '\+switchrealm\["gno.land/r/x"\]' + +-- x_filetest.gno -- +// PKGPATH: gno.land/r/x +package x + +var x int + +func main() { + x = 1 +} + +// Realm: +// xxxx + diff --git a/gnovm/cmd/gno/testdata/gno_test/realm_sync.txtar b/gnovm/cmd/gno/testdata/gno_test/realm_sync.txtar new file mode 100644 index 00000000000..918fb0b88c9 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/realm_sync.txtar @@ -0,0 +1,100 @@ +# Test Realm instruction updated + +gno test -verbose . -update-golden-tests + +! stdout .+ # stdout should be empty +stderr '=== RUN file/x_filetest.gno' +stderr '--- PASS: file/x_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +cmp x_filetest.gno x_filetest.gno.golden + +-- x_filetest.gno -- +// PKGPATH: gno.land/r/x +package x + +var x int + +func main() { + x = 1 +} + +// Realm: +// xxx + +-- x_filetest.gno.golden -- +// PKGPATH: gno.land/r/x +package x + +var x int + +func main() { + x = 1 +} + +// Realm: +// switchrealm["gno.land/r/x"] +// u[58cde29876a8d185e30c727361981efb068f4726:2]={ +// "Blank": {}, +// "ObjectInfo": { +// "ID": "58cde29876a8d185e30c727361981efb068f4726:2", +// "IsEscaped": true, +// "ModTime": "3", +// "RefCount": "2" +// }, +// "Parent": null, +// "Source": { +// "@type": "/gno.RefNode", +// "BlockNode": null, +// "Location": { +// "File": "", +// "Line": "0", +// "Nonce": "0", +// "PkgPath": "gno.land/r/x" +// } +// }, +// "Values": [ +// { +// "T": { +// "@type": "/gno.FuncType", +// "Params": [], +// "Results": [] +// }, +// "V": { +// "@type": "/gno.FuncValue", +// "Closure": { +// "@type": "/gno.RefValue", +// "Escaped": true, +// "ObjectID": "58cde29876a8d185e30c727361981efb068f4726:3" +// }, +// "FileName": "main.gno", +// "IsMethod": false, +// "Name": "main", +// "PkgPath": "gno.land/r/x", +// "Source": { +// "@type": "/gno.RefNode", +// "BlockNode": null, +// "Location": { +// "File": "main.gno", +// "Line": "6", +// "Nonce": "0", +// "PkgPath": "gno.land/r/x" +// } +// }, +// "Type": { +// "@type": "/gno.FuncType", +// "Params": [], +// "Results": [] +// } +// } +// }, +// { +// "N": "AQAAAAAAAAA=", +// "T": { +// "@type": "/gno.PrimitiveType", +// "value": "32" +// } +// } +// ] +// } + diff --git a/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar b/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar new file mode 100644 index 00000000000..3dd890af25a --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar @@ -0,0 +1,32 @@ +# Test native lib + +! gno test -verbose . + +! stdout .+ +stderr 'panic: unknown import path net \[recovered\]' +stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path net' + +gno test -verbose --with-native-fallback . + +! stdout .+ +stderr '=== RUN TestFoo' +stderr '--- PASS: TestFoo' + +-- contract.gno -- +package contract + +import "net" + +func Foo() { + _ = net.IPv4 +} + +-- contract_test.gno -- +package contract + +import "testing" + +func TestFoo(t *testing.T) { + Foo() +} + diff --git a/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar b/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar new file mode 100644 index 00000000000..6144eadba01 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar @@ -0,0 +1,32 @@ +# Test unknow lib + +! gno test -verbose . + +! stdout .+ +stderr 'panic: unknown import path foobarbaz \[recovered\]' +stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path foobarbaz' + +! gno test -verbose --with-native-fallback . + +! stdout .+ +stderr 'panic: unknown import path foobarbaz \[recovered\]' +stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path foobarbaz' + +-- contract.gno -- +package contract + +import "foobarbaz" + +func Foo() { + _ = foobarbaz.Gnognogno +} + +-- contract_test.gno -- +package contract + +import "testing" + +func TestFoo(t *testing.T) { + Foo() +} + diff --git a/gnovm/cmd/gno/testdata/gno_test/valid_filetest.txtar b/gnovm/cmd/gno/testdata/gno_test/valid_filetest.txtar new file mode 100644 index 00000000000..51b5f323654 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/valid_filetest.txtar @@ -0,0 +1,26 @@ +# Test with a valid _filetest.gno file + +gno test . + +! stdout .+ +stderr 'ok \./\. \d\.\d\ds' + +gno test -verbose . + +! stdout .+ +stderr '=== RUN file/valid_filetest.gno' +stderr '--- PASS: file/valid_filetest.gno \(\d\.\d\ds\)' +stderr 'ok \./\. \d\.\d\ds' + +-- valid.gno -- +package valid + +-- valid_filetest.gno -- +package main + +func main() { + println("test") +} + +// Output: +// test diff --git a/gnovm/cmd/gno/testdata/gno_test/valid_test.txtar b/gnovm/cmd/gno/testdata/gno_test/valid_test.txtar new file mode 100644 index 00000000000..cb5f7286f60 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/valid_test.txtar @@ -0,0 +1,18 @@ +# Test with a valid _test.gno file + +gno test . + +! stdout .+ +stderr 'ok \./\. \d\.\d\ds' + +-- valid.gno -- +package valid + +-- valid_test.gno -- +package valid + +import "testing" + +func TestAlwaysValid(t *testing.T) { + // noop +} diff --git a/gnovm/tests/integ/failing1/failing.gno b/gnovm/tests/integ/failing1/failing.gno deleted file mode 100644 index 922c78d1deb..00000000000 --- a/gnovm/tests/integ/failing1/failing.gno +++ /dev/null @@ -1 +0,0 @@ -package failing diff --git a/gnovm/tests/integ/failing1/failing_test.gno b/gnovm/tests/integ/failing1/failing_test.gno deleted file mode 100644 index 4bef56cffe8..00000000000 --- a/gnovm/tests/integ/failing1/failing_test.gno +++ /dev/null @@ -1,7 +0,0 @@ -package failing - -import "testing" - -func TestAlwaysFailing(t *testing.T) { - t.Fatal("failed") -} diff --git a/gnovm/tests/integ/failing2/failing.gno b/gnovm/tests/integ/failing2/failing.gno deleted file mode 100644 index 922c78d1deb..00000000000 --- a/gnovm/tests/integ/failing2/failing.gno +++ /dev/null @@ -1 +0,0 @@ -package failing diff --git a/gnovm/tests/integ/failing2/failing_filetest.gno b/gnovm/tests/integ/failing2/failing_filetest.gno deleted file mode 100644 index afd1d5ce222..00000000000 --- a/gnovm/tests/integ/failing2/failing_filetest.gno +++ /dev/null @@ -1,8 +0,0 @@ -package main - -func main() { - panic("beep boop") -} - -// Output: -// blah diff --git a/gnovm/tests/integ/minimalist-gno1/minim.gno b/gnovm/tests/integ/minimalist-gno1/minim.gno deleted file mode 100644 index 5e4c8fc1955..00000000000 --- a/gnovm/tests/integ/minimalist-gno1/minim.gno +++ /dev/null @@ -1 +0,0 @@ -package minim diff --git a/gnovm/tests/integ/minimalist-gno2/minim.gno b/gnovm/tests/integ/minimalist-gno2/minim.gno deleted file mode 100644 index 5e4c8fc1955..00000000000 --- a/gnovm/tests/integ/minimalist-gno2/minim.gno +++ /dev/null @@ -1 +0,0 @@ -package minim diff --git a/gnovm/tests/integ/minimalist-gno2/minim_test.gno b/gnovm/tests/integ/minimalist-gno2/minim_test.gno deleted file mode 100644 index 5e4c8fc1955..00000000000 --- a/gnovm/tests/integ/minimalist-gno2/minim_test.gno +++ /dev/null @@ -1 +0,0 @@ -package minim diff --git a/gnovm/tests/integ/minimalist-gno3/minim.gno b/gnovm/tests/integ/minimalist-gno3/minim.gno deleted file mode 100644 index 5e4c8fc1955..00000000000 --- a/gnovm/tests/integ/minimalist-gno3/minim.gno +++ /dev/null @@ -1 +0,0 @@ -package minim diff --git a/gnovm/tests/integ/minimalist-gno3/minim_filetest.gno b/gnovm/tests/integ/minimalist-gno3/minim_filetest.gno deleted file mode 100644 index 38dd16da61a..00000000000 --- a/gnovm/tests/integ/minimalist-gno3/minim_filetest.gno +++ /dev/null @@ -1,3 +0,0 @@ -package main - -func main() {} diff --git a/gnovm/tests/integ/native-lib/contract.gno b/gnovm/tests/integ/native-lib/contract.gno deleted file mode 100644 index 65b708c236e..00000000000 --- a/gnovm/tests/integ/native-lib/contract.gno +++ /dev/null @@ -1,7 +0,0 @@ -package contract - -import "net" - -func Foo() { - _ = net.IPv4 -} diff --git a/gnovm/tests/integ/native-lib/contract_test.gno b/gnovm/tests/integ/native-lib/contract_test.gno deleted file mode 100644 index 32416ebf378..00000000000 --- a/gnovm/tests/integ/native-lib/contract_test.gno +++ /dev/null @@ -1,7 +0,0 @@ -package contract - -import "testing" - -func TestFoo(t *testing.T) { - Foo() -} diff --git a/gnovm/tests/integ/unknown-lib/contract.gno b/gnovm/tests/integ/unknown-lib/contract.gno deleted file mode 100644 index e6d07d51692..00000000000 --- a/gnovm/tests/integ/unknown-lib/contract.gno +++ /dev/null @@ -1,7 +0,0 @@ -package contract - -import "foobarbaz" - -func Foo() { - _ = foobarbaz.Gnognogno -} diff --git a/gnovm/tests/integ/unknown-lib/contract_test.gno b/gnovm/tests/integ/unknown-lib/contract_test.gno deleted file mode 100644 index 32416ebf378..00000000000 --- a/gnovm/tests/integ/unknown-lib/contract_test.gno +++ /dev/null @@ -1,7 +0,0 @@ -package contract - -import "testing" - -func TestFoo(t *testing.T) { - Foo() -} diff --git a/gnovm/tests/integ/valid2/valid.gno b/gnovm/tests/integ/valid2/valid.gno deleted file mode 100644 index 691266cbae2..00000000000 --- a/gnovm/tests/integ/valid2/valid.gno +++ /dev/null @@ -1 +0,0 @@ -package valid diff --git a/gnovm/tests/integ/valid2/valid_filetest.gno b/gnovm/tests/integ/valid2/valid_filetest.gno deleted file mode 100644 index 2b74a940881..00000000000 --- a/gnovm/tests/integ/valid2/valid_filetest.gno +++ /dev/null @@ -1,8 +0,0 @@ -package main - -func main() { - println("test") -} - -// Output: -// test diff --git a/go.mod b/go.mod index 331ecc8b122..eb7c3b9a277 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/pelletier/go-toml v1.9.5 github.com/peterbourgon/ff/v3 v3.4.0 github.com/pmezard/go-difflib v1.0.0 + github.com/rogpeppe/go-internal v1.11.0 github.com/stretchr/testify v1.8.4 github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c go.etcd.io/bbolt v1.3.7 diff --git a/go.sum b/go.sum index 08208e72925..c51f8d91df2 100644 --- a/go.sum +++ b/go.sum @@ -172,6 +172,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=