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..66f972c0fad 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,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/syndtr/goleveldb v1.0.0 github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c diff --git a/go.sum b/go.sum index 85e172493fa..27f8f377876 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=