From d838a7d6be78aa01328174b2ad37f006884bf9e5 Mon Sep 17 00:00:00 2001 From: Dave Day Date: Thu, 12 Feb 2015 15:46:13 +1100 Subject: [PATCH] oauth2/google: simplify the mechanism for overriding gcloud config location Change-Id: I360fac6b13d11dda221beaa6b46ff386adfc2ec8 Reviewed-on: https://go-review.googlesource.com/4670 Reviewed-by: David Symonds Run-TryBot: David Symonds --- google/sdk.go | 18 ++++++++---------- google/sdk_test.go | 5 ++++- .../testdata/{.config => }/gcloud/credentials | 0 .../testdata/{.config => }/gcloud/properties | 0 4 files changed, 12 insertions(+), 11 deletions(-) rename google/testdata/{.config => }/gcloud/credentials (100%) rename google/testdata/{.config => }/gcloud/properties (100%) diff --git a/google/sdk.go b/google/sdk.go index 9088849..d83df14 100644 --- a/google/sdk.go +++ b/google/sdk.go @@ -6,6 +6,7 @@ package google import ( "encoding/json" + "errors" "fmt" "net/http" "os" @@ -137,23 +138,20 @@ func (c *SDKConfig) Scopes() []string { return c.conf.Scopes } -func sdkConfigPath() (string, error) { +// sdkConfigPath tries to guess where the gcloud config is located. +// It can be overridden during tests. +var sdkConfigPath = func() (string, error) { if runtime.GOOS == "windows" { return filepath.Join(os.Getenv("APPDATA"), "gcloud"), nil } - unixHomeDir = guessUnixHomeDir() - if unixHomeDir == "" { - return "", fmt.Errorf("unable to get current user home directory: os/user lookup failed; $HOME is empty") + homeDir := guessUnixHomeDir() + if homeDir == "" { + return "", errors.New("unable to get current user home directory: os/user lookup failed; $HOME is empty") } - return filepath.Join(unixHomeDir, ".config", "gcloud"), nil + return filepath.Join(homeDir, ".config", "gcloud"), nil } -var unixHomeDir string - func guessUnixHomeDir() string { - if unixHomeDir != "" { - return unixHomeDir - } usr, err := user.Current() if err == nil { return usr.HomeDir diff --git a/google/sdk_test.go b/google/sdk_test.go index 43090b6..f05a75a 100644 --- a/google/sdk_test.go +++ b/google/sdk_test.go @@ -7,7 +7,10 @@ package google import "testing" func TestSDKConfig(t *testing.T) { - unixHomeDir = "testdata" + sdkConfigPath = func() (string, error) { + return "testdata/gcloud", nil + } + tests := []struct { account string accessToken string diff --git a/google/testdata/.config/gcloud/credentials b/google/testdata/gcloud/credentials similarity index 100% rename from google/testdata/.config/gcloud/credentials rename to google/testdata/gcloud/credentials diff --git a/google/testdata/.config/gcloud/properties b/google/testdata/gcloud/properties similarity index 100% rename from google/testdata/.config/gcloud/properties rename to google/testdata/gcloud/properties