11package gitea
22
33import (
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+
1141func 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+
3082func 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