diff --git a/integration/.containerignore b/integration/.containerignore deleted file mode 100644 index 31b37763d..000000000 --- a/integration/.containerignore +++ /dev/null @@ -1,3 +0,0 @@ -# A .containerignore file to make sure containerignore support works -ignore/** -!ignore/foo diff --git a/integration/dockerfiles/Dockerfile_dockerignore_relative.containerignore b/integration/dockerfiles/Dockerfile_dockerignore_relative.containerignore deleted file mode 100644 index c825fe6c8..000000000 --- a/integration/dockerfiles/Dockerfile_dockerignore_relative.containerignore +++ /dev/null @@ -1,3 +0,0 @@ -# A .containerignore file to make sure containerignore support works -ignore_relative/** -!ignore_relative/foo diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 864c443a6..8b2ad95af 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -773,8 +773,7 @@ func NewFileContextFromDockerfile(dockerfilePath, buildcontext string) (FileCont return fileContext, nil } -// getExcludedFiles returns a list of files to exclude from the .dockerignore -func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { +func getIgnoreFilePath(dockerfilePath, buildcontext string) (string, error) { var path string dockerignorePath := dockerfilePath + ".dockerignore" @@ -788,8 +787,14 @@ func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { } else if FilepathExists(filepath.Join(buildcontext, ".dockerignore")) { path = filepath.Join(buildcontext, ".dockerignore") } else { - return nil, nil + return "", errors.New("Ignore file not found") } + return path, nil +} + +// getExcludedFiles returns a list of files to exclude from the .containerignore or .dockerignore +func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { + path, _ := getIgnoreFilePath(dockerfilePath, buildcontext) logrus.Infof("Using ignorefile: %v", path) contents, err := os.ReadFile(path) if err != nil { diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index 9bd44c835..6522bda16 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/fs" + "io/ioutil" "os" "path/filepath" "reflect" @@ -920,6 +921,84 @@ func Test_childDirInSkiplist(t *testing.T) { } } +func Test_getIgnoreFilePath(t *testing.T) { + dockerfilePath := filepath.Join(os.TempDir(), "Dockerfile") + buildcontext := filepath.Join(os.TempDir(), "buildcontext") + + err := os.MkdirAll(buildcontext, 0755) + if err != nil { + t.Errorf("'%v' folder cannot be created", buildcontext) + } + + cleanup := func() { + os.Remove(dockerfilePath + ".containerignore") + os.Remove(dockerfilePath + ".dockerignore") + os.Remove(filepath.Join(buildcontext, ".containerignore")) + os.Remove(filepath.Join(buildcontext, ".dockerignore")) + } + + t.Run(".containerignore exists next to Dockerfile", func(t *testing.T) { + err := ioutil.WriteFile(dockerfilePath+".containerignore", []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.containerignore", dockerfilePath) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := dockerfilePath + ".containerignore" + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".containerignore exists in the buildcontext", func(t *testing.T) { + err := ioutil.WriteFile(filepath.Join(buildcontext, ".containerignore"), []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.containerignore", buildcontext) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := filepath.Join(buildcontext, ".containerignore") + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".dockerignore exists next to Dockerfile", func(t *testing.T) { + err := ioutil.WriteFile(dockerfilePath+".dockerignore", []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.dockerignore", dockerfilePath) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := dockerfilePath + ".dockerignore" + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".dockerignore exists in the buildcontext", func(t *testing.T) { + err := ioutil.WriteFile(filepath.Join(buildcontext, ".dockerignore"), []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.dockerignore", buildcontext) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := filepath.Join(buildcontext, ".dockerignore") + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + err = os.RemoveAll(buildcontext) + if err != nil { + t.Errorf("cannot clean '%v' folder", buildcontext) + } +} + func Test_correctDockerignoreFileIsUsed(t *testing.T) { type args struct { dockerfilepath string