github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/build/testing_test.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  // TestCopyDir checks that CopyDir copies directories as expected.
    13  func TestCopyDir(t *testing.T) {
    14  	// Create some nested folders to copy.
    15  	os.MkdirAll(TempDir("build"), 0700)
    16  	root := TempDir("build", "TestCopyDir")
    17  	os.MkdirAll(root, 0700)
    18  
    19  	data := make([][]byte, 2)
    20  	for i := range data {
    21  		data[i] = make([]byte, 4e3)
    22  		_, err := rand.Read(data[i])
    23  		if err != nil {
    24  			t.Fatal(err)
    25  		}
    26  	}
    27  
    28  	// Create a file and a directory.
    29  	err := ioutil.WriteFile(filepath.Join(root, "f1"), data[0], 0700)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	err = os.MkdirAll(filepath.Join(root, "d1"), 0700)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	err = ioutil.WriteFile(filepath.Join(root, "d1", "d1f1"), data[1], 0700)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	// Copy the root directory.
    43  	rootCopy := root + "-copied"
    44  	err = CopyDir(root, rootCopy)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	// Verify that the two files, and dir with two files are all correctly
    50  	// copied.
    51  	f1, err := ioutil.ReadFile(filepath.Join(rootCopy, "f1"))
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if !bytes.Equal(f1, data[0]) {
    56  		t.Error("f1 did not match")
    57  	}
    58  	d1f1, err := ioutil.ReadFile(filepath.Join(rootCopy, "d1", "d1f1"))
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if !bytes.Equal(d1f1, data[1]) {
    63  		t.Error("f1 did not match")
    64  	}
    65  }