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

     1  package zfile
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	. "github.com/sohaha/zlsgo"
    11  )
    12  
    13  func TestFile(t *testing.T) {
    14  	tt := NewTest(t)
    15  
    16  	t.Log("c:" + RealPath("/"+"d"))
    17  
    18  	baseDir := "c:\\test"
    19  	fileName := "isPic"
    20  	t.Log(filepath.Join(baseDir, fileName))
    21  	t.Log(RealPath(baseDir + "/" + "d" + "/" + "fileName" + ".jpg"))
    22  	t.Log(RealPath(strings.Join([]string{"a", "b", "c", "dd\\ddd", ".jpg"}, "/")))
    23  
    24  	tmpFile := RealPath("./tmp.tmp")
    25  	_ = WriteFile(tmpFile, []byte(""))
    26  	defer func() {
    27  		_ = Remove(tmpFile)
    28  	}()
    29  
    30  	filePath := "./tmp.tmp"
    31  	tIsFile := FileExist(filePath)
    32  	tt.Equal(true, tIsFile)
    33  
    34  	err := MoveFile(filePath, filePath+".new")
    35  	tt.EqualNil(err)
    36  	tt.EqualTrue(!FileExist(filePath))
    37  	tt.EqualTrue(FileExist(filePath + ".new"))
    38  	_ = WriteFile("./tmp.tmp", []byte(""))
    39  	err = MoveFile(filePath, filePath+".new", true)
    40  	tt.EqualNil(err)
    41  	err = MoveFile(filePath+".new", filePath+".new", true)
    42  	tt.EqualNil(err)
    43  	_ = MoveFile(filePath+".new", filePath)
    44  
    45  	notPath := "zlsgo.php"
    46  	status, _ := PathExist(notPath)
    47  	tt.Equal(0, status)
    48  
    49  	size := FileSize(filePath)
    50  	t.Log(size)
    51  	tt.Equal("0 B", size)
    52  
    53  	RealPath("")
    54  
    55  	t.Log(filepath.Glob(RealPath("/Users/seekwe/Code/Go/zlsgo\\*\\file.go")))
    56  
    57  	dirPath := RealPathMkdir("../zfile", true)
    58  	t.Log(dirPath)
    59  	tIsDir := DirExist(dirPath)
    60  	tt.Equal(true, tIsDir)
    61  
    62  	dirPath = SafePath(dirPath, RealPath(".."))
    63  	t.Log(dirPath, SafePath(dirPath))
    64  	tt.Equal("zfile", dirPath)
    65  
    66  	tmpPath := TmpPath("")
    67  	tt.EqualTrue(tmpPath != "")
    68  	t.Log(tmpPath, TmpPath("666"))
    69  
    70  	path := RealPathMkdir("../tmp")
    71  	path2 := RealPathMkdir(path + "/ooo")
    72  	t.Log(path, path2)
    73  	tt.Equal(true, Rmdir(path, true))
    74  	tt.Equal(true, Rmdir(path))
    75  	ePath := ProgramPath(true)
    76  	ProjectPath = ePath
    77  	path = RealPathMkdir("../ppppp")
    78  	testPath := ePath + "../ppppp"
    79  	t.Log(path, testPath)
    80  	tt.EqualTrue(DirExist(path))
    81  	tt.EqualTrue(DirExist(testPath))
    82  	ok := Rmdir(testPath)
    83  	Rmdir(path)
    84  	t.Log(path, testPath, ok)
    85  }
    86  
    87  func TestPut(t *testing.T) {
    88  	var err error
    89  	tt := NewTest(t)
    90  	defer os.Remove("./text.txt")
    91  	err = PutOffset("./text.txt", []byte(time.Now().String()+"\n"), 0)
    92  	tt.EqualNil(err)
    93  	err = PutAppend("./text.txt", []byte(time.Now().String()+"\n"))
    94  	tt.EqualNil(err)
    95  	_ = os.Remove("./text.txt")
    96  	err = PutAppend("./put/text.txt", []byte(time.Now().String()+"\n"))
    97  	tt.EqualNil(err)
    98  	_ = os.Remove("./put/text.txt")
    99  	err = PutAppend("./put2/text.txt", []byte(time.Now().String()+"\n"))
   100  	tt.EqualNil(err)
   101  	_ = os.Remove("./put2/text.txt")
   102  	err = PutOffset("./text.txt", []byte("\n(ok)\n"), 5)
   103  	tt.EqualNil(err)
   104  }
   105  
   106  func TestGetMimeType(t *testing.T) {
   107  	tt := NewTest(t)
   108  
   109  	m := GetMimeType("test.jpg", nil)
   110  	tt.Log(m)
   111  	tt.Equal("image/jpeg", m)
   112  
   113  	m = GetMimeType("test.html", nil)
   114  	tt.Log(m)
   115  	tt.Equal("text/html", strings.Split(m, ";")[0])
   116  
   117  	m = GetMimeType("test", []byte("<html></html>"))
   118  	tt.Log(m)
   119  	tt.Equal("text/html", strings.Split(m, ";")[0])
   120  }