Skip to content

Commit e171f10

Browse files
committed
fix(terraform): preserve explicit init context
1 parent 297bf1a commit e171f10

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

internal/exec/terraform_execute_helpers_exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func executeMainTerraformCommand( //nolint:revive // argument-limit: opts variad
448448
// implicit init so post-init provisioners can complete and persist provider
449449
// locks for workdir and vendored components.
450450
if err == nil && info.SubCommand == subcommandInit {
451-
dispatchAfterInitFn(atmosConfig, info, componentPath)
451+
dispatchAfterInitFn(atmosConfig, info, componentPath, opts...)
452452
}
453453

454454
exitCode := resolveExitCode(err)

internal/exec/terraform_execute_helpers_workspace_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stretchr/testify/require"
1616

1717
errUtils "github.com/cloudposse/atmos/errors"
18+
provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir"
1819
"github.com/cloudposse/atmos/pkg/schema"
1920
)
2021

@@ -223,16 +224,20 @@ func TestExecuteMainTerraformCommand_ExplicitInitDispatchesAfterInit(t *testing.
223224
t.Cleanup(func() { dispatchAfterInitFn = originalDispatch })
224225

225226
var dispatched bool
226-
dispatchAfterInitFn = func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, _ ...ShellCommandOption) {
227+
var dispatchOpts []ShellCommandOption
228+
dispatchAfterInitFn = func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) {
227229
dispatched = true
230+
dispatchOpts = append([]ShellCommandOption(nil), opts...)
228231
assert.Equal(t, "/tmp/component", componentPath)
229232
assert.Equal(t, subcommandInit, info.SubCommand)
230233
}
231234

232235
atmosConfig := schema.AtmosConfiguration{}
233236
info := schema.ConfigAndStacksInfo{SubCommand: subcommandInit, DryRun: true}
234-
require.NoError(t, executeMainTerraformCommand(&atmosConfig, &info, []string{subcommandInit}, "/tmp/component", false))
237+
ctx := provWorkdir.WithOutputSuppressed(t.Context())
238+
require.NoError(t, executeMainTerraformCommand(&atmosConfig, &info, []string{subcommandInit}, "/tmp/component", false, WithProcessContext(ctx)))
235239
assert.True(t, dispatched, "successful explicit init must dispatch after.terraform.init provisioners")
240+
assert.True(t, provWorkdir.OutputSuppressed(shellCommandContext(dispatchOpts...)))
236241
}
237242

238243
func TestExecuteMainTerraformCommand_FailedExplicitInitSkipsAfterInit(t *testing.T) {

pkg/hooks/step_engine_test.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,49 @@ func TestStepEngineSuppressesTransientOutputWhenWritersAreSet(t *testing.T) {
375375
suppressed: &suppressed,
376376
})
377377

378-
ctx := stepExecContext(&Hook{Kind: stepKindName, Type: "output-suppression-capture-test"})
379-
ctx.Stdout = io.Discard
378+
tests := []struct {
379+
name string
380+
context *ExecContext
381+
run func(*ExecContext) (*Output, error)
382+
setWriter func(*ExecContext)
383+
suppressed bool
384+
}{
385+
{
386+
name: "step stdout",
387+
context: stepExecContext(&Hook{Kind: stepKindName, Type: "output-suppression-capture-test"}),
388+
run: stepEngine{}.Run,
389+
setWriter: func(ctx *ExecContext) { ctx.Stdout = io.Discard },
390+
suppressed: true,
391+
},
392+
{
393+
name: "steps stderr",
394+
context: stepsExecContext(&Hook{Kind: stepsKindName, With: []any{
395+
map[string]any{"type": "output-suppression-capture-test"},
396+
}}),
397+
run: stepsEngine{}.Run,
398+
setWriter: func(ctx *ExecContext) { ctx.Stderr = io.Discard },
399+
suppressed: true,
400+
},
401+
{
402+
name: "step without writers",
403+
context: stepExecContext(&Hook{Kind: stepKindName, Type: "output-suppression-capture-test"}),
404+
run: stepEngine{}.Run,
405+
suppressed: false,
406+
},
407+
}
380408

381-
_, err := stepEngine{}.Run(ctx)
382-
require.NoError(t, err)
383-
assert.True(t, suppressed)
409+
for _, tt := range tests {
410+
t.Run(tt.name, func(t *testing.T) {
411+
suppressed = false
412+
if tt.setWriter != nil {
413+
tt.setWriter(tt.context)
414+
}
415+
416+
_, err := tt.run(tt.context)
417+
require.NoError(t, err)
418+
assert.Equal(t, tt.suppressed, suppressed)
419+
})
420+
}
384421
}
385422

386423
func TestStepHooksDefaultToComponentWorkingDirectory(t *testing.T) {

0 commit comments

Comments
 (0)