github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerignore/dockerignore_test.go (about) 1 package dockerignore // import "github.com/demonoid81/moby/builder/dockerignore" 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 func TestReadAll(t *testing.T) { 12 tmpDir, err := ioutil.TempDir("", "dockerignore-test") 13 if err != nil { 14 t.Fatal(err) 15 } 16 defer os.RemoveAll(tmpDir) 17 18 di, err := ReadAll(nil) 19 if err != nil { 20 t.Fatalf("Expected not to have error, got %v", err) 21 } 22 23 if diLen := len(di); diLen != 0 { 24 t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen) 25 } 26 27 diName := filepath.Join(tmpDir, ".dockerignore") 28 content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile\n# this is a comment\n! /inverted/abs/path\n!\n! \n") 29 err = ioutil.WriteFile(diName, []byte(content), 0777) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 diFd, err := os.Open(diName) 35 if err != nil { 36 t.Fatal(err) 37 } 38 defer diFd.Close() 39 40 di, err = ReadAll(diFd) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 if len(di) != 7 { 46 t.Fatalf("Expected 7 entries, got %v", len(di)) 47 } 48 if di[0] != "test1" { 49 t.Fatal("First element is not test1") 50 } 51 if di[1] != "test2" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar 52 t.Fatal("Second element is not test2") 53 } 54 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 55 t.Fatal("Third element is not a/file/here") 56 } 57 if di[3] != "lastfile" { 58 t.Fatal("Fourth element is not lastfile") 59 } 60 if di[4] != "!inverted/abs/path" { 61 t.Fatal("Fifth element is not !inverted/abs/path") 62 } 63 if di[5] != "!" { 64 t.Fatalf("Sixth element is not !, but %s", di[5]) 65 } 66 if di[6] != "!" { 67 t.Fatalf("Seventh element is not !, but %s", di[6]) 68 } 69 }