33 lines
692 B
Go
33 lines
692 B
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestShouldRefreshFile(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
path := filepath.Join(tmp, "radar.gif")
|
|
|
|
if !shouldRefreshFile(path, time.Minute) {
|
|
t.Fatalf("expected refresh when file missing")
|
|
}
|
|
|
|
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
|
t.Fatalf("write temp file: %v", err)
|
|
}
|
|
if shouldRefreshFile(path, time.Minute) {
|
|
t.Fatalf("expected no refresh for fresh file")
|
|
}
|
|
|
|
old := time.Now().Add(-2 * time.Hour)
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatalf("chtimes: %v", err)
|
|
}
|
|
if !shouldRefreshFile(path, time.Minute) {
|
|
t.Fatalf("expected refresh for stale file")
|
|
}
|
|
}
|