Skip to content

Commit 1ff03fc

Browse files
committed
add comments
1 parent bbc1114 commit 1ff03fc

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

preview.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,26 @@ func (o Output) MarshalJSON() ([]byte, error) {
6868
})
6969
}
7070

71-
// ValidatePresets will iterate over each preset, validate the inputs as a set,
72-
// and attach any diagnostics to the preset if there are issues.
73-
func ValidatePresets(ctx context.Context, input Input, preValid []types.Preset, dir fs.FS) {
71+
// ValidatePrebuilds will iterate over each preset, validate the inputs as a set,
72+
// and attach any diagnostics to the preset if there are issues. This must be done
73+
// because prebuilds have to be valid without user input.
74+
//
75+
// This will only validate presets that have prebuilds configured and have no existing
76+
// error diagnostics.
77+
func ValidatePrebuilds(ctx context.Context, input Input, preValid []types.Preset, dir fs.FS) []types.Preset {
7478
for i := range preValid {
7579
pre := &preValid[i]
7680
if pre.Prebuild == nil || pre.Prebuild.Instances <= 0 {
7781
// No prebuilds, so presets do not need to be valid without user input
7882
continue
7983
}
8084

81-
input.ParameterValues = pre.Parameters
85+
if hcl.Diagnostics(pre.Diagnostics).HasErrors() {
86+
// Piling on diagnostics is not helpful, if an error exists, leave it at that.
87+
continue
88+
}
8289

90+
input.ParameterValues = pre.Parameters
8391
output, diagnostics := Preview(ctx, input, dir)
8492
if diagnostics.HasErrors() {
8593
pre.Diagnostics = append(pre.Diagnostics, diagnostics...)
@@ -102,6 +110,7 @@ func ValidatePresets(ctx context.Context, input Input, preValid []types.Preset,
102110
}
103111
}
104112
}
113+
return preValid
105114
}
106115

107116
func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagnostics hcl.Diagnostics) {

preview_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ func Test_Extract(t *testing.T) {
649649
}
650650
require.False(t, diags.HasErrors())
651651

652+
// Validate prebuilds too
653+
preview.ValidatePrebuilds(context.Background(), tc.input, output.Presets, dirFs)
654+
652655
if len(tc.warnings) > 0 {
653656
for _, w := range tc.warnings {
654657
idx := slices.IndexFunc(diags, func(diagnostic *hcl.Diagnostic) bool {
@@ -736,7 +739,7 @@ func TestPresetValidation(t *testing.T) {
736739
require.False(t, diags.HasErrors())
737740
require.Len(t, diags, 0)
738741

739-
preview.ValidatePresets(context.Background(), tc.input, output.Presets, dirFs)
742+
preview.ValidatePrebuilds(context.Background(), tc.input, output.Presets, dirFs)
740743
for _, preset := range output.Presets {
741744
check, ok := tc.presetAssert[preset.Name]
742745
require.True(t, ok, "unknown preset %s", preset.Name)

0 commit comments

Comments
 (0)