|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "io" |
| 5 | + "io/ioutil" |
4 | 6 | "os"
|
5 | 7 | "path/filepath"
|
6 | 8 | "testing"
|
@@ -220,6 +222,135 @@ func TestInitConfig(t *testing.T) {
|
220 | 222 | })
|
221 | 223 | }
|
222 | 224 |
|
| 225 | +func TestWriteConfig(t *testing.T) { |
| 226 | + t.Parallel() |
| 227 | + |
| 228 | + t.Run("save profile", func(t *testing.T) { |
| 229 | + t.Parallel() |
| 230 | + |
| 231 | + // Arrange |
| 232 | + c := Config{LogLevel: "info"} |
| 233 | + c.ConfigFileFlag = setupTempConfig(t, "./testdata/default-profile.toml") |
| 234 | + c.InitConfig() |
| 235 | + |
| 236 | + // Act |
| 237 | + c.Profile.TeamMode = "new_team_mode" |
| 238 | + err := c.Profile.SaveProfile() |
| 239 | + |
| 240 | + // Assert |
| 241 | + assert.NoError(t, err) |
| 242 | + contentBytes, _ := ioutil.ReadFile(c.viper.ConfigFileUsed()) |
| 243 | + assert.Contains(t, string(contentBytes), `workspace_mode = "new_team_mode"`) |
| 244 | + }) |
| 245 | + |
| 246 | + t.Run("use project", func(t *testing.T) { |
| 247 | + t.Parallel() |
| 248 | + |
| 249 | + // Arrange |
| 250 | + c := Config{LogLevel: "info"} |
| 251 | + c.ConfigFileFlag = setupTempConfig(t, "./testdata/default-profile.toml") |
| 252 | + c.InitConfig() |
| 253 | + |
| 254 | + // Act |
| 255 | + err := c.UseProject("new_team_id", "new_team_mode") |
| 256 | + |
| 257 | + // Assert |
| 258 | + assert.NoError(t, err) |
| 259 | + contentBytes, _ := ioutil.ReadFile(c.viper.ConfigFileUsed()) |
| 260 | + assert.Contains(t, string(contentBytes), `workspace_id = "new_team_id"`) |
| 261 | + }) |
| 262 | + |
| 263 | + t.Run("use profile", func(t *testing.T) { |
| 264 | + t.Parallel() |
| 265 | + |
| 266 | + // Arrange |
| 267 | + c := Config{LogLevel: "info"} |
| 268 | + c.ConfigFileFlag = setupTempConfig(t, "./testdata/multiple-profiles.toml") |
| 269 | + c.InitConfig() |
| 270 | + |
| 271 | + // Act |
| 272 | + c.Profile.Name = "account_3" |
| 273 | + err := c.Profile.UseProfile() |
| 274 | + |
| 275 | + // Assert |
| 276 | + assert.NoError(t, err) |
| 277 | + contentBytes, _ := ioutil.ReadFile(c.viper.ConfigFileUsed()) |
| 278 | + assert.Contains(t, string(contentBytes), `profile = "account_3"`) |
| 279 | + }) |
| 280 | + |
| 281 | + t.Run("remove profile", func(t *testing.T) { |
| 282 | + t.Parallel() |
| 283 | + |
| 284 | + // Arrange |
| 285 | + c := Config{LogLevel: "info"} |
| 286 | + c.ConfigFileFlag = setupTempConfig(t, "./testdata/multiple-profiles.toml") |
| 287 | + c.InitConfig() |
| 288 | + |
| 289 | + // Act |
| 290 | + err := c.Profile.RemoveProfile() |
| 291 | + |
| 292 | + // Assert |
| 293 | + assert.NoError(t, err) |
| 294 | + contentBytes, _ := ioutil.ReadFile(c.viper.ConfigFileUsed()) |
| 295 | + assert.NotContains(t, string(contentBytes), "account_2", `default profile "account_2" should be cleared`) |
| 296 | + assert.NotContains(t, string(contentBytes), `profile =`, `profile key should be cleared`) |
| 297 | + }) |
| 298 | + |
| 299 | + t.Run("remove profile multiple times", func(t *testing.T) { |
| 300 | + t.Parallel() |
| 301 | + |
| 302 | + // Arrange |
| 303 | + c := Config{LogLevel: "info"} |
| 304 | + c.ConfigFileFlag = setupTempConfig(t, "./testdata/multiple-profiles.toml") |
| 305 | + c.InitConfig() |
| 306 | + |
| 307 | + // Act |
| 308 | + err := c.Profile.RemoveProfile() |
| 309 | + |
| 310 | + // Assert |
| 311 | + assert.NoError(t, err) |
| 312 | + contentBytes, _ := ioutil.ReadFile(c.viper.ConfigFileUsed()) |
| 313 | + assert.NotContains(t, string(contentBytes), "account_2", `default profile "account_2" should be cleared`) |
| 314 | + assert.NotContains(t, string(contentBytes), `profile =`, `profile key should be cleared`) |
| 315 | + |
| 316 | + // Remove profile again |
| 317 | + |
| 318 | + c2 := Config{LogLevel: "info"} |
| 319 | + c2.ConfigFileFlag = c.ConfigFileFlag |
| 320 | + c2.InitConfig() |
| 321 | + err = c2.Profile.RemoveProfile() |
| 322 | + |
| 323 | + contentBytes, _ = ioutil.ReadFile(c2.viper.ConfigFileUsed()) |
| 324 | + assert.NoError(t, err) |
| 325 | + assert.NotContains(t, string(contentBytes), "[default]", `default profile "default" should be cleared`) |
| 326 | + assert.NotContains(t, string(contentBytes), `api_key = "test_api_key"`, `default profile "default" should be cleared`) |
| 327 | + |
| 328 | + // Now even though there are some profiles (account_1, account_3), when reading config |
| 329 | + // we won't register any profile. |
| 330 | + // TODO: Consider this case. It's not great UX. This may be an edge case only power users run into |
| 331 | + // given it requires users to be using multiple profiles. |
| 332 | + |
| 333 | + c3 := Config{LogLevel: "info"} |
| 334 | + c3.ConfigFileFlag = c.ConfigFileFlag |
| 335 | + c3.InitConfig() |
| 336 | + assert.Equal(t, "default", c3.Profile.Name, `profile should be "default"`) |
| 337 | + assert.Equal(t, "", c3.Profile.APIKey, "api key should be empty even though there are other profiles") |
| 338 | + }) |
| 339 | +} |
| 340 | + |
| 341 | +// ===== Test helpers ===== |
| 342 | + |
| 343 | +func setupTempConfig(t *testing.T, sourceConfigPath string) string { |
| 344 | + dir := t.TempDir() |
| 345 | + configPath := filepath.Join(dir, "config.toml") |
| 346 | + srcFile, _ := os.Open(sourceConfigPath) |
| 347 | + defer srcFile.Close() |
| 348 | + destFile, _ := os.Create(configPath) |
| 349 | + defer destFile.Close() |
| 350 | + io.Copy(destFile, srcFile) |
| 351 | + return configPath |
| 352 | +} |
| 353 | + |
223 | 354 | // ===== Mock FS =====
|
224 | 355 |
|
225 | 356 | // Mock fs where there's no config file, whether global or local
|
|
0 commit comments