From a502690616d5b8c427771644cd961bcfa36ddff5 Mon Sep 17 00:00:00 2001 From: zenkovev <89307802901@mail.ru> Date: Thu, 19 Dec 2024 13:24:13 +0300 Subject: [PATCH] fix: change BuildLogWriter default behavior --- container.go | 18 +++++++++++------- docker_test.go | 7 ++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/container.go b/container.go index bbd66b0905..35be60fb81 100644 --- a/container.go +++ b/container.go @@ -92,8 +92,8 @@ type FromDockerfile struct { Repo string // the repo label for image, defaults to UUID Tag string // the tag label for image, defaults to UUID BuildArgs map[string]*string // enable user to pass build args to docker daemon - PrintBuildLog bool // enable user to print build log - BuildLogWriter io.Writer // for output of build log if PrintBuildLog is true, defaults to os.Stderr + PrintBuildLog bool // Deprecated: Use BuildLogWriter instead + BuildLogWriter io.Writer // for output of build log, defaults to io.Discard AuthConfigs map[string]registry.AuthConfig // Deprecated. Testcontainers will detect registry credentials automatically. Enable auth configs to be able to pull from an authenticated docker registry // KeepImage describes whether DockerContainer.Terminate should not delete the // container image. Useful for images that are built from a Dockerfile and take a @@ -411,16 +411,20 @@ func (c *ContainerRequest) ShouldKeepBuiltImage() bool { return c.FromDockerfile.KeepImage } +// BuildLogWriter returns the io.Writer for output of log when building a Docker image from +// a Dockerfile. It returns the BuildLogWriter from the ContainerRequest, defaults to io.Discard. +// For backward compatibility, if BuildLogWriter is default and PrintBuildLog is true, +// the function returns os.Stderr. func (c *ContainerRequest) BuildLogWriter() io.Writer { - if c.FromDockerfile.BuildLogWriter == nil { - c.FromDockerfile.BuildLogWriter = os.Stderr + if c.FromDockerfile.BuildLogWriter != nil { + return c.FromDockerfile.BuildLogWriter } - if c.FromDockerfile.PrintBuildLog { - return c.FromDockerfile.BuildLogWriter + c.FromDockerfile.BuildLogWriter = os.Stderr } else { - return io.Discard + c.FromDockerfile.BuildLogWriter = io.Discard } + return c.FromDockerfile.BuildLogWriter } // BuildOptions returns the image build options when building a Docker image from a Dockerfile. diff --git a/docker_test.go b/docker_test.go index 2edac20c34..2595dcffee 100644 --- a/docker_test.go +++ b/docker_test.go @@ -715,7 +715,6 @@ func Test_BuildContainerFromDockerfileWithBuildLogWriter(t *testing.T) { FromDockerfile: FromDockerfile{ Context: filepath.Join(".", "testdata"), Dockerfile: "buildlog.Dockerfile", - PrintBuildLog: true, BuildLogWriter: &buffer, }, } @@ -731,10 +730,8 @@ func Test_BuildContainerFromDockerfileWithBuildLogWriter(t *testing.T) { CleanupContainer(t, c) require.NoError(t, err) - out, err := io.ReadAll(&buffer) - require.NoError(t, err) - - temp := strings.Split(string(out), "\n") + out := buffer.String() + temp := strings.Split(out, "\n") require.NotEmpty(t, temp) assert.Regexpf(t, `^Step\s*1/\d+\s*:\s*FROM alpine$`, temp[0], "Expected stdout first line to be %s. Got '%s'.", "Step 1/* : FROM alpine", temp[0]) }