-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths_test.go
More file actions
454 lines (392 loc) · 11 KB
/
paths_test.go
File metadata and controls
454 lines (392 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package config
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"github.com/dtvem/dtvem/src/internal/constants"
)
func TestGetPaths(t *testing.T) {
paths := DefaultPaths()
// Verify paths is not nil
if paths == nil {
t.Fatal("DefaultPaths() returned nil")
}
// Verify all paths are set
if paths.Root == "" {
t.Error("Root path is empty")
}
if paths.Shims == "" {
t.Error("Shims path is empty")
}
if paths.Versions == "" {
t.Error("Versions path is empty")
}
if paths.Config == "" {
t.Error("Config path is empty")
}
// Verify paths are absolute
if !filepath.IsAbs(paths.Root) {
t.Errorf("Root path %q is not absolute", paths.Root)
}
// Verify subdirectories are under root
if !strings.HasPrefix(paths.Shims, paths.Root) {
t.Errorf("Shims path %q should be under Root %q", paths.Shims, paths.Root)
}
if !strings.HasPrefix(paths.Versions, paths.Root) {
t.Errorf("Versions path %q should be under Root %q", paths.Versions, paths.Root)
}
if !strings.HasPrefix(paths.Config, paths.Root) {
t.Errorf("Config path %q should be under Root %q", paths.Config, paths.Root)
}
}
func TestRuntimeVersionPath(t *testing.T) {
tests := []struct {
name string
runtimeName string
version string
}{
{
name: "Python version path",
runtimeName: "python",
version: "3.11.0",
},
{
name: "Node.js version path",
runtimeName: "node",
version: "18.16.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := RuntimeVersionPath(tt.runtimeName, tt.version)
// Should contain runtime name
if !strings.Contains(result, tt.runtimeName) {
t.Errorf("RuntimeVersionPath(%q, %q) = %q, should contain %q",
tt.runtimeName, tt.version, result, tt.runtimeName)
}
// Should contain version
if !strings.Contains(result, tt.version) {
t.Errorf("RuntimeVersionPath(%q, %q) = %q, should contain %q",
tt.runtimeName, tt.version, result, tt.version)
}
// Should be absolute path
if !filepath.IsAbs(result) {
t.Errorf("RuntimeVersionPath(%q, %q) = %q, should be absolute",
tt.runtimeName, tt.version, result)
}
})
}
}
func TestGlobalConfigPath(t *testing.T) {
result := GlobalConfigPath()
// Should not be empty
if result == "" {
t.Error("GlobalConfigPath() returned empty string")
}
// Should end with 'runtimes.json'
if !strings.HasSuffix(result, RuntimesFileName) {
t.Errorf("GlobalConfigPath() = %q, should end with %q", result, RuntimesFileName)
}
// Should be absolute path
if !filepath.IsAbs(result) {
t.Errorf("GlobalConfigPath() = %q, should be absolute", result)
}
// Should contain 'config'
if !strings.Contains(result, "config") {
t.Errorf("GlobalConfigPath() = %q, should contain 'config'", result)
}
}
func TestShimPath(t *testing.T) {
tests := []struct {
name string
shimName string
wantExt string
}{
{
name: "Python shim",
shimName: "python",
wantExt: "",
},
{
name: "Node shim",
shimName: "node",
wantExt: "",
},
}
// Determine expected extension based on OS
expectedExt := ""
if runtime.GOOS == "windows" {
expectedExt = ".exe"
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ShimPath(tt.shimName)
// Should contain shim name
if !strings.Contains(result, tt.shimName) {
t.Errorf("ShimPath(%q) = %q, should contain %q",
tt.shimName, result, tt.shimName)
}
// Should have .exe extension on Windows
if runtime.GOOS == "windows" {
if !strings.HasSuffix(result, ".exe") {
t.Errorf("ShimPath(%q) = %q, should end with '.exe' on Windows",
tt.shimName, result)
}
}
// Verify correct extension
actualExt := filepath.Ext(result)
if actualExt != expectedExt {
t.Errorf("ShimPath(%q) extension = %q, want %q on %s",
tt.shimName, actualExt, expectedExt, runtime.GOOS)
}
// Should be absolute path
if !filepath.IsAbs(result) {
t.Errorf("ShimPath(%q) = %q, should be absolute",
tt.shimName, result)
}
})
}
}
// resetPathsForTesting resets the paths singleton for testing purposes.
// This allows tests to verify behavior with different environment configurations.
func resetPathsForTesting() {
defaultPaths = nil
pathsOnce = sync.Once{}
}
func TestGetRootDir_WithEnvironmentVariable(t *testing.T) {
// Save original environment
originalRoot := os.Getenv("DTVEM_ROOT")
defer func() {
if originalRoot != "" {
_ = os.Setenv("DTVEM_ROOT", originalRoot)
} else {
_ = os.Unsetenv("DTVEM_ROOT")
}
// Reset paths so it reinitializes
resetPathsForTesting()
}()
// Set custom DTVEM_ROOT
customRoot := "/custom/dtvem/path"
_ = os.Setenv("DTVEM_ROOT", customRoot)
// Reset paths to force reinitialization
resetPathsForTesting()
// Test that getRootDir respects DTVEM_ROOT
result := getRootDir()
if result != customRoot {
t.Errorf("getRootDir() with DTVEM_ROOT=%q = %q, want %q",
customRoot, result, customRoot)
}
}
func TestConfigConstants(t *testing.T) {
// Test LocalConfigDirName
expectedDir := ".dtvem"
if LocalConfigDirName != expectedDir {
t.Errorf("LocalConfigDirName = %q, want %q", LocalConfigDirName, expectedDir)
}
// Test RuntimesFileName
expectedFile := "runtimes.json"
if RuntimesFileName != expectedFile {
t.Errorf("RuntimesFileName = %q, want %q", RuntimesFileName, expectedFile)
}
}
func TestLocalConfigPath(t *testing.T) {
result := LocalConfigPath()
// Should not be empty
if result == "" {
t.Error("LocalConfigPath() returned empty string")
}
// Should contain .dtvem
if !strings.Contains(result, LocalConfigDirName) {
t.Errorf("LocalConfigPath() = %q, should contain %q", result, LocalConfigDirName)
}
// Should end with runtimes.json
if !strings.HasSuffix(result, RuntimesFileName) {
t.Errorf("LocalConfigPath() = %q, should end with %q", result, RuntimesFileName)
}
}
func TestDefaultPaths_ConcurrentAccess(t *testing.T) {
// Reset to ensure clean state
resetPathsForTesting()
defer resetPathsForTesting()
const goroutines = 100
var wg sync.WaitGroup
wg.Add(goroutines)
// Channel to collect results
results := make(chan *Paths, goroutines)
// Launch multiple goroutines to call DefaultPaths concurrently
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
results <- DefaultPaths()
}()
}
wg.Wait()
close(results)
// Collect all results
var first *Paths
for paths := range results {
if first == nil {
first = paths
} else {
// All goroutines should receive the same pointer
if paths != first {
t.Errorf("DefaultPaths() returned different pointers: %p vs %p", first, paths)
}
}
}
// Verify the paths are valid
if first == nil {
t.Fatal("DefaultPaths() returned nil")
}
if first.Root == "" {
t.Error("Root path is empty")
}
}
func TestGetXDGDataPath(t *testing.T) {
home := "/home/testuser"
tests := []struct {
name string
xdgDataHome string
expectedSuffix string
}{
{
name: "XDG_DATA_HOME set",
xdgDataHome: "/custom/data",
expectedSuffix: filepath.Join("/custom/data", "dtvem"),
},
{
name: "XDG_DATA_HOME empty - use default",
xdgDataHome: "",
expectedSuffix: filepath.Join(home, ".local", "share", "dtvem"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save and restore XDG_DATA_HOME
originalXDG := os.Getenv("XDG_DATA_HOME")
defer func() {
if originalXDG != "" {
_ = os.Setenv("XDG_DATA_HOME", originalXDG)
} else {
_ = os.Unsetenv("XDG_DATA_HOME")
}
}()
if tt.xdgDataHome != "" {
_ = os.Setenv("XDG_DATA_HOME", tt.xdgDataHome)
} else {
_ = os.Unsetenv("XDG_DATA_HOME")
}
result := getXDGDataPath(home)
if result != tt.expectedSuffix {
t.Errorf("getXDGDataPath(%q) = %q, want %q", home, result, tt.expectedSuffix)
}
})
}
}
func TestGetRootDir_XDGOnLinux(t *testing.T) {
// This test verifies the XDG behavior on Linux
// On other platforms, it verifies that XDG is NOT used
if runtime.GOOS != constants.OSLinux {
t.Skip("XDG tests only run on Linux")
}
// Save original environment
originalRoot := os.Getenv("DTVEM_ROOT")
originalXDG := os.Getenv("XDG_DATA_HOME")
defer func() {
if originalRoot != "" {
_ = os.Setenv("DTVEM_ROOT", originalRoot)
} else {
_ = os.Unsetenv("DTVEM_ROOT")
}
if originalXDG != "" {
_ = os.Setenv("XDG_DATA_HOME", originalXDG)
} else {
_ = os.Unsetenv("XDG_DATA_HOME")
}
resetPathsForTesting()
}()
// Clear DTVEM_ROOT to test XDG behavior
_ = os.Unsetenv("DTVEM_ROOT")
// Test with custom XDG_DATA_HOME
customXDG := "/tmp/custom-xdg-data"
_ = os.Setenv("XDG_DATA_HOME", customXDG)
resetPathsForTesting()
result := getRootDir()
expected := filepath.Join(customXDG, "dtvem")
if result != expected {
t.Errorf("getRootDir() with XDG_DATA_HOME=%q = %q, want %q", customXDG, result, expected)
}
// Test with XDG_DATA_HOME unset (should use default)
_ = os.Unsetenv("XDG_DATA_HOME")
resetPathsForTesting()
result = getRootDir()
home, _ := os.UserHomeDir()
expected = filepath.Join(home, ".local", "share", "dtvem")
if result != expected {
t.Errorf("getRootDir() with XDG_DATA_HOME unset = %q, want %q", result, expected)
}
}
func TestGetRootDir_NonLinux(t *testing.T) {
// On non-Linux platforms, verify that ~/.dtvem is used regardless of XDG
if runtime.GOOS == constants.OSLinux {
t.Skip("This test only runs on non-Linux platforms")
}
// Save original environment
originalRoot := os.Getenv("DTVEM_ROOT")
originalXDG := os.Getenv("XDG_DATA_HOME")
defer func() {
if originalRoot != "" {
_ = os.Setenv("DTVEM_ROOT", originalRoot)
} else {
_ = os.Unsetenv("DTVEM_ROOT")
}
if originalXDG != "" {
_ = os.Setenv("XDG_DATA_HOME", originalXDG)
} else {
_ = os.Unsetenv("XDG_DATA_HOME")
}
resetPathsForTesting()
}()
// Clear DTVEM_ROOT and set XDG_DATA_HOME
_ = os.Unsetenv("DTVEM_ROOT")
_ = os.Setenv("XDG_DATA_HOME", "/should/be/ignored")
resetPathsForTesting()
result := getRootDir()
home, _ := os.UserHomeDir()
expected := filepath.Join(home, ".dtvem")
if result != expected {
t.Errorf("getRootDir() on %s should ignore XDG_DATA_HOME, got %q, want %q",
runtime.GOOS, result, expected)
}
}
func TestGetRootDir_DTVEMRootOverridesXDG(t *testing.T) {
// Verify that DTVEM_ROOT takes precedence over XDG_DATA_HOME on all platforms
originalRoot := os.Getenv("DTVEM_ROOT")
originalXDG := os.Getenv("XDG_DATA_HOME")
defer func() {
if originalRoot != "" {
_ = os.Setenv("DTVEM_ROOT", originalRoot)
} else {
_ = os.Unsetenv("DTVEM_ROOT")
}
if originalXDG != "" {
_ = os.Setenv("XDG_DATA_HOME", originalXDG)
} else {
_ = os.Unsetenv("XDG_DATA_HOME")
}
resetPathsForTesting()
}()
// Set both DTVEM_ROOT and XDG_DATA_HOME
customRoot := "/custom/dtvem/root"
_ = os.Setenv("DTVEM_ROOT", customRoot)
_ = os.Setenv("XDG_DATA_HOME", "/should/be/ignored")
resetPathsForTesting()
result := getRootDir()
if result != customRoot {
t.Errorf("getRootDir() with DTVEM_ROOT set should return DTVEM_ROOT, got %q, want %q",
result, customRoot)
}
}