github.com/wangyougui/gf/v2@v2.6.5/os/gspath/gspath_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gspath_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/os/gfile"
    13  	"github.com/wangyougui/gf/v2/os/gspath"
    14  	"github.com/wangyougui/gf/v2/test/gtest"
    15  )
    16  
    17  func TestSPath_Api(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		pwd := gfile.Pwd()
    20  		root := pwd
    21  		gfile.Create(gfile.Join(root, "gf_tmp", "gf.txt"))
    22  		defer gfile.Remove(gfile.Join(root, "gf_tmp"))
    23  		fp, isDir := gspath.Search(root, "gf_tmp")
    24  		t.Assert(fp, gfile.Join(root, "gf_tmp"))
    25  		t.Assert(isDir, true)
    26  		fp, isDir = gspath.Search(root, "gf_tmp", "gf.txt")
    27  		t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt"))
    28  		t.Assert(isDir, false)
    29  
    30  		fp, isDir = gspath.SearchWithCache(root, "gf_tmp")
    31  		t.Assert(fp, gfile.Join(root, "gf_tmp"))
    32  		t.Assert(isDir, true)
    33  		fp, isDir = gspath.SearchWithCache(root, "gf_tmp", "gf.txt")
    34  		t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt"))
    35  		t.Assert(isDir, false)
    36  	})
    37  }
    38  
    39  func TestSPath_Basic(t *testing.T) {
    40  	gtest.C(t, func(t *gtest.T) {
    41  		pwd := gfile.Pwd()
    42  		root := pwd
    43  
    44  		gfile.Create(gfile.Join(root, "gf_tmp", "gf.txt"))
    45  		defer gfile.Remove(gfile.Join(root, "gf_tmp"))
    46  		gsp := gspath.New(root, false)
    47  		realPath, err := gsp.Add(gfile.Join(root, "gf_tmp"))
    48  		t.AssertNil(err)
    49  		t.Assert(realPath, gfile.Join(root, "gf_tmp"))
    50  		realPath, err = gsp.Add("gf_tmp1")
    51  		t.Assert(err != nil, true)
    52  		t.Assert(realPath, "")
    53  		realPath, err = gsp.Add(gfile.Join(root, "gf_tmp", "gf.txt"))
    54  		t.Assert(err != nil, true)
    55  		t.Assert(realPath, "")
    56  
    57  		gsp.Remove("gf_tmp1")
    58  
    59  		t.Assert(gsp.Size(), 2)
    60  		t.Assert(len(gsp.Paths()), 2)
    61  		t.Assert(len(gsp.AllPaths()), 0)
    62  		realPath, err = gsp.Set(gfile.Join(root, "gf_tmp1"))
    63  		t.Assert(err != nil, true)
    64  		t.Assert(realPath, "")
    65  		realPath, err = gsp.Set(gfile.Join(root, "gf_tmp", "gf.txt"))
    66  		t.AssertNE(err, nil)
    67  		t.Assert(realPath, "")
    68  
    69  		realPath, err = gsp.Set(root)
    70  		t.AssertNil(err)
    71  		t.Assert(realPath, root)
    72  
    73  		fp, isDir := gsp.Search("gf_tmp")
    74  		t.Assert(fp, gfile.Join(root, "gf_tmp"))
    75  		t.Assert(isDir, true)
    76  		fp, isDir = gsp.Search("gf_tmp", "gf.txt")
    77  		t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt"))
    78  		t.Assert(isDir, false)
    79  		fp, isDir = gsp.Search("/", "gf.txt")
    80  		t.Assert(fp, root)
    81  		t.Assert(isDir, true)
    82  
    83  		gsp = gspath.New(root, true)
    84  		realPath, err = gsp.Add(gfile.Join(root, "gf_tmp"))
    85  		t.AssertNil(err)
    86  		t.Assert(realPath, gfile.Join(root, "gf_tmp"))
    87  
    88  		gfile.Mkdir(gfile.Join(root, "gf_tmp1"))
    89  		gfile.Rename(gfile.Join(root, "gf_tmp1"), gfile.Join(root, "gf_tmp2"))
    90  		gfile.Rename(gfile.Join(root, "gf_tmp2"), gfile.Join(root, "gf_tmp1"))
    91  		defer gfile.Remove(gfile.Join(root, "gf_tmp1"))
    92  		realPath, err = gsp.Add("gf_tmp1")
    93  		t.Assert(err != nil, false)
    94  		t.Assert(realPath, gfile.Join(root, "gf_tmp1"))
    95  
    96  		realPath, err = gsp.Add("gf_tmp3")
    97  		t.Assert(err != nil, true)
    98  		t.Assert(realPath, "")
    99  
   100  		gsp.Remove(gfile.Join(root, "gf_tmp"))
   101  		gsp.Remove(gfile.Join(root, "gf_tmp1"))
   102  		gsp.Remove(gfile.Join(root, "gf_tmp3"))
   103  		t.Assert(gsp.Size(), 3)
   104  		t.Assert(len(gsp.Paths()), 3)
   105  
   106  		gsp.AllPaths()
   107  		gsp.Set(root)
   108  		fp, isDir = gsp.Search("gf_tmp")
   109  		t.Assert(fp, gfile.Join(root, "gf_tmp"))
   110  		t.Assert(isDir, true)
   111  
   112  		fp, isDir = gsp.Search("gf_tmp", "gf.txt")
   113  		t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt"))
   114  		t.Assert(isDir, false)
   115  	})
   116  }