Skip to content

Commit

Permalink
chore: Merge remote-tracking branch 'morgan/dev/morgan/big-test-refac…
Browse files Browse the repository at this point in the history
…tor' into genesis-stdlibs
  • Loading branch information
n0izn0iz committed Nov 26, 2024
2 parents d43e5b2 + 2e1b5df commit 606a741
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
4 changes: 1 addition & 3 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
return fmt.Errorf("list sub packages: %w", err)
}

// Set-up testStore.
// Use a proxyWriter for stdout so that filetests can plug in a writer when
// necessary.
// Set up options to run tests.
stdout := goio.Discard
if cfg.verbose {
stdout = io.Out()
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 @@ -61,7 +61,7 @@ func TestFiles(t *testing.T) {
subTestName := path[len("files/"):]
if strings.HasSuffix(path, "_long.gno") && testing.Short() {
t.Run(subTestName, func(t *testing.T) {
t.Log("skipping in -short")
t.Skip("skipping in -short")
})
return nil
}
Expand Down
29 changes: 14 additions & 15 deletions gnovm/pkg/test/filetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func (opts *TestOptions) runFiletest(filename string, source []byte) (string, er
// multiple mismatches occurred.
updated := false
var returnErr error
// verifies the content against dir.Content; if different, either updates
// dir.Content or appends a new returnErr.
// match verifies the content against dir.Content; if different,
// either updates dir.Content (for opts.Sync) or appends a new returnErr.
match := func(dir *Directive, actual string) {
// remove end-of-line spaces, as these are removed from `fmt` in the filetests anyway.
// Remove end-of-line spaces, as these are removed from `fmt` in the filetests anyway.
actual = reEndOfLineSpaces.ReplaceAllString(actual, "\n")
if dir.Content != actual {
if opts.Sync {
Expand All @@ -93,7 +93,7 @@ func (opts *TestOptions) runFiletest(filename string, source []byte) (string, er

// First, check if we have an error, whether we're supposed to get it.
if result.Error != "" {
// ensure this error was supposed to happen.
// Ensure this error was supposed to happen.
errDirective := dirs.First(DirectiveError)
if errDirective == nil {
return "", fmt.Errorf("unexpected panic: %s\noutput:\n%s\nstack:\n%v",
Expand Down Expand Up @@ -185,7 +185,7 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
return runResult{Error: err.Error()}
}

Check warning on line 186 in gnovm/pkg/test/filetest.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/test/filetest.go#L184-L186

Added lines #L184 - L186 were not covered by tests

// reset and start capturing stdout.
// Reset and start capturing stdout.
opts.filetestBuffer.Reset()
revert := opts.outWriter.tee(&opts.filetestBuffer)
defer revert()
Expand All @@ -209,10 +209,10 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
}
}()

// use last element after / (works also if slash is missing)
// Use last element after / (works also if slash is missing).
pkgName := gno.Name(pkgPath[strings.LastIndexByte(pkgPath, '/')+1:])
if !gno.IsRealmPath(pkgPath) {
// simple case - pure package.
// Simple case - pure package.
pn := gno.NewPackageNode(pkgName, pkgPath, &gno.FileSet{})
pv := pn.NewPackage()
m.Store.SetBlockNode(pn)
Expand All @@ -222,10 +222,10 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
m.RunFiles(n)
m.RunStatement(gno.S(gno.Call(gno.X("main"))))
} else {
// realm case.
// Realm case.
gno.DisableDebug() // until main call.

// remove filetest from name, as that can lead to the package not being
// Remove filetest from name, as that can lead to the package not being
// parsed correctly when using RunMemPackage.
filename = strings.ReplaceAll(filename, "_filetest", "")

Expand All @@ -242,9 +242,9 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
}
orig, tx := m.Store, m.Store.BeginTransaction(nil, nil)
m.Store = tx
// run decls and init functions.
// Run decls and init functions.
m.RunMemPackage(memPkg, true)
// clear store cache and reconstruct machine from committed info
// Clear store cache and reconstruct machine from committed info
// (mimicking on-chain behaviour).
tx.Write()
m.Store = orig
Expand Down Expand Up @@ -377,7 +377,7 @@ func ParseDirectives(source io.Reader) (Directives, error) {
continue
}

// Find if there is a colon (indicating a possible directive)
// Find if there is a colon (indicating a possible directive).
subm := reDirectiveLine.FindStringSubmatch(comment)
switch {
case subm == nil:
Expand All @@ -390,10 +390,9 @@ func ParseDirectives(source io.Reader) (Directives, error) {
last := &parsed[len(parsed)-1]
if last.Name == "" {
last.Content += txt
continue
} else {
last.Content += comment + "\n"
}

Check warning on line 395 in gnovm/pkg/test/filetest.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/test/filetest.go#L390-L395

Added lines #L390 - L395 were not covered by tests
// append to last line's content.
last.Content += comment + "\n"
case subm[1] != "": // output directive, with content on newlines
parsed = append(parsed, Directive{Name: subm[1]})
default: // subm[2] != "", all caps
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Store(
}

// gonative exceptions.
// these are values available using gonative; eventually they should all be removed.
// These are values available using gonative; eventually they should all be removed.
switch pkgPath {
case "os":
pkg := gno.NewPackageNode("os", pkgPath, nil)
Expand Down Expand Up @@ -160,7 +160,7 @@ func Store(
}
db := memdb.NewMemDB()
baseStore = dbadapter.StoreConstructor(db, storetypes.StoreOptions{})
// make a new store
// Make a new store.
resStore = gno.NewStore(nil, baseStore, baseStore)
resStore.SetPackageGetter(getPackage)
resStore.SetNativeStore(teststdlibs.NativeStore)
Expand Down
33 changes: 19 additions & 14 deletions gnovm/pkg/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const (
DefaultHeight = 123
// DefaultTimestamp is the Timestamp value used by default in [Context].
DefaultTimestamp = 1234567890
// DefaultCaller is the result of gno.DerivePkgAddr("user1.gno")
// DefaultCaller is the result of gno.DerivePkgAddr("user1.gno"),
// used as the default caller in [Context].
DefaultCaller crypto.Bech32Address = "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
)

Expand Down Expand Up @@ -169,35 +170,39 @@ func (p *proxyWriter) tee(w io.Writer) (revert func()) {
}
}

func Test(
memPkg *gnovm.MemPackage,
fsDir string, // directory on filesystem of package; used for Sync
opts *TestOptions,
) error {
// Test runs tests on the specified memPkg.
// fsDir is the directory on filesystem of package; it's used in case opts.Sync
// is enabled, and points to the directory where the files are contained if they
// are to be updated.
// opts is a required set of options, which is often shared among different
// tests; you can use [NewTestOptions] for a common base configuration.
func Test(memPkg *gnovm.MemPackage, fsDir string, opts *TestOptions) error {
opts.outWriter.w = opts.Output

var errs error

// Stands for "test", "integration test", and "filetest".
// "integration test" are the test files with `package xxx_test` (they are
// not necessarily integration tests, it's just for our internal reference.)
tset, itset, itfiles, ftfiles := parseMemPackageTests(opts.TestStore, memPkg)

// testing with *_test.gno
// Testing with *_test.gno
if len(tset.Files)+len(itset.Files) > 0 {
// create a common cw/gs for both the `pkg` tests as well as the `pkg_test`
// tests. this allows us to "export" symbols from the pkg tests and
// Create a common cw/gs for both the `pkg` tests as well as the `pkg_test`
// tests. This allows us to "export" symbols from the pkg tests and
// import them from the `pkg_test` tests.
cw := opts.BaseStore.CacheWrap()
gs := opts.TestStore.BeginTransaction(cw, cw)

// run test files in pkg
// Run test files in pkg.
if len(tset.Files) > 0 {
err := opts.runTestFiles(memPkg, tset, cw, gs)
if err != nil {
errs = multierr.Append(errs, err)
}
}

// test xxx_test pkg
// Test xxx_test pkg.
if len(itset.Files) > 0 {
itPkg := &gnovm.MemPackage{
Name: memPkg.Name + "_test",
Expand All @@ -212,7 +217,7 @@ func Test(
}
}

// testing with *_filetest.gno
// Testing with *_filetest.gno.
if len(ftfiles) > 0 {
filter := splitRegexp(opts.RunFlag)
for _, testFile := range ftfiles {
Expand Down Expand Up @@ -377,7 +382,7 @@ func (opts *TestOptions) runTestFiles(
return errs
}

// mirror of stdlibs/testing.Report
// report is a mirror of Gno's stdlibs/testing.Report.
type report struct {
Failed bool
Skipped bool
Expand Down Expand Up @@ -406,7 +411,7 @@ func loadTestFuncs(pkgName string, tfiles *gno.FileSet) (rt []testFunc) {
return
}

// parses test files (skipping filetests) in the memPkg.
// parseMemPackageTests parses test files (skipping filetests) in the memPkg.
func parseMemPackageTests(store gno.Store, memPkg *gnovm.MemPackage) (tset, itset *gno.FileSet, itfiles, ftfiles []*gnovm.MemFile) {
tset = &gno.FileSet{}
itset = &gno.FileSet{}
Expand Down

0 comments on commit 606a741

Please sign in to comment.