implement new function to find ignore file path

This commit is contained in:
Bence Major 2024-09-01 10:01:54 +02:00
parent 9a9dcd9366
commit 21618758eb
4 changed files with 87 additions and 9 deletions

View File

@ -1,3 +0,0 @@
# A .containerignore file to make sure containerignore support works
ignore/**
!ignore/foo

View File

@ -1,3 +0,0 @@
# A .containerignore file to make sure containerignore support works
ignore_relative/**
!ignore_relative/foo

View File

@ -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 {

View File

@ -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