Skip to content

Commit

Permalink
Apply code-review changes
Browse files Browse the repository at this point in the history
Co-Authored-By: Marc Vertes <[email protected]>
  • Loading branch information
thehowl and mvertes committed Nov 25, 2024
1 parent 37eecb1 commit 9f128d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
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
10 changes: 5 additions & 5 deletions gnovm/pkg/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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 @@ -128,7 +128,7 @@ func Store(
return pkg, pkg.NewPackage()

Check warning on line 128 in gnovm/pkg/test/imports.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/test/imports.go#L124-L128

Added lines #L124 - L128 were not covered by tests
}

// load normal stdlib.
// Load normal stdlib.
pn, pv = loadStdlib(rootDir, pkgPath, store, stdout)
if pn != nil {
return
Expand Down Expand Up @@ -157,7 +157,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 All @@ -166,9 +166,9 @@ func Store(

func loadStdlib(rootDir, pkgPath string, store gno.Store, stdout io.Writer) (*gno.PackageNode, *gno.PackageValue) {
dirs := [...]string{
// normal stdlib path.
// Normal stdlib path.
filepath.Join(rootDir, "gnovm", "stdlibs", pkgPath),
// override path. definitions here override the previous if duplicate.
// Override path. Definitions here override the previous if duplicate.
filepath.Join(rootDir, "gnovm", "tests", "stdlibs", pkgPath),
}
files := make([]string, 0, 32) // pre-alloc 32 as a likely high number of files
Expand Down
21 changes: 12 additions & 9 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 @@ -179,25 +180,27 @@ func Test(
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 +215,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 +380,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 +409,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 9f128d5

Please sign in to comment.