Repository navigation
Expand file tree
/
Copy pathcommon.go
More file actions
81 lines (68 loc) · 2.2 KB
/
Copy pathcommon.go
File metadata and controls
81 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package devtest
import (
"context"
"log/slog"
"strings"
"go.opentelemetry.io/otel/trace"
"github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/log/logfilter"
"github.com/ethereum-optimism/optimism/op-service/testreq"
)
// CommonT is a subset of testing.T, extended with a few common utils.
// This interface should not be used directly. Instead, use T in test-scope, or P when operating at package level.
//
// This CommonT interface is minimal enough such that it can be implemented by tooling,
// and a *testing.T can be used with minimal wrapping.
type CommonT interface {
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
SkipNow()
TempDir() string
TempDirWithPrefix(prefix string) string
Cleanup(fn func())
Log(args ...any)
Logf(format string, args ...any)
Helper()
Name() string
Logger() log.Logger
Tracer() trace.Tracer
Ctx() context.Context
Require() *testreq.Assertions
}
func tempDirPattern(prefix string) string {
prefix = strings.NewReplacer("/", "-", "\\", "-", " ", "-", "_", "-").Replace(strings.TrimSpace(prefix))
prefix = strings.Trim(prefix, "-")
if prefix == "" {
prefix = "op-dev"
}
return prefix + "-*"
}
type testScopeCtxKeyType struct{}
// testScopeCtxKey is a key added to the test-context to identify the test-scope.
var testScopeCtxKey = testScopeCtxKeyType{}
// testScopeValue wraps a string to implement slog.LogValuer for context handling
type testScopeValue string
func (t testScopeValue) LogValue() slog.Value {
return slog.StringValue(string(t))
}
// TestScope retrieves the test-scope from the context
func TestScope(ctx context.Context) string {
scope := ctx.Value(testScopeCtxKey)
if scope == nil {
return ""
}
if scopeVal, ok := scope.(testScopeValue); ok {
return string(scopeVal)
}
return ""
}
// AddTestScope combines the sub-scope with the test-scope of the context,
// and returns a context with the updated scope value.
func AddTestScope(ctx context.Context, scope string) context.Context {
prev := TestScope(ctx)
newScope := testScopeValue(prev + "/" + scope)
ctx = logfilter.AddLogAttrToContext(ctx, "scope", newScope)
return context.WithValue(ctx, testScopeCtxKey, newScope)
}