Skip to content

Commit

Permalink
fix: change BuildLogWriter default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkovev committed Dec 19, 2024
1 parent 17530fa commit a502690
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
18 changes: 11 additions & 7 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ func Test_BuildContainerFromDockerfileWithBuildLogWriter(t *testing.T) {
FromDockerfile: FromDockerfile{
Context: filepath.Join(".", "testdata"),
Dockerfile: "buildlog.Dockerfile",
PrintBuildLog: true,
BuildLogWriter: &buffer,
},
}
Expand All @@ -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])
}
Expand Down

0 comments on commit a502690

Please sign in to comment.