Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/config: prevent warning if HOME is not set #2934

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ const (
var (
initConfigDir sync.Once
configDir string
homeDir string
)

// resetHomeDir is used in testing to resets the "homeDir" package variable to
// force re-lookup of the home directory between tests.
func resetHomeDir() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move this function in the tests files? I mean, tests are in the same package, so they have access to the homeDir variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was on the fence what would be better; I put it in the non-test file to make it more visible (I really don't like having to do this 😞)

homeDir = ""
}

func getHomeDir() string {
if homeDir == "" {
homeDir = homedir.Get()
}
return homeDir
}

func setConfigDir() {
if configDir != "" {
return
}
configDir = os.Getenv("DOCKER_CONFIG")
if configDir == "" {
configDir = filepath.Join(homedir.Get(), configFileDir)
configDir = filepath.Join(getHomeDir(), configFileDir)
}
}

Expand Down Expand Up @@ -109,11 +123,7 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
}

// Can't find latest config file so check for the old one
home, err := os.UserHomeDir()
if err != nil {
return configFile, errors.Wrap(err, oldConfigfile)
}
filename = filepath.Join(home, oldConfigfile)
filename = filepath.Join(getHomeDir(), oldConfigfile)
if file, err := os.Open(filename); err == nil {
defer file.Close()
if err := configFile.LegacyLoadFromReader(file); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ password`: "Invalid Auth config file",
email`: "Invalid auth configuration file",
}

resetHomeDir()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit on the fence between calling this function for each test as first step, to make sure the path is clean for the test, OR defer it at first step, to make sure everything will be cleared for other tests. 🤔

func TestOldInvalidsAuth(t *testing.T) {
    defer resetHomeDir()
    ...
}
func TestOldInvalidsAuth(t *testing.T) {
   resetHomeDir()
    ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tricky; any test that uses the homeDir() will initialise the package variable; my current approach was to put it in those tests that expect the variable to be re-resolved (because they patch the HOME variable for the test); changing the HOME variable is something that would only happen in tests (under normal circumstances it would not modify after the cli binary is started, so it would be safe to store it in the variable.

tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
Expand All @@ -131,6 +132,7 @@ email`: "Invalid auth configuration file",
}

func TestOldValidAuth(t *testing.T) {
resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
Expand Down Expand Up @@ -165,6 +167,7 @@ func TestOldValidAuth(t *testing.T) {
}

func TestOldJSONInvalid(t *testing.T) {
resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
Expand All @@ -184,6 +187,7 @@ func TestOldJSONInvalid(t *testing.T) {
}

func TestOldJSON(t *testing.T) {
resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
Expand Down