github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/io/ioutil/tempfile_test.go (about)

     1  // Copyright 2010 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 ioutil
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"regexp"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestTempFile(t *testing.T) {
    16  	dir, err := TempDir("", "TestTempFile_BadDir")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer os.RemoveAll(dir)
    21  
    22  	nonexistentDir := filepath.Join(dir, "_not_exists_")
    23  	f, err := TempFile(nonexistentDir, "foo")
    24  	if f != nil || err == nil {
    25  		t.Errorf("TempFile(%q, `foo`) = %v, %v", nonexistentDir, f, err)
    26  	}
    27  }
    28  
    29  func TestTempFile_pattern(t *testing.T) {
    30  	tests := []struct{ pattern, prefix, suffix string }{
    31  		{"ioutil_test", "ioutil_test", ""},
    32  		{"ioutil_test*", "ioutil_test", ""},
    33  		{"ioutil_test*xyz", "ioutil_test", "xyz"},
    34  	}
    35  	for _, test := range tests {
    36  		f, err := TempFile("", test.pattern)
    37  		if err != nil {
    38  			t.Errorf("TempFile(..., %q) error: %v", test.pattern, err)
    39  			continue
    40  		}
    41  		defer os.Remove(f.Name())
    42  		base := filepath.Base(f.Name())
    43  		f.Close()
    44  		if !(strings.HasPrefix(base, test.prefix) && strings.HasSuffix(base, test.suffix)) {
    45  			t.Errorf("TempFile pattern %q created bad name %q; want prefix %q & suffix %q",
    46  				test.pattern, base, test.prefix, test.suffix)
    47  		}
    48  	}
    49  }
    50  
    51  func TestTempDir(t *testing.T) {
    52  	name, err := TempDir("/_not_exists_", "foo")
    53  	if name != "" || err == nil {
    54  		t.Errorf("TempDir(`/_not_exists_`, `foo`) = %v, %v", name, err)
    55  	}
    56  
    57  	tests := []struct {
    58  		pattern                string
    59  		wantPrefix, wantSuffix string
    60  	}{
    61  		{"ioutil_test", "ioutil_test", ""},
    62  		{"ioutil_test*", "ioutil_test", ""},
    63  		{"ioutil_test*xyz", "ioutil_test", "xyz"},
    64  	}
    65  
    66  	dir := os.TempDir()
    67  
    68  	runTestTempDir := func(t *testing.T, pattern, wantRePat string) {
    69  		name, err := TempDir(dir, pattern)
    70  		if name == "" || err != nil {
    71  			t.Fatalf("TempDir(dir, `ioutil_test`) = %v, %v", name, err)
    72  		}
    73  		defer os.Remove(name)
    74  
    75  		re := regexp.MustCompile(wantRePat)
    76  		if !re.MatchString(name) {
    77  			t.Errorf("TempDir(%q, %q) created bad name\n\t%q\ndid not match pattern\n\t%q", dir, pattern, name, wantRePat)
    78  		}
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		t.Run(tt.pattern, func(t *testing.T) {
    83  			wantRePat := "^" + regexp.QuoteMeta(filepath.Join(dir, tt.wantPrefix)) + "[0-9]+" + regexp.QuoteMeta(tt.wantSuffix) + "$"
    84  			runTestTempDir(t, tt.pattern, wantRePat)
    85  		})
    86  	}
    87  
    88  	// Separately testing "*xyz" (which has no prefix). That is when constructing the
    89  	// pattern to assert on, as in the previous loop, using filepath.Join for an empty
    90  	// prefix filepath.Join(dir, ""), produces the pattern:
    91  	//     ^<DIR>[0-9]+xyz$
    92  	// yet we just want to match
    93  	//     "^<DIR>/[0-9]+xyz"
    94  	t.Run("*xyz", func(t *testing.T) {
    95  		wantRePat := "^" + regexp.QuoteMeta(filepath.Join(dir)) + regexp.QuoteMeta(string(filepath.Separator)) + "[0-9]+xyz$"
    96  		runTestTempDir(t, "*xyz", wantRePat)
    97  	})
    98  }
    99  
   100  // test that we return a nice error message if the dir argument to TempDir doesn't
   101  // exist (or that it's empty and os.TempDir doesn't exist)
   102  func TestTempDir_BadDir(t *testing.T) {
   103  	dir, err := TempDir("", "TestTempDir_BadDir")
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	defer os.RemoveAll(dir)
   108  
   109  	badDir := filepath.Join(dir, "not-exist")
   110  	_, err = TempDir(badDir, "foo")
   111  	if pe, ok := err.(*os.PathError); !ok || !os.IsNotExist(err) || pe.Path != badDir {
   112  		t.Errorf("TempDir error = %#v; want PathError for path %q satisifying os.IsNotExist", err, badDir)
   113  	}
   114  }