Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error {
}

// Construct machine for testing.
tm := test.Machine(newProdGnoStore(), goio.Discard, pkgPath, false)
tm := test.Machine(newProdGnoStore(), goio.Discard, pkgPath, false, nil)
defer tm.Release()

// LINT STEP 4: re-parse for preprocessor.
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/error_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
gno test -v .

stderr '=== RUN ./x_filetest.gno'
stderr '--- PASS: ./x_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./x_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

-- x_filetest.gno --
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/filetest_events.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gno test -print-events -v .

stdout 'test'
stderr '=== RUN ./valid_filetest.gno'
stderr '--- PASS: ./valid_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./valid_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

-- valid.gno --
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/output_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gno test -v .
stdout 'hey'
stdout 'hru?'
stderr '=== RUN ./x_filetest.gno'
stderr '--- PASS: ./x_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./x_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

-- x_filetest.gno --
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/output_sync.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ stdout 'hey'
stdout '^hru\?'

stderr '=== RUN ./x_filetest.gno'
stderr '--- PASS: ./x_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./x_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

cmp x_filetest.gno x_filetest.gno.golden
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/realm_correct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gno test -v .

! stdout .+ # stdout should be empty
stderr '=== RUN ./x_filetest.gno'
stderr '--- PASS: ./x_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./x_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

-- x_filetest.gno --
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/realm_sync.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gno test -v . -update-golden-tests

! stdout .+ # stdout should be empty
stderr '=== RUN ./x_filetest.gno'
stderr '--- PASS: ./x_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./x_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

cmp x_filetest.gno x_filetest.gno.golden
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/test/valid_filetest.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gno test -v .

stdout 'test'
stderr '=== RUN ./valid_filetest.gno'
stderr '--- PASS: ./valid_filetest.gno \(\d+\.\d\ds\)'
stderr '--- PASS: ./valid_filetest.gno \(elapsed: \d+\.\d\ds, gas: \d+\)'
stderr 'ok \. \d+\.\d\ds'

-- valid.gno --
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestFiles(t *testing.T) {
t.Parallel()
opts = newOpts()
}
changed, err := opts.RunFiletest(path, content, opts.TestStore)
changed, _, err := opts.RunFiletest(path, content, opts.TestStore)
if err != nil {
t.Fatal(err.Error())
}
Expand Down
25 changes: 13 additions & 12 deletions gnovm/pkg/test/filetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
teststdlibs "github.com/gnolang/gno/gnovm/tests/stdlibs"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
"github.com/gnolang/gno/tm2/pkg/store/types"
"github.com/pmezard/go-difflib/difflib"
"go.uber.org/multierr"
)
Expand All @@ -26,7 +27,7 @@ import (
// If opts.Sync is enabled, and the filetest's golden output has changed,
// the first string is set to the new generated content of the file.
// Before the filetest is run it will be type-checked.
func (opts *TestOptions) RunFiletest(fname string, source []byte, tgs gno.Store) (string, error) {
func (opts *TestOptions) RunFiletest(fname string, source []byte, tgs gno.Store) (string, types.Gas, error) {
opts.outWriter.w = opts.Output
opts.outWriter.errW = opts.Error
tcheck := true // Go type-check filetests in test/files.
Expand All @@ -37,10 +38,10 @@ func (opts *TestOptions) RunFiletest(fname string, source []byte, tgs gno.Store)
// (cmd/gno/test.go) already type-checked the whole package.
// Go type-checking in filetests is only available for gnovm internal filetests
// in test/files.
func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store, tcheck bool) (string, error) {
func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store, tcheck bool) (string, types.Gas, error) {
dirs, err := ParseDirectives(bytes.NewReader(source))
if err != nil {
return "", fmt.Errorf("error parsing directives: %w", err)
return "", 0, fmt.Errorf("error parsing directives: %w", err)
}

// Sanity check: type-check directives are not available
Expand All @@ -53,13 +54,13 @@ func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store,
pkgPath := dirs.FirstDefault(DirectivePkgPath, "main")
coins, err := std.ParseCoins(dirs.FirstDefault(DirectiveSend, ""))
if err != nil {
return "", err
return "", 0, err
}
ctx := Context("", pkgPath, coins)
maxAllocRaw := dirs.FirstDefault(DirectiveMaxAlloc, "0")
maxAlloc, err := strconv.ParseInt(maxAllocRaw, 10, 64)
if err != nil {
return "", fmt.Errorf("could not parse MAXALLOC directive: %w", err)
return "", 0, fmt.Errorf("could not parse MAXALLOC directive: %w", err)
}

var opslog io.Writer
Expand Down Expand Up @@ -128,7 +129,7 @@ func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store,
Content: "",
})
} else {
return "", fmt.Errorf("unexpected panic: %s\noutput:\n%s\nstacktrace:\n%s\nstack:\n%v",
return "", m.GasMeter.GasConsumed(), fmt.Errorf("unexpected panic: %s\noutput:\n%s\nstacktrace:\n%s\nstack:\n%v",
result.Error, result.Output, result.GnoStacktrace, string(result.GoPanicStack))
}
}
Expand All @@ -141,16 +142,16 @@ func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store,
Content: "",
})
} else {
return "", fmt.Errorf("unexpected output:\n%s", result.Output)
return "", m.GasMeter.GasConsumed(), fmt.Errorf("unexpected output:\n%s", result.Output)
}
}
} else {
err = m.CheckEmpty()
if err != nil {
return "", fmt.Errorf("machine not empty after main: %w", err)
return "", m.GasMeter.GasConsumed(), fmt.Errorf("machine not empty after main: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can also print it to a // Gas: directive for a filetest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. How to make the filetest cause the "machine not empty after main" error?

}
if gno.HasDebugErrors() {
return "", fmt.Errorf("got unexpected debug error(s): %v", gno.GetDebugErrors())
return "", m.GasMeter.GasConsumed(), fmt.Errorf("got unexpected debug error(s): %v", gno.GetDebugErrors())
}
}

Expand Down Expand Up @@ -179,7 +180,7 @@ func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store,
case DirectivePreprocessed:
pn := m.Store.GetBlockNodeSafe(gno.PackageNodeLocation(pkgPath))
if pn == nil {
return "", fmt.Errorf("package %q not preprocessed: %s", pkgPath, result.Error)
return "", m.GasMeter.GasConsumed(), fmt.Errorf("package %q not preprocessed: %s", pkgPath, result.Error)
}
pre := pn.(*gno.PackageNode).FileSet.Files[0].String()
match(dir, pre)
Expand All @@ -206,10 +207,10 @@ func (opts *TestOptions) runFiletest(fname string, source []byte, tgs gno.Store,
}

if updated { // only true if sync == true
return dirs.FileTest(), returnErr
return dirs.FileTest(), m.GasMeter.GasConsumed(), returnErr
}

return "", returnErr
return "", m.GasMeter.GasConsumed(), returnErr
}

// returns a sorted string representation of realm diffs map
Expand Down
15 changes: 9 additions & 6 deletions gnovm/pkg/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
storetypes "github.com/gnolang/gno/tm2/pkg/store/types"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -71,13 +72,14 @@ func Context(caller crypto.Bech32Address, pkgPath string, send std.Coins) *runti

// Machine is a minimal machine, set up with just the Store, Output and Context.
// It is only used for linting/preprocessing.
func Machine(testStore gno.Store, output io.Writer, pkgPath string, debug bool) *gno.Machine {
func Machine(testStore gno.Store, output io.Writer, pkgPath string, debug bool, gasMeter store.GasMeter) *gno.Machine {
return gno.NewMachineWithOptions(gno.MachineOptions{
Store: testStore,
Output: output,
Context: Context("", pkgPath, nil),
Debug: debug,
ReviveEnabled: true,
GasMeter: gasMeter,
})
}

Expand Down Expand Up @@ -318,7 +320,7 @@ func Test(mpkg *std.MemPackage, fsDir string, opts *TestOptions) error {
tcheck := false // already type-checked e.g. by cmd/gno/test.go
// We can not use shared tx gno store (tgs) between _filetest.gno since we need to
// isolate the state between them
changed, err := opts.runFiletest(
changed, gas, err := opts.runFiletest(
testFileName, []byte(testFile.Body), opts.TestStore, tcheck)
if changed != "" {
// Note: changed always == "" if opts.Sync == false.
Expand All @@ -331,11 +333,11 @@ func Test(mpkg *std.MemPackage, fsDir string, opts *TestOptions) error {
duration := time.Since(startedAt)
dstr := fmtDuration(duration)
if err != nil {
fmt.Fprintf(opts.Error, "--- FAIL: %s (%s)\n", testName, dstr)
fmt.Fprintf(opts.Error, "--- FAIL: %s (elapsed: %s, gas: %d)\n", testName, dstr, gas)
fmt.Fprintln(opts.Error, err.Error())
errs = multierr.Append(errs, fmt.Errorf("%s failed", testName))
} else if opts.Verbose {
fmt.Fprintf(opts.Error, "--- PASS: %s (%s)\n", testName, dstr)
fmt.Fprintf(opts.Error, "--- PASS: %s (elapsed: %s, gas: %d)\n", testName, dstr, gas)
}

// XXX: add per-test metrics
Expand Down Expand Up @@ -377,7 +379,7 @@ func (opts *TestOptions) runTestFiles(
opts.TestStore.SetLogStoreOps(nil)

// Check if we already have the package - it may have been eagerly loaded.
m = Machine(tgs, opts.WriterForStore(), mpkg.Path, opts.Debug)
m = Machine(tgs, opts.WriterForStore(), mpkg.Path, opts.Debug, nil)
m.Alloc = alloc
if tgs.GetMemPackage(mpkg.Path) == nil {
m.RunMemPackage(mpkg, false)
Expand All @@ -398,7 +400,7 @@ func (opts *TestOptions) runTestFiles(
// - Run the test files before this for loop (but persist it to store;
// RunFiles doesn't do that currently)
// - Wrap here.
m = Machine(tgs, opts.WriterForStore(), mpkg.Path, opts.Debug)
m = Machine(tgs, opts.WriterForStore(), mpkg.Path, opts.Debug, store.NewInfiniteGasMeter())
m.Alloc = alloc.Reset()
m.SetActivePackage(pv)

Expand Down Expand Up @@ -479,6 +481,7 @@ func (opts *TestOptions) runTestFiles(
},
},
))
fmt.Fprintf(opts.Error, "--- GAS: %d\n", m.GasMeter.GasConsumed())

if opts.Events {
events := m.Context.(*runtime.TestExecContext).EventLogger.Events()
Expand Down
Loading