github.com/sandwich-go/boost@v1.3.29/xos/file_test.go (about)

     1  package xos
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestFile(t *testing.T) {
    11  	Convey("file", t, func() {
    12  		var file = "file.go"
    13  		var dirFullPath0 = filepath.Join(os.TempDir(), "a")
    14  		So(Mkdir(dirFullPath0), ShouldBeNil)
    15  		So(FileCopyToDir(file, dirFullPath0), ShouldBeNil)
    16  		So(ExistsFile(filepath.Join(dirFullPath0, file)), ShouldBeTrue)
    17  		So(Ext(file), ShouldEqual, ".go")
    18  		content0, err0 := FileGetContents(filepath.Join(dirFullPath0, file))
    19  		So(err0, ShouldBeNil)
    20  		content1, err1 := FileGetContents(file)
    21  		So(err1, ShouldBeNil)
    22  		So(content0, ShouldResemble, content1)
    23  		var bFile = filepath.Join(dirFullPath0, "b")
    24  		MustFilePutContents(bFile, []byte("a"))
    25  		So(FilePutContents(bFile, []byte("b")), ShouldBeNil)
    26  		content1, err1 = FileGetContents(bFile)
    27  		So(err1, ShouldBeNil)
    28  		So(content1, ShouldResemble, []byte("b"))
    29  		writer, cancel := MustGetFileWriter(bFile, true)
    30  		_, err1 = writer.Write([]byte("a"))
    31  		So(err1, ShouldBeNil)
    32  		cancel()
    33  		content1, err1 = FileGetContents(bFile)
    34  		So(err1, ShouldBeNil)
    35  		So(content1, ShouldResemble, []byte("ab"))
    36  		So(os.RemoveAll(dirFullPath0), ShouldBeNil)
    37  	})
    38  }