|
1 | 1 | package dockerignore |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "path/filepath" |
| 4 | + "strings" |
6 | 5 | "testing" |
7 | 6 | ) |
8 | 7 |
|
9 | 8 | func TestReadAll(t *testing.T) { |
10 | | - di, err := ReadAll(nil) |
| 9 | + actual, err := ReadAll(nil) |
11 | 10 | if err != nil { |
12 | | - t.Fatalf("Expected not to have error, got %v", err) |
| 11 | + t.Errorf("Expected no error, got %v", err) |
13 | 12 | } |
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) |
17 | 15 | } |
18 | 16 |
|
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 |
25 | 20 |
|
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 | + "!", |
29 | 35 | } |
30 | | - defer diFd.Close() |
31 | 36 |
|
32 | | - di, err = ReadAll(diFd) |
| 37 | + actual, err = ReadAll(strings.NewReader(content)) |
33 | 38 | if err != nil { |
34 | | - t.Fatal(err) |
| 39 | + t.Error(err) |
35 | 40 | } |
36 | 41 |
|
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 | + } |
60 | 53 | } |
61 | 54 | } |
0 commit comments