github.com/gogf/gf/v2@v2.7.4/os/gfile/gfile_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 gfile_test
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/v2/os/gfile"
    16  	"github.com/gogf/gf/v2/os/gtime"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  	"github.com/gogf/gf/v2/util/gconv"
    19  )
    20  
    21  func Test_IsDir(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		paths := "/testfile"
    24  		createDir(paths)
    25  		defer delTestFiles(paths)
    26  
    27  		t.Assert(gfile.IsDir(testpath()+paths), true)
    28  		t.Assert(gfile.IsDir("./testfile2"), false)
    29  		t.Assert(gfile.IsDir("./testfile/tt.txt"), false)
    30  		t.Assert(gfile.IsDir(""), false)
    31  	})
    32  }
    33  
    34  func Test_IsEmpty(t *testing.T) {
    35  	gtest.C(t, func(t *gtest.T) {
    36  		path := "/testdir_" + gconv.String(gtime.TimestampNano())
    37  		createDir(path)
    38  		defer delTestFiles(path)
    39  
    40  		t.Assert(gfile.IsEmpty(testpath()+path), true)
    41  		t.Assert(gfile.IsEmpty(testpath()+path+gfile.Separator+"test.txt"), true)
    42  	})
    43  	gtest.C(t, func(t *gtest.T) {
    44  		path := "/testfile_" + gconv.String(gtime.TimestampNano())
    45  		createTestFile(path, "")
    46  		defer delTestFiles(path)
    47  
    48  		t.Assert(gfile.IsEmpty(testpath()+path), true)
    49  	})
    50  	gtest.C(t, func(t *gtest.T) {
    51  		path := "/testfile_" + gconv.String(gtime.TimestampNano())
    52  		createTestFile(path, "1")
    53  		defer delTestFiles(path)
    54  
    55  		t.Assert(gfile.IsEmpty(testpath()+path), false)
    56  	})
    57  }
    58  
    59  func Test_Create(t *testing.T) {
    60  	gtest.C(t, func(t *gtest.T) {
    61  		var (
    62  			err       error
    63  			filepaths []string
    64  			fileobj   *os.File
    65  		)
    66  		filepaths = append(filepaths, "/testfile_cc1.txt")
    67  		filepaths = append(filepaths, "/testfile_cc2.txt")
    68  		for _, v := range filepaths {
    69  			fileobj, err = gfile.Create(testpath() + v)
    70  			defer delTestFiles(v)
    71  			fileobj.Close()
    72  			t.AssertNil(err)
    73  		}
    74  	})
    75  
    76  	gtest.C(t, func(t *gtest.T) {
    77  		tmpPath := gfile.Join(gfile.Temp(), "test/testfile_cc1.txt")
    78  		fileobj, err := gfile.Create(tmpPath)
    79  		defer gfile.Remove(tmpPath)
    80  		t.AssertNE(fileobj, nil)
    81  		t.AssertNil(err)
    82  		fileobj.Close()
    83  	})
    84  }
    85  
    86  func Test_Open(t *testing.T) {
    87  	gtest.C(t, func(t *gtest.T) {
    88  		var (
    89  			err     error
    90  			files   []string
    91  			flags   []bool
    92  			fileobj *os.File
    93  		)
    94  
    95  		file1 := "/testfile_nc1.txt"
    96  		createTestFile(file1, "")
    97  		defer delTestFiles(file1)
    98  
    99  		files = append(files, file1)
   100  		flags = append(flags, true)
   101  
   102  		files = append(files, "./testfile/file1/c1.txt")
   103  		flags = append(flags, false)
   104  
   105  		for k, v := range files {
   106  			fileobj, err = gfile.Open(testpath() + v)
   107  			fileobj.Close()
   108  			if flags[k] {
   109  				t.AssertNil(err)
   110  			} else {
   111  				t.AssertNE(err, nil)
   112  			}
   113  
   114  		}
   115  
   116  	})
   117  }
   118  
   119  func Test_OpenFile(t *testing.T) {
   120  	gtest.C(t, func(t *gtest.T) {
   121  		var (
   122  			err     error
   123  			files   []string
   124  			flags   []bool
   125  			fileobj *os.File
   126  		)
   127  
   128  		files = append(files, "./testfile/file1/nc1.txt")
   129  		flags = append(flags, false)
   130  
   131  		f1 := "/testfile_tt.txt"
   132  		createTestFile(f1, "")
   133  		defer delTestFiles(f1)
   134  
   135  		files = append(files, f1)
   136  		flags = append(flags, true)
   137  
   138  		for k, v := range files {
   139  			fileobj, err = gfile.OpenFile(testpath()+v, os.O_RDWR, 0666)
   140  			fileobj.Close()
   141  			if flags[k] {
   142  				t.AssertNil(err)
   143  			} else {
   144  				t.AssertNE(err, nil)
   145  			}
   146  
   147  		}
   148  
   149  	})
   150  }
   151  
   152  func Test_OpenWithFlag(t *testing.T) {
   153  	gtest.C(t, func(t *gtest.T) {
   154  		var (
   155  			err     error
   156  			files   []string
   157  			flags   []bool
   158  			fileobj *os.File
   159  		)
   160  
   161  		file1 := "/testfile_t1.txt"
   162  		createTestFile(file1, "")
   163  		defer delTestFiles(file1)
   164  		files = append(files, file1)
   165  		flags = append(flags, true)
   166  
   167  		files = append(files, "/testfiless/dirfiles/t1_no.txt")
   168  		flags = append(flags, false)
   169  
   170  		for k, v := range files {
   171  			fileobj, err = gfile.OpenWithFlag(testpath()+v, os.O_RDWR)
   172  			fileobj.Close()
   173  			if flags[k] {
   174  				t.AssertNil(err)
   175  			} else {
   176  				t.AssertNE(err, nil)
   177  			}
   178  
   179  		}
   180  
   181  	})
   182  }
   183  
   184  func Test_OpenWithFlagPerm(t *testing.T) {
   185  	gtest.C(t, func(t *gtest.T) {
   186  		var (
   187  			err     error
   188  			files   []string
   189  			flags   []bool
   190  			fileobj *os.File
   191  		)
   192  		file1 := "/testfile_nc1.txt"
   193  		createTestFile(file1, "")
   194  		defer delTestFiles(file1)
   195  		files = append(files, file1)
   196  		flags = append(flags, true)
   197  
   198  		files = append(files, "/testfileyy/tt.txt")
   199  		flags = append(flags, false)
   200  
   201  		for k, v := range files {
   202  			fileobj, err = gfile.OpenWithFlagPerm(testpath()+v, os.O_RDWR, 0666)
   203  			fileobj.Close()
   204  			if flags[k] {
   205  				t.AssertNil(err)
   206  			} else {
   207  				t.AssertNE(err, nil)
   208  			}
   209  
   210  		}
   211  
   212  	})
   213  }
   214  
   215  func Test_Exists(t *testing.T) {
   216  
   217  	gtest.C(t, func(t *gtest.T) {
   218  		var (
   219  			flag  bool
   220  			files []string
   221  			flags []bool
   222  		)
   223  
   224  		file1 := "/testfile_GetContents.txt"
   225  		createTestFile(file1, "")
   226  		defer delTestFiles(file1)
   227  
   228  		files = append(files, file1)
   229  		flags = append(flags, true)
   230  
   231  		files = append(files, "./testfile/havefile1/tt_no.txt")
   232  		flags = append(flags, false)
   233  
   234  		for k, v := range files {
   235  			flag = gfile.Exists(testpath() + v)
   236  			if flags[k] {
   237  				t.Assert(flag, true)
   238  			} else {
   239  				t.Assert(flag, false)
   240  			}
   241  
   242  		}
   243  
   244  	})
   245  }
   246  
   247  func Test_Pwd(t *testing.T) {
   248  	gtest.C(t, func(t *gtest.T) {
   249  		paths, err := os.Getwd()
   250  		t.AssertNil(err)
   251  		t.Assert(gfile.Pwd(), paths)
   252  
   253  	})
   254  }
   255  
   256  func Test_IsFile(t *testing.T) {
   257  	gtest.C(t, func(t *gtest.T) {
   258  		var (
   259  			flag  bool
   260  			files []string
   261  			flags []bool
   262  		)
   263  
   264  		file1 := "/testfile_tt.txt"
   265  		createTestFile(file1, "")
   266  		defer delTestFiles(file1)
   267  		files = append(files, file1)
   268  		flags = append(flags, true)
   269  
   270  		dir1 := "/testfiless"
   271  		createDir(dir1)
   272  		defer delTestFiles(dir1)
   273  		files = append(files, dir1)
   274  		flags = append(flags, false)
   275  
   276  		files = append(files, "./testfiledd/tt1.txt")
   277  		flags = append(flags, false)
   278  
   279  		for k, v := range files {
   280  			flag = gfile.IsFile(testpath() + v)
   281  			if flags[k] {
   282  				t.Assert(flag, true)
   283  			} else {
   284  				t.Assert(flag, false)
   285  			}
   286  
   287  		}
   288  
   289  	})
   290  }
   291  
   292  func Test_Info(t *testing.T) {
   293  	gtest.C(t, func(t *gtest.T) {
   294  		var (
   295  			err    error
   296  			paths  string = "/testfile_t1.txt"
   297  			files  os.FileInfo
   298  			files2 os.FileInfo
   299  		)
   300  
   301  		createTestFile(paths, "")
   302  		defer delTestFiles(paths)
   303  		files, err = gfile.Stat(testpath() + paths)
   304  		t.AssertNil(err)
   305  
   306  		files2, err = os.Stat(testpath() + paths)
   307  		t.AssertNil(err)
   308  
   309  		t.Assert(files, files2)
   310  
   311  	})
   312  }
   313  
   314  func Test_Move(t *testing.T) {
   315  	gtest.C(t, func(t *gtest.T) {
   316  		var (
   317  			paths     string = "/ovetest"
   318  			filepaths string = "/testfile_ttn1.txt"
   319  			topath    string = "/testfile_ttn2.txt"
   320  		)
   321  		createDir("/ovetest")
   322  		createTestFile(paths+filepaths, "a")
   323  
   324  		defer delTestFiles(paths)
   325  
   326  		yfile := testpath() + paths + filepaths
   327  		tofile := testpath() + paths + topath
   328  
   329  		t.Assert(gfile.Move(yfile, tofile), nil)
   330  
   331  		// 检查移动后的文件是否真实存在
   332  		_, err := os.Stat(tofile)
   333  		t.Assert(os.IsNotExist(err), false)
   334  
   335  	})
   336  }
   337  
   338  func Test_Rename(t *testing.T) {
   339  	gtest.C(t, func(t *gtest.T) {
   340  		var (
   341  			paths  string = "/testfiles"
   342  			ypath  string = "/testfilettm1.txt"
   343  			topath string = "/testfilettm2.txt"
   344  		)
   345  		createDir(paths)
   346  		createTestFile(paths+ypath, "a")
   347  		defer delTestFiles(paths)
   348  
   349  		ypath = testpath() + paths + ypath
   350  		topath = testpath() + paths + topath
   351  
   352  		t.Assert(gfile.Rename(ypath, topath), nil)
   353  		t.Assert(gfile.IsFile(topath), true)
   354  
   355  		t.AssertNE(gfile.Rename("", ""), nil)
   356  
   357  	})
   358  
   359  }
   360  
   361  func Test_DirNames(t *testing.T) {
   362  	gtest.C(t, func(t *gtest.T) {
   363  		var (
   364  			paths    string = "/testdirs"
   365  			err      error
   366  			readlist []string
   367  		)
   368  		havelist := []string{
   369  			"t1.txt",
   370  			"t2.txt",
   371  		}
   372  
   373  		// 创建测试文件
   374  		createDir(paths)
   375  		for _, v := range havelist {
   376  			createTestFile(paths+"/"+v, "")
   377  		}
   378  		defer delTestFiles(paths)
   379  
   380  		readlist, err = gfile.DirNames(testpath() + paths)
   381  
   382  		t.AssertNil(err)
   383  		t.AssertIN(readlist, havelist)
   384  
   385  		_, err = gfile.DirNames("")
   386  		t.AssertNE(err, nil)
   387  
   388  	})
   389  }
   390  
   391  func Test_Glob(t *testing.T) {
   392  	gtest.C(t, func(t *gtest.T) {
   393  		var (
   394  			paths      string = "/testfiles/*.txt"
   395  			dirpath    string = "/testfiles"
   396  			err        error
   397  			resultlist []string
   398  		)
   399  
   400  		havelist1 := []string{
   401  			"t1.txt",
   402  			"t2.txt",
   403  		}
   404  
   405  		havelist2 := []string{
   406  			testpath() + "/testfiles/t1.txt",
   407  			testpath() + "/testfiles/t2.txt",
   408  		}
   409  
   410  		// ===============================构建测试文件
   411  		createDir(dirpath)
   412  		for _, v := range havelist1 {
   413  			createTestFile(dirpath+"/"+v, "")
   414  		}
   415  		defer delTestFiles(dirpath)
   416  
   417  		resultlist, err = gfile.Glob(testpath()+paths, true)
   418  		t.AssertNil(err)
   419  		t.Assert(resultlist, havelist1)
   420  
   421  		resultlist, err = gfile.Glob(testpath()+paths, false)
   422  
   423  		t.AssertNil(err)
   424  		t.Assert(formatpaths(resultlist), formatpaths(havelist2))
   425  
   426  		_, err = gfile.Glob("", true)
   427  		t.AssertNil(err)
   428  
   429  	})
   430  }
   431  
   432  func Test_Remove(t *testing.T) {
   433  	gtest.C(t, func(t *gtest.T) {
   434  		var (
   435  			paths string = "/testfile_t1.txt"
   436  		)
   437  		createTestFile(paths, "")
   438  		t.Assert(gfile.Remove(testpath()+paths), nil)
   439  
   440  		t.Assert(gfile.Remove(""), nil)
   441  
   442  		defer delTestFiles(paths)
   443  
   444  	})
   445  }
   446  
   447  func Test_IsReadable(t *testing.T) {
   448  	gtest.C(t, func(t *gtest.T) {
   449  		var (
   450  			paths1 string = "/testfile_GetContents.txt"
   451  			paths2 string = "./testfile_GetContents_no.txt"
   452  		)
   453  
   454  		createTestFile(paths1, "")
   455  		defer delTestFiles(paths1)
   456  
   457  		t.Assert(gfile.IsReadable(testpath()+paths1), true)
   458  		t.Assert(gfile.IsReadable(paths2), false)
   459  
   460  	})
   461  }
   462  
   463  func Test_IsWritable(t *testing.T) {
   464  	gtest.C(t, func(t *gtest.T) {
   465  		var (
   466  			paths1 string = "/testfile_GetContents.txt"
   467  			paths2 string = "./testfile_GetContents_no.txt"
   468  		)
   469  
   470  		createTestFile(paths1, "")
   471  		defer delTestFiles(paths1)
   472  		t.Assert(gfile.IsWritable(testpath()+paths1), true)
   473  		t.Assert(gfile.IsWritable(paths2), false)
   474  
   475  	})
   476  }
   477  
   478  func Test_Chmod(t *testing.T) {
   479  	gtest.C(t, func(t *gtest.T) {
   480  		var (
   481  			paths1 string = "/testfile_GetContents.txt"
   482  			paths2 string = "./testfile_GetContents_no.txt"
   483  		)
   484  		createTestFile(paths1, "")
   485  		defer delTestFiles(paths1)
   486  
   487  		t.Assert(gfile.Chmod(testpath()+paths1, 0777), nil)
   488  		t.AssertNE(gfile.Chmod(paths2, 0777), nil)
   489  
   490  	})
   491  }
   492  
   493  // 获取绝对目录地址
   494  func Test_RealPath(t *testing.T) {
   495  	gtest.C(t, func(t *gtest.T) {
   496  		var (
   497  			paths1    string = "/testfile_files"
   498  			readlPath string
   499  
   500  			tempstr string
   501  		)
   502  
   503  		createDir(paths1)
   504  		defer delTestFiles(paths1)
   505  
   506  		readlPath = gfile.RealPath("./")
   507  
   508  		tempstr, _ = filepath.Abs("./")
   509  
   510  		t.Assert(readlPath, tempstr)
   511  
   512  		t.Assert(gfile.RealPath("./nodirs"), "")
   513  
   514  	})
   515  }
   516  
   517  // 获取当前执行文件的目录
   518  func Test_SelfPath(t *testing.T) {
   519  	gtest.C(t, func(t *gtest.T) {
   520  		var (
   521  			paths1    string
   522  			readlPath string
   523  			tempstr   string
   524  		)
   525  		readlPath = gfile.SelfPath()
   526  		readlPath = filepath.ToSlash(readlPath)
   527  
   528  		tempstr, _ = filepath.Abs(os.Args[0])
   529  		paths1 = filepath.ToSlash(tempstr)
   530  		paths1 = strings.Replace(paths1, "./", "/", 1)
   531  
   532  		t.Assert(readlPath, paths1)
   533  
   534  	})
   535  }
   536  
   537  func Test_SelfDir(t *testing.T) {
   538  	gtest.C(t, func(t *gtest.T) {
   539  		var (
   540  			paths1    string
   541  			readlPath string
   542  			tempstr   string
   543  		)
   544  		readlPath = gfile.SelfDir()
   545  
   546  		tempstr, _ = filepath.Abs(os.Args[0])
   547  		paths1 = filepath.Dir(tempstr)
   548  
   549  		t.Assert(readlPath, paths1)
   550  
   551  	})
   552  }
   553  
   554  func Test_Basename(t *testing.T) {
   555  	gtest.C(t, func(t *gtest.T) {
   556  		var (
   557  			paths1    string = "/testfilerr_GetContents.txt"
   558  			readlPath string
   559  		)
   560  
   561  		createTestFile(paths1, "")
   562  		defer delTestFiles(paths1)
   563  
   564  		readlPath = gfile.Basename(testpath() + paths1)
   565  		t.Assert(readlPath, "testfilerr_GetContents.txt")
   566  
   567  	})
   568  }
   569  
   570  func Test_Dir(t *testing.T) {
   571  	gtest.C(t, func(t *gtest.T) {
   572  		var (
   573  			paths1    string = "/testfiless"
   574  			readlPath string
   575  		)
   576  		createDir(paths1)
   577  		defer delTestFiles(paths1)
   578  
   579  		readlPath = gfile.Dir(testpath() + paths1)
   580  
   581  		t.Assert(readlPath, testpath())
   582  
   583  		t.Assert(len(gfile.Dir(".")) > 0, true)
   584  	})
   585  }
   586  
   587  func Test_Ext(t *testing.T) {
   588  	gtest.C(t, func(t *gtest.T) {
   589  		var (
   590  			paths1   string = "/testfile_GetContents.txt"
   591  			dirpath1        = "/testdirs"
   592  		)
   593  		createTestFile(paths1, "")
   594  		defer delTestFiles(paths1)
   595  
   596  		createDir(dirpath1)
   597  		defer delTestFiles(dirpath1)
   598  
   599  		t.Assert(gfile.Ext(testpath()+paths1), ".txt")
   600  		t.Assert(gfile.Ext(testpath()+dirpath1), "")
   601  	})
   602  
   603  	gtest.C(t, func(t *gtest.T) {
   604  		t.Assert(gfile.Ext("/var/www/test.js"), ".js")
   605  		t.Assert(gfile.Ext("/var/www/test.min.js"), ".js")
   606  		t.Assert(gfile.Ext("/var/www/test.js?1"), ".js")
   607  		t.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js")
   608  	})
   609  }
   610  
   611  func Test_ExtName(t *testing.T) {
   612  	gtest.C(t, func(t *gtest.T) {
   613  		t.Assert(gfile.ExtName("/var/www/test.js"), "js")
   614  		t.Assert(gfile.ExtName("/var/www/test.min.js"), "js")
   615  		t.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js")
   616  		t.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js")
   617  	})
   618  }
   619  
   620  func Test_TempDir(t *testing.T) {
   621  	gtest.C(t, func(t *gtest.T) {
   622  		t.Assert(gfile.Temp(), os.TempDir())
   623  	})
   624  }
   625  
   626  func Test_Mkdir(t *testing.T) {
   627  	gtest.C(t, func(t *gtest.T) {
   628  		var (
   629  			tpath string = "/testfile/createdir"
   630  			err   error
   631  		)
   632  
   633  		defer delTestFiles("/testfile")
   634  
   635  		err = gfile.Mkdir(testpath() + tpath)
   636  		t.AssertNil(err)
   637  
   638  		err = gfile.Mkdir("")
   639  		t.AssertNE(err, nil)
   640  
   641  		err = gfile.Mkdir(testpath() + tpath + "2/t1")
   642  		t.AssertNil(err)
   643  
   644  	})
   645  }
   646  
   647  func Test_Stat(t *testing.T) {
   648  	gtest.C(t, func(t *gtest.T) {
   649  		var (
   650  			tpath1   = "/testfile_t1.txt"
   651  			tpath2   = "./testfile_t1_no.txt"
   652  			err      error
   653  			fileiofo os.FileInfo
   654  		)
   655  
   656  		createTestFile(tpath1, "a")
   657  		defer delTestFiles(tpath1)
   658  
   659  		fileiofo, err = gfile.Stat(testpath() + tpath1)
   660  		t.AssertNil(err)
   661  
   662  		t.Assert(fileiofo.Size(), 1)
   663  
   664  		_, err = gfile.Stat(tpath2)
   665  		t.AssertNE(err, nil)
   666  
   667  	})
   668  }
   669  
   670  func Test_MainPkgPath(t *testing.T) {
   671  	gtest.C(t, func(t *gtest.T) {
   672  		reads := gfile.MainPkgPath()
   673  		t.Assert(reads, "")
   674  	})
   675  }
   676  
   677  func Test_SelfName(t *testing.T) {
   678  	gtest.C(t, func(t *gtest.T) {
   679  		t.Assert(len(gfile.SelfName()) > 0, true)
   680  	})
   681  }
   682  
   683  func Test_MTimestamp(t *testing.T) {
   684  	gtest.C(t, func(t *gtest.T) {
   685  		t.Assert(gfile.MTimestamp(gfile.Temp()) > 0, true)
   686  	})
   687  }