55 "path/filepath"
66 "runtime"
77 "strings"
8+ "sync"
89 "testing"
910)
1011
@@ -172,6 +173,13 @@ func TestShimPath(t *testing.T) {
172173 }
173174}
174175
176+ // resetPathsForTesting resets the paths singleton for testing purposes.
177+ // This allows tests to verify behavior with different environment configurations.
178+ func resetPathsForTesting () {
179+ defaultPaths = nil
180+ pathsOnce = sync.Once {}
181+ }
182+
175183func TestGetRootDir_WithEnvironmentVariable (t * testing.T ) {
176184 // Save original environment
177185 originalRoot := os .Getenv ("DTVEM_ROOT" )
@@ -181,16 +189,16 @@ func TestGetRootDir_WithEnvironmentVariable(t *testing.T) {
181189 } else {
182190 _ = os .Unsetenv ("DTVEM_ROOT" )
183191 }
184- // Reset defaultPaths so it reinitializes
185- defaultPaths = nil
192+ // Reset paths so it reinitializes
193+ resetPathsForTesting ()
186194 }()
187195
188196 // Set custom DTVEM_ROOT
189197 customRoot := "/custom/dtvem/path"
190198 _ = os .Setenv ("DTVEM_ROOT" , customRoot )
191199
192- // Reset defaultPaths to force reinitialization
193- defaultPaths = nil
200+ // Reset paths to force reinitialization
201+ resetPathsForTesting ()
194202
195203 // Test that getRootDir respects DTVEM_ROOT
196204 result := getRootDir ()
@@ -232,3 +240,48 @@ func TestLocalConfigPath(t *testing.T) {
232240 t .Errorf ("LocalConfigPath() = %q, should end with %q" , result , RuntimesFileName )
233241 }
234242}
243+
244+ func TestDefaultPaths_ConcurrentAccess (t * testing.T ) {
245+ // Reset to ensure clean state
246+ resetPathsForTesting ()
247+ defer resetPathsForTesting ()
248+
249+ const goroutines = 100
250+ var wg sync.WaitGroup
251+ wg .Add (goroutines )
252+
253+ // Channel to collect results
254+ results := make (chan * Paths , goroutines )
255+
256+ // Launch multiple goroutines to call DefaultPaths concurrently
257+ for i := 0 ; i < goroutines ; i ++ {
258+ go func () {
259+ defer wg .Done ()
260+ results <- DefaultPaths ()
261+ }()
262+ }
263+
264+ wg .Wait ()
265+ close (results )
266+
267+ // Collect all results
268+ var first * Paths
269+ for paths := range results {
270+ if first == nil {
271+ first = paths
272+ } else {
273+ // All goroutines should receive the same pointer
274+ if paths != first {
275+ t .Errorf ("DefaultPaths() returned different pointers: %p vs %p" , first , paths )
276+ }
277+ }
278+ }
279+
280+ // Verify the paths are valid
281+ if first == nil {
282+ t .Fatal ("DefaultPaths() returned nil" )
283+ }
284+ if first .Root == "" {
285+ t .Error ("Root path is empty" )
286+ }
287+ }
0 commit comments