aboutsummaryrefslogtreecommitdiff
path: root/cmd/krino/cache_test.go
blob: 65526b3b49d24753e1871b7ee7662bdca48cc96a (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

// TestRunWritesKeywordCache: a dry run over a directory with a content rule
// leaves a private cache under ~/.cache/krino, and check says where it is.
func TestRunWritesKeywordCache(t *testing.T) {
	h := home(t)
	dl := filepath.Join(h, "dl")
	os.MkdirAll(dl, 0o755)
	p := filepath.Join(dl, "a.txt")
	os.WriteFile(p, []byte("invoice from acme ltd"), 0o644)
	old := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
	os.Chtimes(p, old, old)
	if code, _, errOut := runCLI(t, "init"); code != 0 {
		t.Fatal(errOut)
	}
	if code, _, errOut := runCLI(t, "new", "dl", dl); code != 0 {
		t.Fatal(errOut)
	}
	rules := "(path \"~/dl\")\n(rule \"acme\" (when (content \"acme ltd\")) (move \"Acme\"))\n"
	os.WriteFile(filepath.Join(h, ".config", "krino", "dirs", "dl.conf"), []byte(rules), 0o644)

	for i := 0; i < 2; i++ {
		if code, out, errOut := runCLI(t, "-n"); code != 0 || !strings.Contains(out, "a.txt") {
			t.Fatalf("run %d: exit %d\n%s\n%s", i, code, out, errOut)
		}
	}
	fi, err := os.Stat(filepath.Join(h, ".cache", "krino", "dl.cache"))
	if err != nil {
		t.Fatal(err)
	}
	if fi.Mode().Perm() != 0o600 {
		t.Errorf("cache mode %o, want 600", fi.Mode().Perm())
	}
	if _, out, _ := runCLI(t, "check"); !strings.Contains(out, "cache: ~/.cache/krino\n") {
		t.Errorf("check does not show the cache directory:\n%s", out)
	}
}