Skip to content

Commit 3ae664e

Browse files
committed
internal/cuetdtest: move testing.T out of M
It's common to run multiple subtests inside a single point in the matrix, but it turns out that's problematic: if one of those tests ends up failing by using `m.T`, it will fail the outer test, not the one that's currently running, ending up with the testing package printing "subtest may have called FailNow on a parent test". We could make sure to update `M.T` for each subtest, but that's problematic itself, because multiple subtests will share a given `*M` instance and if any of them happens to invoke `T.Parallel` then there's a race condition. Instead, make `M` just about the point in the matrix and add an explicit `*testing.T` argument to the methods that it has that do checks. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Id83ba0fac6c4db7780d4086820aed4190dd5bd65 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200260 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent e48fb9d commit 3ae664e

File tree

11 files changed

+162
-171
lines changed

11 files changed

+162
-171
lines changed

cue/attribute_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ func TestAttributes(t *testing.T) {
9696
out: "[]",
9797
}}
9898
for _, tc := range testCases {
99-
cuetdtest.FullMatrix.Run(t, tc.path, func(t *cuetdtest.M) {
100-
v := getValue(t, config).LookupPath(cue.ParsePath(tc.path))
99+
cuetdtest.FullMatrix.Run(t, tc.path, func(t *testing.T, m *cuetdtest.M) {
100+
v := getValue(m, config).LookupPath(cue.ParsePath(tc.path))
101101
a := v.Attributes(tc.flags)
102102
got := fmt.Sprint(a)
103103
if got != tc.out {
104104
t.Errorf("got %v; want %v", got, tc.out)
105105
}
106-
107106
})
108107
}
109108
}
@@ -137,8 +136,8 @@ func TestAttributeErr(t *testing.T) {
137136
err: errors.New(`attribute "bar" does not exist`),
138137
}}
139138
for _, tc := range testCases {
140-
cuetdtest.FullMatrix.Run(t, tc.path+"-"+tc.attr, func(t *cuetdtest.M) {
141-
v := getValue(t, config).Lookup("a", tc.path)
139+
cuetdtest.FullMatrix.Run(t, tc.path+"-"+tc.attr, func(t *testing.T, m *cuetdtest.M) {
140+
v := getValue(m, config).Lookup("a", tc.path)
142141
a := v.Attribute(tc.attr)
143142
err := a.Err()
144143
if !cmpError(err, tc.err) {
@@ -152,8 +151,8 @@ func TestAttributeName(t *testing.T) {
152151
const config = `
153152
a: 0 @foo(a,b,c=1) @bar()
154153
`
155-
cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
156-
v := getValue(t, config).Lookup("a")
154+
cuetdtest.FullMatrix.Do(t, func(t *testing.T, m *cuetdtest.M) {
155+
v := getValue(m, config).Lookup("a")
157156
a := v.Attribute("foo")
158157
if got, want := a.Name(), "foo"; got != want {
159158
t.Errorf("got %v; want %v", got, want)
@@ -210,8 +209,8 @@ func TestAttributeString(t *testing.T) {
210209
err: errors.New("field does not exist"),
211210
}}
212211
for _, tc := range testCases {
213-
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *cuetdtest.M) {
214-
v := getValue(t, config).Lookup("a", tc.path)
212+
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *testing.T, m *cuetdtest.M) {
213+
v := getValue(m, config).Lookup("a", tc.path)
215214
a := v.Attribute(tc.attr)
216215
got, err := a.String(tc.pos)
217216
if !cmpError(err, tc.err) {
@@ -221,6 +220,7 @@ func TestAttributeString(t *testing.T) {
221220
t.Errorf("str: got %v; want %v", got, tc.str)
222221
}
223222
})
223+
224224
}
225225
}
226226

@@ -270,8 +270,8 @@ func TestAttributeArg(t *testing.T) {
270270
raw: " s= spaces in value ",
271271
}}
272272
for _, tc := range testCases {
273-
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%d", tc.pos), func(t *cuetdtest.M) {
274-
v := getValue(t, config).Lookup("a")
273+
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%d", tc.pos), func(t *testing.T, m *cuetdtest.M) {
274+
v := getValue(m, config).Lookup("a")
275275
a := v.Attribute("foo")
276276
key, val := a.Arg(tc.pos)
277277
raw := a.RawArg(tc.pos)
@@ -327,8 +327,8 @@ func TestAttributeInt(t *testing.T) {
327327
err: errors.New(`strconv.ParseInt: parsing "c=1": invalid syntax`),
328328
}}
329329
for _, tc := range testCases {
330-
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *cuetdtest.M) {
331-
v := getValue(t, config).Lookup("a", tc.path)
330+
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *testing.T, m *cuetdtest.M) {
331+
v := getValue(m, config).Lookup("a", tc.path)
332332
a := v.Attribute(tc.attr)
333333
got, err := a.Int(tc.pos)
334334
if !cmpError(err, tc.err) {
@@ -384,8 +384,8 @@ func TestAttributeFlag(t *testing.T) {
384384
err: errors.New("field does not exist"),
385385
}}
386386
for _, tc := range testCases {
387-
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *cuetdtest.M) {
388-
v := getValue(t, config).Lookup("a", tc.path)
387+
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *testing.T, m *cuetdtest.M) {
388+
v := getValue(m, config).Lookup("a", tc.path)
389389
a := v.Attribute(tc.attr)
390390
got, err := a.Flag(tc.pos, tc.flag)
391391
if !cmpError(err, tc.err) {
@@ -459,8 +459,8 @@ func TestAttributeLookup(t *testing.T) {
459459
err: errors.New("field does not exist"),
460460
}}
461461
for _, tc := range testCases {
462-
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *cuetdtest.M) {
463-
v := getValue(t, config).Lookup("a", tc.path)
462+
cuetdtest.FullMatrix.Run(t, fmt.Sprintf("%s.%s:%d", tc.path, tc.attr, tc.pos), func(t *testing.T, m *cuetdtest.M) {
463+
v := getValue(m, config).Lookup("a", tc.path)
464464
a := v.Attribute(tc.attr)
465465
got, _, err := a.Lookup(tc.pos, tc.key)
466466
if !cmpError(err, tc.err) {

cue/decode_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ func TestDecode(t *testing.T) {
241241
want: []interface{}{},
242242
}}
243243
for _, tc := range testCases {
244-
cuetdtest.FullMatrix.Run(t, tc.value, func(t *cuetdtest.M) {
245-
err := getValue(t, tc.value).Decode(tc.dst)
246-
checkFatal(t.T, err, tc.err, "init")
244+
cuetdtest.FullMatrix.Run(t, tc.value, func(t *testing.T, m *cuetdtest.M) {
245+
err := getValue(m, tc.value).Decode(tc.dst)
246+
checkFatal(t, err, tc.err, "init")
247247

248248
got := reflect.ValueOf(tc.dst).Elem().Interface()
249249
if diff := cmp.Diff(got, tc.want); diff != "" {
@@ -255,21 +255,21 @@ func TestDecode(t *testing.T) {
255255
}
256256

257257
func TestDecodeIntoCUEValue(t *testing.T) {
258-
cuetdtest.FullMatrix.Do(t, func(t *cuetdtest.M) {
258+
cuetdtest.FullMatrix.Do(t, func(t *testing.T, m *cuetdtest.M) {
259259
// We should be able to decode into a CUE value so we can
260260
// decode partially incomplete values into Go.
261261
// This test doesn't fit within the table used by TestDecode
262262
// because cue values aren't easily comparable with cmp.Diff.
263263
var st struct {
264264
X cue.Value `json:"x"`
265265
}
266-
err := getValue(t, `x: string`).Decode(&st)
266+
err := getValue(m, `x: string`).Decode(&st)
267267
qt.Assert(t, qt.IsNil(err))
268268
qt.Assert(t, qt.Equals(fmt.Sprint(st.X), "string"))
269269

270270
// Check we can decode into a top level value.
271271
var v cue.Value
272-
err = getValue(t, `int`).Decode(&v)
272+
err = getValue(m, `int`).Decode(&v)
273273
qt.Assert(t, qt.IsNil(err))
274274
qt.Assert(t, qt.Equals(fmt.Sprint(v), "int"))
275275
})

0 commit comments

Comments
 (0)