github.com/gogf/gf/v2@v2.7.4/os/gfile/gfile_z_unit_search_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 gfile_test
     8  
     9  import (
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/v2/os/gfile"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  )
    16  
    17  func Test_Search(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		var (
    20  			paths1  string = "/testfiless"
    21  			paths2  string = "./testfile/dirfiles_no"
    22  			tpath   string
    23  			tpath2  string
    24  			tempstr string
    25  			ypaths1 string
    26  			err     error
    27  		)
    28  
    29  		createDir(paths1)
    30  		defer delTestFiles(paths1)
    31  		ypaths1 = paths1
    32  
    33  		tpath, err = gfile.Search(testpath() + paths1)
    34  		t.AssertNil(err)
    35  
    36  		tpath = filepath.ToSlash(tpath)
    37  
    38  		// 自定义优先路径
    39  		tpath2, err = gfile.Search(testpath() + paths1)
    40  		t.AssertNil(err)
    41  		tpath2 = filepath.ToSlash(tpath2)
    42  
    43  		tempstr = testpath()
    44  		paths1 = tempstr + paths1
    45  		paths1 = filepath.ToSlash(paths1)
    46  
    47  		t.Assert(tpath, paths1)
    48  
    49  		t.Assert(tpath2, tpath)
    50  
    51  		// 测试给定目录
    52  		tpath2, err = gfile.Search(paths1, "testfiless")
    53  		tpath2 = filepath.ToSlash(tpath2)
    54  		tempss := filepath.ToSlash(paths1)
    55  		t.Assert(tpath2, tempss)
    56  
    57  		// 测试当前目录
    58  		tempstr, _ = filepath.Abs("./")
    59  		tempstr = testpath()
    60  		paths1 = tempstr + ypaths1
    61  		paths1 = filepath.ToSlash(paths1)
    62  
    63  		t.Assert(tpath2, paths1)
    64  
    65  		// 测试目录不存在时
    66  		_, err = gfile.Search(paths2)
    67  		t.AssertNE(err, nil)
    68  
    69  	})
    70  }