Skip to content

Commit 9c3e59c

Browse files
authored
Merge pull request #620 from rr3khan/fix/gitea-xdg-config-home
fix: Gittea write config to XDG_CONFIG_HOME when set
2 parents 3c27831 + a65842c commit 9c3e59c

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎plugins/gitea/personal_access_token.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gitea
33
import (
44
"context"
55
"os"
6+
"path/filepath"
67

78
"github.com/1Password/shell-plugins/sdk"
89
"github.com/1Password/shell-plugins/sdk/importer"
@@ -14,11 +15,16 @@ import (
1415
)
1516

1617
func ConfigPath() string {
18+
// tea's config resolver honors XDG_CONFIG_HOME on macOS; os.UserConfigDir does not.
19+
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
20+
return filepath.Join(xdgConfigHome, "tea", "config.yml")
21+
}
22+
1723
configDir, err := os.UserConfigDir()
1824
if err != nil {
1925
return "~/.config/tea/config.yml"
2026
}
21-
return configDir + "/tea/config.yml"
27+
return filepath.Join(configDir, "tea", "config.yml")
2228
}
2329

2430
func PersonalAccessToken() schema.CredentialType {

‎plugins/gitea/personal_access_token_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
package gitea
22

33
import (
4+
"os"
5+
"path/filepath"
46
"testing"
57

68
"github.com/1Password/shell-plugins/sdk"
79
"github.com/1Password/shell-plugins/sdk/plugintest"
810
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
911
)
1012

13+
func TestConfigPath(t *testing.T) {
14+
t.Run("honors XDG_CONFIG_HOME when set", func(t *testing.T) {
15+
// tea reads its config from $XDG_CONFIG_HOME/tea/config.yml via
16+
// github.com/adrg/xdg, so the plugin must write to the same place.
17+
xdgConfigHome := filepath.Join(t.TempDir(), "xdg")
18+
t.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
19+
20+
want := filepath.Join(xdgConfigHome, "tea", "config.yml")
21+
if got := ConfigPath(); got != want {
22+
t.Errorf("ConfigPath() = %q, want %q", got, want)
23+
}
24+
})
25+
26+
t.Run("falls back to os.UserConfigDir when XDG_CONFIG_HOME is unset", func(t *testing.T) {
27+
t.Setenv("XDG_CONFIG_HOME", "")
28+
29+
configDir, err := os.UserConfigDir()
30+
if err != nil {
31+
t.Skipf("os.UserConfigDir() unavailable: %v", err)
32+
}
33+
34+
want := filepath.Join(configDir, "tea", "config.yml")
35+
if got := ConfigPath(); got != want {
36+
t.Errorf("ConfigPath() = %q, want %q", got, want)
37+
}
38+
})
39+
}
40+
1141
func TestPersonalAccessTokenProvisioner(t *testing.T) {
1242
plugintest.TestProvisioner(t, PersonalAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
1343
"default": {
@@ -27,6 +57,28 @@ func TestPersonalAccessTokenProvisioner(t *testing.T) {
2757
})
2858
}
2959

60+
func TestPersonalAccessTokenProvisionerHonorsXDGConfigHome(t *testing.T) {
61+
xdgConfigHome := filepath.Join(t.TempDir(), "xdg")
62+
t.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
63+
64+
plugintest.TestProvisioner(t, PersonalAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
65+
"xdg config path": {
66+
ItemFields: map[sdk.FieldName]string{
67+
fieldname.Token: "oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f",
68+
fieldname.HostAddress: "https://git.example.com",
69+
fieldname.User: "example",
70+
},
71+
ExpectedOutput: sdk.ProvisionOutput{
72+
Files: map[string]sdk.OutputFile{
73+
filepath.Join(xdgConfigHome, "tea", "config.yml"): {
74+
Contents: []byte(plugintest.LoadFixture(t, "config.yml")),
75+
},
76+
},
77+
},
78+
},
79+
})
80+
}
81+
3082
func TestPersonalAccessTokenImporter(t *testing.T) {
3183
plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{
3284

@@ -55,3 +107,34 @@ func TestPersonalAccessTokenImporter(t *testing.T) {
55107
},
56108
})
57109
}
110+
111+
func TestPersonalAccessTokenImporterHonorsXDGConfigHome(t *testing.T) {
112+
xdgConfigHome := filepath.Join(t.TempDir(), "xdg")
113+
t.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
114+
115+
plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{
116+
"xdg config file": {
117+
Files: map[string]string{
118+
filepath.Join(xdgConfigHome, "tea", "config.yml"): plugintest.LoadFixture(t, "import_config.yml"),
119+
},
120+
ExpectedCandidates: []sdk.ImportCandidate{
121+
{
122+
Fields: map[sdk.FieldName]string{
123+
fieldname.Token: "oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f",
124+
fieldname.HostAddress: "https://git.example.com",
125+
fieldname.User: "example",
126+
},
127+
NameHint: "git.example.com",
128+
},
129+
{
130+
Fields: map[sdk.FieldName]string{
131+
fieldname.Token: "enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs",
132+
fieldname.HostAddress: "https://gitea.com",
133+
fieldname.User: "example@example.com",
134+
},
135+
NameHint: "gitea.com",
136+
},
137+
},
138+
},
139+
})
140+
}

0 commit comments

Comments
 (0)