TestDispatch: refactor to use subtests again, and fix linting (structcheck)
Instead of using a `initDispatchTestCases()` function, declare the test-table
inside `TestDispatch` itself, and run the tests as subtests.
```
[2019-08-27T15:14:51.072Z] builder/dockerfile/evaluator_test.go:18:2: `name` is unused (structcheck)
[2019-08-27T15:14:51.072Z] 	name, expectedError string
```
@@ -24,8 +24,11 @@ func init() {
reexec.Init()
}
-func initDispatchTestCases() []dispatchTestCase {
- dispatchTestCases := []dispatchTestCase{
+func TestDispatch(t *testing.T) {
+ if runtime.GOOS != "windows" {
+ skip.If(t, os.Getuid() != 0, "skipping test that requires root")
+ }
+ testCases := []dispatchTestCase{
{
name: "ADD multiple files to file",
cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
@@ -92,27 +95,15 @@ func initDispatchTestCases() []dispatchTestCase {
}},
expectedError: "source can't be a URL for COPY",
files: nil,
- }}
-
- return dispatchTestCases
-}
-
-func TestDispatch(t *testing.T) {
- if runtime.GOOS != "windows" {
- skip.If(t, os.Getuid() != 0, "skipping test that requires root")
- }
- testCases := initDispatchTestCases()
-
- for _, testCase := range testCases {
- executeTestCase(t, testCase)
- }
+ },
}
-func executeTestCase(t *testing.T, testCase dispatchTestCase) {
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
defer cleanup()
- for filename, content := range testCase.files {
+ for filename, content := range tc.files {
createTestTempFile(t, contextDir, filename, content, 0777)
}
@@ -142,6 +133,8 @@ func executeTestCase(t *testing.T, testCase dispatchTestCase) {
b := newBuilderWithMockBackend()
sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
- err = dispatch(sb, testCase.cmd)
- assert.Check(t, is.ErrorContains(err, testCase.expectedError))
+ err = dispatch(sb, tc.cmd)
+ assert.Check(t, is.ErrorContains(err, tc.expectedError))
+ })
+ }
}
