Skip to content

Commit 6052f2b

Browse files
committed
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The functions are extremely similar to testify, but with enough slight differences to be confusing (for example, Equal takes its arguments in a different order). We already vendor testify, and it's used in a few places by tests. I also found some problems with pkg/testutil/assert. For example, the NotNil function seems to be broken. It checks the argument against "nil", which only works for an interface. If you pass in a nil map or slice, the equality check will fail. In the interest of avoiding NIH, I'm proposing replacing pkg/testutil/assert with testify. The test code looks almost the same, but we avoid the confusion of having two similar but slightly different assertion packages, and having to maintain our own package instead of using a commonly-used one. In the process, I found a few places where the tests should halt if an assertion fails, so I've made those cases (that I noticed) use "require" instead of "assert", and I've vendored the "require" package from testify alongside the already-present "assert" package. Signed-off-by: Aaron Lehmann <[email protected]>
1 parent 41f4c3c commit 6052f2b

File tree

102 files changed

+2303
-1141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2303
-1141
lines changed

builder/dockerfile/buildargs_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package dockerfile
22

33
import (
4-
"github.com/docker/docker/pkg/testutil/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func strPtr(source string) *string {
@@ -37,7 +38,7 @@ func TestGetAllAllowed(t *testing.T) {
3738
"ArgFromMeta": "frommeta1",
3839
"ArgFromMetaOverriden": "fromdockerfile3",
3940
}
40-
assert.DeepEqual(t, all, expected)
41+
assert.Equal(t, expected, all)
4142
}
4243

4344
func TestGetAllMeta(t *testing.T) {
@@ -59,5 +60,5 @@ func TestGetAllMeta(t *testing.T) {
5960
"ArgOverriddenByOptions": "fromopt2",
6061
"ArgNoDefaultInMetaFromOptions": "fromopt3",
6162
}
62-
assert.DeepEqual(t, all, expected)
63+
assert.Equal(t, expected, all)
6364
}

builder/dockerfile/builder_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"testing"
66

77
"github.com/docker/docker/builder/dockerfile/parser"
8-
"github.com/docker/docker/pkg/testutil/assert"
8+
"github.com/stretchr/testify/assert"
99
)
1010

1111
func TestAddNodesForLabelOption(t *testing.T) {
1212
dockerfile := "FROM scratch"
1313
result, err := parser.Parse(strings.NewReader(dockerfile))
14-
assert.NilError(t, err)
14+
assert.NoError(t, err)
1515

1616
labels := map[string]string{
1717
"org.e": "cli-e",
@@ -27,8 +27,8 @@ func TestAddNodesForLabelOption(t *testing.T) {
2727
"FROM scratch",
2828
`LABEL "org.a"='cli-a' "org.b"='cli-b' "org.c"='cli-c' "org.d"='cli-d' "org.e"='cli-e'`,
2929
}
30-
assert.Equal(t, len(nodes.Children), 2)
30+
assert.Len(t, nodes.Children, 2)
3131
for i, v := range nodes.Children {
32-
assert.Equal(t, v.Original, expected[i])
32+
assert.Equal(t, expected[i], v.Original)
3333
}
3434
}

builder/dockerfile/dispatchers_test.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/docker/docker/api/types/container"
1111
"github.com/docker/docker/api/types/strslice"
1212
"github.com/docker/docker/builder"
13-
"github.com/docker/docker/pkg/testutil/assert"
1413
"github.com/docker/go-connections/nat"
14+
"github.com/stretchr/testify/assert"
1515
)
1616

1717
type commandWithFunction struct {
@@ -137,13 +137,13 @@ func TestEnv2Variables(t *testing.T) {
137137

138138
args := []string{"var1", "val1", "var2", "val2"}
139139
err := env(b, args, nil, "")
140-
assert.NilError(t, err)
140+
assert.NoError(t, err)
141141

142142
expected := []string{
143143
fmt.Sprintf("%s=%s", args[0], args[1]),
144144
fmt.Sprintf("%s=%s", args[2], args[3]),
145145
}
146-
assert.DeepEqual(t, b.runConfig.Env, expected)
146+
assert.Equal(t, expected, b.runConfig.Env)
147147
}
148148

149149
func TestEnvValueWithExistingRunConfigEnv(t *testing.T) {
@@ -153,13 +153,13 @@ func TestEnvValueWithExistingRunConfigEnv(t *testing.T) {
153153

154154
args := []string{"var1", "val1"}
155155
err := env(b, args, nil, "")
156-
assert.NilError(t, err)
156+
assert.NoError(t, err)
157157

158158
expected := []string{
159159
fmt.Sprintf("%s=%s", args[0], args[1]),
160160
"var2=fromenv",
161161
}
162-
assert.DeepEqual(t, b.runConfig.Env, expected)
162+
assert.Equal(t, expected, b.runConfig.Env)
163163
}
164164

165165
func TestMaintainer(t *testing.T) {
@@ -215,49 +215,49 @@ func TestFromScratch(t *testing.T) {
215215
err := from(b, []string{"scratch"}, nil, "")
216216

217217
if runtime.GOOS == "windows" {
218-
assert.Error(t, err, "Windows does not support FROM scratch")
218+
assert.EqualError(t, err, "Windows does not support FROM scratch")
219219
return
220220
}
221221

222-
assert.NilError(t, err)
223-
assert.Equal(t, b.image, "")
224-
assert.Equal(t, b.noBaseImage, true)
222+
assert.NoError(t, err)
223+
assert.Equal(t, "", b.image)
224+
assert.Equal(t, true, b.noBaseImage)
225225
}
226226

227227
func TestFromWithArg(t *testing.T) {
228228
tag, expected := ":sometag", "expectedthisid"
229229

230230
getImage := func(name string) (builder.Image, error) {
231-
assert.Equal(t, name, "alpine"+tag)
231+
assert.Equal(t, "alpine"+tag, name)
232232
return &mockImage{id: "expectedthisid"}, nil
233233
}
234234
b := newBuilderWithMockBackend()
235235
b.docker.(*MockBackend).getImageOnBuildFunc = getImage
236236

237-
assert.NilError(t, arg(b, []string{"THETAG=" + tag}, nil, ""))
237+
assert.NoError(t, arg(b, []string{"THETAG=" + tag}, nil, ""))
238238
err := from(b, []string{"alpine${THETAG}"}, nil, "")
239239

240-
assert.NilError(t, err)
241-
assert.Equal(t, b.image, expected)
242-
assert.Equal(t, b.from.ImageID(), expected)
243-
assert.Equal(t, len(b.buildArgs.GetAllAllowed()), 0)
244-
assert.Equal(t, len(b.buildArgs.GetAllMeta()), 1)
240+
assert.NoError(t, err)
241+
assert.Equal(t, expected, b.image)
242+
assert.Equal(t, expected, b.from.ImageID())
243+
assert.Len(t, b.buildArgs.GetAllAllowed(), 0)
244+
assert.Len(t, b.buildArgs.GetAllMeta(), 1)
245245
}
246246

247247
func TestFromWithUndefinedArg(t *testing.T) {
248248
tag, expected := "sometag", "expectedthisid"
249249

250250
getImage := func(name string) (builder.Image, error) {
251-
assert.Equal(t, name, "alpine")
251+
assert.Equal(t, "alpine", name)
252252
return &mockImage{id: "expectedthisid"}, nil
253253
}
254254
b := newBuilderWithMockBackend()
255255
b.docker.(*MockBackend).getImageOnBuildFunc = getImage
256256
b.options.BuildArgs = map[string]*string{"THETAG": &tag}
257257

258258
err := from(b, []string{"alpine${THETAG}"}, nil, "")
259-
assert.NilError(t, err)
260-
assert.Equal(t, b.image, expected)
259+
assert.NoError(t, err)
260+
assert.Equal(t, expected, b.image)
261261
}
262262

263263
func TestOnbuildIllegalTriggers(t *testing.T) {
@@ -508,11 +508,11 @@ func TestArg(t *testing.T) {
508508
argDef := fmt.Sprintf("%s=%s", argName, argVal)
509509

510510
err := arg(b, []string{argDef}, nil, "")
511-
assert.NilError(t, err)
511+
assert.NoError(t, err)
512512

513513
expected := map[string]string{argName: argVal}
514514
allowed := b.buildArgs.GetAllAllowed()
515-
assert.DeepEqual(t, allowed, expected)
515+
assert.Equal(t, expected, allowed)
516516
}
517517

518518
func TestShell(t *testing.T) {

builder/dockerfile/internals_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"github.com/docker/docker/api/types"
88
"github.com/docker/docker/builder"
99
"github.com/docker/docker/pkg/archive"
10-
"github.com/docker/docker/pkg/testutil/assert"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestEmptyDockerfile(t *testing.T) {
@@ -38,7 +39,7 @@ func TestDockerfileOutsideTheBuildContext(t *testing.T) {
3839
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
3940
defer cleanup()
4041

41-
expectedError := "Forbidden path outside the build context"
42+
expectedError := "Forbidden path outside the build context: ../../Dockerfile ()"
4243

4344
readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
4445
}
@@ -54,7 +55,7 @@ func TestNonExistingDockerfile(t *testing.T) {
5455

5556
func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
5657
tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
57-
assert.NilError(t, err)
58+
require.NoError(t, err)
5859

5960
defer func() {
6061
if err = tarStream.Close(); err != nil {
@@ -63,7 +64,7 @@ func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath,
6364
}()
6465

6566
context, err := builder.MakeTarSumContext(tarStream)
66-
assert.NilError(t, err)
67+
require.NoError(t, err)
6768

6869
defer func() {
6970
if err = context.Close(); err != nil {
@@ -78,5 +79,5 @@ func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath,
7879
b := &Builder{options: options, context: context}
7980

8081
_, err = b.readAndParseDockerfile()
81-
assert.Error(t, err, expectedError)
82+
assert.EqualError(t, err, expectedError)
8283
}

builder/dockerfile/parser/line_parsers_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package parser
22

33
import (
4-
"github.com/docker/docker/pkg/testutil/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestParseNameValOldFormat(t *testing.T) {
910
directive := Directive{}
1011
node, err := parseNameVal("foo bar", "LABEL", &directive)
11-
assert.NilError(t, err)
12+
assert.NoError(t, err)
1213

1314
expected := &Node{
1415
Value: "foo",
1516
Next: &Node{Value: "bar"},
1617
}
17-
assert.DeepEqual(t, node, expected)
18+
assert.Equal(t, expected, node)
1819
}
1920

2021
func TestParseNameValNewFormat(t *testing.T) {
2122
directive := Directive{}
2223
node, err := parseNameVal("foo=bar thing=star", "LABEL", &directive)
23-
assert.NilError(t, err)
24+
assert.NoError(t, err)
2425

2526
expected := &Node{
2627
Value: "foo",
@@ -34,7 +35,7 @@ func TestParseNameValNewFormat(t *testing.T) {
3435
},
3536
},
3637
}
37-
assert.DeepEqual(t, node, expected)
38+
assert.Equal(t, expected, node)
3839
}
3940

4041
func TestNodeFromLabels(t *testing.T) {
@@ -60,6 +61,6 @@ func TestNodeFromLabels(t *testing.T) {
6061
}
6162

6263
node := NodeFromLabels(labels)
63-
assert.DeepEqual(t, node, expected)
64+
assert.Equal(t, expected, node)
6465

6566
}

builder/dockerfile/parser/parser_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"runtime"
1010
"testing"
1111

12-
"github.com/docker/docker/pkg/testutil/assert"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
const testDir = "testfiles"
@@ -18,11 +19,11 @@ const testFileLineInfo = "testfile-line/Dockerfile"
1819

1920
func getDirs(t *testing.T, dir string) []string {
2021
f, err := os.Open(dir)
21-
assert.NilError(t, err)
22+
require.NoError(t, err)
2223
defer f.Close()
2324

2425
dirs, err := f.Readdirnames(0)
25-
assert.NilError(t, err)
26+
require.NoError(t, err)
2627
return dirs
2728
}
2829

@@ -31,11 +32,11 @@ func TestTestNegative(t *testing.T) {
3132
dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
3233

3334
df, err := os.Open(dockerfile)
34-
assert.NilError(t, err)
35+
require.NoError(t, err)
3536
defer df.Close()
3637

3738
_, err = Parse(df)
38-
assert.Error(t, err, "")
39+
assert.Error(t, err)
3940
}
4041
}
4142

@@ -45,21 +46,21 @@ func TestTestData(t *testing.T) {
4546
resultfile := filepath.Join(testDir, dir, "result")
4647

4748
df, err := os.Open(dockerfile)
48-
assert.NilError(t, err)
49+
require.NoError(t, err)
4950
defer df.Close()
5051

5152
result, err := Parse(df)
52-
assert.NilError(t, err)
53+
require.NoError(t, err)
5354

5455
content, err := ioutil.ReadFile(resultfile)
55-
assert.NilError(t, err)
56+
require.NoError(t, err)
5657

5758
if runtime.GOOS == "windows" {
5859
// CRLF --> CR to match Unix behavior
5960
content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
6061
}
6162

62-
assert.Equal(t, result.AST.Dump()+"\n", string(content), "In "+dockerfile)
63+
assert.Contains(t, result.AST.Dump()+"\n", string(content), "In "+dockerfile)
6364
}
6465
}
6566

@@ -101,24 +102,24 @@ func TestParseWords(t *testing.T) {
101102

102103
for _, test := range tests {
103104
words := parseWords(test["input"][0], NewDefaultDirective())
104-
assert.DeepEqual(t, words, test["expect"])
105+
assert.Equal(t, test["expect"], words)
105106
}
106107
}
107108

108109
func TestLineInformation(t *testing.T) {
109110
df, err := os.Open(testFileLineInfo)
110-
assert.NilError(t, err)
111+
require.NoError(t, err)
111112
defer df.Close()
112113

113114
result, err := Parse(df)
114-
assert.NilError(t, err)
115+
require.NoError(t, err)
115116

116117
ast := result.AST
117118
if ast.StartLine != 5 || ast.endLine != 31 {
118119
fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.endLine)
119120
t.Fatal("Root line information doesn't match result.")
120121
}
121-
assert.Equal(t, len(ast.Children), 3)
122+
assert.Len(t, ast.Children, 3)
122123
expected := [][]int{
123124
{5, 5},
124125
{11, 12},

builder/dockerfile/shell_parser_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/docker/docker/pkg/testutil/assert"
10+
"github.com/stretchr/testify/assert"
1111
)
1212

1313
func TestShellParser4EnvVars(t *testing.T) {
1414
fn := "envVarTest"
1515
lineCount := 0
1616

1717
file, err := os.Open(fn)
18-
assert.NilError(t, err)
18+
assert.NoError(t, err)
1919
defer file.Close()
2020

2121
scanner := bufio.NewScanner(file)
@@ -36,7 +36,7 @@ func TestShellParser4EnvVars(t *testing.T) {
3636
}
3737

3838
words := strings.Split(line, "|")
39-
assert.Equal(t, len(words), 3)
39+
assert.Len(t, words, 3)
4040

4141
platform := strings.TrimSpace(words[0])
4242
source := strings.TrimSpace(words[1])
@@ -51,9 +51,9 @@ func TestShellParser4EnvVars(t *testing.T) {
5151
((platform == "U" || platform == "A") && runtime.GOOS != "windows") {
5252
newWord, err := ProcessWord(source, envs, '\\')
5353
if expected == "error" {
54-
assert.Error(t, err, "")
54+
assert.Error(t, err)
5555
} else {
56-
assert.NilError(t, err)
56+
assert.NoError(t, err)
5757
assert.Equal(t, newWord, expected)
5858
}
5959
}

0 commit comments

Comments
 (0)