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.

symlink.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. . "gopkg.in/check.v1"
  6. . "gopkg.in/src-d/go-billy.v4"
  7. "gopkg.in/src-d/go-billy.v4/util"
  8. )
  9. // SymlinkSuite is a convenient test suite to validate any implementation of
  10. // billy.Symlink
  11. type SymlinkSuite struct {
  12. FS interface {
  13. Basic
  14. Symlink
  15. }
  16. }
  17. func (s *SymlinkSuite) TestSymlink(c *C) {
  18. err := util.WriteFile(s.FS, "file", nil, 0644)
  19. c.Assert(err, IsNil)
  20. err = s.FS.Symlink("file", "link")
  21. c.Assert(err, IsNil)
  22. fi, err := s.FS.Stat("link")
  23. c.Assert(err, IsNil)
  24. c.Assert(fi.Name(), Equals, "link")
  25. }
  26. func (s *SymlinkSuite) TestSymlinkCrossDirs(c *C) {
  27. err := util.WriteFile(s.FS, "foo/file", nil, 0644)
  28. c.Assert(err, IsNil)
  29. err = s.FS.Symlink("../foo/file", "bar/link")
  30. c.Assert(err, IsNil)
  31. fi, err := s.FS.Stat("bar/link")
  32. c.Assert(err, IsNil)
  33. c.Assert(fi.Name(), Equals, "link")
  34. }
  35. func (s *SymlinkSuite) TestSymlinkNested(c *C) {
  36. err := util.WriteFile(s.FS, "file", []byte("hello world!"), 0644)
  37. c.Assert(err, IsNil)
  38. err = s.FS.Symlink("file", "linkA")
  39. c.Assert(err, IsNil)
  40. err = s.FS.Symlink("linkA", "linkB")
  41. c.Assert(err, IsNil)
  42. fi, err := s.FS.Stat("linkB")
  43. c.Assert(err, IsNil)
  44. c.Assert(fi.Name(), Equals, "linkB")
  45. c.Assert(fi.Size(), Equals, int64(12))
  46. }
  47. func (s *SymlinkSuite) TestSymlinkWithNonExistentdTarget(c *C) {
  48. err := s.FS.Symlink("file", "link")
  49. c.Assert(err, IsNil)
  50. _, err = s.FS.Stat("link")
  51. c.Assert(os.IsNotExist(err), Equals, true)
  52. }
  53. func (s *SymlinkSuite) TestSymlinkWithExistingLink(c *C) {
  54. err := util.WriteFile(s.FS, "link", nil, 0644)
  55. c.Assert(err, IsNil)
  56. err = s.FS.Symlink("file", "link")
  57. c.Assert(err, Not(IsNil))
  58. }
  59. func (s *SymlinkSuite) TestOpenWithSymlinkToRelativePath(c *C) {
  60. err := util.WriteFile(s.FS, "dir/file", []byte("foo"), 0644)
  61. c.Assert(err, IsNil)
  62. err = s.FS.Symlink("file", "dir/link")
  63. c.Assert(err, IsNil)
  64. f, err := s.FS.Open("dir/link")
  65. c.Assert(err, IsNil)
  66. all, err := ioutil.ReadAll(f)
  67. c.Assert(err, IsNil)
  68. c.Assert(string(all), Equals, "foo")
  69. c.Assert(f.Close(), IsNil)
  70. }
  71. func (s *SymlinkSuite) TestOpenWithSymlinkToAbsolutePath(c *C) {
  72. err := util.WriteFile(s.FS, "dir/file", []byte("foo"), 0644)
  73. c.Assert(err, IsNil)
  74. err = s.FS.Symlink("/dir/file", "dir/link")
  75. c.Assert(err, IsNil)
  76. f, err := s.FS.Open("dir/link")
  77. c.Assert(err, IsNil)
  78. all, err := ioutil.ReadAll(f)
  79. c.Assert(err, IsNil)
  80. c.Assert(string(all), Equals, "foo")
  81. c.Assert(f.Close(), IsNil)
  82. }
  83. func (s *SymlinkSuite) TestReadlink(c *C) {
  84. err := util.WriteFile(s.FS, "file", nil, 0644)
  85. c.Assert(err, IsNil)
  86. _, err = s.FS.Readlink("file")
  87. c.Assert(err, Not(IsNil))
  88. }
  89. func (s *SymlinkSuite) TestReadlinkWithRelativePath(c *C) {
  90. err := util.WriteFile(s.FS, "dir/file", nil, 0644)
  91. c.Assert(err, IsNil)
  92. err = s.FS.Symlink("file", "dir/link")
  93. c.Assert(err, IsNil)
  94. oldname, err := s.FS.Readlink("dir/link")
  95. c.Assert(err, IsNil)
  96. c.Assert(oldname, Equals, "file")
  97. }
  98. func (s *SymlinkSuite) TestReadlinkWithAbsolutePath(c *C) {
  99. err := util.WriteFile(s.FS, "dir/file", nil, 0644)
  100. c.Assert(err, IsNil)
  101. err = s.FS.Symlink("/dir/file", "dir/link")
  102. c.Assert(err, IsNil)
  103. oldname, err := s.FS.Readlink("dir/link")
  104. c.Assert(err, IsNil)
  105. c.Assert(oldname, Equals, expectedSymlinkTarget)
  106. }
  107. func (s *SymlinkSuite) TestReadlinkWithNonExistentTarget(c *C) {
  108. err := s.FS.Symlink("file", "link")
  109. c.Assert(err, IsNil)
  110. oldname, err := s.FS.Readlink("link")
  111. c.Assert(err, IsNil)
  112. c.Assert(oldname, Equals, "file")
  113. }
  114. func (s *SymlinkSuite) TestReadlinkWithNonExistentLink(c *C) {
  115. _, err := s.FS.Readlink("link")
  116. c.Assert(os.IsNotExist(err), Equals, true)
  117. }
  118. func (s *SymlinkSuite) TestStatLink(c *C) {
  119. util.WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)
  120. s.FS.Symlink("bar", "foo/qux")
  121. fi, err := s.FS.Stat("foo/qux")
  122. c.Assert(err, IsNil)
  123. c.Assert(fi.Name(), Equals, "qux")
  124. c.Assert(fi.Size(), Equals, int64(3))
  125. c.Assert(fi.Mode(), Equals, customMode)
  126. c.Assert(fi.ModTime().IsZero(), Equals, false)
  127. c.Assert(fi.IsDir(), Equals, false)
  128. }
  129. func (s *SymlinkSuite) TestLstat(c *C) {
  130. util.WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)
  131. fi, err := s.FS.Lstat("foo/bar")
  132. c.Assert(err, IsNil)
  133. c.Assert(fi.Name(), Equals, "bar")
  134. c.Assert(fi.Size(), Equals, int64(3))
  135. c.Assert(fi.Mode()&os.ModeSymlink != 0, Equals, false)
  136. c.Assert(fi.ModTime().IsZero(), Equals, false)
  137. c.Assert(fi.IsDir(), Equals, false)
  138. }
  139. func (s *SymlinkSuite) TestLstatLink(c *C) {
  140. util.WriteFile(s.FS, "foo/bar", []byte("fosddddaaao"), customMode)
  141. s.FS.Symlink("bar", "foo/qux")
  142. fi, err := s.FS.Lstat("foo/qux")
  143. c.Assert(err, IsNil)
  144. c.Assert(fi.Name(), Equals, "qux")
  145. c.Assert(fi.Mode()&os.ModeSymlink != 0, Equals, true)
  146. c.Assert(fi.ModTime().IsZero(), Equals, false)
  147. c.Assert(fi.IsDir(), Equals, false)
  148. }
  149. func (s *SymlinkSuite) TestRenameWithSymlink(c *C) {
  150. err := s.FS.Symlink("file", "link")
  151. c.Assert(err, IsNil)
  152. err = s.FS.Rename("link", "newlink")
  153. c.Assert(err, IsNil)
  154. _, err = s.FS.Readlink("newlink")
  155. c.Assert(err, IsNil)
  156. }
  157. func (s *SymlinkSuite) TestRemoveWithSymlink(c *C) {
  158. err := util.WriteFile(s.FS, "file", []byte("foo"), 0644)
  159. c.Assert(err, IsNil)
  160. err = s.FS.Symlink("file", "link")
  161. c.Assert(err, IsNil)
  162. err = s.FS.Remove("link")
  163. c.Assert(err, IsNil)
  164. _, err = s.FS.Readlink("link")
  165. c.Assert(os.IsNotExist(err), Equals, true)
  166. _, err = s.FS.Stat("link")
  167. c.Assert(os.IsNotExist(err), Equals, true)
  168. _, err = s.FS.Stat("file")
  169. c.Assert(err, IsNil)
  170. }