-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlp_windows_test.go
632 lines (589 loc) · 16.2 KB
/
lp_windows_test.go
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Use an external test to avoid os/exec -> internal/testenv -> os/exec
// circular dependency.
package exec_test
import (
"errors"
"fmt"
"internal/testenv"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func init() {
registerHelperCommand("printpath", cmdPrintPath)
}
func cmdPrintPath(_ ...string) {
fmt.Println(testenv.Executable(nil))
}
// makePATH returns a PATH variable referring to the
// given directories relative to a root directory.
//
// The empty string results in an empty entry.
// Paths beginning with . are kept as relative entries.
func makePATH(root string, dirs []string) string {
paths := make([]string, 0, len(dirs))
for _, d := range dirs {
switch {
case d == "":
paths = append(paths, "")
case d == "." || (len(d) >= 2 && d[0] == '.' && os.IsPathSeparator(d[1])):
paths = append(paths, filepath.Clean(d))
default:
paths = append(paths, filepath.Join(root, d))
}
}
return strings.Join(paths, string(os.PathListSeparator))
}
// installProgs creates executable files (or symlinks to executable files) at
// multiple destination paths. It uses root as prefix for all destination files.
func installProgs(t *testing.T, root string, files []string) {
for _, f := range files {
dstPath := filepath.Join(root, f)
dir := filepath.Dir(dstPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
if os.IsPathSeparator(f[len(f)-1]) {
continue // directory and PATH entry only.
}
if strings.EqualFold(filepath.Ext(f), ".bat") {
installBat(t, dstPath)
} else {
installExe(t, dstPath)
}
}
}
// installExe installs a copy of the test executable
// at the given location, creating directories as needed.
//
// (We use a copy instead of just a symlink to ensure that os.Executable
// always reports an unambiguous path, regardless of how it is implemented.)
func installExe(t *testing.T, dstPath string) {
src, err := os.Open(testenv.Executable(t))
if err != nil {
t.Fatal(err)
}
defer src.Close()
dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o777)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := dst.Close(); err != nil {
t.Fatal(err)
}
}()
_, err = io.Copy(dst, src)
if err != nil {
t.Fatal(err)
}
}
// installBat creates a batch file at dst that prints its own
// path when run.
func installBat(t *testing.T, dstPath string) {
dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o777)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := dst.Close(); err != nil {
t.Fatal(err)
}
}()
if _, err := fmt.Fprintf(dst, "@echo %s\r\n", dstPath); err != nil {
t.Fatal(err)
}
}
type lookPathTest struct {
name string
PATHEXT string // empty to use default
files []string
PATH []string // if nil, use all parent directories from files
searchFor string
want string
wantErr error
skipCmdExeCheck bool // if true, do not check want against the behavior of cmd.exe
}
var lookPathTests = []lookPathTest{
{
name: "first match",
files: []string{`p1\a.exe`, `p2\a.exe`, `p2\a`},
searchFor: `a`,
want: `p1\a.exe`,
},
{
name: "dirs with extensions",
files: []string{`p1.dir\a`, `p2.dir\a.exe`},
searchFor: `a`,
want: `p2.dir\a.exe`,
},
{
name: "first with extension",
files: []string{`p1\a.exe`, `p2\a.exe`},
searchFor: `a.exe`,
want: `p1\a.exe`,
},
{
name: "specific name",
files: []string{`p1\a.exe`, `p2\b.exe`},
searchFor: `b`,
want: `p2\b.exe`,
},
{
name: "no extension",
files: []string{`p1\b`, `p2\a`},
searchFor: `a`,
wantErr: exec.ErrNotFound,
},
{
name: "directory, no extension",
files: []string{`p1\a.exe`, `p2\a.exe`},
searchFor: `p2\a`,
want: `p2\a.exe`,
},
{
name: "no match",
files: []string{`p1\a.exe`, `p2\a.exe`},
searchFor: `b`,
wantErr: exec.ErrNotFound,
},
{
name: "no match with dir",
files: []string{`p1\b.exe`, `p2\a.exe`},
searchFor: `p2\b`,
wantErr: exec.ErrNotFound,
},
{
name: "extensionless file in CWD ignored",
files: []string{`a`, `p1\a.exe`, `p2\a.exe`},
searchFor: `a`,
want: `p1\a.exe`,
},
{
name: "extensionless file in PATH ignored",
files: []string{`p1\a`, `p2\a.exe`},
searchFor: `a`,
want: `p2\a.exe`,
},
{
name: "specific extension",
files: []string{`p1\a.exe`, `p2\a.bat`},
searchFor: `a.bat`,
want: `p2\a.bat`,
},
{
name: "mismatched extension",
files: []string{`p1\a.exe`, `p2\a.exe`},
searchFor: `a.com`,
wantErr: exec.ErrNotFound,
},
{
name: "doubled extension",
files: []string{`p1\a.exe.exe`},
searchFor: `a.exe`,
want: `p1\a.exe.exe`,
},
{
name: "extension not in PATHEXT",
PATHEXT: `.COM;.BAT`,
files: []string{`p1\a.exe`, `p2\a.exe`},
searchFor: `a.exe`,
want: `p1\a.exe`,
},
{
name: "first allowed by PATHEXT",
PATHEXT: `.COM;.EXE`,
files: []string{`p1\a.bat`, `p2\a.exe`},
searchFor: `a`,
want: `p2\a.exe`,
},
{
name: "first directory containing a PATHEXT match",
PATHEXT: `.COM;.EXE;.BAT`,
files: []string{`p1\a.bat`, `p2\a.exe`},
searchFor: `a`,
want: `p1\a.bat`,
},
{
name: "first PATHEXT entry",
PATHEXT: `.COM;.EXE;.BAT`,
files: []string{`p1\a.bat`, `p1\a.exe`, `p2\a.bat`, `p2\a.exe`},
searchFor: `a`,
want: `p1\a.exe`,
},
{
name: "ignore dir with PATHEXT extension",
files: []string{`a.exe\`},
searchFor: `a`,
wantErr: exec.ErrNotFound,
},
{
name: "ignore empty PATH entry",
files: []string{`a.bat`, `p\a.bat`},
PATH: []string{`p`},
searchFor: `a`,
want: `p\a.bat`,
// If cmd.exe is too old it might not respect NoDefaultCurrentDirectoryInExePath,
// so skip that check.
skipCmdExeCheck: true,
},
{
name: "return ErrDot if found by a different absolute path",
files: []string{`p1\a.bat`, `p2\a.bat`},
PATH: []string{`.\p1`, `p2`},
searchFor: `a`,
want: `p1\a.bat`,
wantErr: exec.ErrDot,
},
{
name: "suppress ErrDot if also found in absolute path",
files: []string{`p1\a.bat`, `p2\a.bat`},
PATH: []string{`.\p1`, `p1`, `p2`},
searchFor: `a`,
want: `p1\a.bat`,
},
}
func TestLookPathWindows(t *testing.T) {
// Not parallel: uses Chdir and Setenv.
// We are using the "printpath" command mode to test exec.Command here,
// so we won't be calling helperCommand to resolve it.
// That may cause it to appear to be unused.
maySkipHelperCommand("printpath")
// Before we begin, find the absolute path to cmd.exe.
// In non-short mode, we will use it to check the ground truth
// of the test's "want" field.
cmdExe, err := exec.LookPath("cmd")
if err != nil {
t.Fatal(err)
}
for _, tt := range lookPathTests {
t.Run(tt.name, func(t *testing.T) {
if tt.want == "" && tt.wantErr == nil {
t.Fatalf("test must specify either want or wantErr")
}
root := t.TempDir()
installProgs(t, root, tt.files)
if tt.PATHEXT != "" {
t.Setenv("PATHEXT", tt.PATHEXT)
t.Logf("set PATHEXT=%s", tt.PATHEXT)
}
var pathVar string
if tt.PATH == nil {
paths := make([]string, 0, len(tt.files))
for _, f := range tt.files {
dir := filepath.Join(root, filepath.Dir(f))
if !slices.Contains(paths, dir) {
paths = append(paths, dir)
}
}
pathVar = strings.Join(paths, string(os.PathListSeparator))
} else {
pathVar = makePATH(root, tt.PATH)
}
t.Setenv("PATH", pathVar)
t.Logf("set PATH=%s", pathVar)
t.Chdir(root)
if !testing.Short() && !(tt.skipCmdExeCheck || errors.Is(tt.wantErr, exec.ErrDot)) {
// Check that cmd.exe, which is our source of ground truth,
// agrees that our test case is correct.
cmd := testenv.Command(t, cmdExe, "/c", tt.searchFor, "printpath")
out, err := cmd.Output()
if err == nil {
gotAbs := strings.TrimSpace(string(out))
wantAbs := ""
if tt.want != "" {
wantAbs = filepath.Join(root, tt.want)
}
if gotAbs != wantAbs {
// cmd.exe disagrees. Probably the test case is wrong?
t.Fatalf("%v\n\tresolved to %s\n\twant %s", cmd, gotAbs, wantAbs)
}
} else if tt.wantErr == nil {
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
t.Fatalf("%v: %v\n%s", cmd, err, ee.Stderr)
}
t.Fatalf("%v: %v", cmd, err)
}
}
got, err := exec.LookPath(tt.searchFor)
if filepath.IsAbs(got) {
got, err = filepath.Rel(root, got)
if err != nil {
t.Fatal(err)
}
}
if got != tt.want {
t.Errorf("LookPath(%#q) = %#q; want %#q", tt.searchFor, got, tt.want)
}
if !errors.Is(err, tt.wantErr) {
t.Errorf("LookPath(%#q): %v; want %v", tt.searchFor, err, tt.wantErr)
}
})
}
}
type commandTest struct {
name string
PATH []string
files []string
dir string
arg0 string
want string
wantPath string // the resolved c.Path, if different from want
wantErrDot bool
wantRunErr error
}
var commandTests = []commandTest{
// testing commands with no slash, like `a.exe`
{
name: "current directory",
files: []string{`a.exe`},
PATH: []string{"."},
arg0: `a.exe`,
want: `a.exe`,
wantErrDot: true,
},
{
name: "with extra PATH",
files: []string{`a.exe`, `p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2", "p"},
arg0: `a.exe`,
want: `a.exe`,
wantErrDot: true,
},
{
name: "with extra PATH and no extension",
files: []string{`a.exe`, `p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2", "p"},
arg0: `a`,
want: `a.exe`,
wantErrDot: true,
},
// testing commands with slash, like `.\a.exe`
{
name: "with dir",
files: []string{`p\a.exe`},
PATH: []string{"."},
arg0: `p\a.exe`,
want: `p\a.exe`,
},
{
name: "with explicit dot",
files: []string{`p\a.exe`},
PATH: []string{"."},
arg0: `.\p\a.exe`,
want: `p\a.exe`,
},
{
name: "with irrelevant PATH",
files: []string{`p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2"},
arg0: `p\a.exe`,
want: `p\a.exe`,
},
{
name: "with slash and no extension",
files: []string{`p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2"},
arg0: `p\a`,
want: `p\a.exe`,
},
// tests commands, like `a.exe`, with c.Dir set
{
// should not find a.exe in p, because LookPath(`a.exe`) will fail when
// called by Command (before Dir is set), and that error is sticky.
name: "not found before Dir",
files: []string{`p\a.exe`},
PATH: []string{"."},
dir: `p`,
arg0: `a.exe`,
want: `p\a.exe`,
wantRunErr: exec.ErrNotFound,
},
{
// LookPath(`a.exe`) will resolve to `.\a.exe`, but prefixing that with
// dir `p\a.exe` will refer to a non-existent file
name: "resolved before Dir",
files: []string{`a.exe`, `p\not_important_file`},
PATH: []string{"."},
dir: `p`,
arg0: `a.exe`,
want: `a.exe`,
wantErrDot: true,
wantRunErr: fs.ErrNotExist,
},
{
// like above, but making test succeed by installing file
// in referred destination (so LookPath(`a.exe`) will still
// find `.\a.exe`, but we successfully execute `p\a.exe`)
name: "relative to Dir",
files: []string{`a.exe`, `p\a.exe`},
PATH: []string{"."},
dir: `p`,
arg0: `a.exe`,
want: `p\a.exe`,
wantErrDot: true,
},
{
// like above, but add PATH in attempt to break the test
name: "relative to Dir with extra PATH",
files: []string{`a.exe`, `p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2", "p"},
dir: `p`,
arg0: `a.exe`,
want: `p\a.exe`,
wantErrDot: true,
},
{
// like above, but use "a" instead of "a.exe" for command
name: "relative to Dir with extra PATH and no extension",
files: []string{`a.exe`, `p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2", "p"},
dir: `p`,
arg0: `a`,
want: `p\a.exe`,
wantErrDot: true,
},
{
// finds `a.exe` in the PATH regardless of Dir because Command resolves the
// full path (using LookPath) before Dir is set.
name: "from PATH with no match in Dir",
files: []string{`p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2", "p"},
dir: `p`,
arg0: `a.exe`,
want: `p2\a.exe`,
},
// tests commands, like `.\a.exe`, with c.Dir set
{
// should use dir when command is path, like ".\a.exe"
name: "relative to Dir with explicit dot",
files: []string{`p\a.exe`},
PATH: []string{"."},
dir: `p`,
arg0: `.\a.exe`,
want: `p\a.exe`,
},
{
// like above, but with PATH added in attempt to break it
name: "relative to Dir with dot and extra PATH",
files: []string{`p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2"},
dir: `p`,
arg0: `.\a.exe`,
want: `p\a.exe`,
},
{
// LookPath(".\a") will fail before Dir is set, and that error is sticky.
name: "relative to Dir with dot and extra PATH and no extension",
files: []string{`p\a.exe`, `p2\a.exe`},
PATH: []string{".", "p2"},
dir: `p`,
arg0: `.\a`,
want: `p\a.exe`,
},
{
// LookPath(".\a") will fail before Dir is set, and that error is sticky.
name: "relative to Dir with different extension",
files: []string{`a.exe`, `p\a.bat`},
PATH: []string{"."},
dir: `p`,
arg0: `.\a`,
want: `p\a.bat`,
},
}
func TestCommand(t *testing.T) {
// Not parallel: uses Chdir and Setenv.
// We are using the "printpath" command mode to test exec.Command here,
// so we won't be calling helperCommand to resolve it.
// That may cause it to appear to be unused.
maySkipHelperCommand("printpath")
for _, tt := range commandTests {
t.Run(tt.name, func(t *testing.T) {
if tt.PATH == nil {
t.Fatalf("test must specify PATH")
}
root := t.TempDir()
installProgs(t, root, tt.files)
pathVar := makePATH(root, tt.PATH)
t.Setenv("PATH", pathVar)
t.Logf("set PATH=%s", pathVar)
t.Chdir(root)
cmd := exec.Command(tt.arg0, "printpath")
cmd.Dir = filepath.Join(root, tt.dir)
if tt.wantErrDot {
if errors.Is(cmd.Err, exec.ErrDot) {
cmd.Err = nil
} else {
t.Fatalf("cmd.Err = %v; want ErrDot", cmd.Err)
}
}
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
t.Logf("%v: %v\n%s", cmd, err, ee.Stderr)
} else {
t.Logf("%v: %v", cmd, err)
}
if !errors.Is(err, tt.wantRunErr) {
t.Errorf("want %v", tt.wantRunErr)
}
return
}
got := strings.TrimSpace(string(out))
if filepath.IsAbs(got) {
got, err = filepath.Rel(root, got)
if err != nil {
t.Fatal(err)
}
}
if got != tt.want {
t.Errorf("\nran %#q\nwant %#q", got, tt.want)
}
gotPath := cmd.Path
wantPath := tt.wantPath
if wantPath == "" {
if strings.Contains(tt.arg0, `\`) {
wantPath = tt.arg0
} else if tt.wantErrDot {
wantPath = strings.TrimPrefix(tt.want, tt.dir+`\`)
} else {
wantPath = filepath.Join(root, tt.want)
}
}
if gotPath != wantPath {
t.Errorf("\ncmd.Path = %#q\nwant %#q", gotPath, wantPath)
}
})
}
}
func TestAbsCommandWithDoubledExtension(t *testing.T) {
t.Parallel()
// We expect that ".com" is always included in PATHEXT, but it may also be
// found in the import path of a Go package. If it is at the root of the
// import path, the resulting executable may be named like "example.com.exe".
//
// Since "example.com" looks like a proper executable name, it is probably ok
// for exec.Command to try to run it directly without re-resolving it.
// However, exec.LookPath should try a little harder to figure it out.
comPath := filepath.Join(t.TempDir(), "example.com")
batPath := comPath + ".bat"
installBat(t, batPath)
cmd := exec.Command(comPath)
out, err := cmd.CombinedOutput()
t.Logf("%v: %v\n%s", cmd, err, out)
if !errors.Is(err, fs.ErrNotExist) {
t.Errorf("Command(%#q).Run: %v\nwant fs.ErrNotExist", comPath, err)
}
resolved, err := exec.LookPath(comPath)
if err != nil || resolved != batPath {
t.Fatalf("LookPath(%#q) = %v, %v; want %#q, <nil>", comPath, resolved, err, batPath)
}
}