-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_test_cache.go
More file actions
143 lines (120 loc) · 3.26 KB
/
git_test_cache.go
File metadata and controls
143 lines (120 loc) · 3.26 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package devflow
import (
"crypto/md5"
"fmt"
"os"
"path/filepath"
"strings"
)
// TestCache provides git-based test caching to avoid re-running tests
// when the code hasn't changed since the last successful test run.
type TestCache struct {
CacheDir string
}
// NewTestCache creates a new TestCache instance
func NewTestCache() *TestCache {
return &TestCache{
CacheDir: filepath.Join(os.TempDir(), "gotest-cache"),
}
}
// GetCacheKey returns a unique key for the current module based on its path
func (tc *TestCache) GetCacheKey() (string, error) {
moduleName, err := getModuleName(".")
if err != nil {
return "", err
}
// Hash the module name to create a safe filename
hash := fmt.Sprintf("%x", md5.Sum([]byte(moduleName)))
return hash[:16], nil
}
// GetCachePath returns the full path to the cache file
func (tc *TestCache) GetCachePath() (string, error) {
key, err := tc.GetCacheKey()
if err != nil {
return "", err
}
return filepath.Join(tc.CacheDir, key), nil
}
// GetGitState returns current git state: commit hash + diff hash
// This uniquely identifies the exact state of the code
func (tc *TestCache) GetGitState() (string, error) {
// Get current commit hash
commitHash, err := RunCommandSilent("git", "rev-parse", "HEAD")
if err != nil {
return "", fmt.Errorf("failed to get commit hash: %w", err)
}
commitHash = strings.TrimSpace(commitHash)
// Get hash of uncommitted changes (if any)
diff, err := RunCommandSilent("git", "diff", "HEAD")
if err != nil {
// No diff or error, use empty
diff = ""
}
// Combine commit + diff hash for unique state
diffHash := fmt.Sprintf("%x", md5.Sum([]byte(diff)))
return commitHash + ":" + diffHash[:8], nil
}
// SaveCache saves the current git state and test message
func (tc *TestCache) SaveCache(message string) error {
state, err := tc.GetGitState()
if err != nil {
return err
}
cachePath, err := tc.GetCachePath()
if err != nil {
return err
}
// Ensure cache directory exists
if err := os.MkdirAll(tc.CacheDir, 0755); err != nil {
return err
}
// Store state and message separated by newline
content := state + "\n" + message
return os.WriteFile(cachePath, []byte(content), 0644)
}
// IsCacheValid checks if tests were already run successfully with the current code
func (tc *TestCache) IsCacheValid() bool {
currentState, err := tc.GetGitState()
if err != nil {
return false
}
cachePath, err := tc.GetCachePath()
if err != nil {
return false
}
cachedData, err := os.ReadFile(cachePath)
if err != nil {
return false // No cache exists
}
// First line is the state
lines := strings.SplitN(string(cachedData), "\n", 2)
if len(lines) < 1 {
return false
}
return strings.TrimSpace(lines[0]) == currentState
}
// GetCachedMessage returns the cached test output message
func (tc *TestCache) GetCachedMessage() string {
cachePath, err := tc.GetCachePath()
if err != nil {
return ""
}
cachedData, err := os.ReadFile(cachePath)
if err != nil {
return ""
}
// Second line is the message
lines := strings.SplitN(string(cachedData), "\n", 2)
if len(lines) < 2 {
return ""
}
return lines[1]
}
// InvalidateCache removes the cache file
func (tc *TestCache) InvalidateCache() error {
cachePath, err := tc.GetCachePath()
if err != nil {
return err
}
return os.Remove(cachePath)
}