github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/build/testing_test.go (about) 1 package build 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "reflect" 9 "sort" 10 "testing" 11 12 "gitlab.com/NebulousLabs/fastrand" 13 ) 14 15 // TestCopyDir checks that CopyDir copies directories as expected. 16 func TestCopyDir(t *testing.T) { 17 // Create some nested folders to copy. 18 os.MkdirAll(TempDir("build"), 0700) 19 root := TempDir("build", t.Name()) 20 os.MkdirAll(root, 0700) 21 22 data := make([][]byte, 2) 23 for i := range data { 24 data[i] = fastrand.Bytes(4e3) 25 } 26 27 // Create a file and a directory. 28 err := ioutil.WriteFile(filepath.Join(root, "f1"), data[0], 0700) 29 if err != nil { 30 t.Fatal(err) 31 } 32 err = os.MkdirAll(filepath.Join(root, "d1"), 0700) 33 if err != nil { 34 t.Fatal(err) 35 } 36 err = ioutil.WriteFile(filepath.Join(root, "d1", "d1f1"), data[1], 0700) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 // Copy the root directory. 42 rootCopy := root + "-copied" 43 err = CopyDir(root, rootCopy) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 // Verify that the two files, and dir with two files are all correctly 49 // copied. 50 f1, err := ioutil.ReadFile(filepath.Join(rootCopy, "f1")) 51 if err != nil { 52 t.Fatal(err) 53 } 54 if !bytes.Equal(f1, data[0]) { 55 t.Error("f1 did not match") 56 } 57 d1f1, err := ioutil.ReadFile(filepath.Join(rootCopy, "d1", "d1f1")) 58 if err != nil { 59 t.Fatal(err) 60 } 61 if !bytes.Equal(d1f1, data[1]) { 62 t.Error("f1 did not match") 63 } 64 } 65 66 // TestExtractTarGz tests that ExtractTarGz can extract a valid .tar.gz file. 67 func TestExtractTarGz(t *testing.T) { 68 dir := TempDir("build", t.Name()) 69 os.MkdirAll(dir, 0700) 70 if err := ExtractTarGz("testdata/test.tar.gz", dir); err != nil { 71 t.Fatal(err) 72 } 73 folder, err := os.Open(dir) 74 if err != nil { 75 t.Fatal(err) 76 } 77 files, err := folder.Readdirnames(-1) 78 if err != nil { 79 t.Fatal(err) 80 } 81 sort.Strings(files) 82 exp := []string{"1", "2", "3"} 83 if !reflect.DeepEqual(files, exp) { 84 t.Fatal("filenames do not match:", files, exp) 85 } 86 }