Skip to content

Commit

Permalink
refac: move testscript in cmd/gno
Browse files Browse the repository at this point in the history
- use a custom command for "gno" instead of env var
- rebase on master so we can use "GNOROOT" instead of "-root-dir"
  • Loading branch information
tbruyelle committed Aug 11, 2023
1 parent 5446309 commit 1b1fddf
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 41 deletions.
43 changes: 42 additions & 1 deletion gnovm/cmd/gno/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -141,3 +144,41 @@ func testMainCaseRun(t *testing.T, tc []testMainCase) {
})
}
}

func SetupTestScript(t *testing.T, txtarDir string) testscript.Params {
// 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 :)
)
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,
}
}
10 changes: 9 additions & 1 deletion gnovm/cmd/gno/test_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package main

import "testing"
import (
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func TestTest(t *testing.T) {
testscript.Run(t, SetupTestScript(t, "testdata/gno_test"))
}

func xTestTest(t *testing.T) {
tc := []testMainCase{
{
args: []string{"test"},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Error instruction correct

exec $GNO test -verbose -root-dir=$ROOTDIR .
gno test -verbose .

stdout 'Machine\.RunMain\(\) panic: oups'
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Error instruction incorrect

! exec $GNO test -verbose -root-dir=$ROOTDIR .
! gno test -verbose .

stdout 'Machine\.RunMain\(\) panic: oups'
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# by the '-update-golden-tests' flag. The Error is only updated when it is
# empty.

! exec $GNO test -verbose -root-dir=$ROOTDIR .
! gno test -verbose .

cmp x_filetest.gno x_filetest.gno.golden

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Output instruction correct

exec $GNO test -verbose -root-dir=$ROOTDIR .
gno test -verbose .

! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Output instruction incorrect

! exec $GNO test -verbose -root-dir=$ROOTDIR .
! gno test -verbose .

! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Output instruction updated

exec $GNO test -verbose -root-dir=$ROOTDIR . -update-golden-tests
gno test -verbose . -update-golden-tests

cmp x_filetest.gno x_filetest.gno.golden

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Realm instruction correct

exec $GNO test -verbose -root-dir=$ROOTDIR .
gno test -verbose .

! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Realm instruction incorrect

! exec $GNO test -verbose -root-dir=$ROOTDIR .
! gno test -verbose .

! stdout .+ # stdout should be empty
stderr '=== RUN file/x_filetest.gno'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test Realm instruction updated

exec $GNO test -verbose -root-dir=$ROOTDIR . -update-golden-tests
gno test -verbose . -update-golden-tests

cmp x_filetest.gno x_filetest.gno.golden

Expand Down
30 changes: 0 additions & 30 deletions gnovm/tests/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import (
"flag"
"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 @@ -104,29 +100,3 @@ 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 := 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
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)
}

0 comments on commit 1b1fddf

Please sign in to comment.