walker_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package ftp
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) {
  10. mock, err := newFtpMock(t, "127.0.0.1")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. defer mock.Close()
  15. c, cErr := Connect(mock.Addr())
  16. if cErr != nil {
  17. t.Fatal(err)
  18. }
  19. w := c.Walk("root")
  20. assert.Equal(t, "root/", w.root)
  21. assert.Equal(t, &c, &w.serverConn)
  22. }
  23. func TestFieldsReturnCorrectData(t *testing.T) {
  24. w := Walker{
  25. cur: item{
  26. path: "/root/",
  27. err: fmt.Errorf("This is an error"),
  28. entry: Entry{
  29. Name: "root",
  30. Size: 123,
  31. Time: time.Now(),
  32. Type: EntryTypeFolder,
  33. },
  34. },
  35. }
  36. assert.Equal(t, "This is an error", w.Err().Error())
  37. assert.Equal(t, "/root/", w.Path())
  38. assert.Equal(t, EntryTypeFolder, w.Stat().Type)
  39. }
  40. func TestSkipDirIsCorrectlySet(t *testing.T) {
  41. w := Walker{}
  42. w.SkipDir()
  43. assert.Equal(t, false, w.descend)
  44. }
  45. func TestNoDescendDoesNotAddToStack(t *testing.T) {
  46. w := new(Walker)
  47. w.cur = item{
  48. path: "/root/",
  49. err: nil,
  50. entry: Entry{
  51. Name: "root",
  52. Size: 123,
  53. Time: time.Now(),
  54. Type: EntryTypeFolder,
  55. },
  56. }
  57. w.stack = []item{
  58. item{
  59. path: "file",
  60. err: nil,
  61. entry: Entry{
  62. Name: "file",
  63. Size: 123,
  64. Time: time.Now(),
  65. Type: EntryTypeFile,
  66. },
  67. },
  68. }
  69. w.SkipDir()
  70. result := w.Next()
  71. assert.Equal(t, true, result, "Result should return true")
  72. assert.Equal(t, 0, len(w.stack))
  73. assert.Equal(t, true, w.descend)
  74. }
  75. func TestEmptyStackReturnsFalse(t *testing.T) {
  76. w := new(Walker)
  77. w.cur = item{
  78. path: "/root/",
  79. err: nil,
  80. entry: Entry{
  81. Name: "root",
  82. Size: 123,
  83. Time: time.Now(),
  84. Type: EntryTypeFolder,
  85. },
  86. }
  87. w.stack = []item{}
  88. w.SkipDir()
  89. result := w.Next()
  90. assert.Equal(t, false, result, "Result should return false")
  91. }
  92. func TestCurAndStackSetCorrectly(t *testing.T) {
  93. w := new(Walker)
  94. w.cur = item{
  95. path: "root/file1",
  96. err: nil,
  97. entry: Entry{
  98. Name: "file1",
  99. Size: 123,
  100. Time: time.Now(),
  101. Type: EntryTypeFile,
  102. },
  103. }
  104. w.stack = []item{
  105. item{
  106. path: "file",
  107. err: nil,
  108. entry: Entry{
  109. Name: "file",
  110. Size: 123,
  111. Time: time.Now(),
  112. Type: EntryTypeFile,
  113. },
  114. },
  115. item{
  116. path: "root/file1",
  117. err: nil,
  118. entry: Entry{
  119. Name: "file1",
  120. Size: 123,
  121. Time: time.Now(),
  122. Type: EntryTypeFile,
  123. },
  124. },
  125. }
  126. result := w.Next()
  127. result = w.Next()
  128. assert.Equal(t, true, result, "Result should return true")
  129. assert.Equal(t, 0, len(w.stack))
  130. assert.Equal(t, "file", w.cur.entry.Name)
  131. }
  132. func TestStackIsPopulatedCorrectly(t *testing.T) {
  133. mock, err := newFtpMock(t, "127.0.0.1")
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. defer mock.Close()
  138. c, cErr := Connect(mock.Addr())
  139. if cErr != nil {
  140. t.Fatal(err)
  141. }
  142. w := Walker{
  143. cur: item{
  144. path: "/root",
  145. entry: Entry{
  146. Name: "root",
  147. Size: 123,
  148. Time: time.Now(),
  149. Type: EntryTypeFolder,
  150. },
  151. },
  152. serverConn: c,
  153. }
  154. w.descend = true
  155. w.Next()
  156. assert.Equal(t, 0, len(w.stack))
  157. assert.Equal(t, "lo", w.cur.entry.Name)
  158. assert.Equal(t, true, strings.HasSuffix(w.cur.path, "/"))
  159. }