2018-02-05 16:05:59 -05:00
|
|
|
package dockerignore // import "github.com/docker/docker/builder/dockerignore"
|
2015-12-14 05:15:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadAll(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "dockerignore-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
di, err := ReadAll(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected not to have error, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diLen := len(di); diLen != 0 {
|
|
|
|
t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
diName := filepath.Join(tmpDir, ".dockerignore")
|
2017-03-24 10:39:47 -04:00
|
|
|
content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile\n# this is a comment\n! /inverted/abs/path\n!\n! \n")
|
2015-12-14 05:15:00 -05:00
|
|
|
err = ioutil.WriteFile(diName, []byte(content), 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
diFd, err := os.Open(diName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer diFd.Close()
|
|
|
|
|
2015-12-14 05:15:00 -05:00
|
|
|
di, err = ReadAll(diFd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-03-24 10:39:47 -04:00
|
|
|
if len(di) != 7 {
|
|
|
|
t.Fatalf("Expected 5 entries, got %v", len(di))
|
|
|
|
}
|
2015-12-14 05:15:00 -05:00
|
|
|
if di[0] != "test1" {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("First element is not test1")
|
2015-12-14 05:15:00 -05:00
|
|
|
}
|
2017-03-24 10:39:47 -04:00
|
|
|
if di[1] != "test2" { // according to https://docs.docker.com/engine/reference/builder/#dockerignore-file, /foo/bar should be treated as foo/bar
|
|
|
|
t.Fatal("Second element is not test2")
|
2015-12-14 05:15:00 -05:00
|
|
|
}
|
2017-03-24 10:39:47 -04:00
|
|
|
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
|
|
|
|
t.Fatal("Third element is not a/file/here")
|
2015-12-14 05:15:00 -05:00
|
|
|
}
|
|
|
|
if di[3] != "lastfile" {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Fourth element is not lastfile")
|
2015-12-14 05:15:00 -05:00
|
|
|
}
|
2017-03-24 10:39:47 -04:00
|
|
|
if di[4] != "!inverted/abs/path" {
|
|
|
|
t.Fatal("Fifth element is not !inverted/abs/path")
|
|
|
|
}
|
|
|
|
if di[5] != "!" {
|
|
|
|
t.Fatalf("Sixth element is not !, but %s", di[5])
|
|
|
|
}
|
|
|
|
if di[6] != "!" {
|
|
|
|
t.Fatalf("Sixth element is not !, but %s", di[6])
|
|
|
|
}
|
2015-12-14 05:15:00 -05:00
|
|
|
}
|