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.

пре 9 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package fs
  2. import (
  3. "bufio"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func setup(handler func(f string)) {
  10. filename := "/tmp/tmpfile"
  11. if file, err := os.Create(filename); err == nil {
  12. defer file.Close()
  13. defer os.Remove(filename)
  14. buffer := bufio.NewWriter(file)
  15. buffer.WriteString("I'm just a temp. file")
  16. buffer.Flush()
  17. handler(filename)
  18. }
  19. }
  20. func TestAbs(t *testing.T) {
  21. Convey("Absolute path check", t, func() {
  22. So(Abs("/tmp"), ShouldEqual, "/tmp")
  23. })
  24. }
  25. func TestCopy(t *testing.T) {
  26. Convey("Copy files/directories recursively", t, func() {
  27. filename := "GoAnywhereFake"
  28. exec.Command("touch", Abs("~/"+filename)).Run()
  29. defer os.Remove("/tmp/" + filename)
  30. err := Copy("~/"+filename, "/tmp")
  31. So(Exists("/tmp/"+filename), ShouldBeTrue)
  32. So(err, ShouldBeNil)
  33. exec.Command("mkdir", Abs("~/GoAnywhere")).Run()
  34. exec.Command("touch", Abs("~/GoAnywhere/Fake")).Run()
  35. defer os.RemoveAll("~/GoAnywhere")
  36. err = Copy("~/GoAnywhere", "/tmp")
  37. So(Exists("/tmp/GoAnywhere"), ShouldBeTrue)
  38. So(err, ShouldBeNil)
  39. })
  40. }
  41. func TestExists(t *testing.T) {
  42. Convey("Checks if the given path exists", t, func() {
  43. exists := Exists("/tmp")
  44. So(exists, ShouldBeTrue)
  45. exists = Exists("/NotExists")
  46. So(exists, ShouldBeFalse)
  47. })
  48. }
  49. func TestIsDir(t *testing.T) {
  50. setup(func(filename string) {
  51. flag := IsDir(filename)
  52. Convey("Checks if the given path is a directory", t, func() {
  53. So(flag, ShouldBeFalse)
  54. })
  55. })
  56. flag := IsDir("/tmp")
  57. Convey("Checks if the given path is a directory", t, func() {
  58. So(flag, ShouldBeTrue)
  59. })
  60. }
  61. func TestIsFile(t *testing.T) {
  62. setup(func(filename string) {
  63. flag := IsFile(filename)
  64. Convey("Checks if the given path is a file", t, func() {
  65. So(flag, ShouldBeTrue)
  66. })
  67. })
  68. flag := IsFile("/tmp")
  69. Convey("Checks if the given path is a file", t, func() {
  70. So(flag, ShouldBeFalse)
  71. })
  72. }