package os import ( "bytes" "fmt" "io/ioutil" "os" "testing" ) func TestCopyFile(t *testing.T) { tmpfile, err := ioutil.TempFile("", "example") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) content := []byte("hello world") if _, err := tmpfile.Write(content); err != nil { t.Fatal(err) } copyfile := fmt.Sprintf("%s.copy", tmpfile.Name()) if err := CopyFile(tmpfile.Name(), copyfile); err != nil { t.Fatal(err) } if _, err := os.Stat(copyfile); os.IsNotExist(err) { t.Fatal("copy should exist") } data, err := ioutil.ReadFile(copyfile) if err != nil { t.Fatal(err) } if !bytes.Equal(data, content) { t.Fatalf("copy file content differs: expected %v, got %v", content, data) } os.Remove(copyfile) }