Skip to content

Commit 1227a83

Browse files
committed
pkg/path: use a subtest helper to test for each OS value
Using a subtest per OS helps so that "return" or "t.Skip" only affect the OS we're currently iterating on rather than all of them. This was already a problem with TestClean, whose t.Skip meant that we were only actually testing for os == Unix. A number of tests also conditionally appended to the test case list, such as appending extra Windows-only test cases when os == Windows. When doing that, append into a new local slice, as otherwise the append would also affect the next OS values. TestClean was broken once it tested all OS values as well; it modified some test case values when os == Windows, so make a copy of the test case slice first to ensure that's OK. While here, TestVolumeName only cares about Windows; simplify. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I9ce635a3034153618a779dd31db030ae76ce5fdc Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1170044 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 55c6db6 commit 1227a83

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

pkg/path/match_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func errp(e error) string {
9696
}
9797

9898
func TestMatch(t *testing.T) {
99-
for _, os := range []OS{Unix, Windows, Plan9} {
99+
testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
100100
for _, tt := range matchTests {
101101
pattern := tt.pattern
102102
s := tt.s
@@ -114,5 +114,5 @@ func TestMatch(t *testing.T) {
114114
pattern, s, os, ok, errp(err), tt.match, errp(tt.err))
115115
}
116116
}
117-
}
117+
})
118118
}

pkg/path/path_test.go

+38-33
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
package path
2020

2121
import (
22+
"fmt"
2223
"reflect"
2324
"runtime"
2425
"testing"
2526
)
2627

28+
func testEachOS(t *testing.T, list []OS, fn func(t *testing.T, os OS)) {
29+
for _, os := range list {
30+
t.Run(fmt.Sprintf("OS=%s", os), func(t *testing.T) {
31+
fn(t, os)
32+
})
33+
}
34+
}
35+
2736
type PathTest struct {
2837
path, result string
2938
}
@@ -101,8 +110,8 @@ var wincleantests = []PathTest{
101110
}
102111

103112
func TestClean(t *testing.T) {
104-
tests := cleantests
105-
for _, os := range []OS{Unix, Windows, Plan9} {
113+
testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
114+
tests := append([]PathTest{}, cleantests...) // TODO: replace with slices.Clone
106115
if os == Windows {
107116
for i := range tests {
108117
tests[i].result = FromSlash(tests[i].result, os)
@@ -132,7 +141,7 @@ func TestClean(t *testing.T) {
132141
t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
133142
}
134143
}
135-
}
144+
})
136145
}
137146

138147
func TestFromAndToSlash(t *testing.T) {
@@ -185,7 +194,7 @@ var winsplitlisttests = []SplitListTest{
185194
}
186195

187196
func TestSplitList(t *testing.T) {
188-
for _, os := range []OS{Unix, Windows, Plan9} {
197+
testEachOS(t, []OS{Unix, Windows, Plan9}, func(t *testing.T, os OS) {
189198
sep := getOS(os).ListSeparator
190199

191200
tests := []SplitListTest{
@@ -201,7 +210,7 @@ func TestSplitList(t *testing.T) {
201210
t.Errorf("SplitList(%#q, %q) = %#q, want %#q", test.list, os, l, test.result)
202211
}
203212
}
204-
}
213+
})
205214
}
206215

207216
type SplitTest struct {
@@ -230,21 +239,20 @@ var winsplittests = []SplitTest{
230239
}
231240

232241
func TestSplit(t *testing.T) {
233-
for _, os := range []OS{Windows, Unix} {
234-
var splittests []SplitTest
235-
splittests = unixsplittests
242+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
243+
tests := unixsplittests
236244
if os == Windows {
237-
splittests = append(splittests, winsplittests...)
245+
tests = append(tests, winsplittests...)
238246
}
239-
for _, test := range splittests {
247+
for _, test := range tests {
240248
pair := Split(test.path, os)
241249
d, f := pair[0], pair[1]
242250
if d != test.dir || f != test.file {
243251
t.Errorf("Split(%q, %q) = %q, %q, want %q, %q",
244252
test.path, os, d, f, test.dir, test.file)
245253
}
246254
}
247-
}
255+
})
248256
}
249257

250258
type JoinTest struct {
@@ -308,17 +316,18 @@ var winjointests = []JoinTest{
308316
}
309317

310318
func TestJoin(t *testing.T) {
311-
for _, os := range []OS{Unix, Windows} {
319+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
320+
tests := jointests
312321
if os == Windows {
313-
jointests = append(jointests, winjointests...)
322+
tests = append(tests, winjointests...)
314323
}
315-
for _, test := range jointests {
324+
for _, test := range tests {
316325
expected := FromSlash(test.path, os)
317326
if p := Join(test.elem, os); p != expected {
318327
t.Errorf("join(%q, %q) = %q, want %q", test.elem, os, p, expected)
319328
}
320329
}
321-
}
330+
})
322331
}
323332

324333
type ExtTest struct {
@@ -334,13 +343,13 @@ var exttests = []ExtTest{
334343
}
335344

336345
func TestExt(t *testing.T) {
337-
for _, os := range []OS{Unix, Windows} {
346+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
338347
for _, test := range exttests {
339348
if x := Ext(test.path, os); x != test.ext {
340349
t.Errorf("Ext(%q, %q) = %q, want %q", test.path, os, x, test.ext)
341350
}
342351
}
343-
}
352+
})
344353
}
345354

346355
var basetests = []PathTest{
@@ -370,7 +379,7 @@ var winbasetests = []PathTest{
370379

371380
func TestBase(t *testing.T) {
372381
tests := basetests
373-
for _, os := range []OS{Unix, Windows} {
382+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
374383
if os == Windows {
375384
// make unix tests work on windows
376385
for i := range tests {
@@ -384,7 +393,7 @@ func TestBase(t *testing.T) {
384393
t.Errorf("Base(%q, %q) = %q, want %q", test.path, os, s, test.result)
385394
}
386395
}
387-
}
396+
})
388397
}
389398

390399
var dirtests = []PathTest{
@@ -415,7 +424,7 @@ var windirtests = []PathTest{
415424
}
416425

417426
func TestDir(t *testing.T) {
418-
for _, os := range []OS{Unix, Windows} {
427+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
419428
tests := dirtests
420429
if os == Windows {
421430
// make unix tests work on windows
@@ -430,7 +439,7 @@ func TestDir(t *testing.T) {
430439
t.Errorf("Dir(%q, %q) = %q, want %q", test.path, os, s, test.result)
431440
}
432441
}
433-
}
442+
})
434443
}
435444

436445
type IsAbsTest struct {
@@ -465,7 +474,7 @@ var winisabstests = []IsAbsTest{
465474
}
466475

467476
func TestIsAbs(t *testing.T) {
468-
for _, os := range []OS{Unix, Windows} {
477+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
469478
var tests []IsAbsTest
470479
if os == Windows {
471480
tests = append(tests, winisabstests...)
@@ -491,7 +500,7 @@ func TestIsAbs(t *testing.T) {
491500
t.Errorf("IsAbs(%q, %q) = %v, want %v", test.path, os, r, test.isAbs)
492501
}
493502
}
494-
}
503+
})
495504
}
496505

497506
type RelTests struct {
@@ -552,7 +561,7 @@ var winreltests = []RelTests{
552561
}
553562

554563
func TestRel(t *testing.T) {
555-
for _, os := range []OS{Unix, Windows} {
564+
testEachOS(t, []OS{Unix, Windows}, func(t *testing.T, os OS) {
556565
tests := append([]RelTests{}, reltests...)
557566
if os == Windows {
558567
for i := range tests {
@@ -575,7 +584,7 @@ func TestRel(t *testing.T) {
575584
t.Errorf("Rel(%q, %q, %q)=%q, want %q", test.root, test.path, os, got, test.want)
576585
}
577586
}
578-
}
587+
})
579588
}
580589

581590
type VolumeNameTest struct {
@@ -609,14 +618,10 @@ var volumenametests = []VolumeNameTest{
609618
}
610619

611620
func TestVolumeName(t *testing.T) {
612-
for _, os := range []OS{Unix, Windows} {
613-
if os != Windows {
614-
return
615-
}
616-
for _, v := range volumenametests {
617-
if vol := VolumeName(v.path, os); vol != v.vol {
618-
t.Errorf("VolumeName(%q, %q)=%q, want %q", v.path, os, vol, v.vol)
619-
}
621+
os := Windows
622+
for _, v := range volumenametests {
623+
if vol := VolumeName(v.path, os); vol != v.vol {
624+
t.Errorf("VolumeName(%q, %q)=%q, want %q", v.path, os, vol, v.vol)
620625
}
621626
}
622627
}

0 commit comments

Comments
 (0)