You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
753 B

  1. package os
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. )
  9. func TestCopyFile(t *testing.T) {
  10. tmpfile, err := ioutil.TempFile("", "example")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. defer os.Remove(tmpfile.Name())
  15. content := []byte("hello world")
  16. if _, err := tmpfile.Write(content); err != nil {
  17. t.Fatal(err)
  18. }
  19. copyfile := fmt.Sprintf("%s.copy", tmpfile.Name())
  20. if err := CopyFile(tmpfile.Name(), copyfile); err != nil {
  21. t.Fatal(err)
  22. }
  23. if _, err := os.Stat(copyfile); os.IsNotExist(err) {
  24. t.Fatal("copy should exist")
  25. }
  26. data, err := ioutil.ReadFile(copyfile)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if !bytes.Equal(data, content) {
  31. t.Fatalf("copy file content differs: expected %v, got %v", content, data)
  32. }
  33. os.Remove(copyfile)
  34. }