github.com/hanwen/go-fuse@v1.0.0/fuse/pathfs/copy_test.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pathfs
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/hanwen/go-fuse/internal/testutil"
    13  )
    14  
    15  func TestCopyFile(t *testing.T) {
    16  	d1 := testutil.TempDir()
    17  	defer os.RemoveAll(d1)
    18  	d2 := testutil.TempDir()
    19  	defer os.RemoveAll(d2)
    20  
    21  	fs1 := NewLoopbackFileSystem(d1)
    22  	fs2 := NewLoopbackFileSystem(d2)
    23  
    24  	content1 := "blabla"
    25  
    26  	err := ioutil.WriteFile(d1+"/file", []byte(content1), 0644)
    27  	if err != nil {
    28  		t.Fatalf("WriteFile failed: %v", err)
    29  	}
    30  
    31  	code := CopyFile(fs1, fs2, "file", "file", nil)
    32  	if !code.Ok() {
    33  		t.Fatal("Unexpected ret code", code)
    34  	}
    35  
    36  	data, err := ioutil.ReadFile(d2 + "/file")
    37  	if content1 != string(data) {
    38  		t.Fatal("Unexpected content", string(data))
    39  	}
    40  
    41  	content2 := "foobar"
    42  
    43  	err = ioutil.WriteFile(d2+"/file", []byte(content2), 0644)
    44  	if err != nil {
    45  		t.Fatalf("WriteFile failed: %v", err)
    46  	}
    47  
    48  	// Copy back: should overwrite.
    49  	code = CopyFile(fs2, fs1, "file", "file", nil)
    50  	if !code.Ok() {
    51  		t.Fatal("Unexpected ret code", code)
    52  	}
    53  
    54  	data, err = ioutil.ReadFile(d1 + "/file")
    55  	if content2 != string(data) {
    56  		t.Fatal("Unexpected content", string(data))
    57  	}
    58  
    59  }