Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
452 lines (408 loc) · 13.5 KB
/
Copy pathintegration_test.go
File metadata and controls
452 lines (408 loc) · 13.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"context"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
"time"
)
// requireAllVar, when set (as in CI), makes a missing Visual Studio or shell a
// test failure rather than a skip.
const requireAllVar = "VSENV_TEST_REQUIRE_ALL"
// missingf skips the test for something it needs that's missing, or fails it
// if requireAllVar is set.
func missingf(t *testing.T, format string, args ...any) {
t.Helper()
if os.Getenv(requireAllVar) != "" {
t.Fatalf(format, args...)
}
t.Skipf(format, args...)
}
// otherArch returns a second target architecture to test besides arch: x86
// on x64, as the default C++ workload installs both, and x64 elsewhere.
func otherArch(arch string) string {
if arch == archX64 {
return archX86
}
return archX64
}
func requireVS(t *testing.T) {
t.Helper()
if _, err := os.Stat(vswhereDefault); err != nil {
missingf(t, "vswhere not installed at %s", vswhereDefault)
}
}
func TestIntegration_RunVsWhere(t *testing.T) {
requireVS(t)
installs, err := runVsWhere(context.Background())
if err != nil {
t.Fatal(err)
}
if len(installs) == 0 {
missingf(t, "no VS installations")
}
for _, i := range installs {
if i.InstanceID == "" {
t.Errorf("entry missing instanceId: %+v", i)
}
if i.InstallationPath == "" {
t.Errorf("entry missing installationPath: %+v", i)
}
if i.InstallationVersion == "" {
t.Errorf("entry missing installationVersion: %+v", i)
}
if _, statErr := os.Stat(i.InstallationPath); statErr != nil {
t.Errorf("installationPath does not exist: %s", i.InstallationPath)
}
}
}
func TestIntegration_DiscoverInstalls_WritesCache(t *testing.T) {
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
a, err := DiscoverInstalls(context.Background(), false)
if err != nil {
t.Fatal(err)
}
if len(a) == 0 {
missingf(t, "no VS installs")
}
cachePath, err := installsCachePath()
if err != nil {
t.Fatal(err)
}
if _, statErr := os.Stat(cachePath); statErr != nil {
t.Errorf("expected cache file written: %v", statErr)
}
// Second call should hit cache. Check by removing vswhere from PATH
// — actually we can't easily; just verify equivalence.
b, err := DiscoverInstalls(context.Background(), false)
if err != nil {
t.Fatal(err)
}
if len(a) != len(b) {
t.Errorf("cached result differs: %d vs %d", len(a), len(b))
}
}
func TestIntegration_DiscoverInstalls_ForceRefresh(t *testing.T) {
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
if _, err := DiscoverInstalls(context.Background(), false); err != nil {
t.Fatal(err)
}
// Force refresh should re-run vswhere even if cache exists
if _, err := DiscoverInstalls(context.Background(), true); err != nil {
t.Fatal(err)
}
}
func TestIntegration_CaptureDevShellEnv(t *testing.T) {
if testing.Short() {
t.Skip("skipping VsDevCmd.bat run in short mode")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
env, err := captureDevShellEnv(context.Background(), install, nativeArch(), nativeArch(), "", baseEnv())
if err != nil {
t.Fatal(err)
}
requiredKeys := []string{"VSINSTALLDIR", "VCINSTALLDIR", "INCLUDE", "LIB", "PATH", "VSCMD_VER"}
for _, k := range requiredKeys {
if lookupFold(env, k) == "" {
t.Errorf("missing expected env var %s in VsDevCmd output", k)
}
}
if v := env["VSCMD_ARG_TGT_ARCH"]; v != nativeArch() {
t.Errorf("VSCMD_ARG_TGT_ARCH = %q, want %s", v, nativeArch())
}
for _, k := range []string{telemetryOptOut, devCmdVar, devCmdArgsVar, selfVar} {
if _, leaked := env[k]; leaked {
t.Errorf("%s leaked into the captured env", k)
}
}
}
// Only catches a regression where the console code page isn't UTF-8, which
// is the default.
func TestIntegration_CaptureDevShellEnv_NonASCII(t *testing.T) {
if testing.Short() {
t.Skip("skipping VsDevCmd.bat run in short mode")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
const value, entry = "José Олег 日本", `C:\Users\José\bin`
base := append(baseEnv(), "VSENV_TEST_NON_ASCII="+value)
for i, kv := range base {
if k, v, _ := strings.Cut(kv, "="); strings.EqualFold(k, "PATH") {
base[i] = k + "=" + entry + ";" + v
}
}
full, err := captureDevShellEnv(context.Background(), install, nativeArch(), nativeArch(), "", base)
if err != nil {
t.Fatal(err)
}
if got := full["VSENV_TEST_NON_ASCII"]; got != value {
t.Errorf("VSENV_TEST_NON_ASCII = %q, want %q", got, value)
}
diff := diffEnv(envMap(base), full)
if _, ok := diff.Vars["VSENV_TEST_NON_ASCII"]; ok {
t.Error("an unchanged non-ASCII variable is in the diff")
}
// Garbled, the original PATH isn't found in VsDevCmd's, so its entries
// end up among the VS ones.
if slices.ContainsFunc(diff.PathBefore, func(p string) bool { return strings.Contains(p, `\Users\Jos`) }) {
t.Errorf("an original PATH entry is among the VS ones: %q", diff.PathBefore)
}
}
func TestIntegration_CaptureDevShellEnv_LongValue(t *testing.T) {
if testing.Short() {
t.Skip("skipping VsDevCmd.bat run in short mode")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
value := strings.Repeat("x", 30000)
base := append(baseEnv(), "VSENV_TEST_LONG="+value)
full, err := captureDevShellEnv(context.Background(), install, nativeArch(), nativeArch(), "", base)
if err != nil {
t.Fatal(err)
}
if got := full["VSENV_TEST_LONG"]; got != value {
t.Errorf("VSENV_TEST_LONG came back %d characters long, want %d", len(got), len(value))
}
if _, ok := diffEnv(envMap(base), full).Vars["VSENV_TEST_LONG"]; ok {
t.Error("an unchanged long variable is in the diff")
}
}
// captureDevEnv relies on VsDevCmd.bat adding the same with minimalPath.
func TestIntegration_CaptureDevShellEnv_MinimalPath(t *testing.T) {
if testing.Short() {
t.Skip("skipping (slow: runs VsDevCmd four times)")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
base := baseEnv()
minimal := withPath(base, minimalPath(base))
for _, arch := range []string{nativeArch(), otherArch(nativeArch())} {
var diffs []devEnv
for _, b := range [][]string{base, minimal} {
full, captureErr := captureDevShellEnv(context.Background(), install, arch, nativeArch(), "", b)
if captureErr != nil {
t.Fatalf("%s: %v", arch, captureErr)
}
diffs = append(diffs, diffEnv(envMap(b), full))
}
if !reflect.DeepEqual(diffs[0], diffs[1]) {
t.Errorf("%s: VsDevCmd.bat adds\n%+v\nwith the default PATH, and\n%+v\nwith a minimal one",
arch, diffs[0], diffs[1])
}
}
}
// VsDevCmd.bat fails with a PATH of about 7000 characters, and leaves out the
// SDK with one over maxCmdPath.
func TestIntegration_CaptureDevEnv_LongPath(t *testing.T) {
if testing.Short() {
t.Skip("skipping (slow: runs VsDevCmd three times)")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
base := baseEnv()
path := lookupFold(envMap(base), "PATH")
for _, n := range []int{7000, 9000} {
long := strings.Repeat(`C:\pad;`, (n-len(path))/len(`C:\pad;`)+1) + path
diff, captureErr := captureDevEnv(context.Background(), install, nativeArch(), nativeArch(), "",
withPath(base, long))
if captureErr != nil {
t.Fatalf("a PATH of %d characters: %v", len(long), captureErr)
}
if !slices.ContainsFunc(diff.PathBefore, func(p string) bool {
return strings.Contains(strings.ToLower(p), `\vc\tools\msvc\`)
}) {
t.Errorf("a PATH of %d characters: PathBefore lacks the MSVC bin dir: %q", len(long), diff.PathBefore)
}
if !slices.ContainsFunc(diff.Lists["LIB"].Before, func(p string) bool {
return strings.Contains(strings.ToLower(p), `\windows kits\`)
}) {
t.Errorf("a PATH of %d characters: LIB lacks the Windows SDK: %q", len(long), diff.Lists["LIB"].Before)
}
}
}
// sdkStateTime finds the SDK that VsDevCmd.bat uses.
func TestIntegration_SDKStateTime(t *testing.T) {
if testing.Short() {
t.Skip("skipping VsDevCmd.bat run in short mode")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
env, err := captureDevShellEnv(context.Background(), install, nativeArch(), nativeArch(), "", baseEnv())
if err != nil {
t.Fatal(err)
}
sdk := lookupFold(env, "WindowsSdkDir")
if sdk == "" {
missingf(t, "no Windows SDK")
}
info, err := os.Stat(filepath.Join(sdk, "Include"))
if err != nil {
t.Fatal(err)
}
if got := sdkStateTime(); !got.Equal(info.ModTime()) {
t.Errorf("sdkStateTime() = %v, want %v, %s's", got, info.ModTime(), sdk)
}
}
func TestIntegration_GetEnv_FromLoadedShell(t *testing.T) {
if testing.Short() {
t.Skip("skipping (slow: runs VsDevCmd twice)")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
clean, err := GetEnv(context.Background(), install, nativeArch(), nativeArch(), "")
if err != nil {
t.Fatal(err)
}
// Rebuild the cache from a process that already has the VS environment,
// as after 'vsenv refresh' in a shell whose profile loaded it.
for _, kv := range mergedEnv(clean) {
if k, v, ok := strings.Cut(kv, "="); ok && k != "" {
t.Setenv(k, v)
}
}
if clearErr := ClearEnvCache(); clearErr != nil {
t.Fatal(clearErr)
}
loaded, err := GetEnv(context.Background(), install, nativeArch(), nativeArch(), "")
if err != nil {
t.Fatal(err)
}
if len(loaded.Vars) != len(clean.Vars) || loaded.Vars["VSINSTALLDIR"] != clean.Vars["VSINSTALLDIR"] {
t.Errorf("capture depends on the calling shell: %d vars vs %d clean", len(loaded.Vars), len(clean.Vars))
}
if !slices.Equal(loaded.PathBefore, clean.PathBefore) || !slices.Equal(loaded.PathAfter, clean.PathAfter) {
t.Errorf("PATH entries differ:\nloaded %q\nclean %q", loaded.PathBefore, clean.PathBefore)
}
if lib := loaded.Lists["LIB"].Before; len(lib) == 0 || !slices.Equal(lib, clean.Lists["LIB"].Before) {
t.Errorf("LIB entries differ:\nloaded %q\nclean %q", lib, clean.Lists["LIB"].Before)
}
}
func TestIntegration_GetEnv_FullPipeline(t *testing.T) {
if testing.Short() {
t.Skip("skipping full pipeline in short mode")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
// Cold call: should run VsDevCmd.bat
cold := time.Now()
env1, err := GetEnv(context.Background(), install, nativeArch(), nativeArch(), "")
if err != nil {
t.Fatal(err)
}
coldDur := time.Since(cold)
if v := env1.Vars["VSINSTALLDIR"]; !strings.Contains(strings.ToLower(v), "visual studio") {
t.Errorf("VSINSTALLDIR doesn't look right: %q", v)
}
if !slices.ContainsFunc(env1.PathBefore, func(p string) bool {
return strings.Contains(strings.ToLower(p), `\vc\tools\msvc\`)
}) {
t.Errorf("PathBefore lacks the MSVC bin dir: %q", env1.PathBefore)
}
// Warm call: cache hit, must be fast
warm := time.Now()
env2, err := GetEnv(context.Background(), install, nativeArch(), nativeArch(), "")
if err != nil {
t.Fatal(err)
}
warmDur := time.Since(warm)
if warmDur > 500*time.Millisecond {
t.Errorf("warm cache took %v (>500ms); cold took %v", warmDur, coldDur)
}
if len(env1.Vars) != len(env2.Vars) {
t.Errorf("cold/warm env size mismatch: %d vs %d", len(env1.Vars), len(env2.Vars))
}
for k, v := range env1.Vars {
if env2.Vars[k] != v {
t.Errorf("warm cache differs for %s: %q vs %q", k, env2.Vars[k], v)
break
}
}
if !slices.Equal(env1.PathBefore, env2.PathBefore) || !slices.Equal(env1.PathAfter, env2.PathAfter) {
t.Error("warm cache PATH entries differ from cold")
}
t.Logf("cold: %v, warm: %v, env vars: %d", coldDur, warmDur, len(env1.Vars))
}
func TestIntegration_GetEnv_DifferentArchsCacheSeparately(t *testing.T) {
if testing.Short() {
t.Skip("skipping (slow: runs VsDevCmd twice)")
}
requireVS(t)
t.Setenv("LOCALAPPDATA", t.TempDir())
install, err := SelectInstall(context.Background(), "")
if err != nil {
missingf(t, "no install: %v", err)
}
native, other := nativeArch(), otherArch(nativeArch())
envNative, err := GetEnv(context.Background(), install, native, native, "")
if err != nil {
t.Fatal(err)
}
envOther, err := GetEnv(context.Background(), install, other, native, "")
if err != nil {
t.Fatalf("%s capture: %v", other, err)
}
if got := envNative.Vars["VSCMD_ARG_TGT_ARCH"]; got != native {
t.Errorf("%s cache has the target arch %q", native, got)
}
if got := envOther.Vars["VSCMD_ARG_TGT_ARCH"]; got != other {
t.Errorf("%s cache has the target arch %q", other, got)
}
// LIB paths should differ between archs
if a, b := envNative.Lists["LIB"].Before, envOther.Lists["LIB"].Before; len(a) == 0 || slices.Equal(a, b) {
t.Errorf("LIB entries should differ between %s and %s: %q and %q", native, other, a, b)
}
}
func TestIntegration_VswhereOutput_HasExpectedFields(t *testing.T) {
requireVS(t)
installs, err := runVsWhere(context.Background())
if err != nil {
t.Fatal(err)
}
if len(installs) == 0 {
missingf(t, "no installs")
}
i := installs[0]
// These four are what we use in cache keys / display.
if i.InstanceID == "" || i.InstallationPath == "" ||
i.InstallationVersion == "" || i.DisplayName == "" {
t.Errorf("vswhere returned an install missing required fields: %+v", i)
}
}