Skip to content

Commit

Permalink
test(gnovm): "*_filetest.gno" instructions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tbruyelle committed Aug 3, 2023
1 parent 1fc25f9 commit db84131
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 0 deletions.
32 changes: 32 additions & 0 deletions gnovm/tests/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions gnovm/tests/testdata/error_correct.txt
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions gnovm/tests/testdata/error_incorrect.txt
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions gnovm/tests/testdata/error_sync.txt
Original file line number Diff line number Diff line change
@@ -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 ***

20 changes: 20 additions & 0 deletions gnovm/tests/testdata/output_correct.txt
Original file line number Diff line number Diff line change
@@ -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?
23 changes: 23 additions & 0 deletions gnovm/tests/testdata/output_incorrect.txt
Original file line number Diff line number Diff line change
@@ -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?
29 changes: 29 additions & 0 deletions gnovm/tests/testdata/output_sync.txt
Original file line number Diff line number Diff line change
@@ -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?

85 changes: 85 additions & 0 deletions gnovm/tests/testdata/realm_correct.txt
Original file line number Diff line number Diff line change
@@ -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"
// }
// }
// ]
// }

26 changes: 26 additions & 0 deletions gnovm/tests/testdata/realm_incorrect.txt
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit db84131

Please sign in to comment.