Skip to content

Commit 6517d04

Browse files
feat: allow existing logger from context (#898)
We should reuse an existing context logger if in test context. This will allow test to setup act with a null logger to assert log messages. Co-authored-by: Markus Wolf <[email protected]> Co-authored-by: Markus Wolf <[email protected]>
1 parent f726339 commit 6517d04

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pkg/common/testflag.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package common
2+
3+
import (
4+
"context"
5+
)
6+
7+
type testFlagContextKey string
8+
9+
const testFlagContextKeyVal = testFlagContextKey("test-context")
10+
11+
// TestContext returns whether the context has the test flag set
12+
func TestContext(ctx context.Context) bool {
13+
val := ctx.Value(testFlagContextKeyVal)
14+
return val != nil
15+
}
16+
17+
// WithTextContext sets the test flag in the context
18+
func WithTestContext(ctx context.Context) context.Context {
19+
return context.WithValue(ctx, testFlagContextKeyVal, true)
20+
}

pkg/runner/logger.go

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func WithJobLogger(ctx context.Context, jobName string, secrets map[string]strin
4848
nextColor++
4949

5050
logger := logrus.New()
51+
if common.TestContext(ctx) {
52+
fieldLogger := common.Logger(ctx)
53+
if fieldLogger != nil {
54+
logger = fieldLogger.(*logrus.Logger)
55+
}
56+
}
5157
logger.SetFormatter(formatter)
5258
logger.SetOutput(os.Stdout)
5359
logger.SetLevel(logrus.GetLevel())

0 commit comments

Comments
 (0)