Skip to content

Commit dab8025

Browse files
committed
test(fscache): ensure os-specific path handling
1 parent ccde4ee commit dab8025

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

store/fscache/benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func BenchmarkFSCache(b *testing.B) {
1414
acceptance.RunB(b, acceptance.FactoryFunc(func() (driver.Conn, func()) {
15-
u := makeRoot(b)
15+
u := makeRootURL(b)
1616
cache, err := fromURL(u)
1717
if err != nil {
1818
b.Fatalf("Failed to create fscache: %v", err)

store/fscache/fscache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ func WithEncryption(key string) Option {
170170
// WithBaseDir sets the base directory for the cache; default: user's OS cache directory.
171171
func WithBaseDir(base string) Option {
172172
return optionFunc(func(c *fsCache) error {
173-
c.base = base
173+
if base != "" {
174+
c.base = base
175+
}
174176
return nil
175177
})
176178
}

store/fscache/fscache_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"io"
2121
"io/fs"
2222
"net/url"
23+
"path/filepath"
24+
"runtime"
2325
"testing"
2426
"time"
2527

@@ -30,9 +32,10 @@ import (
3032

3133
func (c *fsCache) Close() error { return c.root.Close() }
3234

33-
func makeRoot(t testing.TB) *url.URL {
35+
func makeRootURL(t testing.TB) *url.URL {
3436
t.Helper()
35-
u, err := url.Parse("fscache://" + t.TempDir() + "?appname=testapp")
37+
tempDir := filepath.ToSlash(t.TempDir())
38+
u, err := url.Parse("fscache://" + tempDir + "?appname=testapp")
3639
if err != nil {
3740
t.Fatalf("Failed to parse cache URL: %v", err)
3841
}
@@ -41,7 +44,7 @@ func makeRoot(t testing.TB) *url.URL {
4144

4245
func TestFSCache_Acceptance(t *testing.T) {
4346
acceptance.Run(t, acceptance.FactoryFunc(func() (driver.Conn, func()) {
44-
u := makeRoot(t)
47+
u := makeRootURL(t)
4548
cache, err := fromURL(u)
4649
testutil.RequireNoError(t, err, "Failed to create fscache")
4750
cleanup := func() { cache.Close() }
@@ -50,7 +53,7 @@ func TestFSCache_Acceptance(t *testing.T) {
5053
}
5154

5255
func Test_fsCache_SetError(t *testing.T) {
53-
u := makeRoot(t)
56+
u := makeRootURL(t)
5457
cache, err := fromURL(u)
5558
testutil.RequireNoError(t, err, "Failed to create fscache")
5659
t.Cleanup(func() { cache.Close() })
@@ -63,7 +66,7 @@ func Test_fsCache_SetError(t *testing.T) {
6366
}
6467

6568
func Test_fsCache_KeysError(t *testing.T) {
66-
u := makeRoot(t)
69+
u := makeRootURL(t)
6770
cache, err := fromURL(u)
6871
testutil.RequireNoError(t, err, "Failed to create fscache")
6972
t.Cleanup(func() { cache.Close() })
@@ -96,7 +99,7 @@ func TestOpen(t *testing.T) {
9699
{
97100
name: "Valid Root Directory",
98101
args: args{
99-
dsn: "fscache://" + t.TempDir() + "?appname=myapp",
102+
dsn: "fscache://" + filepath.ToSlash(t.TempDir()) + "?appname=myapp",
100103
},
101104
assertion: func(tt *testing.T, got *fsCache, err error) {
102105
testutil.RequireNoError(tt, err)
@@ -119,8 +122,13 @@ func TestOpen(t *testing.T) {
119122
dsn: "fscache://?appname=myapp",
120123
},
121124
setup: func(tt *testing.T) {
122-
tt.Setenv("XDG_CACHE_HOME", "")
123-
tt.Setenv("HOME", "")
125+
switch runtime.GOOS {
126+
case "windows":
127+
tt.Setenv("LocalAppData", "")
128+
default:
129+
tt.Setenv("XDG_CACHE_HOME", "")
130+
tt.Setenv("HOME", "")
131+
}
124132
},
125133
assertion: func(tt *testing.T, got *fsCache, err error) {
126134
testutil.RequireErrorIs(tt, err, ErrUserCacheDir)
@@ -140,7 +148,9 @@ func TestOpen(t *testing.T) {
140148
{
141149
name: "Invalid Root Directory",
142150
args: args{
143-
dsn: "fscache:///../invalid?appname=myapp",
151+
dsn: "fscache://" + filepath.ToSlash(
152+
filepath.VolumeName(t.TempDir())+"/../invalid",
153+
) + "?appname=myapp",
144154
},
145155
assertion: func(tt *testing.T, got *fsCache, err error) {
146156
testutil.RequireErrorIs(tt, err, ErrCreateCacheDir)
@@ -202,6 +212,7 @@ func TestOpen(t *testing.T) {
202212
if err != nil {
203213
t.Fatalf("Failed to parse URL: %v", err)
204214
}
215+
t.Attr("URL", u.String())
205216
if tt.setup != nil {
206217
tt.setup(t)
207218
}

0 commit comments

Comments
 (0)