github.com/zhongdalu/gf@v1.0.0/g/os/gfile/gfile_z_search_test.go (about)

     1  package gfile_test
     2  
     3  import (
     4  	"github.com/zhongdalu/gf/g/os/gfile"
     5  	"github.com/zhongdalu/gf/g/test/gtest"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestSearch(t *testing.T) {
    11  	gtest.Case(t, func() {
    12  		var (
    13  			paths1  string = "/testfiless"
    14  			paths2  string = "./testfile/dirfiles_no"
    15  			tpath   string
    16  			tpath2  string
    17  			tempstr string
    18  			ypaths1 string
    19  			err     error
    20  		)
    21  
    22  		createDir(paths1)
    23  		defer delTestFiles(paths1)
    24  		ypaths1 = paths1
    25  
    26  		tpath, err = gfile.Search(testpath() + paths1)
    27  		gtest.Assert(err, nil)
    28  
    29  		tpath = filepath.ToSlash(tpath)
    30  
    31  		// 自定义优先路径
    32  		tpath2, err = gfile.Search(testpath() + paths1)
    33  		gtest.Assert(err, nil)
    34  		tpath2 = filepath.ToSlash(tpath2)
    35  
    36  		tempstr = testpath()
    37  		paths1 = tempstr + paths1
    38  		paths1 = filepath.ToSlash(paths1)
    39  
    40  		gtest.Assert(tpath, paths1)
    41  
    42  		gtest.Assert(tpath2, tpath)
    43  
    44  		// 测试给定目录
    45  		tpath2, err = gfile.Search(paths1, "testfiless")
    46  		tpath2 = filepath.ToSlash(tpath2)
    47  		tempss := filepath.ToSlash(paths1)
    48  		gtest.Assert(tpath2, tempss)
    49  
    50  		// 测试当前目录
    51  		tempstr, _ = filepath.Abs("./")
    52  		tempstr = testpath()
    53  		paths1 = tempstr + ypaths1
    54  		paths1 = filepath.ToSlash(paths1)
    55  
    56  		gtest.Assert(tpath2, paths1)
    57  
    58  		// 测试目录不存在时
    59  		_, err = gfile.Search(paths2)
    60  		gtest.AssertNE(err, nil)
    61  
    62  	})
    63  }