From bcdd60c51b35750e76cc13f7251ccd3f3d018bc8 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Thu, 23 Jul 2026 22:14:14 +0200 Subject: cli,liturgy: lectio --clean prunes readings cache older than one year --- internal/cli/cli_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'internal/cli/cli_test.go') diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index d0c8791..5004af4 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -2,6 +2,8 @@ package cli import ( "bytes" + "os" + "path/filepath" "strings" "testing" ) @@ -126,6 +128,67 @@ func TestBannerForLang(t *testing.T) { } } +// TestCleanEmptyCache exercises -C/--clean's dispatch as a maintenance mode: +// it must be handled before any render path (and before the network-hitting +// -u/--update path) is reached. Pointing XDG_CACHE_HOME at an empty temp dir +// keeps liturgy.CleanCache's directory-read hermetic (no network), and an +// empty dir exercises the "nothing to remove" branch of the summary line. +func TestCleanEmptyCache(t *testing.T) { + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + var out, errb bytes.Buffer + code := Run([]string{"-C"}, nil, &out, &errb) + if code != 0 { + t.Fatalf("--clean code=%d want 0 (stderr=%q)", code, errb.String()) + } + if !strings.Contains(out.String(), "clean") { + t.Errorf("--clean stdout=%q, want a clean/nothing message", out.String()) + } +} + +// TestCleanRemovesOldEntries exercises the non-empty summary branch: a stale +// cache pair sitting in XDG_CACHE_HOME must be reported as removed, entirely +// via the filesystem (no network involved). +func TestCleanRemovesOldEntries(t *testing.T) { + dir := t.TempDir() + t.Setenv("XDG_CACHE_HOME", dir) + cacheDir := filepath.Join(dir, "lectio") + if err := os.MkdirAll(cacheDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(cacheDir, "2020-01-01.html"), []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + + var out, errb bytes.Buffer + code := Run([]string{"--clean"}, nil, &out, &errb) + if code != 0 { + t.Fatalf("--clean code=%d want 0 (stderr=%q)", code, errb.String()) + } + if !strings.Contains(out.String(), "cleaned 1 cache entry") { + t.Errorf("--clean stdout=%q, want a \"cleaned 1 cache entry\" message", out.String()) + } + if _, err := os.Stat(filepath.Join(cacheDir, "2020-01-01.html")); !os.IsNotExist(err) { + t.Errorf("2020-01-01.html still exists after --clean") + } +} + +// TestCleanPreferredOverUpdate exercises Run's stated precedence: when both +// --clean and -u/--update are given, --clean wins and -u's network-hitting +// harvest path is never reached (proven here by the empty-cache dir plus a +// zero exit code -- a network attempt against no test server would either +// hang or return an error/non-zero code). +func TestCleanPreferredOverUpdate(t *testing.T) { + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + var out, errb bytes.Buffer + code := Run([]string{"--clean", "-u"}, nil, &out, &errb) + if code != 0 { + t.Fatalf("--clean -u code=%d want 0 (stderr=%q)", code, errb.String()) + } + if !strings.Contains(out.String(), "clean") { + t.Errorf("--clean -u stdout=%q, want a clean/nothing message", out.String()) + } +} + // TestDateTokenAnyPosition exercises extractDate directly: the date token // is found regardless of where it appears among other flags. func TestDateTokenAnyPosition(t *testing.T) { -- cgit v1.3