github.com/gogf/gf@v1.16.9/os/gspath/gspath_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/gogf/gf. 6 7 package gspath_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/os/gfile" 13 "github.com/gogf/gf/os/gspath" 14 "github.com/gogf/gf/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.Assert(err, nil) 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 gsp.Remove("gf_tmp1") 57 t.Assert(gsp.Size(), 2) 58 t.Assert(len(gsp.Paths()), 2) 59 t.Assert(len(gsp.AllPaths()), 0) 60 realPath, err = gsp.Set(gfile.Join(root, "gf_tmp1")) 61 t.Assert(err != nil, true) 62 t.Assert(realPath, "") 63 realPath, err = gsp.Set(gfile.Join(root, "gf_tmp", "gf.txt")) 64 t.AssertNE(err, nil) 65 t.Assert(realPath, "") 66 67 realPath, err = gsp.Set(root) 68 t.Assert(err, nil) 69 t.Assert(realPath, root) 70 71 fp, isDir := gsp.Search("gf_tmp") 72 t.Assert(fp, gfile.Join(root, "gf_tmp")) 73 t.Assert(isDir, true) 74 fp, isDir = gsp.Search("gf_tmp", "gf.txt") 75 t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt")) 76 t.Assert(isDir, false) 77 fp, isDir = gsp.Search("/", "gf.txt") 78 t.Assert(fp, root) 79 t.Assert(isDir, true) 80 81 gsp = gspath.New(root, true) 82 realPath, err = gsp.Add(gfile.Join(root, "gf_tmp")) 83 t.Assert(err, nil) 84 t.Assert(realPath, gfile.Join(root, "gf_tmp")) 85 86 gfile.Mkdir(gfile.Join(root, "gf_tmp1")) 87 gfile.Rename(gfile.Join(root, "gf_tmp1"), gfile.Join(root, "gf_tmp2")) 88 gfile.Rename(gfile.Join(root, "gf_tmp2"), gfile.Join(root, "gf_tmp1")) 89 defer gfile.Remove(gfile.Join(root, "gf_tmp1")) 90 realPath, err = gsp.Add("gf_tmp1") 91 t.Assert(err != nil, false) 92 t.Assert(realPath, gfile.Join(root, "gf_tmp1")) 93 94 realPath, err = gsp.Add("gf_tmp3") 95 t.Assert(err != nil, true) 96 t.Assert(realPath, "") 97 98 gsp.Remove(gfile.Join(root, "gf_tmp")) 99 gsp.Remove(gfile.Join(root, "gf_tmp1")) 100 gsp.Remove(gfile.Join(root, "gf_tmp3")) 101 t.Assert(gsp.Size(), 3) 102 t.Assert(len(gsp.Paths()), 3) 103 104 gsp.AllPaths() 105 gsp.Set(root) 106 fp, isDir = gsp.Search("gf_tmp") 107 t.Assert(fp, gfile.Join(root, "gf_tmp")) 108 t.Assert(isDir, true) 109 110 fp, isDir = gsp.Search("gf_tmp", "gf.txt") 111 t.Assert(fp, gfile.Join(root, "gf_tmp", "gf.txt")) 112 t.Assert(isDir, false) 113 114 fp, isDir = gsp.Search("/", "gf.txt") 115 t.Assert(fp, pwd) 116 t.Assert(isDir, true) 117 }) 118 }