Skip to content

Commit

Permalink
[ENC-2013] Add support for caputring test logs (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
DomBlack authored Jan 24, 2024
1 parent ab795bd commit c2c4e03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion overlay/src/testing/encore_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ "unsafe"

// encoreStartTest is called when a test starts running. This allows Encore's testing framework to
// isolate behavior between different tests on global state. It is linked to the Encore runtime via go:linkname.
func encoreStartTest(t *T)
func encoreStartTest(t *T, fn func(t *T))

// encoreEndTest is called when a test ends. This allows Encore's testing framework to clear down any state from the test
// and to perform any assertions on that state that it needs to. It is linked to the Encore runtime via go:linkname.
Expand All @@ -20,3 +20,7 @@ func encorePauseTest(t *T)
// encoreResumeTest is called when a test is resumed after being paused. This allows Encore's testing framework to clear down any state from the test
// and to perform any assertions on that state that it needs to. It is linked to the Encore runtime via go:linkname.
func encoreResumeTest(t *T)

// encoreTestLog is called when a test logs a line. This allows Encore's testing framework to capture the log output
// and emit that log output to the test trace.
func encoreTestLog(line string, frameSkip int)
6 changes: 5 additions & 1 deletion overlay/src/testing/encore_noapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package testing
// isolate behavior between different tests on global state.
//
// This implementation is simply a no-op as it's used when the tests are not being run against an Encore application
func encoreStartTest(t *T) {}
func encoreStartTest(t *T, fn func(t *T)) {}

// encoreEndTest is called when a test ends. This allows Encore's testing framework to clear down any state from the test
// and to perform any assertions on that state that it needs to. It is linked to the Encore runtime via go:linkname.
Expand All @@ -22,3 +22,7 @@ func encorePauseTest(t *T) {}
// encoreResumeTest is called when a test is resumed after being paused. This allows Encore's testing framework to clear down any state from the test
// and to perform any assertions on that state that it needs to. It is linked to the Encore runtime via go:linkname.
func encoreResumeTest(t *T) {}

// encoreTestLog is called when a test logs a line. This allows Encore's testing framework to capture the log output
// and emit that log output to the test trace.
func encoreTestLog(line string, frameSkip int) {}
21 changes: 17 additions & 4 deletions patches/testing_patch.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ test runs against global state. This patch introduces a per test level callback
test and not have state between tests interfere with each other.

diff --git a/src/testing/testing.go b/src/testing/testing.go
index fc34cbf28b..37f54b482b 100644
index fcf7048f23..ec628da6ed 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -1380,10 +1380,14 @@ func (t *T) Parallel() {
@@ -765,6 +765,12 @@ func (c *common) frameSkip(skip int) runtime.Frame {
// and inserts the final newline if needed and indentation spaces for formatting.
// This function must be called with c.mu held.
func (c *common) decorate(s string, skip int) string {
+ // allow encore to capture the log as well;
+ // we do it here so that all the branches which result in a log message
+ // during tests get captured, however we do it _before_ the liens get modified
+ // and indented.
+ encoreTestLog(s, skip+1)
+
frame := c.frameSkip(skip)
file := frame.File
line := frame.Line
@@ -1399,10 +1405,14 @@ func (t *T) Parallel() {
}
running.Delete(t.name)

Expand All @@ -24,11 +37,11 @@ index fc34cbf28b..37f54b482b 100644
if t.chatty != nil {
t.chatty.Updatef(t.name, "=== CONT %s\n", t.name)
}
@@ -1432,12 +1436,14 @@ var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit
@@ -1451,12 +1461,14 @@ var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit

func tRunner(t *T, fn func(t *T)) {
t.runner = callerName(0)
+ encoreStartTest(t)
+ encoreStartTest(t, fn)

// When this goroutine is done, either because fn(t)
// returned normally or because a test failure triggered
Expand Down

0 comments on commit c2c4e03

Please sign in to comment.