Browse code

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
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/08/28 00:26:15
Showing 1 changed files
... ...
@@ -24,8 +24,11 @@ func init() {
24 24
 	reexec.Init()
25 25
 }
26 26
 
27
-func initDispatchTestCases() []dispatchTestCase {
28
-	dispatchTestCases := []dispatchTestCase{
27
+func TestDispatch(t *testing.T) {
28
+	if runtime.GOOS != "windows" {
29
+		skip.If(t, os.Getuid() != 0, "skipping test that requires root")
30
+	}
31
+	testCases := []dispatchTestCase{
29 32
 		{
30 33
 			name: "ADD multiple files to file",
31 34
 			cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
... ...
@@ -92,56 +95,46 @@ func initDispatchTestCases() []dispatchTestCase {
92 92
 			}},
93 93
 			expectedError: "source can't be a URL for COPY",
94 94
 			files:         nil,
95
-		}}
96
-
97
-	return dispatchTestCases
98
-}
99
-
100
-func TestDispatch(t *testing.T) {
101
-	if runtime.GOOS != "windows" {
102
-		skip.If(t, os.Getuid() != 0, "skipping test that requires root")
95
+		},
103 96
 	}
104
-	testCases := initDispatchTestCases()
105 97
 
106
-	for _, testCase := range testCases {
107
-		executeTestCase(t, testCase)
108
-	}
109
-}
98
+	for _, tc := range testCases {
99
+		t.Run(tc.name, func(t *testing.T) {
100
+			contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
101
+			defer cleanup()
110 102
 
111
-func executeTestCase(t *testing.T, testCase dispatchTestCase) {
112
-	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
113
-	defer cleanup()
103
+			for filename, content := range tc.files {
104
+				createTestTempFile(t, contextDir, filename, content, 0777)
105
+			}
114 106
 
115
-	for filename, content := range testCase.files {
116
-		createTestTempFile(t, contextDir, filename, content, 0777)
117
-	}
107
+			tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
118 108
 
119
-	tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
109
+			if err != nil {
110
+				t.Fatalf("Error when creating tar stream: %s", err)
111
+			}
120 112
 
121
-	if err != nil {
122
-		t.Fatalf("Error when creating tar stream: %s", err)
123
-	}
124
-
125
-	defer func() {
126
-		if err = tarStream.Close(); err != nil {
127
-			t.Fatalf("Error when closing tar stream: %s", err)
128
-		}
129
-	}()
113
+			defer func() {
114
+				if err = tarStream.Close(); err != nil {
115
+					t.Fatalf("Error when closing tar stream: %s", err)
116
+				}
117
+			}()
130 118
 
131
-	context, err := remotecontext.FromArchive(tarStream)
119
+			context, err := remotecontext.FromArchive(tarStream)
132 120
 
133
-	if err != nil {
134
-		t.Fatalf("Error when creating tar context: %s", err)
135
-	}
121
+			if err != nil {
122
+				t.Fatalf("Error when creating tar context: %s", err)
123
+			}
136 124
 
137
-	defer func() {
138
-		if err = context.Close(); err != nil {
139
-			t.Fatalf("Error when closing tar context: %s", err)
140
-		}
141
-	}()
125
+			defer func() {
126
+				if err = context.Close(); err != nil {
127
+					t.Fatalf("Error when closing tar context: %s", err)
128
+				}
129
+			}()
142 130
 
143
-	b := newBuilderWithMockBackend()
144
-	sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
145
-	err = dispatch(sb, testCase.cmd)
146
-	assert.Check(t, is.ErrorContains(err, testCase.expectedError))
131
+			b := newBuilderWithMockBackend()
132
+			sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
133
+			err = dispatch(sb, tc.cmd)
134
+			assert.Check(t, is.ErrorContains(err, tc.expectedError))
135
+		})
136
+	}
147 137
 }