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

     1  package gfile_test
     2  
     3  import (
     4  	"github.com/zhongdalu/gf/g/os/gfile"
     5  	"github.com/zhongdalu/gf/g/test/gtest"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  // 创建测试文件
    14  func createTestFile(filename, content string) error {
    15  	TempDir := testpath()
    16  	err := ioutil.WriteFile(TempDir+filename, []byte(content), 0666)
    17  	return err
    18  }
    19  
    20  // 测试完删除文件或目录
    21  func delTestFiles(filenames string) {
    22  	os.RemoveAll(testpath() + filenames)
    23  }
    24  
    25  // 创建目录
    26  func createDir(paths string) {
    27  	TempDir := testpath()
    28  	os.Mkdir(TempDir+paths, 0777)
    29  }
    30  
    31  // 统一格式化文件目录为"/"
    32  func formatpaths(paths []string) []string {
    33  	for k, v := range paths {
    34  		paths[k] = filepath.ToSlash(v)
    35  		paths[k] = strings.Replace(paths[k], "./", "/", 1)
    36  	}
    37  
    38  	return paths
    39  }
    40  
    41  // 统一格式化文件目录为"/"
    42  func formatpath(paths string) string {
    43  	paths = filepath.ToSlash(paths)
    44  	paths = strings.Replace(paths, "./", "/", 1)
    45  	return paths
    46  }
    47  
    48  // 指定返回要测试的目录
    49  func testpath() string {
    50  	return os.TempDir()
    51  }
    52  
    53  func TestGetContents(t *testing.T) {
    54  	gtest.Case(t, func() {
    55  
    56  		var (
    57  			filepaths string = "/testfile_t1.txt"
    58  		)
    59  		createTestFile(filepaths, "my name is jroam")
    60  		defer delTestFiles(filepaths)
    61  
    62  		gtest.Assert(gfile.GetContents(testpath()+filepaths), "my name is jroam")
    63  		gtest.Assert(gfile.GetContents(""), "")
    64  
    65  	})
    66  }
    67  
    68  func TestGetBinContents(t *testing.T) {
    69  	gtest.Case(t, func() {
    70  		var (
    71  			filepaths1  string = "/testfile_t1.txt"                 // 文件存在时
    72  			filepaths2  string = testpath() + "/testfile_t1_no.txt" // 文件不存在时
    73  			readcontent []byte
    74  			str1        string = "my name is jroam"
    75  		)
    76  		createTestFile(filepaths1, str1)
    77  		defer delTestFiles(filepaths1)
    78  		readcontent = gfile.GetBinContents(testpath() + filepaths1)
    79  		gtest.Assert(readcontent, []byte(str1))
    80  
    81  		readcontent = gfile.GetBinContents(filepaths2)
    82  		gtest.Assert(string(readcontent), "")
    83  
    84  		gtest.Assert(string(gfile.GetBinContents(filepaths2)), "")
    85  
    86  	})
    87  }
    88  
    89  // 截断文件为指定的大小
    90  func TestTruncate(t *testing.T) {
    91  	gtest.Case(t, func() {
    92  		var (
    93  			filepaths1 string = "/testfile_GetContentsyyui.txt" //文件存在时
    94  			err        error
    95  			files      *os.File
    96  		)
    97  		createTestFile(filepaths1, "abcdefghijkmln")
    98  		defer delTestFiles(filepaths1)
    99  		err = gfile.Truncate(testpath()+filepaths1, 10)
   100  		gtest.Assert(err, nil)
   101  
   102  		//=========================检查修改文后的大小,是否与期望一致
   103  		files, err = os.Open(testpath() + filepaths1)
   104  		defer files.Close()
   105  		gtest.Assert(err, nil)
   106  		fileinfo, err2 := files.Stat()
   107  		gtest.Assert(err2, nil)
   108  		gtest.Assert(fileinfo.Size(), 10)
   109  
   110  		//====测试当为空时,是否报错
   111  		err = gfile.Truncate("", 10)
   112  		gtest.AssertNE(err, nil)
   113  
   114  	})
   115  }
   116  
   117  func TestPutContents(t *testing.T) {
   118  	gtest.Case(t, func() {
   119  		var (
   120  			filepaths   string = "/testfile_PutContents.txt"
   121  			err         error
   122  			readcontent []byte
   123  		)
   124  		createTestFile(filepaths, "a")
   125  		defer delTestFiles(filepaths)
   126  
   127  		err = gfile.PutContents(testpath()+filepaths, "test!")
   128  		gtest.Assert(err, nil)
   129  
   130  		//==================判断是否真正写入
   131  		readcontent, err = ioutil.ReadFile(testpath() + filepaths)
   132  		gtest.Assert(err, nil)
   133  		gtest.Assert(string(readcontent), "test!")
   134  
   135  		err = gfile.PutContents("", "test!")
   136  		gtest.AssertNE(err, nil)
   137  
   138  	})
   139  }
   140  
   141  func TestPutContentsAppend(t *testing.T) {
   142  	gtest.Case(t, func() {
   143  		var (
   144  			filepaths   string = "/testfile_PutContents.txt"
   145  			err         error
   146  			readcontent []byte
   147  		)
   148  
   149  		createTestFile(filepaths, "a")
   150  		defer delTestFiles(filepaths)
   151  		err = gfile.PutContentsAppend(testpath()+filepaths, "hello")
   152  		gtest.Assert(err, nil)
   153  
   154  		//==================判断是否真正写入
   155  		readcontent, err = ioutil.ReadFile(testpath() + filepaths)
   156  		gtest.Assert(err, nil)
   157  		gtest.Assert(string(readcontent), "ahello")
   158  
   159  		err = gfile.PutContentsAppend("", "hello")
   160  		gtest.AssertNE(err, nil)
   161  
   162  	})
   163  
   164  }
   165  
   166  func TestPutBinContents(t *testing.T) {
   167  	gtest.Case(t, func() {
   168  		var (
   169  			filepaths   string = "/testfile_PutContents.txt"
   170  			err         error
   171  			readcontent []byte
   172  		)
   173  		createTestFile(filepaths, "a")
   174  		defer delTestFiles(filepaths)
   175  
   176  		err = gfile.PutBinContents(testpath()+filepaths, []byte("test!!"))
   177  		gtest.Assert(err, nil)
   178  
   179  		// 判断是否真正写入
   180  		readcontent, err = ioutil.ReadFile(testpath() + filepaths)
   181  		gtest.Assert(err, nil)
   182  		gtest.Assert(string(readcontent), "test!!")
   183  
   184  		err = gfile.PutBinContents("", []byte("test!!"))
   185  		gtest.AssertNE(err, nil)
   186  
   187  	})
   188  }
   189  
   190  func TestPutBinContentsAppend(t *testing.T) {
   191  	gtest.Case(t, func() {
   192  		var (
   193  			filepaths   string = "/testfile_PutContents.txt" //原文件内容: yy
   194  			err         error
   195  			readcontent []byte
   196  		)
   197  		createTestFile(filepaths, "test!!")
   198  		defer delTestFiles(filepaths)
   199  		err = gfile.PutBinContentsAppend(testpath()+filepaths, []byte("word"))
   200  		gtest.Assert(err, nil)
   201  
   202  		// 判断是否真正写入
   203  		readcontent, err = ioutil.ReadFile(testpath() + filepaths)
   204  		gtest.Assert(err, nil)
   205  		gtest.Assert(string(readcontent), "test!!word")
   206  
   207  		err = gfile.PutBinContentsAppend("", []byte("word"))
   208  		gtest.AssertNE(err, nil)
   209  
   210  	})
   211  }
   212  
   213  func TestGetBinContentsByTwoOffsetsByPath(t *testing.T) {
   214  	gtest.Case(t, func() {
   215  		var (
   216  			filepaths   string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
   217  			readcontent []byte
   218  		)
   219  
   220  		createTestFile(filepaths, "abcdefghijk")
   221  		defer delTestFiles(filepaths)
   222  		readcontent = gfile.GetBinContentsByTwoOffsetsByPath(testpath()+filepaths, 2, 5)
   223  
   224  		gtest.Assert(string(readcontent), "cde")
   225  
   226  		readcontent = gfile.GetBinContentsByTwoOffsetsByPath("", 2, 5)
   227  		gtest.Assert(len(readcontent), 0)
   228  
   229  	})
   230  
   231  }
   232  
   233  func TestGetNextCharOffsetByPath(t *testing.T) {
   234  	gtest.Case(t, func() {
   235  		var (
   236  			filepaths  string = "/testfile_GetContents.txt" // 文件内容: abcdefghijk
   237  			localindex int64
   238  		)
   239  		createTestFile(filepaths, "abcdefghijk")
   240  		defer delTestFiles(filepaths)
   241  		localindex = gfile.GetNextCharOffsetByPath(testpath()+filepaths, 'd', 1)
   242  		gtest.Assert(localindex, 3)
   243  
   244  		localindex = gfile.GetNextCharOffsetByPath("", 'd', 1)
   245  		gtest.Assert(localindex, -1)
   246  
   247  	})
   248  }
   249  
   250  func TestGetNextCharOffset(t *testing.T) {
   251  	gtest.Case(t, func() {
   252  		var (
   253  			localindex int64
   254  		)
   255  		reader := strings.NewReader("helloword")
   256  
   257  		localindex = gfile.GetNextCharOffset(reader, 'w', 1)
   258  		gtest.Assert(localindex, 5)
   259  
   260  		localindex = gfile.GetNextCharOffset(reader, 'j', 1)
   261  		gtest.Assert(localindex, -1)
   262  
   263  	})
   264  }
   265  
   266  func TestGetBinContentsByTwoOffsets(t *testing.T) {
   267  	gtest.Case(t, func() {
   268  		var (
   269  			reads []byte
   270  		)
   271  		reader := strings.NewReader("helloword")
   272  
   273  		reads = gfile.GetBinContentsByTwoOffsets(reader, 1, 3)
   274  		gtest.Assert(string(reads), "el")
   275  
   276  		reads = gfile.GetBinContentsByTwoOffsets(reader, 10, 30)
   277  		gtest.Assert(string(reads), "")
   278  
   279  	})
   280  }
   281  
   282  func TestGetBinContentsTilChar(t *testing.T) {
   283  	gtest.Case(t, func() {
   284  		var (
   285  			reads  []byte
   286  			indexs int64
   287  		)
   288  		reader := strings.NewReader("helloword")
   289  
   290  		reads, _ = gfile.GetBinContentsTilChar(reader, 'w', 2)
   291  		gtest.Assert(string(reads), "llow")
   292  
   293  		_, indexs = gfile.GetBinContentsTilChar(reader, 'w', 20)
   294  		gtest.Assert(indexs, -1)
   295  
   296  	})
   297  }
   298  
   299  func TestGetBinContentsTilCharByPath(t *testing.T) {
   300  	gtest.Case(t, func() {
   301  		var (
   302  			reads     []byte
   303  			indexs    int64
   304  			filepaths string = "/testfile_GetContents.txt"
   305  		)
   306  
   307  		createTestFile(filepaths, "abcdefghijklmn")
   308  		defer delTestFiles(filepaths)
   309  
   310  		reads, _ = gfile.GetBinContentsTilCharByPath(testpath()+filepaths, 'c', 2)
   311  		gtest.Assert(string(reads), "c")
   312  
   313  		reads, _ = gfile.GetBinContentsTilCharByPath(testpath()+filepaths, 'y', 1)
   314  		gtest.Assert(string(reads), "")
   315  
   316  		_, indexs = gfile.GetBinContentsTilCharByPath(testpath()+filepaths, 'x', 1)
   317  		gtest.Assert(indexs, -1)
   318  
   319  	})
   320  }
   321  
   322  func TestHome(t *testing.T) {
   323  	gtest.Case(t, func() {
   324  		var (
   325  			reads string
   326  			err   error
   327  		)
   328  
   329  		reads, err = gfile.Home()
   330  		gtest.Assert(err, nil)
   331  		gtest.AssertNE(reads, "")
   332  
   333  	})
   334  }