Skip to content

Commit e382d43

Browse files
authored
Merge pull request #4384 from thaJeztah/config_sync
cli/config: add synchronisation for configDir (Dir, SetDir)
2 parents 8b4d29a + 13e842a commit e382d43

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

cli/config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ var (
3535
configDir string
3636
)
3737

38+
// resetConfigDir is used in testing to reset the "configDir" package variable
39+
// and its sync.Once to force re-lookup between tests.
40+
func resetConfigDir() {
41+
configDir = ""
42+
initConfigDir = new(sync.Once)
43+
}
44+
3845
// Dir returns the directory the configuration file is stored in
3946
func Dir() string {
4047
initConfigDir.Do(func() {
@@ -53,6 +60,8 @@ func ContextStoreDir() string {
5360

5461
// SetDir sets the directory the configuration file is stored in
5562
func SetDir(dir string) {
63+
// trigger the sync.Once to synchronise with Dir()
64+
initConfigDir.Do(func() {})
5665
configDir = filepath.Clean(dir)
5766
}
5867

cli/config/config_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,12 @@ func TestConfigPath(t *testing.T) {
382382

383383
SetDir(oldDir)
384384
}
385+
386+
// TestSetDir verifies that Dir() does not overwrite the value set through
387+
// SetDir() if it has not been run before.
388+
func TestSetDir(t *testing.T) {
389+
const expected = "my_config_dir"
390+
resetConfigDir()
391+
SetDir(expected)
392+
assert.Check(t, is.Equal(Dir(), expected))
393+
}

0 commit comments

Comments
 (0)