|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/spf13/pflag" |
| 11 | + "github.com/spf13/viper" |
| 12 | + |
11 | 13 | "github.com/timescale/tiger-cli/internal/tiger/util" |
12 | 14 | ) |
13 | 15 |
|
@@ -70,8 +72,10 @@ func TestLoad_DefaultValues(t *testing.T) { |
70 | 72 | if cfg.Analytics != DefaultAnalytics { |
71 | 73 | t.Errorf("Expected Analytics %t, got %t", DefaultAnalytics, cfg.Analytics) |
72 | 74 | } |
73 | | - if cfg.ReadOnly != DefaultReadOnly { |
74 | | - t.Errorf("Expected ReadOnly %t, got %t", DefaultReadOnly, cfg.ReadOnly) |
| 75 | + // setupTestConfig writes a config file without read_only, so it's |
| 76 | + // grandfathered to false (see MigrateReadOnly). |
| 77 | + if cfg.ReadOnly { |
| 78 | + t.Errorf("Expected ReadOnly false for pre-existing config file, got true") |
75 | 79 | } |
76 | 80 | if cfg.ConfigDir != tmpDir { |
77 | 81 | t.Errorf("Expected ConfigDir %s, got %s", tmpDir, cfg.ConfigDir) |
@@ -176,6 +180,153 @@ func TestLoad_MigrateVersionCheck(t *testing.T) { |
176 | 180 | } |
177 | 181 | } |
178 | 182 |
|
| 183 | +func TestLoad_MigrateReadOnly(t *testing.T) { |
| 184 | + tests := []struct { |
| 185 | + name string |
| 186 | + fileBody *string // nil means no config file |
| 187 | + env map[string]string |
| 188 | + want bool |
| 189 | + }{ |
| 190 | + { |
| 191 | + name: "no config file gets read-only default", |
| 192 | + fileBody: nil, |
| 193 | + want: DefaultReadOnly, |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "pre-existing config file without key is grandfathered", |
| 197 | + fileBody: util.Ptr("output: json\n"), |
| 198 | + want: false, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "explicit read_only in config file is respected", |
| 202 | + fileBody: util.Ptr("read_only: true\n"), |
| 203 | + want: true, |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "env var overrides grandfathered value", |
| 207 | + fileBody: util.Ptr("output: json\n"), |
| 208 | + env: map[string]string{"TIGER_READ_ONLY": "true"}, |
| 209 | + want: true, |
| 210 | + }, |
| 211 | + } |
| 212 | + |
| 213 | + for _, tt := range tests { |
| 214 | + t.Run(tt.name, func(t *testing.T) { |
| 215 | + tmpDir := t.TempDir() |
| 216 | + t.Cleanup(ResetGlobalConfig) |
| 217 | + |
| 218 | + if tt.fileBody != nil { |
| 219 | + configFile := GetConfigFile(tmpDir) |
| 220 | + if err := os.WriteFile(configFile, []byte(*tt.fileBody), 0644); err != nil { |
| 221 | + t.Fatalf("Failed to write config file: %v", err) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + os.Setenv("TIGER_CONFIG_DIR", tmpDir) |
| 226 | + t.Cleanup(func() { os.Unsetenv("TIGER_CONFIG_DIR") }) |
| 227 | + for k, val := range tt.env { |
| 228 | + os.Setenv(k, val) |
| 229 | + t.Cleanup(func() { os.Unsetenv(k) }) |
| 230 | + } |
| 231 | + |
| 232 | + setupViper(t, tmpDir) |
| 233 | + |
| 234 | + cfg, err := Load() |
| 235 | + if err != nil { |
| 236 | + t.Fatalf("Load() failed: %v", err) |
| 237 | + } |
| 238 | + if cfg.ReadOnly != tt.want { |
| 239 | + t.Errorf("ReadOnly = %t, want %t", cfg.ReadOnly, tt.want) |
| 240 | + } |
| 241 | + }) |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +// TestReadOnlyMaterializedOnWrite verifies that Set/Unset/Reset record |
| 246 | +// read_only explicitly, so rewritten files aren't grandfathered to false. |
| 247 | +func TestReadOnlyMaterializedOnWrite(t *testing.T) { |
| 248 | + loadReadOnly := func(t *testing.T, tmpDir string) bool { |
| 249 | + t.Helper() |
| 250 | + ResetGlobalConfig() |
| 251 | + setupViper(t, tmpDir) |
| 252 | + cfg, err := Load() |
| 253 | + if err != nil { |
| 254 | + t.Fatalf("Load() failed: %v", err) |
| 255 | + } |
| 256 | + return cfg.ReadOnly |
| 257 | + } |
| 258 | + |
| 259 | + setup := func(t *testing.T, fileBody *string) (string, *Config) { |
| 260 | + t.Helper() |
| 261 | + tmpDir := t.TempDir() |
| 262 | + t.Cleanup(ResetGlobalConfig) |
| 263 | + os.Setenv("TIGER_CONFIG_DIR", tmpDir) |
| 264 | + t.Cleanup(func() { os.Unsetenv("TIGER_CONFIG_DIR") }) |
| 265 | + if fileBody != nil { |
| 266 | + if err := os.WriteFile(GetConfigFile(tmpDir), []byte(*fileBody), 0644); err != nil { |
| 267 | + t.Fatalf("Failed to write config file: %v", err) |
| 268 | + } |
| 269 | + } |
| 270 | + setupViper(t, tmpDir) |
| 271 | + cfg, err := Load() |
| 272 | + if err != nil { |
| 273 | + t.Fatalf("Load() failed: %v", err) |
| 274 | + } |
| 275 | + return tmpDir, cfg |
| 276 | + } |
| 277 | + |
| 278 | + t.Run("Set on fresh install pins read-only default", func(t *testing.T) { |
| 279 | + tmpDir, cfg := setup(t, nil) |
| 280 | + if err := cfg.Set("output", "json"); err != nil { |
| 281 | + t.Fatalf("Set() failed: %v", err) |
| 282 | + } |
| 283 | + if got := loadReadOnly(t, tmpDir); got != DefaultReadOnly { |
| 284 | + t.Errorf("ReadOnly = %t after Set on fresh install, want %t", got, DefaultReadOnly) |
| 285 | + } |
| 286 | + }) |
| 287 | + |
| 288 | + t.Run("Set on grandfathered config pins false", func(t *testing.T) { |
| 289 | + tmpDir, cfg := setup(t, util.Ptr("service_id: svc-123\n")) |
| 290 | + if err := cfg.Set("output", "json"); err != nil { |
| 291 | + t.Fatalf("Set() failed: %v", err) |
| 292 | + } |
| 293 | + // Assert on the file itself rather than the loaded value: the |
| 294 | + // grandfathering shim would yield an effective false even if Set |
| 295 | + // failed to write the key. |
| 296 | + v := viper.New() |
| 297 | + v.SetConfigFile(GetConfigFile(tmpDir)) |
| 298 | + if err := v.ReadInConfig(); err != nil { |
| 299 | + t.Fatalf("Failed to read config file: %v", err) |
| 300 | + } |
| 301 | + if !v.InConfig("read_only") { |
| 302 | + t.Fatal("read_only not written to config file by Set") |
| 303 | + } |
| 304 | + if v.GetBool("read_only") { |
| 305 | + t.Error("read_only = true in config file after Set on grandfathered config, want false") |
| 306 | + } |
| 307 | + }) |
| 308 | + |
| 309 | + t.Run("Unset read_only restores current default", func(t *testing.T) { |
| 310 | + tmpDir, cfg := setup(t, util.Ptr("read_only: false\n")) |
| 311 | + if err := cfg.Unset("read_only"); err != nil { |
| 312 | + t.Fatalf("Unset() failed: %v", err) |
| 313 | + } |
| 314 | + if got := loadReadOnly(t, tmpDir); got != DefaultReadOnly { |
| 315 | + t.Errorf("ReadOnly = %t after Unset, want %t", got, DefaultReadOnly) |
| 316 | + } |
| 317 | + }) |
| 318 | + |
| 319 | + t.Run("Reset restores current default", func(t *testing.T) { |
| 320 | + tmpDir, cfg := setup(t, util.Ptr("read_only: false\noutput: json\n")) |
| 321 | + if err := cfg.Reset(); err != nil { |
| 322 | + t.Fatalf("Reset() failed: %v", err) |
| 323 | + } |
| 324 | + if got := loadReadOnly(t, tmpDir); got != DefaultReadOnly { |
| 325 | + t.Errorf("ReadOnly = %t after Reset, want %t", got, DefaultReadOnly) |
| 326 | + } |
| 327 | + }) |
| 328 | +} |
| 329 | + |
179 | 330 | func TestLoad_FromEnvironmentVariables(t *testing.T) { |
180 | 331 | tmpDir := setupTestConfig(t) |
181 | 332 |
|
|
0 commit comments