-
Notifications
You must be signed in to change notification settings - Fork 1
/
urlfilecache_test.go
156 lines (131 loc) · 3.67 KB
/
urlfilecache_test.go
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
package urlfilecache
import (
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/adrg/xdg"
"github.com/stretchr/testify/require"
)
var ts *httptest.Server
func TestMain(m *testing.M) {
// Setup code
dummyTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ifModSince := r.Header.Get("If-Modified-Since"); ifModSince != "" {
ifModSinceTime, err := http.ParseTime(ifModSince)
if err == nil && !dummyTime.After(ifModSinceTime) {
w.WriteHeader(http.StatusNotModified)
return
}
}
w.Header().Set("Last-Modified", dummyTime.Format(http.TimeFormat))
_, _ = w.Write([]byte("test content"))
}))
// Run tests
code := m.Run()
// Teardown code
ts.Close()
os.Exit(code)
}
func TestURLFileCache(t *testing.T) {
path, err := ToPath(ts.URL)
defer os.Remove(path)
require.NoError(t, err)
// Verify file exists and contains expected content
content, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, "test content", string(content))
}
func TestToCustomPath(t *testing.T) {
f, err := os.CreateTemp("", "urlfilecache")
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name())
dst := f.Name()
// try A
require.NoError(t, ToCustomPath(ts.URL, dst))
timeA := getMtime(dst)
// Set mtime of dst to LATER
timeB := timeA.Add(time.Second * 1)
require.NoError(t, os.Chtimes(dst, timeB, timeB))
// try B, should not re-download, because newer
require.NoError(t, ToCustomPath(ts.URL, dst))
timeC := getMtime(dst)
require.Equal(t, timeB, timeC, "2nd download should not have touched mtime")
}
func TestReplaceSelf(t *testing.T) {
self, err := os.Executable()
require.NoError(t, err)
require.NoError(t, ToCustomPath(ts.URL, self))
}
func TestGetCachePath(t *testing.T) {
// Save original XDG paths to restore later
origCacheHome := xdg.CacheHome
defer func() {
xdg.CacheHome = origCacheHome
xdg.Reload()
}()
tmpDir, err := os.MkdirTemp("", "urlfilecache-test-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tests := []struct {
name string
setupFunc func() // Setup the test environment
wantErr bool
wantDir string // Expected directory prefix for cache path
}{
{
name: "normal case - cache dir exists and is writable",
setupFunc: func() {
cacheDir := filepath.Join(tmpDir, "normal", ".cache")
require.NoError(t, os.MkdirAll(cacheDir, 0o755))
xdg.CacheHome = cacheDir
},
wantErr: false,
wantDir: filepath.Join(tmpDir, "normal", ".cache"),
},
{
name: "cache dir doesn't exist - should create it",
setupFunc: func() {
xdg.CacheHome = filepath.Join(tmpDir, "missing", ".cache")
},
wantErr: false,
wantDir: filepath.Join(tmpDir, "missing", ".cache"),
},
{
name: "cache dir not writable - should fallback to other locations",
setupFunc: func() {
xdg.CacheHome = filepath.Join("/", "readonly", ".cache")
},
wantErr: false,
wantDir: "/tmp",
},
}
testURL := "https://example.com/test.txt"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupFunc()
got, err := getCachePath(testURL)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.NotEmpty(t, got)
// Try to create the directory structure
dir := filepath.Dir(got)
require.Equal(t, filepath.Join(tt.wantDir, "urlfilecache.test"), dir)
err = os.MkdirAll(dir, 0o755)
require.NoError(t, err, "Should be able to create cache directory structure")
// Try to create a test file
f, err := os.Create(got)
require.NoError(t, err, "Should be able to create file in cache path")
f.Close()
})
}
}