Skip to content

Commit 7f7ef5a

Browse files
committed
fix: Fixing initialization of BoilerplateOptions
1 parent d958a59 commit 7f7ef5a

File tree

2 files changed

+62
-93
lines changed

2 files changed

+62
-93
lines changed

cli/commands/scaffold/scaffold.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ const (
106106
rootFileName = "RootFileName"
107107
)
108108

109+
// NewBoilerplateOptions creates a new BoilerplateOptions struct
110+
func NewBoilerplateOptions(templateFolder, outputFolder string, vars map[string]any, terragruntOpts *options.TerragruntOptions) *boilerplate_options.BoilerplateOptions {
111+
return &boilerplate_options.BoilerplateOptions{
112+
TemplateFolder: templateFolder,
113+
OutputFolder: outputFolder,
114+
OnMissingKey: boilerplate_options.DefaultMissingKeyAction,
115+
OnMissingConfig: boilerplate_options.DefaultMissingConfigAction,
116+
Vars: vars,
117+
ShellCommandAnswers: map[string]bool{},
118+
NoShell: terragruntOpts.NoShell,
119+
NoHooks: terragruntOpts.NoHooks,
120+
NonInteractive: terragruntOpts.NonInteractive,
121+
DisableDependencyPrompt: terragruntOpts.NoDependencyPrompt,
122+
}
123+
}
124+
109125
func Run(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, moduleURL, templateURL string) error {
110126
// Apply catalog configuration settings, with CLI flags taking precedence
111127
applyCatalogConfigToScaffold(ctx, l, opts)
@@ -199,17 +215,7 @@ func Run(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, mod
199215
}
200216

201217
l.Infof("Running boilerplate generation to %s", outputDir)
202-
boilerplateOpts := &boilerplate_options.BoilerplateOptions{
203-
OutputFolder: outputDir,
204-
OnMissingKey: boilerplate_options.DefaultMissingKeyAction,
205-
OnMissingConfig: boilerplate_options.DefaultMissingConfigAction,
206-
Vars: vars,
207-
NoShell: opts.NoShell,
208-
NoHooks: opts.NoHooks,
209-
NonInteractive: opts.NonInteractive,
210-
DisableDependencyPrompt: opts.NoDependencyPrompt,
211-
TemplateFolder: boilerplateDir,
212-
}
218+
boilerplateOpts := NewBoilerplateOptions(boilerplateDir, outputDir, vars, opts)
213219

214220
emptyDep := variables.Dependency{}
215221
if err := templates.ProcessTemplate(boilerplateOpts, boilerplateOpts, emptyDep); err != nil {

cli/commands/scaffold/scaffold_test.go

Lines changed: 45 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ import (
1919
"github.com/stretchr/testify/require"
2020
)
2121

22+
// newTestBoilerplateOptions creates a BoilerplateOptions for testing
23+
func newTestBoilerplateOptions(templateFolder, outputFolder string, vars map[string]any, noShell, noHooks bool) *boilerplateoptions.BoilerplateOptions {
24+
return &boilerplateoptions.BoilerplateOptions{
25+
TemplateFolder: templateFolder,
26+
OutputFolder: outputFolder,
27+
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
28+
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
29+
Vars: vars,
30+
ShellCommandAnswers: map[string]bool{},
31+
NoShell: noShell,
32+
NoHooks: noHooks,
33+
NonInteractive: true,
34+
DisableDependencyPrompt: false,
35+
}
36+
}
37+
2238
func TestDefaultTemplateVariables(t *testing.T) {
2339
t.Parallel()
2440

@@ -64,17 +80,7 @@ func TestDefaultTemplateVariables(t *testing.T) {
6480
err = os.WriteFile(util.JoinPath(templateDir, "boilerplate.yml"), []byte(scaffold.DefaultBoilerplateConfig), 0644)
6581
require.NoError(t, err)
6682

67-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
68-
OutputFolder: outputDir,
69-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
70-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
71-
Vars: vars,
72-
NoShell: true,
73-
NoHooks: true,
74-
NonInteractive: true,
75-
DisableDependencyPrompt: false,
76-
TemplateFolder: templateDir,
77-
}
83+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, vars, true, true)
7884

7985
emptyDep := variables.Dependency{}
8086
err = templates.ProcessTemplate(boilerplateOpts, boilerplateOpts, emptyDep)
@@ -469,17 +475,7 @@ shell_output = "{{ shell "echo SHELL_EXECUTED" }}"
469475
require.NoError(t, err)
470476

471477
// Create BoilerplateOptions with NoShell=true
472-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
473-
OutputFolder: outputDir,
474-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
475-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
476-
Vars: map[string]any{},
477-
NoShell: true, // Disable shell functions
478-
NoHooks: false,
479-
NonInteractive: true,
480-
DisableDependencyPrompt: false,
481-
TemplateFolder: templateDir,
482-
}
478+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, map[string]any{}, true, false)
483479

484480
// Process the template
485481
emptyDep := variables.Dependency{}
@@ -531,23 +527,13 @@ variables:
531527
templateContent := `# Test template with shell function
532528
test_var = "{{ .TestVar }}"
533529
# This shell function SHOULD execute when NoShell=false
534-
shell_output = "{{ shell "echo SHELL_EXECUTED" }}"
530+
shell_output = "{{ shell "echo" "SHELL_EXECUTED" }}"
535531
`
536532
err = os.WriteFile(util.JoinPath(templateDir, "test.txt"), []byte(templateContent), 0644)
537533
require.NoError(t, err)
538534

539535
// Create BoilerplateOptions with NoShell=false
540-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
541-
OutputFolder: outputDir,
542-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
543-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
544-
Vars: map[string]any{},
545-
NoShell: false, // Enable shell functions
546-
NoHooks: false,
547-
NonInteractive: true,
548-
DisableDependencyPrompt: false,
549-
TemplateFolder: templateDir,
550-
}
536+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, map[string]any{}, false, false)
551537

552538
// Process the template
553539
emptyDep := variables.Dependency{}
@@ -592,10 +578,14 @@ variables:
592578
593579
hooks:
594580
before:
595-
- command: echo "BEFORE_HOOK_EXECUTED" > ` + outputDir + `/hook_output.txt
581+
- command: touch
582+
args:
583+
- ` + outputDir + `/before_hook_not_executed.txt
596584
description: "Test hook that should NOT execute"
597585
after:
598-
- command: echo "AFTER_HOOK_EXECUTED" >> ` + outputDir + `/hook_output.txt
586+
- command: touch
587+
args:
588+
- ` + outputDir + `/after_hook_not_executed.txt
599589
description: "Test hook that should NOT execute"
600590
`
601591
err = os.WriteFile(util.JoinPath(templateDir, "boilerplate.yml"), []byte(boilerplateConfig), 0644)
@@ -609,17 +599,7 @@ test_var = "{{ .TestVar }}"
609599
require.NoError(t, err)
610600

611601
// Create BoilerplateOptions with NoHooks=true
612-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
613-
OutputFolder: outputDir,
614-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
615-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
616-
Vars: map[string]any{},
617-
NoShell: false,
618-
NoHooks: true, // Disable hooks
619-
NonInteractive: true,
620-
DisableDependencyPrompt: false,
621-
TemplateFolder: templateDir,
622-
}
602+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, map[string]any{}, false, true)
623603

624604
// Process the template
625605
emptyDep := variables.Dependency{}
@@ -634,9 +614,11 @@ test_var = "{{ .TestVar }}"
634614
require.NoError(t, err)
635615
assert.Contains(t, content, "test-value", "Template variable should be processed")
636616

637-
// Verify that hooks did NOT execute (hook_output.txt should not exist)
638-
hookOutputFile := util.JoinPath(outputDir, "hook_output.txt")
639-
assert.NoFileExists(t, hookOutputFile, "Hook output file should not exist when NoHooks=true")
617+
// Verify that hooks did NOT execute (hook files should not exist)
618+
beforeHookFile := util.JoinPath(outputDir, "before_hook_not_executed.txt")
619+
afterHookFile := util.JoinPath(outputDir, "after_hook_not_executed.txt")
620+
assert.NoFileExists(t, beforeHookFile, "Before hook file should not exist when NoHooks=true")
621+
assert.NoFileExists(t, afterHookFile, "After hook file should not exist when NoHooks=true")
640622
}
641623

642624
// TestBoilerplateHooksEnabled tests that NoHooks=false allows hooks to execute
@@ -663,10 +645,14 @@ variables:
663645
664646
hooks:
665647
before:
666-
- command: echo "BEFORE_HOOK_EXECUTED" > ` + outputDir + `/hook_output.txt
648+
- command: touch
649+
args:
650+
- ` + outputDir + `/before_hook_executed.txt
667651
description: "Test hook that SHOULD execute"
668652
after:
669-
- command: echo "AFTER_HOOK_EXECUTED" >> ` + outputDir + `/hook_output.txt
653+
- command: touch
654+
args:
655+
- ` + outputDir + `/after_hook_executed.txt
670656
description: "Test hook that SHOULD execute"
671657
`
672658
err = os.WriteFile(util.JoinPath(templateDir, "boilerplate.yml"), []byte(boilerplateConfig), 0644)
@@ -680,17 +666,7 @@ test_var = "{{ .TestVar }}"
680666
require.NoError(t, err)
681667

682668
// Create BoilerplateOptions with NoHooks=false
683-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
684-
OutputFolder: outputDir,
685-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
686-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
687-
Vars: map[string]any{},
688-
NoShell: false,
689-
NoHooks: false, // Enable hooks
690-
NonInteractive: true,
691-
DisableDependencyPrompt: false,
692-
TemplateFolder: templateDir,
693-
}
669+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, map[string]any{}, false, false)
694670

695671
// Process the template
696672
emptyDep := variables.Dependency{}
@@ -705,14 +681,11 @@ test_var = "{{ .TestVar }}"
705681
require.NoError(t, err)
706682
assert.Contains(t, content, "test-value", "Template variable should be processed")
707683

708-
// Verify that hooks DID execute (hook_output.txt should exist with content)
709-
hookOutputFile := util.JoinPath(outputDir, "hook_output.txt")
710-
require.FileExists(t, hookOutputFile, "Hook output file should exist when NoHooks=false")
711-
712-
hookContent, err := util.ReadFileAsString(hookOutputFile)
713-
require.NoError(t, err)
714-
assert.Contains(t, hookContent, "BEFORE_HOOK_EXECUTED", "Before hook should execute when NoHooks=false")
715-
assert.Contains(t, hookContent, "AFTER_HOOK_EXECUTED", "After hook should execute when NoHooks=false")
684+
// Verify that hooks DID execute (before and after hook files should exist)
685+
beforeHookFile := util.JoinPath(outputDir, "before_hook_executed.txt")
686+
afterHookFile := util.JoinPath(outputDir, "after_hook_executed.txt")
687+
require.FileExists(t, beforeHookFile, "Before hook file should exist when NoHooks=false")
688+
require.FileExists(t, afterHookFile, "After hook file should exist when NoHooks=false")
716689
}
717690

718691
// TestBoilerplateBothFlagsDisabled tests that both NoShell=true and NoHooks=true work together
@@ -754,17 +727,7 @@ shell_result = "{{ shell "echo SHELL_EXECUTED" }}"
754727
require.NoError(t, err)
755728

756729
// Create BoilerplateOptions with both NoShell=true and NoHooks=true
757-
boilerplateOpts := &boilerplateoptions.BoilerplateOptions{
758-
OutputFolder: outputDir,
759-
OnMissingKey: boilerplateoptions.DefaultMissingKeyAction,
760-
OnMissingConfig: boilerplateoptions.DefaultMissingConfigAction,
761-
Vars: map[string]any{},
762-
NoShell: true, // Disable shell functions
763-
NoHooks: true, // Disable hooks
764-
NonInteractive: true,
765-
DisableDependencyPrompt: false,
766-
TemplateFolder: templateDir,
767-
}
730+
boilerplateOpts := newTestBoilerplateOptions(templateDir, outputDir, map[string]any{}, true, true)
768731

769732
// Process the template
770733
emptyDep := variables.Dependency{}

0 commit comments

Comments
 (0)