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 <dsymonds@golang.org>
Run-TryBot: David Symonds <dsymonds@golang.org>
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 @@
import (
"encoding/json"
+ "errors"
"fmt"
"net/http"
"os"
@@ -137,23 +138,20 @@
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 @@
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