github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zfile/handle_test.go (about)

     1  package zfile
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	. "github.com/sohaha/zlsgo"
     8  )
     9  
    10  func TestCopy(tt *testing.T) {
    11  	t := NewTest(tt)
    12  	dest := RealPathMkdir("../tmp", true)
    13  	defer Rmdir(dest)
    14  	err := CopyFile("../doc.go", dest+"tmp.tmp")
    15  	t.Equal(nil, err)
    16  	err = CopyDir("../znet", dest, func(srcFilePath, destFilePath string) bool {
    17  		return srcFilePath == "../znet/timeout/timeout.go"
    18  	})
    19  	t.Equal(nil, err)
    20  }
    21  
    22  func TestRW(t *testing.T) {
    23  	var err error
    24  	var text []byte
    25  	tt := NewTest(t)
    26  	str := []byte("666")
    27  
    28  	_ = WriteFile("./text.txt", str)
    29  	text, err = ReadFile("./text.txt")
    30  	tt.EqualNil(err)
    31  	tt.Equal(str, text)
    32  	t.Log(string(text))
    33  
    34  	_ = WriteFile("./text.txt", str, true)
    35  	text, err = ReadFile("./text.txt")
    36  	tt.EqualNil(err)
    37  	t.Log(string(text))
    38  	tt.Equal([]byte("666666"), text)
    39  
    40  	_ = WriteFile("./text.txt", str)
    41  	text, err = ReadFile("./text.txt")
    42  	tt.EqualNil(err)
    43  	t.Log(string(text))
    44  	tt.Equal(str, text)
    45  	_ = os.Remove("./text.txt")
    46  }
    47  
    48  func TestReadLineFile(t *testing.T) {
    49  	_ = WriteFile("./TestReadLineFile.txt", []byte("111\n2222\nTestReadLineFile\n88"))
    50  	defer os.Remove("./TestReadLineFile.txt")
    51  	tt := NewTest(t)
    52  	file := "./TestReadLineFile.txt"
    53  	i := 4
    54  	err := ReadLineFile(file, func(line int, data []byte) error {
    55  		t.Log(line, string(data))
    56  		i--
    57  		return nil
    58  	})
    59  	tt.EqualNil(err)
    60  	tt.Equal(0, i)
    61  
    62  	_ = WriteFile("./TestReadLineFile.txt", []byte("111\n2222\nTestReadLineFile\n88\n"))
    63  	i = 5
    64  	err = ReadLineFile(file, func(line int, data []byte) error {
    65  		t.Log(line, string(data))
    66  		i--
    67  		return nil
    68  	})
    69  	tt.EqualNil(err)
    70  	tt.Equal(0, i)
    71  }