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