github.com/kaydxh/golang@v0.0.131/go/os/file_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package os_test
    23  
    24  import (
    25  	"fmt"
    26  	"io"
    27  	"io/ioutil"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  
    32  	io_ "github.com/kaydxh/golang/go/io"
    33  	os_ "github.com/kaydxh/golang/go/os"
    34  	"github.com/stretchr/testify/assert"
    35  )
    36  
    37  func TestOpenAll(t *testing.T) {
    38  	dir, err := ioutil.TempDir("", "dir")
    39  	if err != nil {
    40  		t.Errorf("expect nil, got %v", err)
    41  	}
    42  	defer os.RemoveAll(dir)
    43  
    44  	f, err := os_.OpenAll(dir, os.O_CREATE, 0755)
    45  	if err != nil {
    46  		t.Errorf("expect nil, got %v", err)
    47  	}
    48  
    49  	_ = f
    50  
    51  	fmt.Println(dir)
    52  
    53  	for i := 0; i < 10; i++ {
    54  		dirName := filepath.Join(dir, fmt.Sprintf("dir-%d", i))
    55  		f, err = os_.OpenAll(dirName, os.O_CREATE|os.O_RDWR, 0755)
    56  		if err != nil {
    57  			t.Errorf("expect nil, got %v", err)
    58  		}
    59  		defer f.Close()
    60  
    61  		n, err := io.WriteString(f, "hello world")
    62  		if err != nil {
    63  			t.Errorf("expect nil, got %v", err)
    64  		}
    65  		assert.Equal(t, len("hello world"), n)
    66  		fmt.Println(dirName)
    67  	}
    68  }
    69  
    70  func TestSameFile(t *testing.T) {
    71  	total := 2
    72  	fileNames := make([]string, total)
    73  	dir, err := ioutil.TempDir("", "dir")
    74  	if err != nil {
    75  		t.Errorf("expect nil, got %v", err)
    76  	}
    77  	defer os.RemoveAll(dir)
    78  
    79  	for i := 0; i < total; i++ {
    80  		fileNames[i] = filepath.Join(dir, fmt.Sprintf("dir-%d", i))
    81  
    82  		buf := []byte("hello world")
    83  		err = ioutil.WriteFile(fileNames[i], buf, 0777)
    84  		if err != nil {
    85  			t.Errorf("expect nil, got %v", err)
    86  		}
    87  		fmt.Println(fileNames[i])
    88  	}
    89  
    90  	ok := os_.SameFile(fileNames[0], fileNames[0])
    91  	assert.Truef(t, ok, "fileName %s is the same of %s", fileNames[0], fileNames[0])
    92  
    93  	ok = os_.SameFile(fileNames[0], fileNames[1])
    94  	assert.Falsef(t, ok, "fileName %s is not the same of %s", fileNames[0], fileNames[1])
    95  
    96  }
    97  
    98  func TestSymLink(t *testing.T) {
    99  	dir, err := ioutil.TempDir("", "dir")
   100  	if err != nil {
   101  		t.Errorf("expect nil, got %v", err)
   102  	}
   103  	defer os.RemoveAll(dir)
   104  
   105  	oldname := filepath.Join(dir, "oldname")
   106  
   107  	buf := []byte("hello world")
   108  	err = ioutil.WriteFile(oldname, buf, 0777)
   109  	if err != nil {
   110  		t.Errorf("expect nil, got %v", err)
   111  	}
   112  
   113  	newname := "test.link"
   114  	err = os_.SymLink(oldname, newname)
   115  	if err != nil {
   116  		t.Fatalf("expect nil, got %v", err)
   117  	}
   118  
   119  	data, err := io_.ReadFile(newname)
   120  	if err != nil {
   121  		t.Errorf("expect nil, got %v", err)
   122  	}
   123  
   124  	assert.Equal(t, buf, data)
   125  
   126  }
   127  
   128  func TestPath(t *testing.T) {
   129  
   130  	filename := "./workspace/studyspace/.././11.jpg"
   131  	///Users/kayxhding/workspace/studyspace/git-kayxhding/github.com/kaydxh/golang/go/os/workspace/11.jpg
   132  	fmt.Println(filepath.Abs(filename))
   133  	//workspace/11.jpg
   134  	fmt.Println(filepath.Clean(filename))
   135  	//../11.jpg
   136  	fmt.Println(filepath.Rel("./workspace/22", filename))
   137  }
   138  
   139  func TestReadDirNames(t *testing.T) {
   140  	dir := "/usr/local"
   141  	allFiles, err := os_.ReadDirNames(dir, true)
   142  	if err != nil {
   143  		t.Errorf("expect nil, got %v", err)
   144  	}
   145  
   146  	t.Logf("got sub dirs: %v", allFiles)
   147  
   148  }
   149  
   150  func TestReadSubDirs(t *testing.T) {
   151  	dir := "/usr/local"
   152  	subDirs, err := os_.ReadDirSubDirNames(dir, true)
   153  	if err != nil {
   154  		t.Errorf("expect nil, got %v", err)
   155  	}
   156  
   157  	t.Logf("got sub dirs: %v", subDirs)
   158  }
   159  
   160  func TestReadDirFileNames(t *testing.T) {
   161  	dir := "/usr/local"
   162  	fileNames, err := os_.ReadDirFileNames(dir, true)
   163  	if err != nil {
   164  		t.Errorf("expect nil, got %v", err)
   165  	}
   166  
   167  	t.Logf("got filenames: %v", fileNames)
   168  
   169  }
   170  
   171  func TestMakeTempDirAll(t *testing.T) {
   172  	//  /var/folders/by/jt3bf2n52fv6v7lrbndtdmjr0000gn/T/example690523200.12
   173  	dir, err := os_.MakeTempDirAll("", "example*.12")
   174  	if err != nil {
   175  		t.Errorf("expect nil, got %v", err)
   176  	}
   177  
   178  	t.Logf("got dir: %v", dir)
   179  
   180  }