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

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