github.com/zhongdalu/gf@v1.0.0/g/os/gspath/gspath_unit_test.go (about)

     1  package gspath_test
     2  
     3  import (
     4  	"github.com/zhongdalu/gf/g/os/gfile"
     5  	"github.com/zhongdalu/gf/g/os/gspath"
     6  	"github.com/zhongdalu/gf/g/test/gtest"
     7  	"testing"
     8  )
     9  
    10  func TestSPath_Api(t *testing.T) {
    11  	gtest.Case(t, func() {
    12  		pwd := gfile.Pwd()
    13  		root := pwd + gfile.Separator
    14  		gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
    15  		defer gfile.Remove(root + "gf_tmp")
    16  		fp, isDir := gspath.Search(root, "gf_tmp")
    17  		gtest.Assert(fp, root+"gf_tmp")
    18  		gtest.Assert(isDir, true)
    19  		fp, isDir = gspath.Search(root, "gf_tmp", "gf.txt")
    20  		gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
    21  		gtest.Assert(isDir, false)
    22  
    23  		fp, isDir = gspath.SearchWithCache(root, "gf_tmp")
    24  		gtest.Assert(fp, root+"gf_tmp")
    25  		gtest.Assert(isDir, true)
    26  		fp, isDir = gspath.SearchWithCache(root, "gf_tmp", "gf.txt")
    27  		gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
    28  		gtest.Assert(isDir, false)
    29  	})
    30  }
    31  
    32  func TestSPath_Basic(t *testing.T) {
    33  	gtest.Case(t, func() {
    34  		pwd := gfile.Pwd()
    35  		root := pwd + gfile.Separator
    36  		gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
    37  		defer gfile.Remove(root + "gf_tmp")
    38  		gsp := gspath.New(root, false)
    39  		realPath, err := gsp.Add(root + "gf_tmp")
    40  		gtest.Assert(err, nil)
    41  		gtest.Assert(realPath, root+"gf_tmp")
    42  		realPath, err = gsp.Add("gf_tmp1")
    43  		gtest.Assert(err != nil, true)
    44  		gtest.Assert(realPath, "")
    45  		realPath, err = gsp.Add(root + "gf_tmp" + gfile.Separator + "gf.txt")
    46  		gtest.Assert(err != nil, true)
    47  		gtest.Assert(realPath, "")
    48  		gsp.Remove("gf_tmp1")
    49  		gtest.Assert(gsp.Size(), 2)
    50  		gtest.Assert(len(gsp.Paths()), 2)
    51  		gtest.Assert(len(gsp.AllPaths()), 0)
    52  		realPath, err = gsp.Set(root + "gf_tmp1")
    53  		gtest.Assert(err != nil, true)
    54  		gtest.Assert(realPath, "")
    55  		realPath, err = gsp.Set(root + "gf_tmp" + gfile.Separator + "gf.txt")
    56  		gtest.Assert(err != nil, true)
    57  		gtest.Assert(realPath, "")
    58  		gsp.Set(root)
    59  		fp, isDir := gsp.Search("gf_tmp")
    60  		gtest.Assert(fp, root+"gf_tmp")
    61  		gtest.Assert(isDir, true)
    62  		fp, isDir = gsp.Search("gf_tmp", "gf.txt")
    63  		gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
    64  		gtest.Assert(isDir, false)
    65  		fp, isDir = gsp.Search("/", "gf.txt")
    66  		gtest.Assert(fp, root+gfile.Separator)
    67  		gtest.Assert(isDir, true)
    68  
    69  		gsp = gspath.New(root, true)
    70  		realPath, err = gsp.Add(root + "gf_tmp")
    71  		gtest.Assert(err, nil)
    72  		gtest.Assert(realPath, root+"gf_tmp")
    73  
    74  		gfile.Mkdir(root + "gf_tmp1")
    75  		gfile.Rename(root+"gf_tmp1", root+"gf_tmp2")
    76  		gfile.Rename(root+"gf_tmp2", root+"gf_tmp1")
    77  		defer gfile.Remove(root + "gf_tmp1")
    78  		realPath, err = gsp.Add("gf_tmp1")
    79  		gtest.Assert(err != nil, false)
    80  		gtest.Assert(realPath, root+"gf_tmp1")
    81  		realPath, err = gsp.Add("gf_tmp3")
    82  		gtest.Assert(err != nil, true)
    83  		gtest.Assert(realPath, "")
    84  		gsp.Remove(root + "gf_tmp")
    85  		gsp.Remove(root + "gf_tmp1")
    86  		gsp.Remove(root + "gf_tmp3")
    87  		gtest.Assert(gsp.Size(), 3)
    88  		gtest.Assert(len(gsp.Paths()), 3)
    89  		gsp.AllPaths()
    90  		gsp.Set(root)
    91  		fp, isDir = gsp.Search("gf_tmp")
    92  		gtest.Assert(fp, root+"gf_tmp")
    93  		gtest.Assert(isDir, true)
    94  		fp, isDir = gsp.Search("gf_tmp", "gf.txt")
    95  		gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
    96  		gtest.Assert(isDir, false)
    97  		fp, isDir = gsp.Search("/", "gf.txt")
    98  		gtest.Assert(fp, pwd)
    99  		gtest.Assert(isDir, true)
   100  	})
   101  }