Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/api/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
)

func TestResolveOptions(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfigWithSocket())

tests := []struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/graphql_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
)

func TestGraphQLClient(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand All @@ -35,6 +38,9 @@ func TestGraphQLClient(t *testing.T) {
}

func TestGraphQLClientDoError(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand All @@ -57,6 +63,9 @@ func TestGraphQLClientDoError(t *testing.T) {
}

func TestGraphQLClientQueryError(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand All @@ -79,6 +88,9 @@ func TestGraphQLClientQueryError(t *testing.T) {
}

func TestGraphQLClientMutateError(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand Down
3 changes: 3 additions & 0 deletions pkg/api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
)

func TestHTTPClient(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand Down
3 changes: 3 additions & 0 deletions pkg/api/rest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
)

func TestRESTClient(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

Expand Down
3 changes: 3 additions & 0 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ func TestKnownHosts(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("GH_CONFIG_DIR", "")
t.Setenv("GH_HOST", "")
t.Setenv("GH_TOKEN", "")
if tt.ghHost != "" {
t.Setenv("GH_HOST", tt.ghHost)
}
Expand Down
17 changes: 10 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
appData = "AppData"
ghConfigDir = "GH_CONFIG_DIR"
ghDataDir = "GH_DATA_DIR"
localAppData = "LocalAppData"
xdgConfigHome = "XDG_CONFIG_HOME"
xdgDataHome = "XDG_DATA_HOME"
Expand Down Expand Up @@ -280,16 +281,18 @@ func StateDir() string {

// DataDir returns the path to the data directory.
//
// Data path precedence: XDG_DATA_HOME, LocalAppData (windows only), HOME.
// Data path precedence: GH_DATA_DIR, XDG_DATA_HOME, LocalAppData (windows only), HOME.
func DataDir() string {
var path string
if a := os.Getenv(xdgDataHome); a != "" {
path = filepath.Join(a, "gh")
} else if b := os.Getenv(localAppData); runtime.GOOS == "windows" && b != "" {
path = filepath.Join(b, "GitHub CLI")
if a := os.Getenv(ghDataDir); a != "" {
path = a
} else if b := os.Getenv(xdgDataHome); b != "" {
path = filepath.Join(b, "gh")
} else if c := os.Getenv(localAppData); runtime.GOOS == "windows" && c != "" {
path = filepath.Join(c, "GitHub CLI")
} else {
c, _ := os.UserHomeDir()
path = filepath.Join(c, ".local", "share", "gh")
d, _ := os.UserHomeDir()
path = filepath.Join(d, ".local", "share", "gh")
}
return path
}
Expand Down
24 changes: 23 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestConfigDir(t *testing.T) {
{
name: "XDG_CONFIG_HOME specified",
env: map[string]string{
"GH_CONFIG_DIR": "",
"XDG_CONFIG_HOME": tempDir,
},
output: filepath.Join(tempDir, "gh"),
Expand All @@ -57,7 +58,8 @@ func TestConfigDir(t *testing.T) {
name: "AppData specified",
onlyWindows: true,
env: map[string]string{
"AppData": tempDir,
"GH_CONFIG_DIR": "",
"AppData": tempDir,
},
output: filepath.Join(tempDir, "GitHub CLI"),
},
Expand All @@ -74,6 +76,7 @@ func TestConfigDir(t *testing.T) {
name: "XDG_CONFIG_HOME and AppData specified",
onlyWindows: true,
env: map[string]string{
"GH_CONFIG_DIR": "",
"XDG_CONFIG_HOME": tempDir,
"AppData": tempDir,
},
Expand Down Expand Up @@ -170,6 +173,7 @@ func TestDataDir(t *testing.T) {
{
name: "HOME/USERPROFILE specified",
env: map[string]string{
"GH_DATA_DIR": "",
"XDG_DATA_HOME": "",
"GH_CONFIG_DIR": "",
"XDG_CONFIG_HOME": "",
Expand All @@ -182,6 +186,7 @@ func TestDataDir(t *testing.T) {
{
name: "XDG_DATA_HOME specified",
env: map[string]string{
"GH_DATA_DIR": "",
"XDG_DATA_HOME": tempDir,
},
output: filepath.Join(tempDir, "gh"),
Expand All @@ -190,6 +195,7 @@ func TestDataDir(t *testing.T) {
name: "LocalAppData specified",
onlyWindows: true,
env: map[string]string{
"GH_DATA_DIR": "",
"LocalAppData": tempDir,
},
output: filepath.Join(tempDir, "GitHub CLI"),
Expand All @@ -198,11 +204,27 @@ func TestDataDir(t *testing.T) {
name: "XDG_DATA_HOME and LocalAppData specified",
onlyWindows: true,
env: map[string]string{
"GH_DATA_DIR": "",
"XDG_DATA_HOME": tempDir,
"LocalAppData": tempDir,
},
output: filepath.Join(tempDir, "gh"),
},
{
name: "GH_DATA_DIR specified",
env: map[string]string{
"GH_DATA_DIR": filepath.Join(tempDir, "gh_data_dir"),
},
output: filepath.Join(tempDir, "gh_data_dir"),
},
{
name: "GH_DATA_DIR and XDG_DATA_HOME specified",
env: map[string]string{
"GH_DATA_DIR": filepath.Join(tempDir, "gh_data_dir"),
"XDG_DATA_HOME": tempDir,
},
output: filepath.Join(tempDir, "gh_data_dir"),
},
}

for _, tt := range tests {
Expand Down