fix(daemon): use correct systemd user target and isolate test cache

This commit is contained in:
Deivid Soto 2026-04-06 18:49:44 +02:00
parent 6f81a2f3ea
commit 4cf07c411c
3 changed files with 24 additions and 7 deletions

View file

@ -22,11 +22,10 @@ Type=simple
ExecStart={{.BinPath}} start ExecStart={{.BinPath}} start
Restart=always Restart=always
RestartSec=10 RestartSec=10
User={{.User}}
Environment=HOME={{.Home}} Environment=HOME={{.Home}}
[Install] [Install]
WantedBy=multi-user.target WantedBy=default.target
` `
const launchdTemplate = `<?xml version="1.0" encoding="UTF-8"?> const launchdTemplate = `<?xml version="1.0" encoding="UTF-8"?>

View file

@ -18,15 +18,17 @@ type versionCache struct {
CheckedAt time.Time `json:"checkedAt"` CheckedAt time.Time `json:"checkedAt"`
} }
// cacheFilePath returns the path to the version cache file. // cacheFilePathFn returns the path to the version cache file.
func cacheFilePath() string { // Overridable in tests to avoid polluting the real cache.
// NOTE: not safe for parallel tests — callers must not use t.Parallel().
var cacheFilePathFn = func() string {
return filepath.Join(config.DataDir(), "latest-version.json") return filepath.Join(config.DataDir(), "latest-version.json")
} }
// ReadCachedVersion returns the cached latest version if it's fresh (< cacheTTL). // ReadCachedVersion returns the cached latest version if it's fresh (< cacheTTL).
// Returns empty string if cache is missing, stale, or corrupt. // Returns empty string if cache is missing, stale, or corrupt.
func ReadCachedVersion() string { func ReadCachedVersion() string {
data, err := os.ReadFile(cacheFilePath()) data, err := os.ReadFile(cacheFilePathFn())
if err != nil { if err != nil {
return "" return ""
} }
@ -50,14 +52,16 @@ func writeCachedVersion(version string) {
if err != nil { if err != nil {
return return
} }
path := cacheFilePath() path := cacheFilePathFn()
os.MkdirAll(filepath.Dir(path), 0o755) os.MkdirAll(filepath.Dir(path), 0o755)
// Best-effort write — ignore errors // Best-effort write — ignore errors
tmp := path + ".tmp" tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o644); err != nil { if err := os.WriteFile(tmp, data, 0o644); err != nil {
return return
} }
os.Rename(tmp, path) if os.Rename(tmp, path) != nil {
os.Remove(tmp)
}
} }
// CheckLatestCached returns the latest version, using cache when fresh. // CheckLatestCached returns the latest version, using cache when fresh.

View file

@ -316,6 +316,16 @@ func swapHTTPClient(c *http.Client) func() {
return func() { httpClient = orig } return func() { httpClient = orig }
} }
// swapCacheDir redirects the version cache to a temp directory to avoid
// polluting the real ~/.local/share/unarr/latest-version.json during tests.
func swapCacheDir(t *testing.T) func() {
t.Helper()
tmpDir := t.TempDir()
orig := cacheFilePathFn
cacheFilePathFn = func() string { return filepath.Join(tmpDir, "latest-version.json") }
return func() { cacheFilePathFn = orig }
}
// rewriteTransport redirects all requests to the given base URL, // rewriteTransport redirects all requests to the given base URL,
// preserving path and query. // preserving path and query.
type rewriteTransport struct { type rewriteTransport struct {
@ -563,6 +573,10 @@ func TestFetchLatestVersionWithHTTPTest(t *testing.T) {
}) })
defer restore() defer restore()
// Redirect cache to temp dir so tests don't pollute the real cache
restoreCache := swapCacheDir(t)
defer restoreCache()
ver, err := CheckLatest(context.Background()) ver, err := CheckLatest(context.Background())
if tt.wantErr { if tt.wantErr {
if err == nil { if err == nil {