-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_test.go
More file actions
521 lines (450 loc) · 14 KB
/
Copy pathversion_test.go
File metadata and controls
521 lines (450 loc) · 14 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package config
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestReadVersionFile(t *testing.T) {
tests := []struct {
name string
content string
runtimeName string
expected string
expectError bool
}{
{
name: "read existing runtime",
content: `{"python": "3.11.0", "node": "18.16.0"}`,
runtimeName: "python",
expected: "3.11.0",
expectError: false,
},
{
name: "read different runtime",
content: `{"python": "3.11.0", "node": "18.16.0"}`,
runtimeName: "node",
expected: "18.16.0",
expectError: false,
},
{
name: "runtime not in config",
content: `{"python": "3.11.0"}`,
runtimeName: "node",
expected: "",
expectError: true,
},
{
name: "invalid JSON",
content: `{invalid json}`,
runtimeName: "python",
expected: "",
expectError: true,
},
{
name: "empty config",
content: `{}`,
runtimeName: "python",
expected: "",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temp file
tmpFile := filepath.Join(t.TempDir(), "runtimes.json")
if err := os.WriteFile(tmpFile, []byte(tt.content), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
version, err := readVersionFile(tmpFile, tt.runtimeName)
if tt.expectError {
if err == nil {
t.Error("readVersionFile() expected error, got nil")
}
} else {
if err != nil {
t.Errorf("readVersionFile() unexpected error: %v", err)
}
if version != tt.expected {
t.Errorf("readVersionFile() = %q, want %q", version, tt.expected)
}
}
})
}
}
func TestReadVersionFile_FileNotFound(t *testing.T) {
_, err := readVersionFile("/nonexistent/file.json", "python")
if err == nil {
t.Error("readVersionFile() with nonexistent file should return error")
}
}
func TestReadAllRuntimes(t *testing.T) {
tests := []struct {
name string
content string
expectedLen int
expectedKeys []string
expectError bool
}{
{
name: "multiple runtimes",
content: `{"python": "3.11.0", "node": "18.16.0", "ruby": "3.2.0"}`,
expectedLen: 3,
expectedKeys: []string{"python", "node", "ruby"},
expectError: false,
},
{
name: "single runtime",
content: `{"python": "3.11.0"}`,
expectedLen: 1,
expectedKeys: []string{"python"},
expectError: false,
},
{
name: "empty config",
content: `{}`,
expectedLen: 0,
expectedKeys: []string{},
expectError: false,
},
{
name: "invalid JSON",
content: `{invalid json}`,
expectedLen: 0,
expectedKeys: nil,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temp file
tmpFile := filepath.Join(t.TempDir(), "runtimes.json")
if err := os.WriteFile(tmpFile, []byte(tt.content), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
config, err := ReadAllRuntimes(tmpFile)
if tt.expectError {
if err == nil {
t.Error("ReadAllRuntimes() expected error, got nil")
}
return
}
if err != nil {
t.Errorf("ReadAllRuntimes() unexpected error: %v", err)
return
}
if len(config) != tt.expectedLen {
t.Errorf("ReadAllRuntimes() returned %d runtimes, want %d", len(config), tt.expectedLen)
}
// Verify all expected keys are present
for _, key := range tt.expectedKeys {
if _, ok := config[key]; !ok {
t.Errorf("ReadAllRuntimes() missing expected runtime %q", key)
}
}
})
}
}
func TestReadAllRuntimes_FileNotFound(t *testing.T) {
_, err := ReadAllRuntimes("/nonexistent/file.json")
if err == nil {
t.Error("ReadAllRuntimes() with nonexistent file should return error")
}
}
func TestReadAllRuntimes_Values(t *testing.T) {
content := `{"python": "3.11.0", "node": "18.16.0"}`
tmpFile := filepath.Join(t.TempDir(), "runtimes.json")
if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
config, err := ReadAllRuntimes(tmpFile)
if err != nil {
t.Fatalf("ReadAllRuntimes() error: %v", err)
}
tests := []struct {
runtime string
version string
}{
{"python", "3.11.0"},
{"node", "18.16.0"},
}
for _, tt := range tests {
t.Run(tt.runtime, func(t *testing.T) {
version, ok := config[tt.runtime]
if !ok {
t.Errorf("ReadAllRuntimes() missing runtime %q", tt.runtime)
return
}
if version != tt.version {
t.Errorf("ReadAllRuntimes()[%q] = %q, want %q", tt.runtime, version, tt.version)
}
})
}
}
func TestRuntimesConfig_Type(t *testing.T) {
// Test that RuntimesConfig is a map[string]string
var config RuntimesConfig = make(map[string]string)
config["test"] = "1.0.0"
if val, ok := config["test"]; !ok || val != "1.0.0" {
t.Error("RuntimesConfig should be a map[string]string")
}
}
// Complex tests for directory walking and version resolution
func TestFindLocalRuntimesFile_DirectoryWalking(t *testing.T) {
// Create a temporary directory structure:
// temp/
// └── project/
// └── subdir/
// └── deep/
// └── .dtvem/runtimes.json (this is where we'll run from)
tmpRoot := t.TempDir()
projectDir := filepath.Join(tmpRoot, "project")
subDir := filepath.Join(projectDir, "subdir")
deepDir := filepath.Join(subDir, "deep")
if err := os.MkdirAll(deepDir, 0755); err != nil {
t.Fatalf("Failed to create directory structure: %v", err)
}
// Create config file at project level
configDir := filepath.Join(projectDir, ".dtvem")
if err := os.MkdirAll(configDir, 0755); err != nil {
t.Fatalf("Failed to create .dtvem directory: %v", err)
}
configPath := filepath.Join(configDir, "runtimes.json")
configContent := `{"python": "3.11.0", "node": "18.16.0"}`
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config file: %v", err)
}
// Change to deep directory
originalDir, _ := os.Getwd()
defer func() { _ = os.Chdir(originalDir) }()
if err := os.Chdir(deepDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
// FindLocalRuntimesFile should walk up and find the config
foundPath, err := FindLocalRuntimesFile()
if err != nil {
t.Fatalf("FindLocalRuntimesFile() error: %v", err)
}
// Resolve symlinks for comparison (macOS uses /var -> /private/var symlink)
foundPathResolved, err := filepath.EvalSymlinks(foundPath)
if err != nil {
t.Fatalf("Failed to resolve symlinks in found path: %v", err)
}
configPathResolved, err := filepath.EvalSymlinks(configPath)
if err != nil {
t.Fatalf("Failed to resolve symlinks in config path: %v", err)
}
if foundPathResolved != configPathResolved {
t.Errorf("FindLocalRuntimesFile() = %q, want %q", foundPathResolved, configPathResolved)
}
}
func TestFindLocalRuntimesFile_TraversesThroughGitRoot(t *testing.T) {
// Create structure:
// temp/
// └── outer/
// └── .dtvem/runtimes.json
// └── repo/
// └── .git/
// └── subdir/
// (run from here - SHOULD find outer config)
tmpRoot := t.TempDir()
outerDir := filepath.Join(tmpRoot, "outer")
repoDir := filepath.Join(outerDir, "repo")
subDir := filepath.Join(repoDir, "subdir")
if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatalf("Failed to create directory structure: %v", err)
}
// Create outer config (SHOULD be found)
outerConfigDir := filepath.Join(outerDir, ".dtvem")
if err := os.MkdirAll(outerConfigDir, 0755); err != nil {
t.Fatalf("Failed to create outer .dtvem: %v", err)
}
outerConfigPath := filepath.Join(outerConfigDir, "runtimes.json")
if err := os.WriteFile(outerConfigPath, []byte(`{"python": "3.11.0"}`), 0644); err != nil {
t.Fatalf("Failed to write outer config: %v", err)
}
// Create .git directory at repo level (should NOT stop traversal)
gitDir := filepath.Join(repoDir, ".git")
if err := os.MkdirAll(gitDir, 0755); err != nil {
t.Fatalf("Failed to create .git directory: %v", err)
}
// Change to subdir and try to find config
originalDir, _ := os.Getwd()
defer func() { _ = os.Chdir(originalDir) }()
if err := os.Chdir(subDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
// SHOULD find the outer config (traverses through git root)
foundPath, err := FindLocalRuntimesFile()
if err != nil {
t.Fatalf("FindLocalRuntimesFile() error: %v", err)
}
// Resolve symlinks for comparison (macOS uses /var -> /private/var symlink)
foundPathResolved, err := filepath.EvalSymlinks(foundPath)
if err != nil {
t.Fatalf("Failed to resolve symlinks in found path: %v", err)
}
outerConfigPathResolved, err := filepath.EvalSymlinks(outerConfigPath)
if err != nil {
t.Fatalf("Failed to resolve symlinks in outer config path: %v", err)
}
if foundPathResolved != outerConfigPathResolved {
t.Errorf("FindLocalRuntimesFile() = %q, want %q", foundPathResolved, outerConfigPathResolved)
}
}
func TestFindLocalRuntimesFile_NoConfigFound(t *testing.T) {
// Create empty directory structure with no config
tmpRoot := t.TempDir()
testDir := filepath.Join(tmpRoot, "test")
if err := os.MkdirAll(testDir, 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
originalDir, _ := os.Getwd()
defer func() { _ = os.Chdir(originalDir) }()
if err := os.Chdir(testDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
_, err := FindLocalRuntimesFile()
if err == nil {
t.Error("FindLocalRuntimesFile() should return error when no config found")
}
}
func TestSetGlobalVersion_CreatesFile(t *testing.T) {
// Use temp directory for global config
tmpRoot := t.TempDir()
// Set HOME to temp for this test
t.Setenv("HOME", tmpRoot)
t.Setenv("USERPROFILE", tmpRoot)
// Set a global version
err := SetGlobalVersion("python", "3.11.0")
if err != nil {
t.Fatalf("SetGlobalVersion() error: %v", err)
}
// Verify file was created and contains correct content
actualConfigPath := GlobalConfigPath()
data, err := os.ReadFile(actualConfigPath)
if err != nil {
t.Fatalf("Failed to read config file: %v", err)
}
var config RuntimesConfig
if err := json.Unmarshal(data, &config); err != nil {
t.Fatalf("Failed to parse config: %v", err)
}
if config["python"] != "3.11.0" {
t.Errorf("Config python version = %q, want %q", config["python"], "3.11.0")
}
}
func TestSetGlobalVersion_UpdatesExisting(t *testing.T) {
tmpRoot := t.TempDir()
t.Setenv("HOME", tmpRoot)
t.Setenv("USERPROFILE", tmpRoot)
// Set initial version
if err := SetGlobalVersion("python", "3.11.0"); err != nil {
t.Fatalf("SetGlobalVersion() initial error: %v", err)
}
// Update to new version
if err := SetGlobalVersion("python", "3.12.0"); err != nil {
t.Fatalf("SetGlobalVersion() update error: %v", err)
}
// Verify it was updated
version, err := GlobalVersion("python")
if err != nil {
t.Fatalf("GlobalVersion() error: %v", err)
}
if version != "3.12.0" {
t.Errorf("GlobalVersion() = %q, want %q", version, "3.12.0")
}
}
func TestSetGlobalVersion_MultipleRuntimes(t *testing.T) {
tmpRoot := t.TempDir()
t.Setenv("HOME", tmpRoot)
t.Setenv("USERPROFILE", tmpRoot)
// Set versions for multiple runtimes
runtimes := map[string]string{
"python": "3.11.0",
"node": "18.16.0",
"ruby": "3.2.0",
}
for runtime, version := range runtimes {
if err := SetGlobalVersion(runtime, version); err != nil {
t.Fatalf("SetGlobalVersion(%q, %q) error: %v", runtime, version, err)
}
}
// Verify all were saved
for runtime, expectedVersion := range runtimes {
version, err := GlobalVersion(runtime)
if err != nil {
t.Errorf("GlobalVersion(%q) error: %v", runtime, err)
continue
}
if version != expectedVersion {
t.Errorf("GlobalVersion(%q) = %q, want %q", runtime, version, expectedVersion)
}
}
}
func TestGlobalVersion_NoConfigFile(t *testing.T) {
// On a clean system with no config file, GlobalVersion should return ("", nil)
tmpRoot := t.TempDir()
t.Setenv("HOME", tmpRoot)
t.Setenv("USERPROFILE", tmpRoot)
ResetPathsCache()
defer ResetPathsCache()
version, err := GlobalVersion("python")
if err != nil {
t.Errorf("GlobalVersion() with no config file should return nil error, got: %v", err)
}
if version != "" {
t.Errorf("GlobalVersion() with no config file should return empty string, got: %q", version)
}
}
func TestGlobalVersion_RuntimeNotInConfig(t *testing.T) {
// When config exists but runtime is not in it, GlobalVersion should return ("", nil)
tmpRoot := t.TempDir()
t.Setenv("HOME", tmpRoot)
t.Setenv("USERPROFILE", tmpRoot)
ResetPathsCache()
defer ResetPathsCache()
// Set a version for python so the config file exists
if err := SetGlobalVersion("python", "3.11.0"); err != nil {
t.Fatalf("SetGlobalVersion() setup error: %v", err)
}
// Ask for node which is not in the config
version, err := GlobalVersion("node")
if err != nil {
t.Errorf("GlobalVersion() for missing runtime should return nil error, got: %v", err)
}
if version != "" {
t.Errorf("GlobalVersion() for missing runtime should return empty string, got: %q", version)
}
}
func TestSetLocalVersion_CreatesDirectoryAndFile(t *testing.T) {
// Create temp directory and change to it
tmpRoot := t.TempDir()
originalDir, _ := os.Getwd()
defer func() { _ = os.Chdir(originalDir) }()
if err := os.Chdir(tmpRoot); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
// Set local version (should create .dtvem/runtimes.json)
err := SetLocalVersion("python", "3.11.0")
if err != nil {
t.Fatalf("SetLocalVersion() error: %v", err)
}
// Verify file was created
configPath := filepath.Join(tmpRoot, ".dtvem", "runtimes.json")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Fatal("SetLocalVersion() did not create config file")
}
// Verify content
data, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("Failed to read config: %v", err)
}
var config RuntimesConfig
if err := json.Unmarshal(data, &config); err != nil {
t.Fatalf("Failed to parse config: %v", err)
}
if config["python"] != "3.11.0" {
t.Errorf("Config python version = %q, want %q", config["python"], "3.11.0")
}
}