Skip to content

Commit 3184b3b

Browse files
authored
Merge pull request #191 from shelltime/claude/expand-test-coverage-sXhZd
Expand test coverage with comprehensive test cases
2 parents ed80256 + 2e5a1eb commit 3184b3b

18 files changed

Lines changed: 6589 additions & 0 deletions

commands/utils_test.go

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestExpandPath_Tilde(t *testing.T) {
11+
homeDir, err := os.UserHomeDir()
12+
if err != nil {
13+
t.Skipf("Cannot get home dir: %v", err)
14+
}
15+
16+
testCases := []struct {
17+
input string
18+
expected string
19+
}{
20+
{"~", homeDir},
21+
{"~/", homeDir},
22+
{"~/Documents", filepath.Join(homeDir, "Documents")},
23+
{"~/.config/shelltime", filepath.Join(homeDir, ".config/shelltime")},
24+
}
25+
26+
for _, tc := range testCases {
27+
t.Run(tc.input, func(t *testing.T) {
28+
result, err := expandPath(tc.input)
29+
if err != nil {
30+
t.Fatalf("expandPath(%q) failed: %v", tc.input, err)
31+
}
32+
if result != tc.expected {
33+
t.Errorf("expandPath(%q) = %q, expected %q", tc.input, result, tc.expected)
34+
}
35+
})
36+
}
37+
}
38+
39+
func TestExpandPath_Absolute(t *testing.T) {
40+
testCases := []struct {
41+
input string
42+
expected string
43+
}{
44+
{"/usr/bin", "/usr/bin"},
45+
{"/tmp", "/tmp"},
46+
{"/home/user/file.txt", "/home/user/file.txt"},
47+
}
48+
49+
for _, tc := range testCases {
50+
t.Run(tc.input, func(t *testing.T) {
51+
result, err := expandPath(tc.input)
52+
if err != nil {
53+
t.Fatalf("expandPath(%q) failed: %v", tc.input, err)
54+
}
55+
if result != tc.expected {
56+
t.Errorf("expandPath(%q) = %q, expected %q", tc.input, result, tc.expected)
57+
}
58+
})
59+
}
60+
}
61+
62+
func TestExpandPath_Relative(t *testing.T) {
63+
// Get current working directory
64+
cwd, err := os.Getwd()
65+
if err != nil {
66+
t.Fatalf("Cannot get cwd: %v", err)
67+
}
68+
69+
testCases := []struct {
70+
input string
71+
expected string
72+
}{
73+
{"file.txt", filepath.Join(cwd, "file.txt")},
74+
{"subdir/file.txt", filepath.Join(cwd, "subdir/file.txt")},
75+
{"./file.txt", filepath.Join(cwd, "file.txt")},
76+
}
77+
78+
for _, tc := range testCases {
79+
t.Run(tc.input, func(t *testing.T) {
80+
result, err := expandPath(tc.input)
81+
if err != nil {
82+
t.Fatalf("expandPath(%q) failed: %v", tc.input, err)
83+
}
84+
if result != tc.expected {
85+
t.Errorf("expandPath(%q) = %q, expected %q", tc.input, result, tc.expected)
86+
}
87+
})
88+
}
89+
}
90+
91+
func TestAdjustPathForCurrentUser_UsersPath(t *testing.T) {
92+
homeDir, err := os.UserHomeDir()
93+
if err != nil {
94+
t.Skipf("Cannot get home dir: %v", err)
95+
}
96+
97+
testCases := []struct {
98+
name string
99+
input string
100+
expected string
101+
}{
102+
{
103+
"Users path with 4 parts",
104+
"/Users/someuser/Documents/file.txt",
105+
homeDir + "/Documents/file.txt",
106+
},
107+
{
108+
"Users path with nested dirs",
109+
"/Users/anotheruser/projects/app/src/main.go",
110+
homeDir + "/projects/app/src/main.go",
111+
},
112+
}
113+
114+
for _, tc := range testCases {
115+
t.Run(tc.name, func(t *testing.T) {
116+
result := AdjustPathForCurrentUser(tc.input)
117+
if result != tc.expected {
118+
t.Errorf("AdjustPathForCurrentUser(%q) = %q, expected %q", tc.input, result, tc.expected)
119+
}
120+
})
121+
}
122+
}
123+
124+
func TestAdjustPathForCurrentUser_HomePath(t *testing.T) {
125+
homeDir, err := os.UserHomeDir()
126+
if err != nil {
127+
t.Skipf("Cannot get home dir: %v", err)
128+
}
129+
130+
testCases := []struct {
131+
name string
132+
input string
133+
expected string
134+
}{
135+
{
136+
"home path with 4 parts",
137+
"/home/someuser/Documents/file.txt",
138+
homeDir + "/Documents/file.txt",
139+
},
140+
{
141+
"home path with nested dirs",
142+
"/home/anotheruser/.config/app/config.yaml",
143+
homeDir + "/.config/app/config.yaml",
144+
},
145+
}
146+
147+
for _, tc := range testCases {
148+
t.Run(tc.name, func(t *testing.T) {
149+
result := AdjustPathForCurrentUser(tc.input)
150+
if result != tc.expected {
151+
t.Errorf("AdjustPathForCurrentUser(%q) = %q, expected %q", tc.input, result, tc.expected)
152+
}
153+
})
154+
}
155+
}
156+
157+
func TestAdjustPathForCurrentUser_RootPath(t *testing.T) {
158+
homeDir, err := os.UserHomeDir()
159+
if err != nil {
160+
t.Skipf("Cannot get home dir: %v", err)
161+
}
162+
163+
testCases := []struct {
164+
name string
165+
input string
166+
expected string
167+
}{
168+
{
169+
"root path",
170+
"/root/.bashrc",
171+
homeDir + "/.bashrc",
172+
},
173+
{
174+
"root path with subdir",
175+
"/root/scripts/deploy.sh",
176+
homeDir + "/scripts/deploy.sh",
177+
},
178+
}
179+
180+
for _, tc := range testCases {
181+
t.Run(tc.name, func(t *testing.T) {
182+
result := AdjustPathForCurrentUser(tc.input)
183+
if result != tc.expected {
184+
t.Errorf("AdjustPathForCurrentUser(%q) = %q, expected %q", tc.input, result, tc.expected)
185+
}
186+
})
187+
}
188+
}
189+
190+
func TestAdjustPathForCurrentUser_NoMatch(t *testing.T) {
191+
testCases := []struct {
192+
name string
193+
input string
194+
}{
195+
{"absolute path", "/usr/bin/ls"},
196+
{"var path", "/var/log/messages"},
197+
{"tmp path", "/tmp/file.txt"},
198+
{"etc path", "/etc/hosts"},
199+
}
200+
201+
for _, tc := range testCases {
202+
t.Run(tc.name, func(t *testing.T) {
203+
result := AdjustPathForCurrentUser(tc.input)
204+
// Should return unchanged
205+
if result != tc.input {
206+
t.Errorf("AdjustPathForCurrentUser(%q) = %q, expected unchanged", tc.input, result)
207+
}
208+
})
209+
}
210+
}
211+
212+
func TestAdjustPathForCurrentUser_ShortPaths(t *testing.T) {
213+
testCases := []struct {
214+
name string
215+
input string
216+
}{
217+
{"short Users path", "/Users/user"},
218+
{"short home path", "/home/user"},
219+
{"very short", "/home"},
220+
{"root only", "/"},
221+
}
222+
223+
for _, tc := range testCases {
224+
t.Run(tc.name, func(t *testing.T) {
225+
// Should not panic on short paths
226+
result := AdjustPathForCurrentUser(tc.input)
227+
// Just verify it doesn't panic and returns something
228+
if result == "" {
229+
t.Error("Result should not be empty")
230+
}
231+
})
232+
}
233+
}
234+
235+
func TestAdjustPathForCurrentUser_EmptyPath(t *testing.T) {
236+
result := AdjustPathForCurrentUser("")
237+
if result != "" {
238+
t.Errorf("Expected empty string, got %q", result)
239+
}
240+
}
241+
242+
func TestAdjustPathForCurrentUser_PreservesSubpath(t *testing.T) {
243+
homeDir, err := os.UserHomeDir()
244+
if err != nil {
245+
t.Skipf("Cannot get home dir: %v", err)
246+
}
247+
248+
input := "/Users/someuser/very/deeply/nested/path/to/file.txt"
249+
result := AdjustPathForCurrentUser(input)
250+
251+
// Should preserve the subpath after username
252+
expectedSuffix := "/very/deeply/nested/path/to/file.txt"
253+
if !strings.HasSuffix(result, expectedSuffix) {
254+
t.Errorf("Result should preserve subpath. Got: %q, expected suffix: %q", result, expectedSuffix)
255+
}
256+
257+
// Should start with home dir
258+
if !strings.HasPrefix(result, homeDir) {
259+
t.Errorf("Result should start with home dir. Got: %q, expected prefix: %q", result, homeDir)
260+
}
261+
}
262+
263+
func TestExpandPath_EmptyString(t *testing.T) {
264+
// Empty string should be expanded to current directory
265+
cwd, _ := os.Getwd()
266+
result, err := expandPath("")
267+
if err != nil {
268+
t.Fatalf("expandPath(\"\") failed: %v", err)
269+
}
270+
if result != cwd {
271+
t.Errorf("expandPath(\"\") = %q, expected %q", result, cwd)
272+
}
273+
}
274+
275+
func TestExpandPath_SingleDot(t *testing.T) {
276+
cwd, _ := os.Getwd()
277+
result, err := expandPath(".")
278+
if err != nil {
279+
t.Fatalf("expandPath(\".\") failed: %v", err)
280+
}
281+
if result != cwd {
282+
t.Errorf("expandPath(\".\") = %q, expected %q", result, cwd)
283+
}
284+
}

0 commit comments

Comments
 (0)