Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd/gno): move test output within gno code #1594

Merged
merged 4 commits into from
Jan 31, 2024
Merged
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
38 changes: 5 additions & 33 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,22 +499,15 @@
m.RunFiles(n)

for _, test := range testFuncs.Tests {
if verbose {
io.ErrPrintfln("=== RUN %s", test.Name)
}

testFuncStr := fmt.Sprintf("%q", test.Name)

startedAt := time.Now()
eval := m.Eval(gno.Call("runtest", testFuncStr))
duration := time.Since(startedAt)
dstr := fmtDuration(duration)

ret := eval[0].GetString()
if ret == "" {
err := errors.New("failed to execute unit test: %q", test.Name)
errs = multierr.Append(errs, err)
io.ErrPrintfln("--- FAIL: %s (%v)", test.Name, duration)
io.ErrPrintfln("--- FAIL: %s [internal gno testing error]", test.Name)

Check warning on line 510 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L510

Added line #L510 was not covered by tests
continue
}

Expand All @@ -523,30 +516,13 @@
err = json.Unmarshal([]byte(ret), &rep)
if err != nil {
errs = multierr.Append(errs, err)
io.ErrPrintfln("--- FAIL: %s (%s)", test.Name, dstr)
io.ErrPrintfln("--- FAIL: %s [internal gno testing error]", test.Name)

Check warning on line 519 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L519

Added line #L519 was not covered by tests
continue
}

switch {
case rep.Filtered:
io.ErrPrintfln("--- FILT: %s", test.Name)
// noop
case rep.Skipped:
if verbose {
io.ErrPrintfln("--- SKIP: %s", test.Name)
}
case rep.Failed:
if rep.Failed {
err := errors.New("failed: %q", test.Name)
errs = multierr.Append(errs, err)
io.ErrPrintfln("--- FAIL: %s (%s)", test.Name, dstr)
default:
if verbose {
io.ErrPrintfln("--- PASS: %s (%s)", test.Name, dstr)
}
}

if rep.Output != "" && (verbose || rep.Failed) {
io.ErrPrintfln("output: %s", rep.Output)
}

if printRuntimeMetrics {
Expand Down Expand Up @@ -574,12 +550,8 @@

// mirror of stdlibs/testing.Report
type report struct {
Name string
Verbose bool
Failed bool
Skipped bool
Filtered bool
Output string
Failed bool
Skipped bool
}

var testmainTmpl = template.Must(template.New("testmain").Parse(`
Expand Down
52 changes: 29 additions & 23 deletions gnovm/cmd/gno/testdata/gno_test/flag_run.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,42 @@ gno test ./run_test.gno

gno test -verbose .

stdout '=== RUN TestRun/hello'
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
stderr '=== RUN TestRun/hi_you'
stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

gno test -verbose -run .* .

stdout '=== RUN TestRun/hello'
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
stderr '=== RUN TestRun/hi_you'
stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

gno test -verbose -run NotExists .

! stdout .+
stderr '=== RUN TestRun'
stderr '--- FILT: TestRun'
! stderr '=== RUN TestRun'

gno test -verbose -run .*/hello .

stdout '=== RUN TestRun/hello'
! stdout '=== RUN TestRun/hi_you'
! stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
! stderr '=== RUN TestRun/hi_you'
! stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

gno test -verbose -run .*/hi .

! stdout '=== RUN TestRun/hello'
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'
! stdout .+
! stderr '=== RUN TestRun/hello'
stderr '=== RUN TestRun/hi_you'
stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

Expand All @@ -50,25 +53,28 @@ stderr '--- PASS: TestRun'

gno test -verbose -run Run/.* .

stdout '=== RUN TestRun/hello'
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
stderr '=== RUN TestRun/hi_you'
stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

gno test -verbose -run Run/ .

stdout '=== RUN TestRun/hello'
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
stderr '=== RUN TestRun/hi_you'
stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

gno test -verbose -run Run/hello .

stdout '=== RUN TestRun/hello'
! stdout '=== RUN TestRun/hi_you'
! stdout '=== RUN TestRun/hi_me'
! stdout .+
stderr '=== RUN TestRun/hello'
! stderr '=== RUN TestRun/hi_you'
! stderr '=== RUN TestRun/hi_me'
stderr '=== RUN TestRun'
stderr '--- PASS: TestRun'

Expand Down
20 changes: 20 additions & 0 deletions gnovm/cmd/gno/testdata/gno_test/panic.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test panic output in a test

! gno test .

! stdout .+
stderr '--- FAIL: TestPanic'
stderr 'panic: hello world'
stderr 'FAIL'

-- panic.gno --
package valid

-- panic_test.gno --
package valid

import "testing"

func TestPanic(t *testing.T) {
panic("hello world")
}
62 changes: 62 additions & 0 deletions gnovm/cmd/gno/testdata/gno_test/recover.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Test recovering

gno test -verbose -run 'TestRecover$' .

! stdout .+
stderr 'recovered bad panic!'
stderr '--- PASS'
stderr 'ok '

gno test -verbose -run 'TestRecoverSkip$' .

! stdout .+
stderr 'skipped'
! stderr 'recovered.*testing.Recover'
stderr '--- SKIP'
stderr 'ok '

gno test -verbose -run 'TestBadRecoverSkip$' .

! stdout .+
# should contain warning about using testing.Recover
stderr 'recovered.*testing.Recover'
# the test will still be marked as skipped
stderr '--- SKIP'
stderr 'ok '

-- recov.gno --
package recov

-- recov_test.gno --
package recov

import "testing"

func TestRecover(t *testing.T) {
defer func() {
err := testing.Recover()
t.Log("recovered", err)
}()

panic("bad panic!")
}

func TestRecoverSkip(t *testing.T) {
defer func() {
err := testing.Recover()
t.Log("recovered", err)
}()

t.Skip("skipped")
panic("bad panic!")
}

func TestBadRecoverSkip(t *testing.T) {
defer func() {
err := recover()
t.Log("recovered", err)
}()

t.Skip("skipped")
panic("bad panic!")
}
24 changes: 24 additions & 0 deletions gnovm/cmd/gno/testdata/gno_test/skip.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Test skipping a test

gno test -verbose .

! stdout .+
stderr 'Hey'
stderr 'I.m on strike!'
! stderr 'this shouldn.t print'
stderr '--- SKIP: TestSkip'
stderr 'ok '

-- skip.gno --
package skip

-- skip_test.gno --
package skip

import "testing"

func TestSkip(t *testing.T) {
t.Log("Hey")
t.Skip("I'm on strike!")
t.Log("so this shouldn't print")
}
14 changes: 2 additions & 12 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,8 @@

// mirror of stdlibs/testing.Report
var report struct {
Name string
Verbose bool
Failed bool
Skipped bool
Filtered bool
Output string
Skipped bool
Failed bool

Check warning on line 395 in gnovm/pkg/gnolang/machine.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/machine.go#L394-L395

Added lines #L394 - L395 were not covered by tests
}
err := json.Unmarshal([]byte(ret), &report)
if err != nil {
Expand All @@ -405,17 +401,11 @@
}

switch {
case report.Filtered:
// noop
case report.Skipped:
t.SkipNow()
case report.Failed:
t.Fail()
}

if report.Output != "" && (report.Verbose || report.Failed) {
t.Log(report.Output)
}
})
}

Expand Down
18 changes: 18 additions & 0 deletions gnovm/stdlibs/native.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading