Skip to content

Commit

Permalink
internal: adapt to testing.TB.Context in Go 1.24
Browse files Browse the repository at this point in the history
Thanks to Roger's proposal at https://go.dev/issue/36532,
Go at master just gained a new method in the testing package:

    pkg testing, type TB interface, Context() context.Context

Our cuetdtest.M and cuetxtar.Test types implement testing.TB
as of Go 1.23, but with the addition of this new method,
our own `Context() cue.Context` methods conflict with the above.

Rename our Context methods to CueContext, which resolves this conflict
and is likely less confusing in the context of a testing.TB anyway.

Add testing.TB interface implementation assertions in both packages.
This way, the next time Go's testing.TB interface grows,
the errors will directly point to these two packages as build errors
as opposed to pointing to the dozen of downstream test packages.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I51869b22f42209e133056adca35a6970e47facc9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199833
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mvdan committed Aug 22, 2024
1 parent 186b6e2 commit b5ac87c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cue/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Benchmark(b *testing.B) {
b.Run(m.Name(), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctx := m.Context()
ctx := m.CueContext()
value := ctx.BuildInstance(cuetxtar.Load(a, b.TempDir())[0])
value.Validate()
}
Expand Down
28 changes: 14 additions & 14 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
func getValue(t *cuetdtest.M, body string) cue.Value {
t.Helper()

ctx := t.Context()
ctx := t.CueContext()
return ctx.CompileString(body, cue.Filename("test"))
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func TestAPI(t *testing.T) {
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
t.TODO_V3()

ctx := t.Context()
ctx := t.CueContext()

valIn := mustCompile(t, ctx, tc.input)
valOut := tc.fun(valIn)
Expand Down Expand Up @@ -963,7 +963,7 @@ func TestFieldType(t *testing.T) {

func TestLookup(t *testing.T) {
cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
val := mustCompile(t, ctx, `
#V: {
x: int
Expand Down Expand Up @@ -1168,7 +1168,7 @@ providers: {

func TestFillPath(t *testing.T) {
cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

val := ctx.BuildExpr(ast.NewStruct("bar", ast.NewString("baz")))
if err := val.Err(); err != nil {
Expand Down Expand Up @@ -1403,7 +1403,7 @@ func TestFillPathError(t *testing.T) {

for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
v := mustCompile(t, ctx, tc.in)
v = v.FillPath(tc.path, tc.x)

Expand Down Expand Up @@ -1659,7 +1659,7 @@ func TestAllows(t *testing.T) {
if tc.todo_nosharing {
t.TODO_NoSharing()
}
ctx := t.Context()
ctx := t.CueContext()
v := mustCompile(t, ctx, tc.in)
v = v.LookupPath(path)

Expand Down Expand Up @@ -1722,7 +1722,7 @@ func TestValue_LookupDef(t *testing.T) {

for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, tc.def, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
v := mustCompile(t, ctx, tc.in)
v = v.LookupDef(tc.def)
got := fmt.Sprint(v)
Expand Down Expand Up @@ -2336,7 +2336,7 @@ func TestEquals(t *testing.T) {
}}
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

a := mustCompile(t, ctx, tc.a)
b := mustCompile(t, ctx, tc.b)
Expand Down Expand Up @@ -2495,7 +2495,7 @@ func TestValidate(t *testing.T) {
t.TODO_V3()
}

ctx := t.Context()
ctx := t.CueContext()
val := ctx.CompileString(tc.in, cue.Filename("validate"))
err := val.Validate(tc.opts...)
if gotErr := err != nil; gotErr != tc.err {
Expand Down Expand Up @@ -2528,7 +2528,7 @@ func TestPath(t *testing.T) {
}
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, strings.Join(tc, "."), func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
val := mustCompile(t, ctx, config)

v := val.Lookup(tc[0])
Expand Down Expand Up @@ -2697,7 +2697,7 @@ func TestValueDoc(t *testing.T) {
`

cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
v1 := mustCompile(t, ctx, config)
v2 := mustCompile(t, ctx, config2)
both := v1.Unify(v2)
Expand Down Expand Up @@ -3168,7 +3168,7 @@ func TestReferencePath(t *testing.T) {
}}
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

val := ctx.CompileString(tc.input, cue.Filename("in"))
v := val.Lookup("v", "w", "x")
Expand Down Expand Up @@ -3263,7 +3263,7 @@ a: x: y: z: "x"`,
t.TODO_V3()
}

c := t.Context()
c := t.CueContext()
v := c.CompileString(tc.value)
v = v.LookupPath(cue.ParsePath("a"))
pos := v.Pos().String()
Expand Down Expand Up @@ -3519,7 +3519,7 @@ func TestPathCorrection(t *testing.T) {
continue
}
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

// don't use mustCompile because some test cases here need to
// inspect the value error.
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestDecode(t *testing.T) {
}
cfg.Strict = t.HasTag("strict")

ctx := t.Context()
ctx := t.CueContext()

fsys, err := txtar.FS(t.Archive)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/dep/dep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestVisit(t *testing.T) {
}

test.Run(t, func(t *cuetxtar.Test) {
val := t.Context().BuildInstance(t.Instance())
val := t.CueContext().BuildInstance(t.Instance())
if val.Err() != nil {
t.Fatal(val.Err())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/core/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func formatNode(t *testing.T, n ast.Node) []byte {
// different from parsed or evaluated CUE, such as having Vertex values.
func TestGenerated(t *testing.T) {
cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

testCases := []struct {
in func(ctx *adt.OpContext) (adt.Expr, error)
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestFromAPI(t *testing.T) {
// Issue #1204
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, "", func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()

v := ctx.BuildExpr(tc.expr)

Expand Down
2 changes: 1 addition & 1 deletion internal/core/export/self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestSelfContained(t *testing.T) {
}

test.Run(t, func(t *cuetxtar.Test) {
r := t.Context()
r := t.CueContext()

a := t.Instances()

Expand Down
8 changes: 6 additions & 2 deletions internal/cuetdtest/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ type M struct {
version internal.EvaluatorVersion
}

// Ensure that M always implements testing.TB.
// Note that testing.TB may gain new methods in future Go releases.
var _ testing.TB = (*M)(nil)

func (t *M) Name() string { return t.name }
func (t *M) Fallback() string { return t.fallback }
func (t *M) IsDefault() bool { return t.name == DefaultVersion }

func (t *M) Context() *cue.Context {
func (t *M) CueContext() *cue.Context {
ctx := cuecontext.New()
r := (*runtime.Runtime)(ctx)
r.SetVersion(t.version)
Expand All @@ -49,7 +53,7 @@ func (t *M) Context() *cue.Context {

// Runtime creates a runtime that is configured according to the matrix.
func (t *M) Runtime() *runtime.Runtime {
return (*runtime.Runtime)(t.Context())
return (*runtime.Runtime)(t.CueContext())
}

const DefaultVersion = "v2"
Expand Down
12 changes: 8 additions & 4 deletions internal/cuetxtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type Test struct {
hasGold bool
}

// Ensure that Test always implements testing.TB.
// Note that testing.TB may gain new methods in future Go releases.
var _ testing.TB = (*Test)(nil)

// Write implements [io.Writer] by writing to the output for the test,
// which will be tested against the main golden file.
func (t *Test) Write(b []byte) (n int, err error) {
Expand Down Expand Up @@ -345,13 +349,13 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {

// Runtime returns a new runtime based on the configuration of the test.
func (t *Test) Runtime() *runtime.Runtime {
return (*runtime.Runtime)(t.Context())
return (*runtime.Runtime)(t.CueContext())
}

// Context returns a new cue.Context based on the configuration of the test.
func (t *Test) Context() *cue.Context {
// CueContext returns a new cue.CueContext based on the configuration of the test.
func (t *Test) CueContext() *cue.Context {
if t.M != nil {
return t.M.Context()
return t.M.CueContext()
}
return cuecontext.New()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ a: x: "hello"
}}
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, tc.name, func(t *cuetdtest.M) {
ctx := t.Context()
ctx := t.CueContext()
// it is not fatal if x or y contain errors: some test cases
// rely on interacting with such errors.
x := ctx.CompileString(tc.x, cue.Filename("x"))
Expand Down
2 changes: 1 addition & 1 deletion tools/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestFlow(t *testing.T) {
}

test.Run(t, func(t *cuetxtar.Test) {
v := t.Context().BuildInstance(t.Instance())
v := t.CueContext().BuildInstance(t.Instance())
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}
Expand Down
2 changes: 1 addition & 1 deletion tools/trim/trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTrimFiles(t *testing.T) {
test.Run(t, func(t *cuetxtar.Test) {

a := t.Instance()
ctx := t.Context()
ctx := t.CueContext()
val := ctx.BuildInstance(a)
// Note: don't require val.Err to be nil because there are deliberate
// errors in some tests, to ensure trim still works even with some errors.
Expand Down

0 comments on commit b5ac87c

Please sign in to comment.