File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
3946func 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
5562func SetDir (dir string ) {
63+ // trigger the sync.Once to synchronise with Dir()
64+ initConfigDir .Do (func () {})
5665 configDir = filepath .Clean (dir )
5766}
5867
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments