github.com/wangyougui/gf/v2@v2.6.5/os/gres/gres_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 gres_test
     8  
     9  import (
    10  	_ "github.com/wangyougui/gf/v2/os/gres/testdata/data"
    11  
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/wangyougui/gf/v2/frame/g"
    16  	"github.com/wangyougui/gf/v2/os/gfile"
    17  	"github.com/wangyougui/gf/v2/os/gres"
    18  	"github.com/wangyougui/gf/v2/os/gtime"
    19  	"github.com/wangyougui/gf/v2/test/gtest"
    20  )
    21  
    22  func Test_PackFolderToGoFile(t *testing.T) {
    23  	gtest.C(t, func(t *gtest.T) {
    24  		var (
    25  			srcPath    = gtest.DataPath("files")
    26  			goFilePath = gfile.Temp(gtime.TimestampNanoStr(), "testdata.go")
    27  			pkgName    = "testdata"
    28  			err        = gres.PackToGoFile(srcPath, goFilePath, pkgName)
    29  		)
    30  		t.AssertNil(err)
    31  		_ = gfile.Remove(goFilePath)
    32  	})
    33  }
    34  
    35  func Test_PackMultiFilesToGoFile(t *testing.T) {
    36  	gres.Dump()
    37  	gtest.C(t, func(t *gtest.T) {
    38  		var (
    39  			srcPath    = gtest.DataPath("files")
    40  			goFilePath = gfile.Temp(gtime.TimestampNanoStr(), "data.go")
    41  			pkgName    = "data"
    42  			array, err = gfile.ScanDir(srcPath, "*", false)
    43  		)
    44  		t.AssertNil(err)
    45  		err = gres.PackToGoFile(strings.Join(array, ","), goFilePath, pkgName)
    46  		t.AssertNil(err)
    47  		defer gfile.Remove(goFilePath)
    48  
    49  		t.AssertNil(gfile.CopyFile(goFilePath, gtest.DataPath("data/data.go")))
    50  	})
    51  }
    52  
    53  func Test_Pack(t *testing.T) {
    54  	gtest.C(t, func(t *gtest.T) {
    55  		var (
    56  			srcPath   = gtest.DataPath("files")
    57  			data, err = gres.Pack(srcPath)
    58  		)
    59  		t.AssertNil(err)
    60  
    61  		r := gres.New()
    62  		err = r.Add(string(data))
    63  		t.AssertNil(err)
    64  		t.Assert(r.Contains("files/"), true)
    65  	})
    66  }
    67  
    68  func Test_PackToFile(t *testing.T) {
    69  	gtest.C(t, func(t *gtest.T) {
    70  		var (
    71  			srcPath = gtest.DataPath("files")
    72  			dstPath = gfile.Temp(gtime.TimestampNanoStr())
    73  			err     = gres.PackToFile(srcPath, dstPath)
    74  		)
    75  		t.AssertNil(err)
    76  
    77  		defer gfile.Remove(dstPath)
    78  
    79  		r := gres.New()
    80  		err = r.Load(dstPath)
    81  		t.AssertNil(err)
    82  		t.Assert(r.Contains("files"), true)
    83  	})
    84  }
    85  
    86  func Test_PackWithPrefix1(t *testing.T) {
    87  	gtest.C(t, func(t *gtest.T) {
    88  		var (
    89  			srcPath    = gtest.DataPath("files")
    90  			goFilePath = gfile.Temp(gtime.TimestampNanoStr(), "testdata.go")
    91  			pkgName    = "testdata"
    92  			err        = gres.PackToGoFile(srcPath, goFilePath, pkgName, "www/gf-site/test")
    93  		)
    94  		t.AssertNil(err)
    95  		_ = gfile.Remove(goFilePath)
    96  	})
    97  }
    98  
    99  func Test_PackWithPrefix2(t *testing.T) {
   100  	gtest.C(t, func(t *gtest.T) {
   101  		var (
   102  			srcPath    = gtest.DataPath("files")
   103  			goFilePath = gfile.Temp(gtime.TimestampNanoStr(), "testdata.go")
   104  			pkgName    = "testdata"
   105  			err        = gres.PackToGoFile(srcPath, goFilePath, pkgName, "/var/www/gf-site/test")
   106  		)
   107  		t.AssertNil(err)
   108  		_ = gfile.Remove(goFilePath)
   109  	})
   110  }
   111  
   112  func Test_Unpack(t *testing.T) {
   113  	gtest.C(t, func(t *gtest.T) {
   114  		var (
   115  			srcPath    = gtest.DataPath("testdata.txt")
   116  			files, err = gres.Unpack(srcPath)
   117  		)
   118  		t.AssertNil(err)
   119  		t.Assert(len(files), 63)
   120  	})
   121  }
   122  
   123  func Test_Basic(t *testing.T) {
   124  	gres.Dump()
   125  	gtest.C(t, func(t *gtest.T) {
   126  		t.Assert(gres.Get("none"), nil)
   127  		t.Assert(gres.Contains("none"), false)
   128  		t.Assert(gres.Contains("dir1"), true)
   129  	})
   130  
   131  	gtest.C(t, func(t *gtest.T) {
   132  		path := "dir1/test1"
   133  		file := gres.Get(path)
   134  		t.AssertNE(file, nil)
   135  		t.Assert(file.Name(), path)
   136  
   137  		info := file.FileInfo()
   138  		t.AssertNE(info, nil)
   139  		t.Assert(info.IsDir(), false)
   140  		t.Assert(info.Name(), "test1")
   141  
   142  		rc, err := file.Open()
   143  		t.AssertNil(err)
   144  		defer rc.Close()
   145  
   146  		b := make([]byte, 5)
   147  		n, err := rc.Read(b)
   148  		t.Assert(n, 5)
   149  		t.AssertNil(err)
   150  		t.Assert(string(b), "test1")
   151  
   152  		t.Assert(file.Content(), "test1 content")
   153  	})
   154  
   155  	gtest.C(t, func(t *gtest.T) {
   156  		path := "dir2"
   157  		file := gres.Get(path)
   158  		t.AssertNE(file, nil)
   159  		t.Assert(file.Name(), path)
   160  
   161  		info := file.FileInfo()
   162  		t.AssertNE(info, nil)
   163  		t.Assert(info.IsDir(), true)
   164  		t.Assert(info.Name(), "dir2")
   165  
   166  		rc, err := file.Open()
   167  		t.AssertNil(err)
   168  		defer rc.Close()
   169  
   170  		t.Assert(file.Content(), nil)
   171  	})
   172  
   173  	gtest.C(t, func(t *gtest.T) {
   174  		path := "dir2/test2"
   175  		file := gres.Get(path)
   176  		t.AssertNE(file, nil)
   177  		t.Assert(file.Name(), path)
   178  		t.Assert(file.Content(), "test2 content")
   179  	})
   180  }
   181  
   182  func Test_Get(t *testing.T) {
   183  	gres.Dump()
   184  	gtest.C(t, func(t *gtest.T) {
   185  		t.AssertNE(gres.Get("dir1/test1"), nil)
   186  	})
   187  	gtest.C(t, func(t *gtest.T) {
   188  		file := gres.GetWithIndex("dir1", g.SliceStr{"test1"})
   189  		t.AssertNE(file, nil)
   190  		t.Assert(file.Name(), "dir1/test1")
   191  	})
   192  	gtest.C(t, func(t *gtest.T) {
   193  		t.Assert(gres.GetContent("dir1"), "")
   194  		t.Assert(gres.GetContent("dir1/test1"), "test1 content")
   195  	})
   196  }
   197  
   198  func Test_ScanDir(t *testing.T) {
   199  	gres.Dump()
   200  	gtest.C(t, func(t *gtest.T) {
   201  		path := "dir1"
   202  		files := gres.ScanDir(path, "*", false)
   203  		t.AssertNE(files, nil)
   204  		t.Assert(len(files), 2)
   205  	})
   206  	gtest.C(t, func(t *gtest.T) {
   207  		path := "dir1"
   208  		files := gres.ScanDir(path, "*", true)
   209  		t.AssertNE(files, nil)
   210  		t.Assert(len(files), 3)
   211  	})
   212  
   213  	gtest.C(t, func(t *gtest.T) {
   214  		path := "dir1"
   215  		files := gres.ScanDir(path, "*.*", true)
   216  		t.AssertNE(files, nil)
   217  		t.Assert(len(files), 1)
   218  		t.Assert(files[0].Name(), "dir1/sub/sub-test1.txt")
   219  		t.Assert(files[0].Content(), "sub-test1 content")
   220  	})
   221  }
   222  
   223  func Test_ScanDirFile(t *testing.T) {
   224  	gres.Dump()
   225  	gtest.C(t, func(t *gtest.T) {
   226  		path := "dir2"
   227  		files := gres.ScanDirFile(path, "*", false)
   228  		t.AssertNE(files, nil)
   229  		t.Assert(len(files), 1)
   230  	})
   231  	gtest.C(t, func(t *gtest.T) {
   232  		path := "dir2"
   233  		files := gres.ScanDirFile(path, "*", true)
   234  		t.AssertNE(files, nil)
   235  		t.Assert(len(files), 2)
   236  	})
   237  
   238  	gtest.C(t, func(t *gtest.T) {
   239  		path := "dir2"
   240  		files := gres.ScanDirFile(path, "*.*", true)
   241  		t.AssertNE(files, nil)
   242  		t.Assert(len(files), 1)
   243  		t.Assert(files[0].Name(), "dir2/sub/sub-test2.txt")
   244  		t.Assert(files[0].Content(), "sub-test2 content")
   245  	})
   246  }
   247  
   248  func Test_Export(t *testing.T) {
   249  	gres.Dump()
   250  	gtest.C(t, func(t *gtest.T) {
   251  		var (
   252  			src = `template-res`
   253  			dst = gfile.Temp(gtime.TimestampNanoStr())
   254  			err = gres.Export(src, dst)
   255  		)
   256  		defer gfile.Remove(dst)
   257  		t.AssertNil(err)
   258  		files, err := gfile.ScanDir(dst, "*", true)
   259  		t.AssertNil(err)
   260  		t.Assert(len(files), 14)
   261  
   262  		name := `template-res/index.html`
   263  		t.Assert(gfile.GetContents(gfile.Join(dst, name)), gres.GetContent(name))
   264  	})
   265  	gtest.C(t, func(t *gtest.T) {
   266  		var (
   267  			src = `template-res`
   268  			dst = gfile.Temp(gtime.TimestampNanoStr())
   269  			err = gres.Export(src, dst, gres.ExportOption{
   270  				RemovePrefix: `template-res`,
   271  			})
   272  		)
   273  		defer gfile.Remove(dst)
   274  		t.AssertNil(err)
   275  		files, err := gfile.ScanDir(dst, "*", true)
   276  		t.AssertNil(err)
   277  		t.Assert(len(files), 13)
   278  
   279  		nameInRes := `template-res/index.html`
   280  		nameInSys := `index.html`
   281  		t.Assert(gfile.GetContents(gfile.Join(dst, nameInSys)), gres.GetContent(nameInRes))
   282  	})
   283  	gtest.C(t, func(t *gtest.T) {
   284  		var (
   285  			src = `template-res/layout1/container.html`
   286  			dst = gfile.Temp(gtime.TimestampNanoStr())
   287  			err = gres.Export(src, dst, gres.ExportOption{
   288  				RemovePrefix: `template-res`,
   289  			})
   290  		)
   291  		defer gfile.Remove(dst)
   292  		t.AssertNil(err)
   293  		files, err := gfile.ScanDir(dst, "*", true)
   294  		t.AssertNil(err)
   295  		t.Assert(len(files), 2)
   296  	})
   297  }
   298  
   299  func Test_IsEmpty(t *testing.T) {
   300  	gtest.C(t, func(t *gtest.T) {
   301  		t.Assert(gres.IsEmpty(), false)
   302  	})
   303  }
   304  
   305  func TestFile_Name(t *testing.T) {
   306  	gtest.C(t, func(t *gtest.T) {
   307  		var (
   308  			src = `template-res`
   309  		)
   310  		t.Assert(gres.Get(src).Name(), src)
   311  	})
   312  }
   313  
   314  func TestFile_Export(t *testing.T) {
   315  	gres.Dump()
   316  	gtest.C(t, func(t *gtest.T) {
   317  		var (
   318  			src = `template-res`
   319  			dst = gfile.Temp(gtime.TimestampNanoStr())
   320  			err = gres.Get(src).Export(dst)
   321  		)
   322  		defer gfile.Remove(dst)
   323  		t.AssertNil(err)
   324  		files, err := gfile.ScanDir(dst, "*", true)
   325  		t.AssertNil(err)
   326  		t.Assert(len(files), 14)
   327  	})
   328  	gtest.C(t, func(t *gtest.T) {
   329  		var (
   330  			src = `template-res`
   331  			dst = gfile.Temp(gtime.TimestampNanoStr())
   332  			err = gres.Get(src).Export(dst, gres.ExportOption{
   333  				RemovePrefix: `template-res`,
   334  			})
   335  		)
   336  		defer gfile.Remove(dst)
   337  		t.AssertNil(err)
   338  		files, err := gfile.ScanDir(dst, "*", true)
   339  		t.AssertNil(err)
   340  		t.Assert(len(files), 13)
   341  	})
   342  	gtest.C(t, func(t *gtest.T) {
   343  		var (
   344  			src = `template-res/layout1/container.html`
   345  			dst = gfile.Temp(gtime.TimestampNanoStr())
   346  			err = gres.Get(src).Export(dst, gres.ExportOption{
   347  				RemovePrefix: `template-res`,
   348  			})
   349  		)
   350  		defer gfile.Remove(dst)
   351  		t.AssertNil(err)
   352  		files, err := gfile.ScanDir(dst, "*", true)
   353  		t.AssertNil(err)
   354  		t.Assert(len(files), 2)
   355  	})
   356  }