Skip to content

Commit d5e33d0

Browse files
committed
test: Methods related to config updates
1 parent 5006f6c commit d5e33d0

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-8
lines changed

pkg/config/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ func (c *Config) InitConfig() {
9090
}
9191

9292
// Read config file
93-
if err := c.viper.ReadInConfig(); err == nil {
94-
log.WithFields(log.Fields{
95-
"prefix": "config.Config.InitConfig",
96-
"path": c.viper.ConfigFileUsed(),
97-
}).Debug("Reading config file")
93+
log.WithFields(log.Fields{
94+
"prefix": "config.Config.InitConfig",
95+
"path": c.viper.ConfigFileUsed(),
96+
}).Debug("Reading config file")
97+
if err := c.viper.ReadInConfig(); err != nil {
98+
log.Fatal(err)
9899
}
99100

100101
// Construct the config struct
@@ -175,8 +176,8 @@ func (c *Config) writeConfig() error {
175176
}
176177

177178
log.WithFields(log.Fields{
178-
"prefix": "config.Config.WriteConfig",
179-
"path": c.viper.WriteConfig(),
179+
"prefix": "config.Config.writeConfig",
180+
"path": c.viper.ConfigFileUsed(),
180181
}).Debug("Writing config")
181182

182183
return c.viper.WriteConfig()

pkg/config/config_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"io"
5+
"io/ioutil"
46
"os"
57
"path/filepath"
68
"testing"
@@ -220,6 +222,135 @@ func TestInitConfig(t *testing.T) {
220222
})
221223
}
222224

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+
223354
// ===== Mock FS =====
224355

225356
// Mock fs where there's no config file, whether global or local

pkg/config/profile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

3-
import "github.com/hookdeck/hookdeck-cli/pkg/validators"
3+
import (
4+
"github.com/hookdeck/hookdeck-cli/pkg/validators"
5+
)
46

57
type Profile struct {
68
Name string // profile name

0 commit comments

Comments
 (0)