Skip to content

Commit

Permalink
feat: support for passing env vars to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Feb 19, 2024
1 parent 7a30684 commit ac97472
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func WithHostConfigModifier(modifier func(hostConfig *container.HostConfig)) Cus
}
}

// WithEnv appends the environment variables to the container
func WithEnv(env map[string]string) CustomizeRequestOption {
return func(req *GenericContainerRequest) {
for k, v := range env {
req.Env[k] = v
}
}
}

// WithImage sets the image for a container
func WithImage(image string) CustomizeRequestOption {
return func(req *GenericContainerRequest) {
Expand Down
18 changes: 18 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ func TestOverrideContainerRequest(t *testing.T) {
assert.Equal(t, wait.ForLog("foo"), req.WaitingFor)
}

func TestWithEnv(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"FOO": "BAR",
},
},
}

testcontainers.WithEnv(map[string]string{
"BAR": "BAZ",
"FOO": "BAR2", // this should override the FOO env
})(&req)

assert.Equal(t, "BAR2", req.Env["FOO"])
assert.Equal(t, "BAZ", req.Env["BAR"])
}

type msgsLogConsumer struct {
msgs []string
}
Expand Down

0 comments on commit ac97472

Please sign in to comment.