github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/libs/os/os_test.go (about)

     1  package os
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func TestCopyFile(t *testing.T) {
    12  	tmpfile, err := ioutil.TempFile("", "example")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	defer os.Remove(tmpfile.Name())
    17  	content := []byte("hello world")
    18  	if _, err := tmpfile.Write(content); err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	copyfile := fmt.Sprintf("%s.copy", tmpfile.Name())
    23  	if err := CopyFile(tmpfile.Name(), copyfile); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if _, err := os.Stat(copyfile); os.IsNotExist(err) {
    27  		t.Fatal("copy should exist")
    28  	}
    29  	data, err := ioutil.ReadFile(copyfile)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if !bytes.Equal(data, content) {
    34  		t.Fatalf("copy file content differs: expected %v, got %v", content, data)
    35  	}
    36  	os.Remove(copyfile)
    37  }