Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6da05c5d6c |
@@ -7,6 +7,7 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/pkg/archive"
|
||||
"github.com/moby/patternmatcher"
|
||||
@@ -41,6 +42,11 @@ func tarBuildContext(contextDir, dockerfile string) (io.ReadCloser, error) {
|
||||
// Dockerfile if there is one, otherwise from .dockerignore at the root of the
|
||||
// context. Without either file there are no patterns.
|
||||
func readDockerignore(contextDir, dockerfile string) ([]string, error) {
|
||||
// Docker reads the Dockerfile path as a path inside the build context:
|
||||
// cleaned, with a leading / and any .. that would lead out of the context
|
||||
// dropped, so ./Dockerfile and /Dockerfile both name the Dockerfile at the
|
||||
// root.
|
||||
dockerfile = strings.TrimPrefix(filepath.Join("/", dockerfile), "/")
|
||||
if dockerfile == "" {
|
||||
dockerfile = defaultDockerfileName
|
||||
}
|
||||
|
||||
@@ -105,6 +105,38 @@ func TestPerformBuildFollowsDockerignore(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPerformBuildReadsDockerfilePathInsideContext checks that ./Dockerfile
|
||||
// and /Dockerfile name the Dockerfile at the root of the context, as Docker
|
||||
// reads them, so an ignore file that names the Dockerfile does not leave it
|
||||
// out.
|
||||
func TestPerformBuildReadsDockerfilePathInsideContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, dockerfile := range []string{"./Dockerfile", "/Dockerfile"} {
|
||||
t.Run(dockerfile, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
contextDir := t.TempDir()
|
||||
writeFiles(t, contextDir, map[string]string{
|
||||
defaultDockerignoreName: "Dockerfile\nsecret.txt\n",
|
||||
defaultDockerfileName: "",
|
||||
testMainGo: "",
|
||||
testSecretFile: "",
|
||||
})
|
||||
|
||||
got, err := buildContextFiles(t, contextDir, dockerfile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
want := []string{defaultDockerignoreName, defaultDockerfileName, testMainGo}
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("build context holds %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPerformBuildFailsOnMalformedDockerignore checks that a pattern the
|
||||
// docker command line would reject fails the build.
|
||||
func TestPerformBuildFailsOnMalformedDockerignore(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user