-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_status_test.go
More file actions
96 lines (74 loc) · 2.85 KB
/
Copy pathcache_status_test.go
File metadata and controls
96 lines (74 loc) · 2.85 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
package repomap
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInspectCache_Missing(t *testing.T) {
t.Parallel()
got := InspectCache(context.Background(), t.TempDir(), t.TempDir())
assert.False(t, got.Exists)
assert.False(t, got.Usable)
assert.Equal(t, "missing_cache", got.Reason)
}
func TestInspectCache_FreshAndContentChanged(t *testing.T) {
t.Parallel()
root := t.TempDir()
cacheDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(root, "go.mod"), []byte("module example.com/cache\n\ngo 1.22\n"), 0o644))
path := filepath.Join(root, "a.go")
require.NoError(t, os.WriteFile(path, []byte("package a\n\nfunc Run() {}\n"), 0o644))
m := New(root, DefaultConfig())
m.SetCacheDir(cacheDir)
require.NoError(t, m.Build(context.Background()))
fresh := InspectCache(context.Background(), root, cacheDir)
assert.True(t, fresh.Exists)
assert.True(t, fresh.Usable)
assert.False(t, fresh.Stale)
assert.Equal(t, "fresh", fresh.Reason)
require.NoError(t, os.WriteFile(path, []byte("package a\n\nfunc Run() error { return nil }\n"), 0o644))
stale := InspectCache(context.Background(), root, cacheDir)
assert.True(t, stale.Stale)
assert.Equal(t, "content_changed", stale.Reason)
}
func TestInspectCache_Corrupt(t *testing.T) {
t.Parallel()
root := t.TempDir()
cacheDir := t.TempDir()
require.NoError(t, os.MkdirAll(cacheDir, 0o755))
require.NoError(t, os.WriteFile(cachePath(cacheDir, root), []byte("{bad json"), 0o644))
got := InspectCache(context.Background(), root, cacheDir)
assert.True(t, got.Exists)
assert.False(t, got.Usable)
assert.Equal(t, "corrupt_cache", got.Reason)
}
func TestInspectCache_DirtyGitDigestFreshness(t *testing.T) {
t.Parallel()
repo := newGitRepo(t)
cacheDir := t.TempDir()
path := filepath.Join(repo, "feature.py")
require.NoError(t, os.WriteFile(path, []byte("def feature():\n return 1\n"), 0o644))
buildWithCache(t, repo, cacheDir)
fresh := InspectCache(context.Background(), repo, cacheDir)
assert.True(t, fresh.Usable)
assert.False(t, fresh.Stale)
assert.Equal(t, "fresh", fresh.Reason)
require.NoError(t, os.WriteFile(path, []byte("def feature():\n return 2\n"), 0o644))
stale := InspectCache(context.Background(), repo, cacheDir)
assert.True(t, stale.Stale)
assert.Equal(t, "content_changed", stale.Reason)
}
func TestSaveCacheLegacySignature(t *testing.T) {
t.Parallel()
root := t.TempDir()
cacheDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(root, "go.mod"), []byte("module example.com/cache\n\ngo 1.22\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(root, "a.go"), []byte("package a\n\nfunc Run() {}\n"), 0o644))
m := New(root, DefaultConfig())
require.NoError(t, m.Build(context.Background()))
require.NoError(t, m.SaveCache(cacheDir))
require.True(t, m.LoadCache(cacheDir))
}