Skip to content

Commit 31b6fdb

Browse files
committed
fix(init): validate inferred preset before --update writes anything
When the preset comes from Chart.yaml rather than --preset, an unknown name previously surfaced only in WritePresetLock — after Step 1 had already rewritten charts/talm/, leaving the project half-updated. Validate against the embedded presets up front, mirroring the --preset flag path, so the failure happens before any file is touched. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 957c873 commit 31b6fdb

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎pkg/commands/init.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,23 @@ func updateTalmLibraryChart() error {
13431343
// the same fact.
13441344
return err
13451345
}
1346+
1347+
// Validate the inferred preset BEFORE any writes. Without this
1348+
// fail-fast, an unknown preset only surfaces in WritePresetLock —
1349+
// after Step 1 has already rewritten charts/talm/, leaving the
1350+
// project half-updated.
1351+
availablePresets, err := generated.AvailablePresets()
1352+
if err != nil {
1353+
return errors.Wrap(err, "failed to get available presets")
1354+
}
1355+
1356+
if !isValidPreset(presetName, availablePresets) {
1357+
//nolint:wrapcheck // cockroachdb/errors.WithHint at boundary.
1358+
return errors.WithHint(
1359+
errors.Newf("preset %q from Chart.yaml is not embedded in this talm binary. Valid presets are: %v", presetName, availablePresets),
1360+
"pass --preset with one of the embedded presets, or use a talm build that ships your project's preset",
1361+
)
1362+
}
13461363
}
13471364

13481365
presetFiles, err := generated.PresetFiles()

‎pkg/commands/init_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,59 @@ func TestInitUpdate_PresetNotFoundError_SingleWrapped(t *testing.T) {
419419
}
420420
}
421421

422+
// TestInitUpdate_UnknownInferredPreset_FailsBeforeWrites pins the fail-fast
423+
// contract for `talm init --update` with the preset inferred from
424+
// Chart.yaml: a preset this binary does not embed must be rejected BEFORE
425+
// Step 1 touches charts/talm/. Without up-front validation the inferred
426+
// path rewrites the whole vendored library and only then fails in
427+
// WritePresetLock, leaving the project half-updated.
428+
func TestInitUpdate_UnknownInferredPreset_FailsBeforeWrites(t *testing.T) {
429+
imageOrig := initCmdFlags.image
430+
presetOrig := initCmdFlags.preset
431+
rootOrig := Config.RootDir
432+
t.Cleanup(func() {
433+
initCmdFlags.image = imageOrig
434+
initCmdFlags.preset = presetOrig
435+
Config.RootDir = rootOrig
436+
})
437+
438+
initCmdFlags.image = ""
439+
initCmdFlags.preset = ""
440+
441+
dir := t.TempDir()
442+
Config.RootDir = dir
443+
444+
chartYaml := "apiVersion: v2\nname: test\nversion: 0.1.0\n" +
445+
"dependencies:\n" +
446+
" - name: talm\n" +
447+
" version: 0.1.0\n" +
448+
" - name: no-such-preset\n" +
449+
" version: 0.1.0\n"
450+
if err := os.WriteFile(filepath.Join(dir, "Chart.yaml"), []byte(chartYaml), 0o644); err != nil {
451+
t.Fatalf("seed Chart.yaml: %v", err)
452+
}
453+
454+
err := updateTalmLibraryChart()
455+
if err == nil {
456+
t.Fatal("expected --update with an unknown inferred preset to error; got nil")
457+
}
458+
459+
if !strings.Contains(err.Error(), "no-such-preset") {
460+
t.Errorf("error must name the offending preset; got: %v", err)
461+
}
462+
463+
hints := errors.GetAllHints(err)
464+
if len(hints) == 0 {
465+
t.Fatalf("expected a hint guiding the operator to a valid preset; got bare error: %v", err)
466+
}
467+
468+
// Fail-fast contract: nothing may have been written before the
469+
// validation fired.
470+
if _, statErr := os.Stat(filepath.Join(dir, "charts")); !os.IsNotExist(statErr) {
471+
t.Error("charts/ was created before the inferred preset was validated; --update must fail before any writes")
472+
}
473+
}
474+
422475
// TestValidateImageRefShape_RejectsMalformed pins the shape check
423476
// that runs before any --image value reaches the preset rewrite.
424477
// Catches operator typos (::malformed, no-slash:tag, trailing

0 commit comments

Comments
 (0)