Skip to content

Commit de0d103

Browse files
committed
fix tests
1 parent 291355d commit de0d103

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

internal/stringutils/stringutils.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ const (
1818
modHundred = 100
1919
)
2020

21-
var illegalChars = regexp.MustCompile(`[<>:"/\\|?*\s]+`)
21+
func illegalChars() *regexp.Regexp {
22+
return regexp.MustCompile(`[<>:"/\\|?*\s]+`)
23+
}
24+
25+
// NowFunc is used to get the current time. It defaults to time.Now, but
26+
// can be overridden in tests.
27+
var NowFunc = time.Now
28+
29+
// UserHomeDirFunc is used to get the user's home directory.
30+
// It defaults to os.UserHomeDir but can be overridden in tests.
31+
var UserHomeDirFunc = os.UserHomeDir
2232

2333
// FormatDate formats given date as Django's format date style (most of them).
2434
// https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#date
2535
func FormatDate(format string, date *time.Time) string {
26-
d := time.Now()
36+
d := NowFunc()
2737
if date != nil {
2838
d = *date
2939
}
@@ -84,7 +94,7 @@ func FormatDate(format string, date *time.Time) string {
8494
}
8595

8696
func sanitizeFilename(input string) string {
87-
return illegalChars.ReplaceAllString(input, "_")
97+
return illegalChars().ReplaceAllString(input, "_")
8898
}
8999

90100
// GetFormattedFilename returns formated filename.
@@ -103,8 +113,10 @@ func GetFormattedFilename(s string, req *http.Request) string {
103113
sArgs := argReplacer.Replace(s)
104114

105115
if strings.HasPrefix(sArgs, "~") {
106-
home, err := os.UserHomeDir()
116+
home, err := UserHomeDirFunc()
107117
if err != nil {
118+
sArgs = sArgs[1:]
119+
108120
goto RETURN
109121
}
110122

internal/stringutils/stringutils_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stringutils_test
22

33
import (
4+
"errors"
45
"net/http"
56
"net/url"
67
"testing"
@@ -136,6 +137,17 @@ func parseDateWithNanoseconds(dateString string) *time.Time {
136137
}
137138

138139
func TestGetFormattedFilename(t *testing.T) {
140+
fixedTime := time.Date(2024, time.December, 26, 15, 0, 0, 0, time.UTC)
141+
originalNowFunc := stringutils.NowFunc
142+
stringutils.NowFunc = func() time.Time { return fixedTime }
143+
defer func() { stringutils.NowFunc = originalNowFunc }()
144+
145+
originalHomeDirFunc := stringutils.UserHomeDirFunc
146+
stringutils.UserHomeDirFunc = func() (string, error) {
147+
return "/home/testuser", nil
148+
}
149+
defer func() { stringutils.UserHomeDirFunc = originalHomeDirFunc }()
150+
139151
tests := []struct {
140152
name string
141153
format string
@@ -151,6 +163,15 @@ func TestGetFormattedFilename(t *testing.T) {
151163
},
152164
expected: "2024-12-26-localhost_9000-_example_path.raw",
153165
},
166+
{
167+
name: "Basic formatting with sanitized hostname and URL under home",
168+
format: "~/%Y-%m-%d-{hostname}-{url}.raw",
169+
req: &http.Request{
170+
Host: "localhost:9000",
171+
URL: &url.URL{Path: "/example/path"},
172+
},
173+
expected: "/home/testuser/2024-12-26-localhost_9000-_example_path.raw",
174+
},
154175
{
155176
name: "Special characters in hostname and URL",
156177
format: "%Y-%m-%d-%F-{hostname}-{url}.raw",
@@ -196,3 +217,26 @@ func TestGetFormattedFilename(t *testing.T) {
196217
})
197218
}
198219
}
220+
221+
func TestGetFormattedFilename_HomeErr(t *testing.T) {
222+
fixedTime := time.Date(2024, time.December, 26, 15, 0, 0, 0, time.UTC)
223+
originalNowFunc := stringutils.NowFunc
224+
stringutils.NowFunc = func() time.Time { return fixedTime }
225+
defer func() { stringutils.NowFunc = originalNowFunc }()
226+
227+
originalHomeDirFunc := stringutils.UserHomeDirFunc
228+
stringutils.UserHomeDirFunc = func() (string, error) {
229+
return "", errors.New("error")
230+
}
231+
defer func() { stringutils.UserHomeDirFunc = originalHomeDirFunc }()
232+
233+
format := "~/%Y-%m-%d-{hostname}-{url}.raw"
234+
req := &http.Request{
235+
Host: "localhost:9000",
236+
URL: &url.URL{Path: "/example/path"},
237+
}
238+
expected := "/2024-12-26-localhost_9000-_example_path.raw"
239+
240+
result := stringutils.GetFormattedFilename(format, req)
241+
assert.Equal(t, expected, result)
242+
}

0 commit comments

Comments
 (0)