@@ -10,7 +10,6 @@ import (
1010 "strings"
1111 "testing"
1212
13- "github.com/adrg/xdg"
1413 "github.com/go-chi/chi/v5"
1514 "github.com/stretchr/testify/assert"
1615 "github.com/stretchr/testify/require"
@@ -19,31 +18,31 @@ import (
1918 "github.com/stacklok/toolhive/pkg/logger"
2019)
2120
22- func MockConfig (t * testing.T , cfg * config.Config ) func () {
21+ func CreateTestConfigProvider (t * testing.T , cfg * config.Config ) (config. Provider , func () ) {
2322 t .Helper ()
2423
2524 // Create a temporary directory for the test
2625 tempDir := t .TempDir ()
2726
28- // TODO: see if there's a way to avoid changing env vars during tests.
29- // Save original XDG_CONFIG_HOME
30- originalXDGConfigHome := os .Getenv ("XDG_CONFIG_HOME" )
31- t .Setenv ("XDG_CONFIG_HOME" , tempDir )
32- xdg .Reload ()
33-
3427 // Create the config directory structure
3528 configDir := filepath .Join (tempDir , "toolhive" )
3629 err := os .MkdirAll (configDir , 0755 )
3730 require .NoError (t , err )
3831
32+ // Set up the config file path
33+ configPath := filepath .Join (configDir , "config.yaml" )
34+
35+ // Create a path-based config provider
36+ provider := config .NewPathProvider (configPath )
37+
3938 // Write the config file if one is provided
4039 if cfg != nil {
41- err = config .UpdateConfig (func (c * config.Config ) { * c = * cfg })
40+ err = provider .UpdateConfig (func (c * config.Config ) { * c = * cfg })
4241 require .NoError (t , err )
4342 }
4443
45- return func () {
46- t . Setenv ( "XDG_CONFIG_HOME" , originalXDGConfigHome )
44+ return provider , func () {
45+ // Cleanup is handled by t.TempDir( )
4746 }
4847}
4948
@@ -58,41 +57,39 @@ func TestRegistryRouter(t *testing.T) {
5857
5958//nolint:paralleltest // Cannot use t.Parallel() with t.Setenv() in Go 1.24+
6059func TestGetRegistryInfo (t * testing.T ) {
60+ t .Parallel ()
6161 logger .Initialize ()
6262
63- // Setup temporary config to avoid modifying user's real config
64- cleanup := MockConfig (t , nil )
65- t .Cleanup (cleanup )
66-
6763 tests := []struct {
6864 name string
69- setupConfig func ()
65+ config * config. Config
7066 expectedType RegistryType
7167 expectedSource string
7268 }{
7369 {
7470 name : "default registry" ,
75- setupConfig : func () {
76- _ = config .UnsetRegistry ()
71+ config : & config.Config {
72+ RegistryUrl : "" ,
73+ LocalRegistryPath : "" ,
7774 },
7875 expectedType : RegistryTypeDefault ,
7976 expectedSource : "" ,
8077 },
8178 {
8279 name : "URL registry" ,
83- setupConfig : func () {
84- _ = config .SetRegistryURL ("https://test.com/registry.json" , false )
80+ config : & config.Config {
81+ RegistryUrl : "https://test.com/registry.json" ,
82+ AllowPrivateRegistryIp : false ,
83+ LocalRegistryPath : "" ,
8584 },
8685 expectedType : RegistryTypeURL ,
8786 expectedSource : "https://test.com/registry.json" ,
8887 },
8988 {
9089 name : "file registry" ,
91- setupConfig : func () {
92- _ = config .UnsetRegistry ()
93- _ = config .UpdateConfig (func (c * config.Config ) {
94- c .LocalRegistryPath = "/tmp/test-registry.json"
95- })
90+ config : & config.Config {
91+ RegistryUrl : "" ,
92+ LocalRegistryPath : "/tmp/test-registry.json" ,
9693 },
9794 expectedType : RegistryTypeFile ,
9895 expectedSource : "/tmp/test-registry.json" ,
@@ -101,87 +98,17 @@ func TestGetRegistryInfo(t *testing.T) {
10198
10299 for _ , tt := range tests {
103100 t .Run (tt .name , func (t * testing.T ) {
104- if tt . setupConfig != nil {
105- tt .setupConfig ( )
106- }
101+ t . Parallel ()
102+ configProvider , cleanup := CreateTestConfigProvider ( t , tt .config )
103+ defer cleanup ()
107104
108- registryType , source := getRegistryInfo ( )
105+ registryType , source := getRegistryInfoWithProvider ( configProvider )
109106 assert .Equal (t , tt .expectedType , registryType , "Registry type should match expected" )
110107 assert .Equal (t , tt .expectedSource , source , "Registry source should match expected" )
111108 })
112109 }
113110}
114111
115- //nolint:paralleltest // uses MockConfig (env mutation)
116- func TestSetRegistryURL_SchemeAndPrivateIPs (t * testing.T ) {
117- logger .Initialize ()
118-
119- // Isolate config (XDG) for each run
120- cleanup := MockConfig (t , nil )
121- t .Cleanup (cleanup )
122-
123- tests := []struct {
124- name string
125- url string
126- allowPrivateIPs bool
127- wantErr bool
128- }{
129- // Scheme enforcement when NOT allowing private IPs
130- {
131- name : "reject http (public) when not allowing private IPs" ,
132- url : "http://example.com/registry.json" ,
133- allowPrivateIPs : false ,
134- wantErr : true ,
135- },
136- {
137- name : "accept https (public) when not allowing private IPs" ,
138- url : "https://example.com/registry.json" ,
139- allowPrivateIPs : false ,
140- wantErr : false ,
141- },
142-
143- // When allowing private IPs, https is allowed to private hosts
144- {
145- name : "accept https to loopback when allowing private IPs" ,
146- url : "https://127.0.0.1/registry.json" ,
147- allowPrivateIPs : true ,
148- wantErr : false ,
149- },
150- {
151- name : "accept http to loopback when allowing private IPs" ,
152- url : "http://127.0.0.1/registry.json" ,
153- allowPrivateIPs : true ,
154- wantErr : false ,
155- },
156- }
157-
158- for _ , tt := range tests {
159- tt := tt
160- t .Run (tt .name , func (t * testing.T ) {
161- // Start from a clean slate each case
162- require .NoError (t , config .UnsetRegistry ())
163-
164- err := config .SetRegistryURL (tt .url , tt .allowPrivateIPs )
165- if tt .wantErr {
166- require .Error (t , err , "expected error but got nil" )
167-
168- // Verify nothing was persisted
169- regType , src := getRegistryInfo ()
170- assert .Equal (t , RegistryTypeDefault , regType , "registry should remain default on error" )
171- assert .Equal (t , "" , src , "source should be empty on error" )
172- return
173- }
174-
175- require .NoError (t , err , "unexpected error from SetRegistryURL" )
176-
177- // Confirm via the same helper used elsewhere
178- regType , src := getRegistryInfo ()
179- assert .Equal (t , RegistryTypeURL , regType , "should be URL type after successful SetRegistryURL" )
180- assert .Equal (t , tt .url , src , "source should be the URL we set" )
181- })
182- }
183- }
184-
185112func TestRegistryAPI_PutEndpoint (t * testing.T ) {
186113 t .Parallel ()
187114
0 commit comments