Merge pull request #4384 from thaJeztah/config_sync

cli/config: add synchronisation for configDir (Dir, SetDir)
This commit is contained in:
Sebastiaan van Stijn 2023-06-28 14:11:36 +02:00 committed by GitHub
commit e382d43f20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -35,6 +35,13 @@ var (
configDir string
)
// resetConfigDir is used in testing to reset the "configDir" package variable
// and its sync.Once to force re-lookup between tests.
func resetConfigDir() {
configDir = ""
initConfigDir = new(sync.Once)
}
// Dir returns the directory the configuration file is stored in
func Dir() string {
initConfigDir.Do(func() {
@ -53,6 +60,8 @@ func ContextStoreDir() string {
// SetDir sets the directory the configuration file is stored in
func SetDir(dir string) {
// trigger the sync.Once to synchronise with Dir()
initConfigDir.Do(func() {})
configDir = filepath.Clean(dir)
}

View File

@ -382,3 +382,12 @@ func TestConfigPath(t *testing.T) {
SetDir(oldDir)
}
// TestSetDir verifies that Dir() does not overwrite the value set through
// SetDir() if it has not been run before.
func TestSetDir(t *testing.T) {
const expected = "my_config_dir"
resetConfigDir()
SetDir(expected)
assert.Check(t, is.Equal(Dir(), expected))
}