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.

dir.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package test
  2. import (
  3. "os"
  4. "strconv"
  5. . "gopkg.in/check.v1"
  6. . "gopkg.in/src-d/go-billy.v4"
  7. "gopkg.in/src-d/go-billy.v4/util"
  8. )
  9. // DirSuite is a convenient test suite to validate any implementation of
  10. // billy.Dir
  11. type DirSuite struct {
  12. FS interface {
  13. Basic
  14. Dir
  15. }
  16. }
  17. func (s *DirSuite) TestMkdirAll(c *C) {
  18. err := s.FS.MkdirAll("empty", os.FileMode(0755))
  19. c.Assert(err, IsNil)
  20. fi, err := s.FS.Stat("empty")
  21. c.Assert(err, IsNil)
  22. c.Assert(fi.IsDir(), Equals, true)
  23. }
  24. func (s *DirSuite) TestMkdirAllNested(c *C) {
  25. err := s.FS.MkdirAll("foo/bar/baz", os.FileMode(0755))
  26. c.Assert(err, IsNil)
  27. fi, err := s.FS.Stat("foo/bar/baz")
  28. c.Assert(err, IsNil)
  29. c.Assert(fi.IsDir(), Equals, true)
  30. fi, err = s.FS.Stat("foo/bar")
  31. c.Assert(err, IsNil)
  32. c.Assert(fi.IsDir(), Equals, true)
  33. fi, err = s.FS.Stat("foo")
  34. c.Assert(err, IsNil)
  35. c.Assert(fi.IsDir(), Equals, true)
  36. }
  37. func (s *DirSuite) TestMkdirAllIdempotent(c *C) {
  38. err := s.FS.MkdirAll("empty", 0755)
  39. c.Assert(err, IsNil)
  40. fi, err := s.FS.Stat("empty")
  41. c.Assert(err, IsNil)
  42. c.Assert(fi.IsDir(), Equals, true)
  43. // idempotent
  44. err = s.FS.MkdirAll("empty", 0755)
  45. c.Assert(err, IsNil)
  46. fi, err = s.FS.Stat("empty")
  47. c.Assert(err, IsNil)
  48. c.Assert(fi.IsDir(), Equals, true)
  49. }
  50. func (s *DirSuite) TestMkdirAllAndCreate(c *C) {
  51. err := s.FS.MkdirAll("dir", os.FileMode(0755))
  52. c.Assert(err, IsNil)
  53. f, err := s.FS.Create("dir/bar/foo")
  54. c.Assert(err, IsNil)
  55. c.Assert(f.Close(), IsNil)
  56. fi, err := s.FS.Stat("dir/bar/foo")
  57. c.Assert(err, IsNil)
  58. c.Assert(fi.IsDir(), Equals, false)
  59. }
  60. func (s *DirSuite) TestMkdirAllWithExistingFile(c *C) {
  61. f, err := s.FS.Create("dir/foo")
  62. c.Assert(err, IsNil)
  63. c.Assert(f.Close(), IsNil)
  64. err = s.FS.MkdirAll("dir/foo", os.FileMode(0755))
  65. c.Assert(err, NotNil)
  66. fi, err := s.FS.Stat("dir/foo")
  67. c.Assert(err, IsNil)
  68. c.Assert(fi.IsDir(), Equals, false)
  69. }
  70. func (s *DirSuite) TestStatDir(c *C) {
  71. s.FS.MkdirAll("foo/bar", 0644)
  72. fi, err := s.FS.Stat("foo/bar")
  73. c.Assert(err, IsNil)
  74. c.Assert(fi.Name(), Equals, "bar")
  75. c.Assert(fi.Mode().IsDir(), Equals, true)
  76. c.Assert(fi.ModTime().IsZero(), Equals, false)
  77. c.Assert(fi.IsDir(), Equals, true)
  78. }
  79. func (s *BasicSuite) TestStatDeep(c *C) {
  80. files := []string{"foo", "bar", "qux/baz", "qux/qux"}
  81. for _, name := range files {
  82. err := util.WriteFile(s.FS, name, nil, 0644)
  83. c.Assert(err, IsNil)
  84. }
  85. // Some implementations detect directories based on a prefix
  86. // for all files; it's easy to miss path separator handling there.
  87. fi, err := s.FS.Stat("qu")
  88. c.Assert(os.IsNotExist(err), Equals, true, Commentf("error: %s", err))
  89. c.Assert(fi, IsNil)
  90. fi, err = s.FS.Stat("qux")
  91. c.Assert(err, IsNil)
  92. c.Assert(fi.Name(), Equals, "qux")
  93. c.Assert(fi.IsDir(), Equals, true)
  94. fi, err = s.FS.Stat("qux/baz")
  95. c.Assert(err, IsNil)
  96. c.Assert(fi.Name(), Equals, "baz")
  97. c.Assert(fi.IsDir(), Equals, false)
  98. }
  99. func (s *DirSuite) TestReadDir(c *C) {
  100. files := []string{"foo", "bar", "qux/baz", "qux/qux"}
  101. for _, name := range files {
  102. err := util.WriteFile(s.FS, name, nil, 0644)
  103. c.Assert(err, IsNil)
  104. }
  105. info, err := s.FS.ReadDir("/")
  106. c.Assert(err, IsNil)
  107. c.Assert(info, HasLen, 3)
  108. info, err = s.FS.ReadDir("/qux")
  109. c.Assert(err, IsNil)
  110. c.Assert(info, HasLen, 2)
  111. }
  112. func (s *DirSuite) TestReadDirNested(c *C) {
  113. max := 100
  114. path := "/"
  115. for i := 0; i <= max; i++ {
  116. path = s.FS.Join(path, strconv.Itoa(i))
  117. }
  118. files := []string{s.FS.Join(path, "f1"), s.FS.Join(path, "f2")}
  119. for _, name := range files {
  120. err := util.WriteFile(s.FS, name, nil, 0644)
  121. c.Assert(err, IsNil)
  122. }
  123. path = "/"
  124. for i := 0; i < max; i++ {
  125. path = s.FS.Join(path, strconv.Itoa(i))
  126. info, err := s.FS.ReadDir(path)
  127. c.Assert(err, IsNil)
  128. c.Assert(info, HasLen, 1)
  129. }
  130. path = s.FS.Join(path, strconv.Itoa(max))
  131. info, err := s.FS.ReadDir(path)
  132. c.Assert(err, IsNil)
  133. c.Assert(info, HasLen, 2)
  134. }
  135. func (s *DirSuite) TestReadDirWithMkDirAll(c *C) {
  136. err := s.FS.MkdirAll("qux", 0644)
  137. c.Assert(err, IsNil)
  138. files := []string{"qux/baz", "qux/qux"}
  139. for _, name := range files {
  140. err := util.WriteFile(s.FS, name, nil, 0644)
  141. c.Assert(err, IsNil)
  142. }
  143. info, err := s.FS.ReadDir("/")
  144. c.Assert(err, IsNil)
  145. c.Assert(info, HasLen, 1)
  146. c.Assert(info[0].IsDir(), Equals, true)
  147. info, err = s.FS.ReadDir("/qux")
  148. c.Assert(err, IsNil)
  149. c.Assert(info, HasLen, 2)
  150. }
  151. func (s *DirSuite) TestReadDirFileInfo(c *C) {
  152. err := util.WriteFile(s.FS, "foo", []byte{'F', 'O', 'O'}, 0644)
  153. c.Assert(err, IsNil)
  154. info, err := s.FS.ReadDir("/")
  155. c.Assert(err, IsNil)
  156. c.Assert(info, HasLen, 1)
  157. c.Assert(info[0].Size(), Equals, int64(3))
  158. c.Assert(info[0].IsDir(), Equals, false)
  159. c.Assert(info[0].Name(), Equals, "foo")
  160. }
  161. func (s *DirSuite) TestReadDirFileInfoDirs(c *C) {
  162. files := []string{"qux/baz/foo"}
  163. for _, name := range files {
  164. err := util.WriteFile(s.FS, name, []byte{'F', 'O', 'O'}, 0644)
  165. c.Assert(err, IsNil)
  166. }
  167. info, err := s.FS.ReadDir("qux")
  168. c.Assert(err, IsNil)
  169. c.Assert(info, HasLen, 1)
  170. c.Assert(info[0].IsDir(), Equals, true)
  171. c.Assert(info[0].Name(), Equals, "baz")
  172. info, err = s.FS.ReadDir("qux/baz")
  173. c.Assert(err, IsNil)
  174. c.Assert(info, HasLen, 1)
  175. c.Assert(info[0].Size(), Equals, int64(3))
  176. c.Assert(info[0].IsDir(), Equals, false)
  177. c.Assert(info[0].Name(), Equals, "foo")
  178. c.Assert(info[0].Mode(), Not(Equals), 0)
  179. }
  180. func (s *DirSuite) TestRenameToDir(c *C) {
  181. err := util.WriteFile(s.FS, "foo", nil, 0644)
  182. c.Assert(err, IsNil)
  183. err = s.FS.Rename("foo", "bar/qux")
  184. c.Assert(err, IsNil)
  185. old, err := s.FS.Stat("foo")
  186. c.Assert(old, IsNil)
  187. c.Assert(os.IsNotExist(err), Equals, true)
  188. dir, err := s.FS.Stat("bar")
  189. c.Assert(dir, NotNil)
  190. c.Assert(err, IsNil)
  191. file, err := s.FS.Stat("bar/qux")
  192. c.Assert(file.Name(), Equals, "qux")
  193. c.Assert(err, IsNil)
  194. }
  195. func (s *DirSuite) TestRenameDir(c *C) {
  196. err := s.FS.MkdirAll("foo", 0644)
  197. c.Assert(err, IsNil)
  198. err = util.WriteFile(s.FS, "foo/bar", nil, 0644)
  199. c.Assert(err, IsNil)
  200. err = s.FS.Rename("foo", "bar")
  201. c.Assert(err, IsNil)
  202. dirfoo, err := s.FS.Stat("foo")
  203. c.Assert(dirfoo, IsNil)
  204. c.Assert(os.IsNotExist(err), Equals, true)
  205. dirbar, err := s.FS.Stat("bar")
  206. c.Assert(err, IsNil)
  207. c.Assert(dirbar, NotNil)
  208. foo, err := s.FS.Stat("foo/bar")
  209. c.Assert(os.IsNotExist(err), Equals, true)
  210. c.Assert(foo, IsNil)
  211. bar, err := s.FS.Stat("bar/bar")
  212. c.Assert(err, IsNil)
  213. c.Assert(bar, NotNil)
  214. }