github.com/gogf/gf@v1.16.9/os/gres/gres_z_unit_2_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 gres_test 8 9 import ( 10 _ "github.com/gogf/gf/os/gres/testdata/data" 11 12 "testing" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/test/gtest" 16 17 "github.com/gogf/gf/os/gres" 18 ) 19 20 func Test_Basic(t *testing.T) { 21 gres.Dump() 22 gtest.C(t, func(t *gtest.T) { 23 t.Assert(gres.Get("none"), nil) 24 t.Assert(gres.Contains("none"), false) 25 t.Assert(gres.Contains("dir1"), true) 26 }) 27 28 gtest.C(t, func(t *gtest.T) { 29 path := "dir1/test1" 30 file := gres.Get(path) 31 t.AssertNE(file, nil) 32 t.Assert(file.Name(), path) 33 34 info := file.FileInfo() 35 t.AssertNE(info, nil) 36 t.Assert(info.IsDir(), false) 37 t.Assert(info.Name(), "test1") 38 39 rc, err := file.Open() 40 t.Assert(err, nil) 41 defer rc.Close() 42 43 b := make([]byte, 5) 44 n, err := rc.Read(b) 45 t.Assert(n, 5) 46 t.Assert(err, nil) 47 t.Assert(string(b), "test1") 48 49 t.Assert(file.Content(), "test1 content") 50 }) 51 52 gtest.C(t, func(t *gtest.T) { 53 path := "dir2" 54 file := gres.Get(path) 55 t.AssertNE(file, nil) 56 t.Assert(file.Name(), path) 57 58 info := file.FileInfo() 59 t.AssertNE(info, nil) 60 t.Assert(info.IsDir(), true) 61 t.Assert(info.Name(), "dir2") 62 63 rc, err := file.Open() 64 t.Assert(err, nil) 65 defer rc.Close() 66 67 t.Assert(file.Content(), nil) 68 }) 69 70 gtest.C(t, func(t *gtest.T) { 71 path := "dir2/test2" 72 file := gres.Get(path) 73 t.AssertNE(file, nil) 74 t.Assert(file.Name(), path) 75 t.Assert(file.Content(), "test2 content") 76 }) 77 } 78 79 func Test_Get(t *testing.T) { 80 gres.Dump() 81 gtest.C(t, func(t *gtest.T) { 82 t.AssertNE(gres.Get("dir1/test1"), nil) 83 }) 84 gtest.C(t, func(t *gtest.T) { 85 file := gres.GetWithIndex("dir1", g.SliceStr{"test1"}) 86 t.AssertNE(file, nil) 87 t.Assert(file.Name(), "dir1/test1") 88 }) 89 gtest.C(t, func(t *gtest.T) { 90 t.Assert(gres.GetContent("dir1"), "") 91 t.Assert(gres.GetContent("dir1/test1"), "test1 content") 92 }) 93 } 94 95 func Test_ScanDir(t *testing.T) { 96 gres.Dump() 97 gtest.C(t, func(t *gtest.T) { 98 path := "dir1" 99 files := gres.ScanDir(path, "*", false) 100 t.AssertNE(files, nil) 101 t.Assert(len(files), 2) 102 }) 103 gtest.C(t, func(t *gtest.T) { 104 path := "dir1" 105 files := gres.ScanDir(path, "*", true) 106 t.AssertNE(files, nil) 107 t.Assert(len(files), 3) 108 }) 109 110 gtest.C(t, func(t *gtest.T) { 111 path := "dir1" 112 files := gres.ScanDir(path, "*.*", true) 113 t.AssertNE(files, nil) 114 t.Assert(len(files), 1) 115 t.Assert(files[0].Name(), "dir1/sub/sub-test1.txt") 116 t.Assert(files[0].Content(), "sub-test1 content") 117 }) 118 } 119 120 func Test_ScanDirFile(t *testing.T) { 121 gres.Dump() 122 gtest.C(t, func(t *gtest.T) { 123 path := "dir2" 124 files := gres.ScanDirFile(path, "*", false) 125 t.AssertNE(files, nil) 126 t.Assert(len(files), 1) 127 }) 128 gtest.C(t, func(t *gtest.T) { 129 path := "dir2" 130 files := gres.ScanDirFile(path, "*", true) 131 t.AssertNE(files, nil) 132 t.Assert(len(files), 2) 133 }) 134 135 gtest.C(t, func(t *gtest.T) { 136 path := "dir2" 137 files := gres.ScanDirFile(path, "*.*", true) 138 t.AssertNE(files, nil) 139 t.Assert(len(files), 1) 140 t.Assert(files[0].Name(), "dir2/sub/sub-test2.txt") 141 t.Assert(files[0].Content(), "sub-test2 content") 142 }) 143 }