Skip to content

Commit 8621801

Browse files
authored
fix(bump): run pre-bump extensions before plugin validation (#175)
Swapped execution order so pre-bump extension hooks run before pre-bump plugin validations. This allows extensions to set up state (e.g., modify .version file) that plugins need to validate. Also added logic to re-read the .version file after extensions run, in case an extension modified it. Fixes #173
1 parent 79ca2af commit 8621801

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

cmd/sley/bumpcmd/bump_plugins_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,52 @@ func TestSingleModuleBump_ValidateDependencyConsistencyFails(t *testing.T) {
562562
}
563563
}
564564

565+
func TestSingleModuleBump_ValidateDependencyConsistencyWithAutoSync(t *testing.T) {
566+
tmpDir := t.TempDir()
567+
versionPath := filepath.Join(tmpDir, ".version")
568+
testutils.WriteTempVersionFile(t, tmpDir, "1.0.0")
569+
570+
// Create package.json with different version
571+
pkgPath := filepath.Join(tmpDir, "package.json")
572+
if err := os.WriteFile(pkgPath, []byte(`{"version": "0.9.0"}`), 0644); err != nil {
573+
t.Fatal(err)
574+
}
575+
576+
// Save and restore
577+
origGetDependencyCheckerFn := dependencycheck.GetDependencyCheckerFn
578+
defer func() { dependencycheck.GetDependencyCheckerFn = origGetDependencyCheckerFn }()
579+
580+
// Create dependency checker with auto-sync enabled - should NOT fail on inconsistencies
581+
plugin := dependencycheck.NewDependencyChecker(&dependencycheck.Config{
582+
Enabled: true,
583+
AutoSync: true, // This should cause the validation to pass
584+
Files: []dependencycheck.FileConfig{
585+
{Path: pkgPath, Field: "version", Format: "json"},
586+
},
587+
})
588+
589+
cfg := &config.Config{Path: versionPath}
590+
registry := plugins.NewPluginRegistry()
591+
if err := registry.RegisterDependencyChecker(plugin); err != nil {
592+
t.Fatalf("failed to register dependency checker: %v", err)
593+
}
594+
appCli := testutils.BuildCLIForTests(cfg.Path, []*cli.Command{Run(cfg, registry)})
595+
596+
// With auto-sync enabled, bump should succeed even with inconsistent versions
597+
err := appCli.Run(context.Background(), []string{
598+
"sley", "bump", "major",
599+
})
600+
if err != nil {
601+
t.Errorf("expected no error with auto-sync enabled, got: %v", err)
602+
}
603+
604+
// Verify the version was bumped
605+
got := testutils.ReadTempVersionFile(t, tmpDir)
606+
if got != "2.0.0" {
607+
t.Errorf("expected version 2.0.0, got %q", got)
608+
}
609+
}
610+
565611
func TestValidateDependencyConsistency_AutoSyncSkipsError(t *testing.T) {
566612
tmpDir := t.TempDir()
567613

cmd/sley/bumpcmd/common.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ func executeSingleModuleBump(
5252
// Apply pre-release and build metadata
5353
newVersion.Build = calculateNewBuild(params.meta, params.preserveMeta, previousVersion.Build)
5454

55-
// Execute all pre-bump validations
56-
if err := executePreBumpValidations(registry, newVersion, previousVersion, params.bumpType); err != nil {
55+
// Run pre-bump extension hooks first - extensions may set up state that plugins need to validate
56+
if err := runPreBumpExtensionHooks(ctx, cfg, execCtx.Path, newVersion.String(), previousVersion.String(), params.bumpType, params.skipHooks); err != nil {
5757
return err
5858
}
5959

60-
// Run pre-bump extension hooks
61-
if err := runPreBumpExtensionHooks(ctx, cfg, execCtx.Path, newVersion.String(), previousVersion.String(), params.bumpType, params.skipHooks); err != nil {
60+
// Re-read the current version in case an extension modified the .version file
61+
// (e.g., an extension that fetches the latest release from GitHub and updates .version)
62+
if !params.skipHooks {
63+
previousVersion, err = semver.ReadVersion(execCtx.Path)
64+
if err != nil {
65+
return err
66+
}
67+
// Recalculate the new version based on the potentially updated previous version
68+
newVersion = params.versionCalc(previousVersion)
69+
newVersion.Build = calculateNewBuild(params.meta, params.preserveMeta, previousVersion.Build)
70+
}
71+
72+
// Execute all pre-bump validations after extensions have run
73+
if err := executePreBumpValidations(registry, newVersion, previousVersion, params.bumpType); err != nil {
6274
return err
6375
}
6476

0 commit comments

Comments
 (0)