github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/path/filepath/path_windows_test.go (about)

     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  	"testing"
    14  )
    15  
    16  func TestWinSplitListTestsAreValid(t *testing.T) {
    17  	comspec := os.Getenv("ComSpec")
    18  	if comspec == "" {
    19  		t.Fatal("%ComSpec% must be set")
    20  	}
    21  
    22  	for ti, tt := range winsplitlisttests {
    23  		testWinSplitListTestIsValid(t, ti, tt, comspec)
    24  	}
    25  }
    26  
    27  func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
    28  	comspec string) {
    29  
    30  	const (
    31  		cmdfile             = `printdir.cmd`
    32  		perm    os.FileMode = 0700
    33  	)
    34  
    35  	tmp, err := ioutil.TempDir("", "testWinSplitListTestIsValid")
    36  	if err != nil {
    37  		t.Fatalf("TempDir failed: %v", err)
    38  	}
    39  	defer os.RemoveAll(tmp)
    40  
    41  	for i, d := range tt.result {
    42  		if d == "" {
    43  			continue
    44  		}
    45  		if cd := filepath.Clean(d); filepath.VolumeName(cd) != "" ||
    46  			cd[0] == '\\' || cd == ".." || (len(cd) >= 3 && cd[0:3] == `..\`) {
    47  			t.Errorf("%d,%d: %#q refers outside working directory", ti, i, d)
    48  			return
    49  		}
    50  		dd := filepath.Join(tmp, d)
    51  		if _, err := os.Stat(dd); err == nil {
    52  			t.Errorf("%d,%d: %#q already exists", ti, i, d)
    53  			return
    54  		}
    55  		if err = os.MkdirAll(dd, perm); err != nil {
    56  			t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
    57  			return
    58  		}
    59  		fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
    60  		if err = ioutil.WriteFile(fn, data, perm); err != nil {
    61  			t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
    62  			return
    63  		}
    64  	}
    65  
    66  	for i, d := range tt.result {
    67  		if d == "" {
    68  			continue
    69  		}
    70  		exp := []byte(d + "\r\n")
    71  		cmd := &exec.Cmd{
    72  			Path: comspec,
    73  			Args: []string{`/c`, cmdfile},
    74  			Env:  []string{`Path=` + tt.list},
    75  			Dir:  tmp,
    76  		}
    77  		out, err := cmd.CombinedOutput()
    78  		switch {
    79  		case err != nil:
    80  			t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out)
    81  			return
    82  		case !reflect.DeepEqual(out, exp):
    83  			t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out)
    84  			return
    85  		default:
    86  			// unshadow cmdfile in next directory
    87  			err = os.Remove(filepath.Join(tmp, d, cmdfile))
    88  			if err != nil {
    89  				t.Fatalf("Remove test command failed: %v", err)
    90  			}
    91  		}
    92  	}
    93  }