pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

copy_test.go (1105B)


      1 package main
      2 
      3 import (
      4 	"io/ioutil"
      5 	"os"
      6 	"path/filepath"
      7 	"testing"
      8 )
      9 
     10 func TestCopyFile(test *testing.T) {
     11 	test.Parallel()
     12 
     13 	tdir := filepath.Join(os.TempDir(), "pagr_test", "TestCopyFile")
     14 	if err := os.MkdirAll(tdir, 0775); err != nil {
     15 		test.Errorf("failed to create temporary test dir: %s", tdir)
     16 	}
     17 	src := filepath.Join(tdir, "/src")
     18 	srcData := []byte("data")
     19 	dst := filepath.Join(tdir, "/dst")
     20 
     21 	if err := ioutil.WriteFile(src, srcData, 0666); err != nil {
     22 		test.Error("setup failed, could not write", tdir+"/src")
     23 	}
     24 
     25 	if err := CopyFile(src, dst); err != nil {
     26 		test.Fatal("CopyFile failed", err)
     27 	}
     28 	if _, err := os.Stat(dst); err != nil {
     29 		test.Fatalf("could not stat '%s'", dst)
     30 	}
     31 
     32 	if buf, err := ioutil.ReadFile(dst); err != nil {
     33 		test.Errorf("could not read '%s'", dst)
     34 	} else if len(buf) < len(srcData) {
     35 		test.Fatalf("not all srcData (%s) copied to '%s' (%s)", srcData, dst, buf)
     36 	} else if string(buf) != string(srcData) {
     37 		test.Fatalf("copied srcData (%s) does not match source (%s)", buf, srcData)
     38 	}
     39 
     40 	if err := os.RemoveAll(tdir); err != nil {
     41 		test.Error(err)
     42 	}
     43 }