Merge pull request #4346 from thaJeztah/build_errors

build: error if Dockerfile name is passed with Dockerfile from stdin
This commit is contained in:
Sebastiaan van Stijn 2023-06-21 11:48:12 +02:00 committed by GitHub
commit c96484a114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -34,8 +34,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var errStdinConflict = errors.New("invalid argument: can't use stdin for both build context and dockerfile")
type buildOptions struct { type buildOptions struct {
context string context string
dockerfileName string dockerfileName string
@ -189,7 +187,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
if options.dockerfileFromStdin() { if options.dockerfileFromStdin() {
if options.contextFromStdin() { if options.contextFromStdin() {
return errStdinConflict return errors.New("invalid argument: can't use stdin for both build context and dockerfile")
} }
dockerfileCtx = dockerCli.In() dockerfileCtx = dockerCli.In()
} }

View File

@ -155,6 +155,9 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
if dockerfileName == "-" { if dockerfileName == "-" {
return nil, "", errors.New("build context is not an archive") return nil, "", errors.New("build context is not an archive")
} }
if dockerfileName != "" {
return nil, "", errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")
}
dockerfileDir, err := WriteTempDockerfile(rc) dockerfileDir, err := WriteTempDockerfile(rc)
if err != nil { if err != nil {

View File

@ -152,6 +152,13 @@ func TestGetContextFromReaderString(t *testing.T) {
} }
} }
func TestGetContextFromReaderStringConflict(t *testing.T) {
rdr, relDockerfile, err := GetContextFromReader(io.NopCloser(strings.NewReader(dockerfileContents)), "custom.Dockerfile")
assert.Check(t, is.Equal(rdr, nil))
assert.Check(t, is.Equal(relDockerfile, ""))
assert.Check(t, is.ErrorContains(err, "ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles"))
}
func TestGetContextFromReaderTar(t *testing.T) { func TestGetContextFromReaderTar(t *testing.T) {
contextDir := createTestTempDir(t) contextDir := createTestTempDir(t)
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents) createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)