github.com/searKing/golang/go@v1.2.117/os/file_test.go (about)

     1  package os_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	os_ "github.com/searKing/golang/go/os"
    10  )
    11  
    12  // tmpDir creates a temporary directory and returns its name.
    13  func tmpFile(t *testing.T) string {
    14  	tmp, err := os.CreateTemp("", "")
    15  	if err != nil {
    16  		t.Fatalf("temp file creation failed: %v", err)
    17  	}
    18  	defer os.Remove(tmp.Name())
    19  	defer tmp.Close()
    20  	return tmp.Name()
    21  }
    22  
    23  func TestCreateAll(t *testing.T) {
    24  	tmp := tmpFile(t)
    25  	f, err := os_.CreateAll(tmp)
    26  	if err != nil {
    27  		t.Fatalf("temp file CreateAll failed: %v", err)
    28  	}
    29  	err = f.Close()
    30  	if err != nil {
    31  		t.Fatalf("temp file Close failed: %v", err)
    32  	}
    33  	if err := os.Remove(tmp); err != nil {
    34  		t.Fatalf("temp file Remove failed: %v", err)
    35  	}
    36  }
    37  
    38  func TestTouchAll(t *testing.T) {
    39  	tmp := tmpFile(t)
    40  	f, err := os_.TouchAll(tmp)
    41  	if err != nil {
    42  		t.Fatalf("temp file TouchAll failed: %v", err)
    43  	}
    44  	err = f.Close()
    45  	if err != nil {
    46  		t.Fatalf("temp file Close failed: %v", err)
    47  	}
    48  	if err := os.Remove(tmp); err != nil {
    49  		t.Fatalf("temp file Remove failed: %v", err)
    50  	}
    51  }
    52  
    53  func TestCreateAllIfNotExist(t *testing.T) {
    54  	tmp := tmpFile(t)
    55  	f, err := os_.CreateAllIfNotExist(tmp)
    56  	if err != nil {
    57  		t.Fatalf("temp file CreateAllIfNotExist failed: %v", err)
    58  	}
    59  	err = f.Close()
    60  	if err != nil {
    61  		t.Fatalf("temp file Close failed: %v", err)
    62  	}
    63  	if err := os.Remove(tmp); err != nil {
    64  		t.Fatalf("temp file CreateAllIfNotExist failed: %v", err)
    65  	}
    66  }
    67  
    68  func TestRelink(t *testing.T) {
    69  	tmpOld := tmpFile(t)
    70  	tmpNew := tmpFile(t)
    71  	func() {
    72  		f, err := os_.CreateAllIfNotExist(tmpOld)
    73  		if err != nil {
    74  			t.Fatalf("temp file CreateAllIfNotExist failed: %v", err)
    75  		}
    76  		defer f.Close()
    77  	}()
    78  	err := os_.ReLink(tmpOld, tmpNew)
    79  	if err != nil {
    80  		t.Fatalf("temp file ReSymlink failed: %v", err)
    81  	}
    82  	if err := os.Remove(tmpOld); err != nil {
    83  		t.Fatalf("temp file[%s] Remove failed: %v", tmpOld, err)
    84  	}
    85  	if err := os.Remove(tmpNew); err != nil {
    86  		t.Fatalf("temp file[%s] Remove failed: %v", tmpNew, err)
    87  	}
    88  }
    89  
    90  func TestReSymlink(t *testing.T) {
    91  	tmpOld := tmpFile(t)
    92  	tmpNew := tmpFile(t)
    93  	func() {
    94  		f, err := os_.CreateAllIfNotExist(tmpOld)
    95  		if err != nil {
    96  			t.Fatalf("temp file CreateAllIfNotExist failed: %v", err)
    97  		}
    98  		defer f.Close()
    99  	}()
   100  	err := os_.ReSymlink(tmpOld, tmpNew)
   101  	if err != nil {
   102  		t.Fatalf("temp file ReSymlink failed: %v", err)
   103  	}
   104  	if err := os.Remove(tmpOld); err != nil {
   105  		t.Fatalf("temp file[%s] Remove failed: %v", tmpOld, err)
   106  	}
   107  	if err := os.Remove(tmpNew); err != nil {
   108  		t.Fatalf("temp file[%s] Remove failed: %v", tmpNew, err)
   109  	}
   110  }
   111  
   112  func TestNextFile(t *testing.T) {
   113  	t.Parallel()
   114  
   115  	dir, err := os.MkdirTemp("", "TestNextFileBadDir")
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	defer os.RemoveAll(dir)
   120  
   121  	tests := []struct{ pattern, prefix, suffix string }{
   122  		{filepath.Join(dir, "tempfile_test"), "tempfile_test", ""},
   123  		{filepath.Join(dir, "tempfile_test*"), "tempfile_test", ""},
   124  		{filepath.Join(dir, "tempfile_test*xyz"), "tempfile_test", "xyz"},
   125  	}
   126  
   127  	for _, test := range tests {
   128  		f, seq, err := os_.NextFile(test.pattern, 0)
   129  		if err != nil {
   130  			t.Errorf("CreateTemp(..., %q) error: %v", test.pattern, err)
   131  			continue
   132  		}
   133  		defer os.Remove(f.Name())
   134  		base := filepath.Base(f.Name())
   135  		f.Close()
   136  		_ = seq
   137  		if !(strings.HasPrefix(base, test.prefix) && strings.HasSuffix(base, test.suffix)) {
   138  			t.Errorf("NextFile pattern %q created bad name %q; want prefix %q suffix %q",
   139  				test.pattern, base, test.prefix, test.suffix)
   140  		}
   141  	}
   142  }