|
| 1 | +package bump |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/indaco/sley/internal/config" |
| 10 | + "github.com/indaco/sley/internal/plugins" |
| 11 | + "github.com/indaco/sley/internal/plugins/dependencycheck" |
| 12 | + "github.com/indaco/sley/internal/testutils" |
| 13 | + "github.com/urfave/cli/v3" |
| 14 | +) |
| 15 | + |
| 16 | +/* ------------------------------------------------------------------------- */ |
| 17 | +/* COORDINATED VERSIONING TESTS */ |
| 18 | +/* ------------------------------------------------------------------------- */ |
| 19 | + |
| 20 | +func TestCoordinatedVersioning_SingleRootBumpSyncsSubmodules(t *testing.T) { |
| 21 | + tmpDir := t.TempDir() |
| 22 | + |
| 23 | + // Create root .version file |
| 24 | + rootVersionPath := filepath.Join(tmpDir, ".version") |
| 25 | + if err := os.WriteFile(rootVersionPath, []byte("1.0.0\n"), 0644); err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + // Create submodule .version files (simulating coordinated versioning setup) |
| 30 | + submodules := []string{"services/api", "services/web"} |
| 31 | + for _, submod := range submodules { |
| 32 | + submodDir := filepath.Join(tmpDir, submod) |
| 33 | + if err := os.MkdirAll(submodDir, 0755); err != nil { |
| 34 | + t.Fatalf("failed to create submodule dir %s: %v", submodDir, err) |
| 35 | + } |
| 36 | + subVersionPath := filepath.Join(submodDir, ".version") |
| 37 | + if err := os.WriteFile(subVersionPath, []byte("1.0.0\n"), 0644); err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Create config with explicit root path (single-module mode) and dependency-check |
| 43 | + // configured to sync the submodule .version files (coordinated versioning) |
| 44 | + cfg := &config.Config{ |
| 45 | + Path: rootVersionPath, // Explicit path triggers single-module mode |
| 46 | + } |
| 47 | + |
| 48 | + // Configure dependency-check plugin with submodule .version files as sync targets |
| 49 | + depPlugin := dependencycheck.NewDependencyChecker(&dependencycheck.Config{ |
| 50 | + Enabled: true, |
| 51 | + AutoSync: true, |
| 52 | + Files: []dependencycheck.FileConfig{ |
| 53 | + {Path: filepath.Join(tmpDir, "services/api/.version"), Format: "raw"}, |
| 54 | + {Path: filepath.Join(tmpDir, "services/web/.version"), Format: "raw"}, |
| 55 | + }, |
| 56 | + }) |
| 57 | + |
| 58 | + registry := plugins.NewPluginRegistry() |
| 59 | + if err := registry.RegisterDependencyChecker(depPlugin); err != nil { |
| 60 | + t.Fatalf("failed to register dependency checker: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Build CLI with explicit path for single-module mode |
| 64 | + appCli := testutils.BuildCLIForTests(rootVersionPath, []*cli.Command{Run(cfg, registry)}) |
| 65 | + |
| 66 | + // Run single-module bump (no --all flag, no --module flag) |
| 67 | + err := testutils.RunCLITestAllowError(t, appCli, []string{ |
| 68 | + "sley", "bump", "patch", |
| 69 | + }, tmpDir) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("bump patch failed: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + // Verify root version was bumped |
| 75 | + rootData, err := os.ReadFile(rootVersionPath) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to read root .version: %v", err) |
| 78 | + } |
| 79 | + rootVersion := strings.TrimSpace(string(rootData)) |
| 80 | + if rootVersion != "1.0.1" { |
| 81 | + t.Errorf("expected root version '1.0.1', got %q", rootVersion) |
| 82 | + } |
| 83 | + |
| 84 | + // Verify all submodule .version files were synced to 1.0.1 |
| 85 | + for _, submod := range submodules { |
| 86 | + subVersionPath := filepath.Join(tmpDir, submod, ".version") |
| 87 | + subData, err := os.ReadFile(subVersionPath) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("failed to read %s/.version: %v", submod, err) |
| 90 | + } |
| 91 | + subVersion := strings.TrimSpace(string(subData)) |
| 92 | + if subVersion != "1.0.1" { |
| 93 | + t.Errorf("expected %s version '1.0.1', got %q", submod, subVersion) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestCoordinatedVersioning_MinorBumpSyncsSubmodules(t *testing.T) { |
| 99 | + tmpDir := t.TempDir() |
| 100 | + |
| 101 | + // Create root .version file |
| 102 | + rootVersionPath := filepath.Join(tmpDir, ".version") |
| 103 | + if err := os.WriteFile(rootVersionPath, []byte("1.2.3\n"), 0644); err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + |
| 107 | + // Create submodule .version files |
| 108 | + submodDir := filepath.Join(tmpDir, "packages/core") |
| 109 | + if err := os.MkdirAll(submodDir, 0755); err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + subVersionPath := filepath.Join(submodDir, ".version") |
| 113 | + if err := os.WriteFile(subVersionPath, []byte("1.2.3\n"), 0644); err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + cfg := &config.Config{ |
| 118 | + Path: rootVersionPath, |
| 119 | + } |
| 120 | + |
| 121 | + depPlugin := dependencycheck.NewDependencyChecker(&dependencycheck.Config{ |
| 122 | + Enabled: true, |
| 123 | + AutoSync: true, |
| 124 | + Files: []dependencycheck.FileConfig{ |
| 125 | + {Path: subVersionPath, Format: "raw"}, |
| 126 | + }, |
| 127 | + }) |
| 128 | + |
| 129 | + registry := plugins.NewPluginRegistry() |
| 130 | + if err := registry.RegisterDependencyChecker(depPlugin); err != nil { |
| 131 | + t.Fatalf("failed to register dependency checker: %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + appCli := testutils.BuildCLIForTests(rootVersionPath, []*cli.Command{Run(cfg, registry)}) |
| 135 | + |
| 136 | + // Run minor bump |
| 137 | + err := testutils.RunCLITestAllowError(t, appCli, []string{ |
| 138 | + "sley", "bump", "minor", |
| 139 | + }, tmpDir) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("bump minor failed: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + // Verify root version was bumped to 1.3.0 |
| 145 | + rootData, err := os.ReadFile(rootVersionPath) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("failed to read root .version: %v", err) |
| 148 | + } |
| 149 | + rootVersion := strings.TrimSpace(string(rootData)) |
| 150 | + if rootVersion != "1.3.0" { |
| 151 | + t.Errorf("expected root version '1.3.0', got %q", rootVersion) |
| 152 | + } |
| 153 | + |
| 154 | + // Verify submodule was synced |
| 155 | + subData, err := os.ReadFile(subVersionPath) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("failed to read packages/core/.version: %v", err) |
| 158 | + } |
| 159 | + subVersion := strings.TrimSpace(string(subData)) |
| 160 | + if subVersion != "1.3.0" { |
| 161 | + t.Errorf("expected packages/core version '1.3.0', got %q", subVersion) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestCoordinatedVersioning_SyncsManifestsAndVersionFiles(t *testing.T) { |
| 166 | + tmpDir := t.TempDir() |
| 167 | + |
| 168 | + // Create root .version file |
| 169 | + rootVersionPath := filepath.Join(tmpDir, ".version") |
| 170 | + if err := os.WriteFile(rootVersionPath, []byte("2.0.0\n"), 0644); err != nil { |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + |
| 174 | + // Create submodule .version file |
| 175 | + submodDir := filepath.Join(tmpDir, "app") |
| 176 | + if err := os.MkdirAll(submodDir, 0755); err != nil { |
| 177 | + t.Fatal(err) |
| 178 | + } |
| 179 | + subVersionPath := filepath.Join(submodDir, ".version") |
| 180 | + if err := os.WriteFile(subVersionPath, []byte("2.0.0\n"), 0644); err != nil { |
| 181 | + t.Fatal(err) |
| 182 | + } |
| 183 | + |
| 184 | + // Create package.json in the app directory |
| 185 | + pkgJSONPath := filepath.Join(submodDir, "package.json") |
| 186 | + if err := os.WriteFile(pkgJSONPath, []byte(`{"name": "app", "version": "2.0.0"}`), 0644); err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + |
| 190 | + cfg := &config.Config{ |
| 191 | + Path: rootVersionPath, |
| 192 | + } |
| 193 | + |
| 194 | + // Configure dependency-check to sync both .version files and manifests |
| 195 | + depPlugin := dependencycheck.NewDependencyChecker(&dependencycheck.Config{ |
| 196 | + Enabled: true, |
| 197 | + AutoSync: true, |
| 198 | + Files: []dependencycheck.FileConfig{ |
| 199 | + {Path: subVersionPath, Format: "raw"}, |
| 200 | + {Path: pkgJSONPath, Field: "version", Format: "json"}, |
| 201 | + }, |
| 202 | + }) |
| 203 | + |
| 204 | + registry := plugins.NewPluginRegistry() |
| 205 | + if err := registry.RegisterDependencyChecker(depPlugin); err != nil { |
| 206 | + t.Fatalf("failed to register dependency checker: %v", err) |
| 207 | + } |
| 208 | + |
| 209 | + appCli := testutils.BuildCLIForTests(rootVersionPath, []*cli.Command{Run(cfg, registry)}) |
| 210 | + |
| 211 | + // Run major bump |
| 212 | + err := testutils.RunCLITestAllowError(t, appCli, []string{ |
| 213 | + "sley", "bump", "major", |
| 214 | + }, tmpDir) |
| 215 | + if err != nil { |
| 216 | + t.Fatalf("bump major failed: %v", err) |
| 217 | + } |
| 218 | + |
| 219 | + // Verify root version was bumped to 3.0.0 |
| 220 | + rootData, err := os.ReadFile(rootVersionPath) |
| 221 | + if err != nil { |
| 222 | + t.Fatalf("failed to read root .version: %v", err) |
| 223 | + } |
| 224 | + rootVersion := strings.TrimSpace(string(rootData)) |
| 225 | + if rootVersion != "3.0.0" { |
| 226 | + t.Errorf("expected root version '3.0.0', got %q", rootVersion) |
| 227 | + } |
| 228 | + |
| 229 | + // Verify submodule .version was synced |
| 230 | + subData, err := os.ReadFile(subVersionPath) |
| 231 | + if err != nil { |
| 232 | + t.Fatalf("failed to read app/.version: %v", err) |
| 233 | + } |
| 234 | + subVersion := strings.TrimSpace(string(subData)) |
| 235 | + if subVersion != "3.0.0" { |
| 236 | + t.Errorf("expected app version '3.0.0', got %q", subVersion) |
| 237 | + } |
| 238 | + |
| 239 | + // Verify package.json was synced |
| 240 | + pkgData, err := os.ReadFile(pkgJSONPath) |
| 241 | + if err != nil { |
| 242 | + t.Fatalf("failed to read package.json: %v", err) |
| 243 | + } |
| 244 | + if !strings.Contains(string(pkgData), `"version": "3.0.0"`) { |
| 245 | + t.Errorf("expected package.json to contain version 3.0.0, got: %s", string(pkgData)) |
| 246 | + } |
| 247 | +} |
0 commit comments