-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_manager_test.go
More file actions
104 lines (88 loc) · 2.64 KB
/
Copy pathjump_manager_test.go
File metadata and controls
104 lines (88 loc) · 2.64 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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestJumpManager(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "jump-test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Create a test config file path
configPath := filepath.Join(tempDir, "test_jump_locations.json")
// Create a test jump manager
jm := &JumpManager{
locations: make(map[string]string),
configPath: configPath,
}
// Test adding locations
t.Run("AddLocation", func(t *testing.T) {
// Add a location
err := jm.AddLocation("test", "/test/path")
if err != nil {
t.Fatalf("Failed to add location: %v", err)
}
// Verify location was added
if path, exists := jm.locations["test"]; !exists || path != "/test/path" {
t.Errorf("Location not added correctly, got: %s, want: %s", path, "/test/path")
}
// Verify config file was created
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Errorf("Config file was not created: %v", err)
}
})
// Test listing locations
t.Run("ListLocations", func(t *testing.T) {
// Add another location
err := jm.AddLocation("home", "/home/user")
if err != nil {
t.Fatalf("Failed to add location: %v", err)
}
// List locations
locations := jm.ListLocations()
if len(locations) != 2 {
t.Errorf("Expected 2 locations, got: %d", len(locations))
}
// Verify the locations are in alphabetical order
if locations[0] != "home" || locations[1] != "test" {
t.Errorf("Locations not in alphabetical order: %v", locations)
}
})
// Test removing a location
t.Run("RemoveLocation", func(t *testing.T) {
// Remove a location
err := jm.RemoveLocation("test")
if err != nil {
t.Fatalf("Failed to remove location: %v", err)
}
// Verify location was removed
if _, exists := jm.locations["test"]; exists {
t.Errorf("Location was not removed")
}
// Verify only one location remains
locations := jm.ListLocations()
if len(locations) != 1 || locations[0] != "home" {
t.Errorf("Expected only 'home' location, got: %v", locations)
}
})
// Test loading saved locations
t.Run("LoadLocations", func(t *testing.T) {
// Create a new jump manager with the same config path
newJm := &JumpManager{
locations: make(map[string]string),
configPath: configPath,
}
// Load locations
err := newJm.loadLocations()
if err != nil {
t.Fatalf("Failed to load locations: %v", err)
}
// Verify the location was loaded
if path, exists := newJm.locations["home"]; !exists || path != "/home/user" {
t.Errorf("Location not loaded correctly, got: %s, want: %s", path, "/home/user")
}
})
}