From 4b960b08b526688b1a79052c899f336cae7e3d25 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Thu, 3 Aug 2023 16:41:09 +0200 Subject: [PATCH] test(gnovm): "*_filetest.gno" instructions Add tests for Output, Error and Realm instructions The change introduces a new test dependency github.com/rogpeppe/go-internal/testscripts, which allows to create tests bases on txtar files. This is very handy for this kind of tests, because: - you can easily creates a set of files that can be used for a command (here the `gno test` command) - you can assert stderr and stdout expectations in a very readable and maintainable way - you can compare files using the `cmp` command, which is very useful to assert of the `-update-golden-tests` behavior. These tests were added to help work on #1015, which will potentially alter the behavior of "*filetest.gno" instructions updates. --- gnovm/tests/file_test.go | 32 ++++++++ gnovm/tests/testdata/error_correct.txt | 18 +++++ gnovm/tests/testdata/error_incorrect.txt | 17 ++++ gnovm/tests/testdata/error_sync.txt | 29 +++++++ gnovm/tests/testdata/output_correct.txt | 20 +++++ gnovm/tests/testdata/output_incorrect.txt | 23 ++++++ gnovm/tests/testdata/output_sync.txt | 29 +++++++ gnovm/tests/testdata/realm_correct.txt | 85 ++++++++++++++++++++ gnovm/tests/testdata/realm_incorrect.txt | 26 +++++++ gnovm/tests/testdata/realm_sync.txt | 95 +++++++++++++++++++++++ go.mod | 1 + go.sum | 3 + 12 files changed, 378 insertions(+) create mode 100644 gnovm/tests/testdata/error_correct.txt create mode 100644 gnovm/tests/testdata/error_incorrect.txt create mode 100644 gnovm/tests/testdata/error_sync.txt create mode 100644 gnovm/tests/testdata/output_correct.txt create mode 100644 gnovm/tests/testdata/output_incorrect.txt create mode 100644 gnovm/tests/testdata/output_sync.txt create mode 100644 gnovm/tests/testdata/realm_correct.txt create mode 100644 gnovm/tests/testdata/realm_incorrect.txt create mode 100644 gnovm/tests/testdata/realm_sync.txt diff --git a/gnovm/tests/file_test.go b/gnovm/tests/file_test.go index f070546ab74..87c23a07b31 100644 --- a/gnovm/tests/file_test.go +++ b/gnovm/tests/file_test.go @@ -2,13 +2,18 @@ package tests import ( "flag" + "fmt" "io/fs" "os" + "os/exec" "path" "path/filepath" "strings" "testing" + "github.com/rogpeppe/go-internal/testscript" + "github.com/stretchr/testify/require" + gno "github.com/gnolang/gno/gnovm/pkg/gnolang" ) @@ -100,3 +105,30 @@ func runFileTest(t *testing.T, path string, opts ...RunFileTestOption) { t.Fatalf("got error: %v", err) } } + +func TestRunFileTest(t *testing.T) { + // Get root location of github.com/gnolang/gno + goModPath, err := exec.Command("go", "env", "GOMOD").CombinedOutput() + require.NoError(t, err) + rootDir := path.Dir(string(goModPath)) + // Build a fresh gno binary in a temp directory + gnoBin := path.Join(t.TempDir(), "gno") + err = exec.Command("go", "build", "-o", gnoBin, path.Join(rootDir, "gnovm", "cmd", "gno")).Run() + fmt.Println(exec.Command("go", "build", path.Join(rootDir, "gnovm", "cmd", "gno"), "-o", gnoBin)) + require.NoError(t, err) + // Define script params + params := testscript.Params{ + Setup: func(env *testscript.Env) error { + // Envs to have access to gno binary and path in test scripts + env.Vars = append(env.Vars, + "ROOTDIR="+rootDir, + "GNO="+gnoBin, + ) + return nil + }, + // Location of test scripts + Dir: "testdata", + } + // Run test scripts + testscript.Run(t, params) +} diff --git a/gnovm/tests/testdata/error_correct.txt b/gnovm/tests/testdata/error_correct.txt new file mode 100644 index 00000000000..a2730a346b8 --- /dev/null +++ b/gnovm/tests/testdata/error_correct.txt @@ -0,0 +1,18 @@ +# Test Error instruction correct + +exec $GNO test -verbose -root-dir=$ROOTDIR . + +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/tests/testdata/error_incorrect.txt b/gnovm/tests/testdata/error_incorrect.txt new file mode 100644 index 00000000000..0ce8a1a308d --- /dev/null +++ b/gnovm/tests/testdata/error_incorrect.txt @@ -0,0 +1,17 @@ +# Test Error instruction incorrect + +! exec $GNO test -verbose -root-dir=$ROOTDIR . + +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/tests/testdata/error_sync.txt b/gnovm/tests/testdata/error_sync.txt new file mode 100644 index 00000000000..eabf4025dd3 --- /dev/null +++ b/gnovm/tests/testdata/error_sync.txt @@ -0,0 +1,29 @@ +# 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. + +! exec $GNO test -verbose -root-dir=$ROOTDIR . + +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/tests/testdata/output_correct.txt b/gnovm/tests/testdata/output_correct.txt new file mode 100644 index 00000000000..8322bd082c7 --- /dev/null +++ b/gnovm/tests/testdata/output_correct.txt @@ -0,0 +1,20 @@ +# Test Output instruction correct + +exec $GNO test -verbose -root-dir=$ROOTDIR . + +! stdout .+ +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/tests/testdata/output_incorrect.txt b/gnovm/tests/testdata/output_incorrect.txt new file mode 100644 index 00000000000..27d3b58af63 --- /dev/null +++ b/gnovm/tests/testdata/output_incorrect.txt @@ -0,0 +1,23 @@ +# Test Output instruction incorrect + +! exec $GNO test -verbose -root-dir=$ROOTDIR . + +! stdout .+ +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/tests/testdata/output_sync.txt b/gnovm/tests/testdata/output_sync.txt new file mode 100644 index 00000000000..e0bcdeb012e --- /dev/null +++ b/gnovm/tests/testdata/output_sync.txt @@ -0,0 +1,29 @@ +# Test Output instruction updated + +exec $GNO test -verbose -root-dir=$ROOTDIR . -update-golden-tests + +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/tests/testdata/realm_correct.txt b/gnovm/tests/testdata/realm_correct.txt new file mode 100644 index 00000000000..cd874103d06 --- /dev/null +++ b/gnovm/tests/testdata/realm_correct.txt @@ -0,0 +1,85 @@ +# Test Realm instruction correct + +exec $GNO test -verbose -root-dir=$ROOTDIR . + +! stdout .+ +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/tests/testdata/realm_incorrect.txt b/gnovm/tests/testdata/realm_incorrect.txt new file mode 100644 index 00000000000..a1b5a98875d --- /dev/null +++ b/gnovm/tests/testdata/realm_incorrect.txt @@ -0,0 +1,26 @@ +# Test Realm instruction incorrect + +! exec $GNO test -verbose -root-dir=$ROOTDIR . + +! stdout .+ +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/tests/testdata/realm_sync.txt b/gnovm/tests/testdata/realm_sync.txt new file mode 100644 index 00000000000..a96efe23b1d --- /dev/null +++ b/gnovm/tests/testdata/realm_sync.txt @@ -0,0 +1,95 @@ +# Test Realm instruction updated + +exec $GNO test -verbose -root-dir=$ROOTDIR . -update-golden-tests + +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/go.mod b/go.mod index 9042d49acfa..a69d656a021 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/lucasb-eyer/go-colorful v1.0.3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect go.opencensus.io v0.22.5 // indirect go.uber.org/atomic v1.7.0 // indirect golang.org/x/sys v0.10.0 // indirect diff --git a/go.sum b/go.sum index 85e172493fa..bdebd29852d 100644 --- a/go.sum +++ b/go.sum @@ -170,6 +170,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= @@ -181,6 +183,7 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6 github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=