Commit eab626deed
Unsigned
Layout: unified · split
Build/curd-windows-build.iss +1 −1
| @@ -1,6 +1,6 @@ | |||
| 1 | [Setup] | 1 | [Setup] |
| 2 | AppName=Curd Installer | 2 | AppName=Curd Installer |
| 3 | AppVersion=2.0.2 | 3 | AppVersion=2.0.3 |
| 4 | DefaultDirName={userappdata}\Curd | 4 | DefaultDirName={userappdata}\Curd |
| 5 | PrivilegesRequired=lowest | 5 | PrivilegesRequired=lowest |
| 6 | AllowNoIcons=yes | 6 | AllowNoIcons=yes |
README.md +1 −1
| @@ -378,7 +378,7 @@ If the browser reaches the localhost callback page but curd does not continue au | |||
| 378 | | `AnimeNameLanguage` | Enum | `english`, `romaji` | Sets the preferred language for anime names. | | 378 | | `AnimeNameLanguage` | Enum | `english`, `romaji` | Sets the preferred language for anime names. | |
| 379 | | `MpvArgs` | List | all mpv args eg ["--fullscreen=yes", "--mute=yes"] | Add args to mpv player | | 379 | | `MpvArgs` | List | all mpv args eg ["--fullscreen=yes", "--mute=yes"] | Add args to mpv player | |
| 380 | | `MpvPlaybackStartTimeout` | Integer | `1` to `600` (seconds) | How long to wait for MPV playback to start before trying another provider. Default: `20` | | 380 | | `MpvPlaybackStartTimeout` | Integer | `1` to `600` (seconds) | How long to wait for MPV playback to start before trying another provider. Default: `20` | |
| 381 | | `AddMissingOptions` | Boolean | `true`, `false` | When `true`, any **new** default keys missing from `curd.conf` are **appended once** (never a full rewrite every start). Set `false` to opt out of writing new keys. Defaults still apply in memory always. | | 381 | | `AddMissingOptions` | Boolean | `true`, `false` | When `true`, on a **version upgrade** only options registered as new for that release are **appended** to `curd.conf` (not every historical default). Set `false` to opt out. Defaults still apply in memory always. | |
| 382 | | `VimKeys` | Boolean | `true`, `false` | When `true`, selection menus use vim motions: `j`/`k`/`h`/`l` (and arrows) to move, `/` or `?` to search. Default: `false` (type-to-filter immediately). Add `VimKeys=true` to `~/.config/curd/curd.conf` to enable. | | 382 | | `VimKeys` | Boolean | `true`, `false` | When `true`, selection menus use vim motions: `j`/`k`/`h`/`l` (and arrows) to move, `/` or `?` to search. Default: `false` (type-to-filter immediately). Add `VimKeys=true` to `~/.config/curd/curd.conf` to enable. | |
| 383 | | `AlternateScreen` | Boolean | `true`, `false` | Toggles the use of an alternate screen buffer for cleaner UI. | | 383 | | `AlternateScreen` | Boolean | `true`, `false` | Toggles the use of an alternate screen buffer for cleaner UI. | |
| 384 | | `RofiSelection` | Boolean | `true`, `false` | Enables or disables anime selection via Rofi. | | 384 | | `RofiSelection` | Boolean | `true`, `false` | Enables or disables anime selection via Rofi. | |
VERSION.txt +1 −1
| @@ -1 +1 @@ | |||
| 1 | 2.0.2\ No newline at end of file | 1 | 2.0.3 |
cmd/curd/main.go +1 −1
| @@ -17,7 +17,7 @@ var version string // Will be set by ldflags during build | |||
| 17 | 17 | ||
| 18 | func resolvedVersion() string { | 18 | func resolvedVersion() string { |
| 19 | if version == "" { | 19 | if version == "" { |
| 20 | return "2.0.2" | 20 | return "2.0.3" |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | return version | 23 | return version |
internal/config.go +4 −14
| @@ -283,20 +283,10 @@ func LoadConfig(configPath string) (CurdConfig, error) { | |||
| 283 | } | 283 | } |
| 284 | } | 284 | } |
| 285 | 285 | ||
| 286 | // Append brand-new default keys (e.g. VimKeys) once when absent — never rewrite the | 286 | // New config keys are NOT bulk-appended here. Only MigrateOnVersionUpgrade |
| 287 | // whole file for them. After they're on disk, subsequent starts do nothing. | 287 | // injects options registered in configOptionsIntroducedInVersion() for the |
| 288 | if addMissing && !createdConfig { | 288 | // versions the user actually crossed — so sparse configs never get every |
| 289 | if added := injectMissingConfigDefaults(fileMap); len(added) > 0 { | 289 | // historical default dumped in on a normal launch. |
| 290 | // Keep workMap in sync with anything just injected. | ||
| 291 | for _, key := range added { | ||
| 292 | workMap[key] = fileMap[key] | ||
| 293 | } | ||
| 294 | if err := appendConfigKeys(configPath, fileMap, added); err != nil { | ||
| 295 | return CurdConfig{}, fmt.Errorf("error appending new config options: %v", err) | ||
| 296 | } | ||
| 297 | Log(fmt.Sprintf("Appended new config options: %s", strings.Join(added, ", "))) | ||
| 298 | } | ||
| 299 | } | ||
| 300 | 290 | ||
| 301 | // Persist legacy tracking / provider-token normalize (rewrite only those cases). | 291 | // Persist legacy tracking / provider-token normalize (rewrite only those cases). |
| 302 | if addMissing && updated { | 292 | if addMissing && updated { |
internal/provider_migrate.go +48 −13
| @@ -106,25 +106,58 @@ func writeStoredCurdVersion(storagePath, version string) error { | |||
| 106 | return os.WriteFile(storageVersionFilePath(storagePath), []byte(version+"\n"), 0644) | 106 | return os.WriteFile(storageVersionFilePath(storagePath), []byte(version+"\n"), 0644) |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | // injectMissingConfigDefaults adds any defaultConfigMap keys that are absent from | 109 | // configOptionsIntroducedInVersion maps config keys to the first curd release that |
| 110 | // configMap. Returns the sorted list of newly injected keys. | 110 | // introduced them. Only these keys are eligible for automatic append on upgrade. |
| 111 | func injectMissingConfigDefaults(configMap map[string]string) []string { | 111 | // Baseline options (Player, StoragePath, …) are NOT listed — they only appear via |
| 112 | // createDefaultConfig for brand-new installs, never re-appended into sparse configs. | ||
| 113 | // | ||
| 114 | // When adding a new option: | ||
| 115 | // 1. Add it to CurdConfig + defaultConfigMap() | ||
| 116 | // 2. Register it here under the release version that ships it | ||
| 117 | // 3. Bump VERSION.txt so MigrateOnVersionUpgrade runs for existing users | ||
| 118 | func configOptionsIntroducedInVersion() map[string]string { | ||
| 119 | return map[string]string{ | ||
| 120 | // 2.0.3 — playback fallback timeout + vim selection motions | ||
| 121 | "MpvPlaybackStartTimeout": "2.0.3", | ||
| 122 | "VimKeys": "2.0.3", | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | // injectConfigOptionsSince appends defaults for options introduced in versions | ||
| 127 | // (fromVersion, toVersion] that are still missing from configMap. | ||
| 128 | // Example: from 2.0.2 → 2.0.3 injects only keys introduced in 2.0.3, not every | ||
| 129 | // historical default. | ||
| 130 | func injectConfigOptionsSince(configMap map[string]string, fromVersion, toVersion string) []string { | ||
| 112 | if configMap == nil { | 131 | if configMap == nil { |
| 113 | return nil | 132 | return nil |
| 114 | } | 133 | } |
| 115 | defaults := defaultConfigMap() | 134 | defaults := defaultConfigMap() |
| 116 | keys := make([]string, 0, len(defaults)) | 135 | introduced := configOptionsIntroducedInVersion() |
| 117 | for key := range defaults { | 136 | |
| 137 | keys := make([]string, 0, len(introduced)) | ||
| 138 | for key := range introduced { | ||
| 118 | keys = append(keys, key) | 139 | keys = append(keys, key) |
| 119 | } | 140 | } |
| 120 | sort.Strings(keys) | 141 | sort.Strings(keys) |
| 121 | 142 | ||
| 122 | added := make([]string, 0) | 143 | added := make([]string, 0) |
| 123 | for _, key := range keys { | 144 | for _, key := range keys { |
| 145 | since := introduced[key] | ||
| 146 | // Only options born after the user's previous version and at/before this release. | ||
| 147 | if !versionLess(fromVersion, since) { | ||
| 148 | continue // introduced at or before fromVersion — user already "passed" that release | ||
| 149 | } | ||
| 150 | if !versionLessOrEqual(since, toVersion) { | ||
| 151 | continue // not shipped yet in toVersion | ||
| 152 | } | ||
| 124 | if _, exists := configMap[key]; exists { | 153 | if _, exists := configMap[key]; exists { |
| 125 | continue | 154 | continue |
| 126 | } | 155 | } |
| 127 | configMap[key] = defaults[key] | 156 | value, ok := defaults[key] |
| 157 | if !ok { | ||
| 158 | continue | ||
| 159 | } | ||
| 160 | configMap[key] = value | ||
| 128 | added = append(added, key) | 161 | added = append(added, key) |
| 129 | } | 162 | } |
| 130 | return added | 163 | return added |
| @@ -155,9 +188,9 @@ func appendConfigKeys(configPath string, configMap map[string]string, keys []str | |||
| 155 | } | 188 | } |
| 156 | 189 | ||
| 157 | // MigrateOnVersionUpgrade updates stored state and config when curd is upgraded. | 190 | // MigrateOnVersionUpgrade updates stored state and config when curd is upgraded. |
| 158 | // On version change it injects any new default config options (append-only) and | 191 | // On version change it appends only config options registered as introduced in |
| 159 | // runs provider migrations. Same-version launches do not rewrite the config file | 192 | // versions (storedVersion, appVersion], then runs provider migrations. |
| 160 | // just to fill defaults — that avoids churning user config every start. | 193 | // Same-version launches do not rewrite the config file. |
| 161 | // Returns whether the config file was updated. | 194 | // Returns whether the config file was updated. |
| 162 | func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion string) (bool, error) { | 195 | func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion string) (bool, error) { |
| 163 | if config == nil { | 196 | if config == nil { |
| @@ -176,8 +209,9 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s | |||
| 176 | 209 | ||
| 177 | storedVersion := readStoredCurdVersion(storagePath) | 210 | storedVersion := readStoredCurdVersion(storagePath) |
| 178 | configUpdated := false | 211 | configUpdated := false |
| 212 | versionChanged := storedVersion != appVersion | ||
| 179 | 213 | ||
| 180 | if storedVersion != appVersion && strings.TrimSpace(configPath) != "" { | 214 | if versionChanged && strings.TrimSpace(configPath) != "" { |
| 181 | configMap, err := LoadConfigFromFile(configPath) | 215 | configMap, err := LoadConfigFromFile(configPath) |
| 182 | if err != nil { | 216 | if err != nil { |
| 183 | return false, err | 217 | return false, err |
| @@ -195,12 +229,13 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s | |||
| 195 | } | 229 | } |
| 196 | 230 | ||
| 197 | if addMissing { | 231 | if addMissing { |
| 198 | if added := injectMissingConfigDefaults(configMap); len(added) > 0 { | 232 | if added := injectConfigOptionsSince(configMap, storedVersion, appVersion); len(added) > 0 { |
| 199 | if err := appendConfigKeys(configPath, configMap, added); err != nil { | 233 | if err := appendConfigKeys(configPath, configMap, added); err != nil { |
| 200 | return false, fmt.Errorf("append new config options: %w", err) | 234 | return false, fmt.Errorf("append new config options: %w", err) |
| 201 | } | 235 | } |
| 202 | configUpdated = true | 236 | configUpdated = true |
| 203 | Log(fmt.Sprintf("Injected new config options on upgrade to %s: %s", appVersion, strings.Join(added, ", "))) | 237 | Log(fmt.Sprintf("Injected config options for upgrade %s → %s: %s", |
| 238 | storedVersion, appVersion, strings.Join(added, ", "))) | ||
| 204 | } | 239 | } |
| 205 | } | 240 | } |
| 206 | 241 | ||
| @@ -217,7 +252,7 @@ func MigrateOnVersionUpgrade(configPath string, config *CurdConfig, appVersion s | |||
| 217 | next := PopulateConfig(configMap) | 252 | next := PopulateConfig(configMap) |
| 218 | normalizeTrackingConfig(&next) | 253 | normalizeTrackingConfig(&next) |
| 219 | *config = next | 254 | *config = next |
| 220 | } else if storedVersion != appVersion { | 255 | } else if versionChanged { |
| 221 | // No config path, still run in-memory provider migration. | 256 | // No config path, still run in-memory provider migration. |
| 222 | if nextProvider, changed := migrateProviderConfig(config.Provider); changed { | 257 | if nextProvider, changed := migrateProviderConfig(config.Provider); changed { |
| 223 | config.Provider = nextProvider | 258 | config.Provider = nextProvider |
internal/provider_migrate_test.go +51 −40
| @@ -114,11 +114,10 @@ func TestMigrateOnVersionUpgradeWritesVersionAndUpdatesProvider(t *testing.T) { | |||
| 114 | } | 114 | } |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) { | 117 | func TestLoadConfigDoesNotDumpAllDefaultsIntoSparseFile(t *testing.T) { |
| 118 | tempDir := t.TempDir() | 118 | tempDir := t.TempDir() |
| 119 | configPath := filepath.Join(tempDir, "curd.conf") | 119 | configPath := filepath.Join(tempDir, "curd.conf") |
| 120 | storagePath := filepath.Join(tempDir, "share") | 120 | storagePath := filepath.Join(tempDir, "share") |
| 121 | // Minimal config: no VimKeys — must be appended on load (not a full rewrite). | ||
| 122 | initial := "StoragePath=" + storagePath + "\n" + | 121 | initial := "StoragePath=" + storagePath + "\n" + |
| 123 | "AddMissingOptions=true\n" + | 122 | "AddMissingOptions=true\n" + |
| 124 | "Provider=stacked\n" + | 123 | "Provider=stacked\n" + |
| @@ -131,8 +130,9 @@ func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) { | |||
| 131 | if err != nil { | 130 | if err != nil { |
| 132 | t.Fatalf("LoadConfig: %v", err) | 131 | t.Fatalf("LoadConfig: %v", err) |
| 133 | } | 132 | } |
| 133 | // In-memory defaults still apply. | ||
| 134 | if config.VimKeys { | 134 | if config.VimKeys { |
| 135 | t.Fatal("VimKeys default should be false") | 135 | t.Fatal("VimKeys default should be false in memory") |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | after, err := os.ReadFile(configPath) | 138 | after, err := os.ReadFile(configPath) |
| @@ -140,32 +140,19 @@ func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) { | |||
| 140 | t.Fatalf("read config: %v", err) | 140 | t.Fatalf("read config: %v", err) |
| 141 | } | 141 | } |
| 142 | text := string(after) | 142 | text := string(after) |
| 143 | if !strings.Contains(text, "VimKeys=false") { | 143 | // Sparse file must not gain every historical default on a normal load. |
| 144 | t.Fatalf("expected VimKeys=false in config file:\n%s", text) | 144 | if strings.Contains(text, "VimKeys=") { |
| 145 | } | 145 | t.Fatalf("LoadConfig must not append versioned options without an upgrade:\n%s", text) |
| 146 | // Original lines preserved (append-only). | ||
| 147 | if !strings.Contains(text, "Player=mpv") || !strings.Contains(text, "StoragePath="+storagePath) { | ||
| 148 | t.Fatalf("original keys should remain:\n%s", text) | ||
| 149 | } | ||
| 150 | |||
| 151 | // Second load must not duplicate VimKeys. | ||
| 152 | if _, err := LoadConfig(configPath); err != nil { | ||
| 153 | t.Fatalf("second LoadConfig: %v", err) | ||
| 154 | } | ||
| 155 | after2, err := os.ReadFile(configPath) | ||
| 156 | if err != nil { | ||
| 157 | t.Fatalf("read config again: %v", err) | ||
| 158 | } | 146 | } |
| 159 | if strings.Count(string(after2), "VimKeys=") != 1 { | 147 | if strings.Contains(text, "SkipOp=") { |
| 160 | t.Fatalf("VimKeys should appear once, got:\n%s", after2) | 148 | t.Fatalf("LoadConfig must not dump baseline defaults into sparse configs:\n%s", text) |
| 161 | } | 149 | } |
| 162 | } | 150 | } |
| 163 | 151 | ||
| 164 | func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { | 152 | func TestMigrateOnVersionUpgradeInjectsOnlyOptionsForCrossedVersions(t *testing.T) { |
| 165 | tempDir := t.TempDir() | 153 | tempDir := t.TempDir() |
| 166 | configPath := filepath.Join(tempDir, "curd.conf") | 154 | configPath := filepath.Join(tempDir, "curd.conf") |
| 167 | storagePath := filepath.Join(tempDir, "share") | 155 | storagePath := filepath.Join(tempDir, "share") |
| 168 | // Full-enough config written first so LoadConfig isn't under test here. | ||
| 169 | initial := "StoragePath=" + storagePath + "\n" + | 156 | initial := "StoragePath=" + storagePath + "\n" + |
| 170 | "AddMissingOptions=true\n" + | 157 | "AddMissingOptions=true\n" + |
| 171 | "Provider=stacked\n" + | 158 | "Provider=stacked\n" + |
| @@ -173,11 +160,10 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { | |||
| 173 | if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil { | 160 | if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil { |
| 174 | t.Fatalf("write config: %v", err) | 161 | t.Fatalf("write config: %v", err) |
| 175 | } | 162 | } |
| 176 | // Pretend we're already on an older stored version with a sparse file. | ||
| 177 | if err := os.MkdirAll(storagePath, 0755); err != nil { | 163 | if err := os.MkdirAll(storagePath, 0755); err != nil { |
| 178 | t.Fatalf("mkdir storage: %v", err) | 164 | t.Fatalf("mkdir storage: %v", err) |
| 179 | } | 165 | } |
| 180 | if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.0\n"), 0644); err != nil { | 166 | if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.2\n"), 0644); err != nil { |
| 181 | t.Fatalf("write version: %v", err) | 167 | t.Fatalf("write version: %v", err) |
| 182 | } | 168 | } |
| 183 | 169 | ||
| @@ -188,17 +174,12 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { | |||
| 188 | "Player": "mpv", | 174 | "Player": "mpv", |
| 189 | }) | 175 | }) |
| 190 | 176 | ||
| 191 | // Wipe keys that LoadConfig would have added so migration is the injector. | ||
| 192 | if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil { | ||
| 193 | t.Fatalf("rewrite sparse config: %v", err) | ||
| 194 | } | ||
| 195 | |||
| 196 | updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3") | 177 | updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |
| 197 | if err != nil { | 178 | if err != nil { |
| 198 | t.Fatalf("MigrateOnVersionUpgrade: %v", err) | 179 | t.Fatalf("MigrateOnVersionUpgrade: %v", err) |
| 199 | } | 180 | } |
| 200 | if !updated { | 181 | if !updated { |
| 201 | t.Fatal("expected config file to gain new options on version upgrade") | 182 | t.Fatal("expected config file to gain 2.0.3 options on upgrade") |
| 202 | } | 183 | } |
| 203 | 184 | ||
| 204 | after, err := os.ReadFile(configPath) | 185 | after, err := os.ReadFile(configPath) |
| @@ -207,10 +188,16 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { | |||
| 207 | } | 188 | } |
| 208 | text := string(after) | 189 | text := string(after) |
| 209 | if !strings.Contains(text, "VimKeys=false") { | 190 | if !strings.Contains(text, "VimKeys=false") { |
| 210 | t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text) | 191 | t.Fatalf("expected VimKeys=false for 2.0.3 upgrade:\n%s", text) |
| 192 | } | ||
| 193 | if !strings.Contains(text, "MpvPlaybackStartTimeout=20") { | ||
| 194 | t.Fatalf("expected MpvPlaybackStartTimeout for 2.0.3 upgrade:\n%s", text) | ||
| 195 | } | ||
| 196 | // Must NOT dump unrelated baseline keys. | ||
| 197 | if strings.Contains(text, "SkipOp=") || strings.Contains(text, "DiscordPresence=") { | ||
| 198 | t.Fatalf("upgrade must not inject unregistered baseline options:\n%s", text) | ||
| 211 | } | 199 | } |
| 212 | 200 | ||
| 213 | // Same version: no further rewrite. | ||
| 214 | updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3") | 201 | updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |
| 215 | if err != nil { | 202 | if err != nil { |
| 216 | t.Fatalf("second migrate: %v", err) | 203 | t.Fatalf("second migrate: %v", err) |
| @@ -220,20 +207,44 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { | |||
| 220 | } | 207 | } |
| 221 | } | 208 | } |
| 222 | 209 | ||
| 223 | func TestInjectMissingConfigDefaultsIdempotent(t *testing.T) { | 210 | func TestInjectConfigOptionsSinceOnlyCrossedVersions(t *testing.T) { |
| 224 | m := map[string]string{"Player": "mpv"} | 211 | m := map[string]string{"Player": "mpv"} |
| 225 | added := injectMissingConfigDefaults(m) | 212 | // Already on 2.0.3 — nothing new. |
| 226 | if len(added) == 0 { | 213 | if added := injectConfigOptionsSince(m, "2.0.3", "2.0.3"); len(added) != 0 { |
| 227 | t.Fatal("expected missing keys to be injected") | 214 | t.Fatalf("same version should inject nothing, got %v", added) |
| 228 | } | 215 | } |
| 229 | if _, ok := m["VimKeys"]; !ok { | 216 | // 2.0.2 → 2.0.3 gets VimKeys + timeout only. |
| 230 | t.Fatal("expected VimKeys default") | 217 | added := injectConfigOptionsSince(m, "2.0.2", "2.0.3") |
| 218 | if len(added) != 2 { | ||
| 219 | t.Fatalf("expected 2 options for 2.0.2→2.0.3, got %v", added) | ||
| 220 | } | ||
| 221 | want := map[string]bool{"VimKeys": true, "MpvPlaybackStartTimeout": true} | ||
| 222 | for _, key := range added { | ||
| 223 | if !want[key] { | ||
| 224 | t.Fatalf("unexpected injected key %q in %v", key, added) | ||
| 225 | } | ||
| 231 | } | 226 | } |
| 232 | if second := injectMissingConfigDefaults(m); len(second) != 0 { | 227 | // Already present keys are skipped. |
| 228 | if second := injectConfigOptionsSince(m, "2.0.2", "2.0.3"); len(second) != 0 { | ||
| 233 | t.Fatalf("second inject should be empty, got %v", second) | 229 | t.Fatalf("second inject should be empty, got %v", second) |
| 234 | } | 230 | } |
| 235 | } | 231 | } |
| 236 | 232 | ||
| 233 | func TestCompareVersionsOrdering(t *testing.T) { | ||
| 234 | if !versionLess("2.0.2", "2.0.3") { | ||
| 235 | t.Fatal("2.0.2 should be < 2.0.3") | ||
| 236 | } | ||
| 237 | if !versionLess("", "2.0.3") { | ||
| 238 | t.Fatal("empty stored version should be older than a release") | ||
| 239 | } | ||
| 240 | if versionLess("2.0.3", "2.0.3") { | ||
| 241 | t.Fatal("equal versions should not be less") | ||
| 242 | } | ||
| 243 | if !versionLessOrEqual("2.0.3", "2.0.3") { | ||
| 244 | t.Fatal("equal versions should be <= ") | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 237 | func TestConfiguredProviderNamesUsesStackedByDefault(t *testing.T) { | 248 | func TestConfiguredProviderNamesUsesStackedByDefault(t *testing.T) { |
| 238 | withAllProvidersEnabledForTest(t) | 249 | withAllProvidersEnabledForTest(t) |
| 239 | got := ConfiguredProviderNames(&CurdConfig{}) | 250 | got := ConfiguredProviderNames(&CurdConfig{}) |
internal/version.go +84
| @@ -2,6 +2,7 @@ package internal | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "path/filepath" | 4 | "path/filepath" |
| 5 | "strconv" | ||
| 5 | "strings" | 6 | "strings" |
| 6 | ) | 7 | ) |
| 7 | 8 | ||
| @@ -26,3 +27,86 @@ func CurdVersion() string { | |||
| 26 | func storageVersionFilePath(storagePath string) string { | 27 | func storageVersionFilePath(storagePath string) string { |
| 27 | return filepath.Join(strings.TrimSpace(storagePath), "curd_version") | 28 | return filepath.Join(strings.TrimSpace(storagePath), "curd_version") |
| 28 | } | 29 | } |
| 30 | |||
| 31 | // parseVersionParts parses "2.0.3" / "v2.0.3-rc1" into [major, minor, patch]. | ||
| 32 | // Non-numeric suffixes are ignored. Unparseable versions return nil. | ||
| 33 | func parseVersionParts(version string) []int { | ||
| 34 | version = strings.TrimSpace(version) | ||
| 35 | version = strings.TrimPrefix(version, "v") | ||
| 36 | version = strings.TrimPrefix(version, "V") | ||
| 37 | if version == "" || strings.EqualFold(version, "dev") { | ||
| 38 | return nil | ||
| 39 | } | ||
| 40 | // Drop pre-release / build metadata: 2.0.3-rc1+meta → 2.0.3 | ||
| 41 | if i := strings.IndexAny(version, "-+"); i >= 0 { | ||
| 42 | version = version[:i] | ||
| 43 | } | ||
| 44 | parts := strings.Split(version, ".") | ||
| 45 | if len(parts) == 0 { | ||
| 46 | return nil | ||
| 47 | } | ||
| 48 | out := make([]int, 0, 3) | ||
| 49 | for i := 0; i < 3; i++ { | ||
| 50 | if i >= len(parts) { | ||
| 51 | out = append(out, 0) | ||
| 52 | continue | ||
| 53 | } | ||
| 54 | n, err := strconv.Atoi(parts[i]) | ||
| 55 | if err != nil { | ||
| 56 | return nil | ||
| 57 | } | ||
| 58 | out = append(out, n) | ||
| 59 | } | ||
| 60 | return out | ||
| 61 | } | ||
| 62 | |||
| 63 | // compareVersions returns -1 if a<b, 0 if a==b, 1 if a>b. | ||
| 64 | // Unparseable versions compare as equal to each other and less than parseable ones | ||
| 65 | // only when empty; "dev" is treated as greater than any release (no auto-inject from dev churn). | ||
| 66 | func compareVersions(a, b string) int { | ||
| 67 | a = strings.TrimSpace(a) | ||
| 68 | b = strings.TrimSpace(b) | ||
| 69 | if a == b { | ||
| 70 | return 0 | ||
| 71 | } | ||
| 72 | ap := parseVersionParts(a) | ||
| 73 | bp := parseVersionParts(b) | ||
| 74 | if ap == nil && bp == nil { | ||
| 75 | if a < b { | ||
| 76 | return -1 | ||
| 77 | } | ||
| 78 | if a > b { | ||
| 79 | return 1 | ||
| 80 | } | ||
| 81 | return 0 | ||
| 82 | } | ||
| 83 | if ap == nil { | ||
| 84 | // empty/unknown stored version → treat as older than any real release | ||
| 85 | if a == "" { | ||
| 86 | return -1 | ||
| 87 | } | ||
| 88 | // "dev" or garbage: treat as equal to itself only; vs release → greater (skip inject churn) | ||
| 89 | return 1 | ||
| 90 | } | ||
| 91 | if bp == nil { | ||
| 92 | if b == "" { | ||
| 93 | return 1 | ||
| 94 | } | ||
| 95 | return -1 | ||
| 96 | } | ||
| 97 | for i := 0; i < 3; i++ { | ||
| 98 | if ap[i] < bp[i] { | ||
| 99 | return -1 | ||
| 100 | } | ||
| 101 | if ap[i] > bp[i] { | ||
| 102 | return 1 | ||
| 103 | } | ||
| 104 | } | ||
| 105 | return 0 | ||
| 106 | } | ||
| 107 | |||
| 108 | // versionLess reports whether a is strictly older than b. | ||
| 109 | func versionLess(a, b string) bool { return compareVersions(a, b) < 0 } | ||
| 110 | |||
| 111 | // versionLessOrEqual reports whether a is older than or equal to b. | ||
| 112 | func versionLessOrEqual(a, b string) bool { return compareVersions(a, b) <= 0 } | ||