Skip to content

Commit 01b25b1

Browse files
committed
frontend/dockerfile/dockerignore: cleanup unit test
- don't use a temp-file for the test as all we need is a reader - use a const and string-literal for the test-content, which makes it slightly more readable - don't use hard-coded tests for each line, but use an "expected" slice - don't fail early if line-numbers don't match Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 687091b commit 01b25b1

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed
Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,54 @@
11
package dockerignore
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"strings"
65
"testing"
76
)
87

98
func TestReadAll(t *testing.T) {
10-
di, err := ReadAll(nil)
9+
actual, err := ReadAll(nil)
1110
if err != nil {
12-
t.Fatalf("Expected not to have error, got %v", err)
11+
t.Errorf("Expected no error, got %v", err)
1312
}
14-
15-
if diLen := len(di); diLen != 0 {
16-
t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
13+
if entries := len(actual); entries != 0 {
14+
t.Fatalf("Expected to have zero entries, got %d", entries)
1715
}
1816

19-
diName := filepath.Join(t.TempDir(), ".dockerignore")
20-
content := "test1\n/test2\n/a/file/here\n\nlastfile\n# this is a comment\n! /inverted/abs/path\n!\n! \n"
21-
err = os.WriteFile(diName, []byte(content), 0600)
22-
if err != nil {
23-
t.Fatal(err)
24-
}
17+
const content = `test1
18+
/test2
19+
/a/file/here
2520
26-
diFd, err := os.Open(diName)
27-
if err != nil {
28-
t.Fatal(err)
21+
lastfile
22+
# this is a comment
23+
! /inverted/abs/path
24+
!
25+
! `
26+
27+
expected := []string{
28+
"test1",
29+
"test2", // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
30+
"a/file/here", // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
31+
"lastfile",
32+
"!inverted/abs/path",
33+
"!",
34+
"!",
2935
}
30-
defer diFd.Close()
3136

32-
di, err = ReadAll(diFd)
37+
actual, err = ReadAll(strings.NewReader(content))
3338
if err != nil {
34-
t.Fatal(err)
39+
t.Error(err)
3540
}
3641

37-
if len(di) != 7 {
38-
t.Fatalf("Expected 7 entries, got %v", len(di))
39-
}
40-
if di[0] != "test1" {
41-
t.Fatal("First element is not test1")
42-
}
43-
if di[1] != "test2" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
44-
t.Fatal("Second element is not test2")
45-
}
46-
if di[2] != "a/file/here" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
47-
t.Fatal("Third element is not a/file/here")
48-
}
49-
if di[3] != "lastfile" {
50-
t.Fatal("Fourth element is not lastfile")
51-
}
52-
if di[4] != "!inverted/abs/path" {
53-
t.Fatal("Fifth element is not !inverted/abs/path")
54-
}
55-
if di[5] != "!" {
56-
t.Fatalf("Sixth element is not !, but %s", di[5])
57-
}
58-
if di[6] != "!" {
59-
t.Fatalf("Seventh element is not !, but %s", di[6])
42+
if len(actual) != len(expected) {
43+
t.Errorf("Expected %d entries, got %v", len(expected), len(actual))
44+
}
45+
for i, expectedLine := range expected {
46+
if i >= len(actual) {
47+
t.Errorf(`missing line %d: expected: "%s", got none`, i+1, expectedLine)
48+
continue
49+
}
50+
if actual[i] != expectedLine {
51+
t.Errorf(`line %d: expected: "%s", got: "%s"`, i+1, expectedLine, actual[i])
52+
}
6053
}
6154
}

0 commit comments

Comments
 (0)