1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package filepath_test 6 7 import ( 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "reflect" 13 "syscall" 14 "testing" 15 ) 16 17 func init() { 18 tmpdir, err := ioutil.TempDir("", "symtest") 19 if err != nil { 20 panic("failed to create temp directory: " + err.Error()) 21 } 22 defer os.RemoveAll(tmpdir) 23 24 err = os.Symlink("target", filepath.Join(tmpdir, "symlink")) 25 if err == nil { 26 return 27 } 28 29 err = err.(*os.LinkError).Err 30 switch err { 31 case syscall.EWINDOWS, syscall.ERROR_PRIVILEGE_NOT_HELD: 32 supportsSymlinks = false 33 } 34 } 35 36 func TestWinSplitListTestsAreValid(t *testing.T) { 37 comspec := os.Getenv("ComSpec") 38 if comspec == "" { 39 t.Fatal("%ComSpec% must be set") 40 } 41 42 for ti, tt := range winsplitlisttests { 43 testWinSplitListTestIsValid(t, ti, tt, comspec) 44 } 45 } 46 47 func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest, 48 comspec string) { 49 50 const ( 51 cmdfile = `printdir.cmd` 52 perm os.FileMode = 0700 53 ) 54 55 tmp, err := ioutil.TempDir("", "testWinSplitListTestIsValid") 56 if err != nil { 57 t.Fatalf("TempDir failed: %v", err) 58 } 59 defer os.RemoveAll(tmp) 60 61 for i, d := range tt.result { 62 if d == "" { 63 continue 64 } 65 if cd := filepath.Clean(d); filepath.VolumeName(cd) != "" || 66 cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) { 67 t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d) 68 return 69 } 70 dd := filepath.Join(tmp, d) 71 if _, err := os.Stat(dd); err == nil { 72 t.Errorf("%d,%d: %#q already exists", ti, i, d) 73 return 74 } 75 if err = os.MkdirAll(dd, perm); err != nil { 76 t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err) 77 return 78 } 79 fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n") 80 if err = ioutil.WriteFile(fn, data, perm); err != nil { 81 t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err) 82 return 83 } 84 } 85 86 for i, d := range tt.result { 87 if d == "" { 88 continue 89 } 90 exp := []byte(d + "\r\n") 91 cmd := &exec.Cmd{ 92 Path: comspec, 93 Args: []string{`/c`, cmdfile}, 94 Env: []string{`Path=` + tt.list}, 95 Dir: tmp, 96 } 97 out, err := cmd.CombinedOutput() 98 switch { 99 case err != nil: 100 t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out) 101 return 102 case !reflect.DeepEqual(out, exp): 103 t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out) 104 return 105 default: 106 // unshadow cmdfile in next directory 107 err = os.Remove(filepath.Join(tmp, d, cmdfile)) 108 if err != nil { 109 t.Fatalf("Remove test command failed: %v", err) 110 } 111 } 112 } 113 } 114