No Description
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.

fs_test.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package fs
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. )
  8. func TestExists(t *testing.T) {
  9. tmp, err := ioutil.TempDir("", "fs-test-")
  10. defer func() {
  11. os.RemoveAll(tmp)
  12. }()
  13. if err != nil {
  14. t.Fatalf("couldn't create temp dir: %v", err)
  15. }
  16. filename := filepath.Join(tmp, "foo")
  17. if Exists(filename) {
  18. t.Errorf("shouldn't have been able to find non-existant file: %s", filename)
  19. }
  20. f, err := os.Create(filename)
  21. if err != nil {
  22. t.Errorf("problem opening fresh file (%q): %v", filename, err)
  23. }
  24. f.Close()
  25. if !Exists(filename) {
  26. t.Errorf("failure to find existant file: %s", filename)
  27. }
  28. dirname := filepath.Join(tmp, "somedir")
  29. if err := os.Mkdir(dirname, 0777); err != nil {
  30. t.Errorf("problem creating directory: %v", err)
  31. }
  32. if !Exists(dirname) {
  33. t.Errorf("failure to find existant directory: %s", dirname)
  34. }
  35. }
  36. func TestIsDir(t *testing.T) {
  37. tmp, err := ioutil.TempDir("", "fs-test-")
  38. defer func() {
  39. os.RemoveAll(tmp)
  40. }()
  41. if err != nil {
  42. t.Fatalf("couldn't create temp dir: %v", err)
  43. }
  44. dirname := filepath.Join(tmp, "somedir")
  45. if err := os.Mkdir(dirname, 0777); err != nil {
  46. t.Errorf("problem creating directory: %v", err)
  47. }
  48. if isdir := IsDir(filepath.Join(tmp, "notexist")); isdir {
  49. t.Errorf("failed to correctly interpret non-existant file: got %t, want %t", isdir, false)
  50. }
  51. if !Exists(dirname) {
  52. t.Errorf("failure to find existant directory: %s", dirname)
  53. }
  54. if !IsDir(dirname) {
  55. t.Errorf("failure to detect directory: %s", dirname)
  56. }
  57. filename := filepath.Join(tmp, "foo")
  58. f, err := os.Create(filename)
  59. if err != nil {
  60. t.Errorf("problem opening fresh file (%q): %v", filename, err)
  61. }
  62. f.Close()
  63. if isdir := IsDir(filename); isdir {
  64. t.Errorf("failure to correctly interpret file as directory; got %t, want %t", isdir, false)
  65. }
  66. }