github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/dockerignore/dockerignore_test.go (about) 1 package 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") 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 di[0] != "test1" { 46 t.Fatalf("First element is not test1") 47 } 48 if di[1] != "/test2" { 49 t.Fatalf("Second element is not /test2") 50 } 51 if di[2] != "/a/file/here" { 52 t.Fatalf("Third element is not /a/file/here") 53 } 54 if di[3] != "lastfile" { 55 t.Fatalf("Fourth element is not lastfile") 56 } 57 }