Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
222 lines (199 loc) · 6.99 KB
/
Copy pathcache_test.go
File metadata and controls
222 lines (199 loc) · 6.99 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"context"
"encoding/json"
"os"
"testing"
"time"
)
func TestEnvCachePath_DeterministicForSameInput(t *testing.T) {
install := &Install{InstanceID: "abc", InstallationVersion: "18.5.0"}
a, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
b, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
if a != b {
t.Errorf("same input should yield same path: %q vs %q", a, b)
}
}
func TestEnvCachePath_DiffersByInstance(t *testing.T) {
a, _ := envCachePath(&Install{InstanceID: "one", InstallationVersion: "1"}, "x64", "x64", "")
b, _ := envCachePath(&Install{InstanceID: "two", InstallationVersion: "1"}, "x64", "x64", "")
if a == b {
t.Error("different instances should yield different paths")
}
}
func TestEnvCachePath_DiffersByVersion(t *testing.T) {
a, _ := envCachePath(&Install{InstanceID: "i", InstallationVersion: "18.5.0"}, "x64", "x64", "")
b, _ := envCachePath(&Install{InstanceID: "i", InstallationVersion: "18.5.1"}, "x64", "x64", "")
if a == b {
t.Error("different versions should yield different paths")
}
}
func TestEnvCachePath_DiffersByArch(t *testing.T) {
install := &Install{InstanceID: "i", InstallationVersion: "1"}
a, _ := envCachePath(install, "x64", "x64", "")
b, _ := envCachePath(install, "arm64", "x64", "")
if a == b {
t.Error("different target arch should yield different paths")
}
}
func TestEnvCachePath_DiffersByHostArch(t *testing.T) {
install := &Install{InstanceID: "i", InstallationVersion: "1"}
a, _ := envCachePath(install, "x64", "x64", "")
b, _ := envCachePath(install, "x64", "x86", "")
if a == b {
t.Error("different host arch should yield different paths")
}
}
func TestEnvCachePath_DiffersByDevCmdArgs(t *testing.T) {
install := &Install{InstanceID: "i", InstallationVersion: "1"}
a, _ := envCachePath(install, "x64", "x64", "")
b, _ := envCachePath(install, "x64", "x64", "-vcvars_ver=14.39")
if a == b {
t.Error("different dev-cmd-args should yield different paths")
}
}
func TestGetEnv_CacheHit_SkipsDevCmd(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
t.Setenv("ProgramData", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
path, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
preseeded := envCache{
Schema: cacheSchemaVersion,
InstanceID: install.InstanceID,
InstallationVersion: install.InstallationVersion,
Arch: "x64",
HostArch: "x64",
SDKs: sdkStateTime(),
Env: devEnv{Vars: map[string]string{"FOO": "bar"}, PathBefore: []string{`C:\fake`}},
}
data, _ := json.MarshalIndent(preseeded, "", " ")
if writeErr := os.WriteFile(path, data, 0o600); writeErr != nil {
t.Fatal(writeErr)
}
got, err := GetEnv(context.Background(), install, "x64", "x64", "")
if err != nil {
t.Fatalf("GetEnv: %v", err)
}
if got.Vars["FOO"] != "bar" || len(got.PathBefore) != 1 || got.PathBefore[0] != `C:\fake` {
t.Errorf("expected cache-hit env, got %v", got)
}
}
func TestGetEnv_StaleSchema_ForcesRebuild(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
path, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
stale := map[string]any{"schema": 999, "env": map[string]any{"vars": map[string]string{"FOO": "stale"}}}
data, _ := json.Marshal(stale)
if writeErr := os.WriteFile(path, data, 0o600); writeErr != nil {
t.Fatal(writeErr)
}
// GetEnv should not return the stale FOO; it'll try to spawn cmd.exe and fail
// because the install path is fake. We just verify it didn't blindly use the cache.
got, err := GetEnv(context.Background(), install, "x64", "x64", "")
if err == nil && got.Vars["FOO"] == "stale" {
t.Error("stale-schema cache should have been rejected")
}
}
func TestGetEnv_InstanceModified_ForcesRebuild(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
t.Setenv("ProgramData", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
path, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
cached := envCache{
Schema: cacheSchemaVersion,
SDKs: sdkStateTime(),
Env: devEnv{Vars: map[string]string{"FOO": "old"}},
}
data, _ := json.Marshal(cached)
if writeErr := os.WriteFile(path, data, 0o600); writeErr != nil {
t.Fatal(writeErr)
}
// The VS Installer touched another instance after the cache was written.
writeInstanceState(t, "other", time.Now().Add(time.Hour))
// As with a stale schema, the rebuild fails on the fake install path; what
// matters is that the cached value wasn't returned.
got, err := GetEnv(context.Background(), install, "x64", "x64", "")
if err == nil && got.Vars["FOO"] == "old" {
t.Error("cache older than the instance's state should have been rejected")
}
}
func TestGetEnv_FutureInstanceState_StillHits(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
t.Setenv("ProgramData", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
// A state.json dated ahead of the clock mustn't make every run rebuild.
writeInstanceState(t, "x", time.Now().Add(time.Hour))
path, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
cached := envCache{
Schema: cacheSchemaVersion,
Instances: instanceStateTime("*"),
SDKs: sdkStateTime(),
Env: devEnv{Vars: map[string]string{"FOO": "cached"}},
}
data, _ := json.Marshal(cached)
if writeErr := os.WriteFile(path, data, 0o600); writeErr != nil {
t.Fatal(writeErr)
}
got, err := GetEnv(context.Background(), install, "x64", "x64", "")
if err != nil {
t.Fatalf("GetEnv: %v", err)
}
if got.Vars["FOO"] != "cached" {
t.Errorf("expected the cached env, got %v", got)
}
}
func TestGetEnv_SDKsChanged_ForcesRebuild(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
t.Setenv("ProgramData", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
path, err := envCachePath(install, "x64", "x64", "")
if err != nil {
t.Fatal(err)
}
cached := envCache{
Schema: cacheSchemaVersion,
Instances: instanceStateTime("*"),
SDKs: sdkStateTime().Add(-time.Hour),
Env: devEnv{Vars: map[string]string{"FOO": "old"}},
}
data, _ := json.Marshal(cached)
if writeErr := os.WriteFile(path, data, 0o600); writeErr != nil {
t.Fatal(writeErr)
}
got, err := GetEnv(context.Background(), install, "x64", "x64", "")
if err == nil && got.Vars["FOO"] == "old" {
t.Error("cache older than the SDKs should have been rejected")
}
}
func TestClearEnvCache(t *testing.T) {
t.Setenv("LOCALAPPDATA", t.TempDir())
install := &Install{InstanceID: "x", InstallationVersion: "1.0"}
path, _ := envCachePath(install, "x64", "x64", "")
if err := os.WriteFile(path, []byte("{}"), 0o600); err != nil {
t.Fatal(err)
}
if err := ClearEnvCache(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("expected cache file removed, stat err=%v", err)
}
}