Repository navigation
Expand file tree
/
Copy pathcachecmd_test.go
More file actions
118 lines (106 loc) · 3.9 KB
/
Copy pathcachecmd_test.go
File metadata and controls
118 lines (106 loc) · 3.9 KB
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
package tui3
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/Agent-Field/codeaf/internal/cachedir"
"github.com/Agent-Field/codeaf/internal/home"
)
// seedCacheDir fills the cache under the state root the fixture has already
// moved: sheetApp points CODEAF_HOME at its own temporary directory, so the
// seeding has to come AFTER the app and write where the app will look.
func seedCacheDir(t *testing.T) {
t.Helper()
dir := filepath.Join(cachedir.Root(), "toolchain", "npm")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "blob"), make([]byte, 4096), 0o644); err != nil {
t.Fatal(err)
}
}
// runCacheNote runs one /cache form through the dispatch and hands back the
// note it produced, the way the loop would: the command runs off the loop and
// its message is fed back through Update.
func runCacheNote(t *testing.T, a *app, line string) string {
t.Helper()
cmd := a.slash(line)
if cmd == nil {
return lastNote(t, a)
}
a.Update(cmd())
return lastNote(t, a)
}
// THE GUARD: a bare /cache clean is always the question and never the act.
// Only the sentence typed out in full — /cache clean now — deletes anything,
// and the question that precedes it names the size and what stays safe.
func TestCacheCleanStopsAtTheQuestionUntilTypedInFull(t *testing.T) {
a, _ := sheetApp(t)
seedCacheDir(t)
note := runCacheNote(t, a, "/cache clean")
for _, want := range []string{"this deletes the shared build cache", "4.0 KB", "not touched", "/cache clean now"} {
if !strings.Contains(note, want) {
t.Errorf("the question is missing %q: %q", want, note)
}
}
if cachedir.Size() == 0 {
t.Fatal("/cache clean deleted without the typed confirmation")
}
note = runCacheNote(t, a, "/cache clean now")
if !strings.Contains(note, "cache cleaned · 4.0 KB freed") {
t.Fatalf("the confirmed clean did not report what left: %q", note)
}
if cachedir.Size() != 0 {
t.Fatal("/cache clean now left the cache standing")
}
// Asked again over nothing, both forms answer with the one empty sentence
// rather than a zero or a silence.
if note = runCacheNote(t, a, "/cache clean"); note != cacheEmptyWord {
t.Fatalf("clean over an empty cache said %q, want %q", note, cacheEmptyWord)
}
}
// The reading form answers both states, and never with a figure of zero.
func TestBareCacheReadsTheSizeAndTheEmptiness(t *testing.T) {
a, _ := sheetApp(t)
seedCacheDir(t)
if note := runCacheNote(t, a, "/cache"); !strings.Contains(note, "the cache holds 4.0 KB") {
t.Fatalf("bare /cache did not say the size: %q", note)
}
if _, err := cachedir.Clean(); err != nil {
t.Fatal(err)
}
if note := runCacheNote(t, a, "/cache"); !strings.Contains(note, "the cache is empty") {
t.Fatalf("bare /cache over nothing did not say so: %q", note)
}
}
// A word that is not a form changes nothing and says the two that are.
func TestCacheRefusesAnUnknownArgumentCalmly(t *testing.T) {
a, _ := sheetApp(t)
seedCacheDir(t)
if note := runCacheNote(t, a, "/cache purge"); !strings.Contains(note, "/cache takes clean") {
t.Fatalf("an unknown argument was not refused in the house shape: %q", note)
}
if cachedir.Size() == 0 {
t.Fatal("an unknown argument deleted the cache")
}
}
func TestCacheQuestionNamesRetainedLegacyLocation(t *testing.T) {
a, _ := sheetApp(t)
path := home.Join("workspace-caches", "legacy", "cache")
if err := os.MkdirAll(path, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "compiler"), []byte("cache"), 0600); err != nil {
t.Fatal(err)
}
for _, command := range []string{"/cache", "/cache clean"} {
note := runCacheNote(t, a, command)
if !strings.Contains(note, "workspace-caches") {
t.Fatalf("%s omitted retained cache location: %s", command, note)
}
}
if raw, err := os.ReadFile(filepath.Join(path, "compiler")); err != nil || string(raw) != "cache" {
t.Fatal("cache question changed old cache bytes")
}
}