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.

tempfile.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package test
  2. import (
  3. "strings"
  4. . "gopkg.in/check.v1"
  5. "gopkg.in/src-d/go-billy.v4"
  6. "gopkg.in/src-d/go-billy.v4/util"
  7. )
  8. // TempFileSuite is a convenient test suite to validate any implementation of
  9. // billy.TempFile
  10. type TempFileSuite struct {
  11. FS interface {
  12. billy.Basic
  13. billy.TempFile
  14. }
  15. }
  16. func (s *TempFileSuite) TestTempFile(c *C) {
  17. f, err := s.FS.TempFile("", "bar")
  18. c.Assert(err, IsNil)
  19. c.Assert(f.Close(), IsNil)
  20. c.Assert(strings.Index(f.Name(), "bar"), Not(Equals), -1)
  21. }
  22. func (s *TempFileSuite) TestTempFileWithPath(c *C) {
  23. f, err := s.FS.TempFile("foo", "bar")
  24. c.Assert(err, IsNil)
  25. c.Assert(f.Close(), IsNil)
  26. c.Assert(strings.HasPrefix(f.Name(), s.FS.Join("foo", "bar")), Equals, true)
  27. }
  28. func (s *TempFileSuite) TestTempFileFullWithPath(c *C) {
  29. f, err := s.FS.TempFile("/foo", "bar")
  30. c.Assert(err, IsNil)
  31. c.Assert(f.Close(), IsNil)
  32. c.Assert(strings.Index(f.Name(), s.FS.Join("foo", "bar")), Not(Equals), -1)
  33. }
  34. func (s *TempFileSuite) TestRemoveTempFile(c *C) {
  35. f, err := s.FS.TempFile("test-dir", "test-prefix")
  36. c.Assert(err, IsNil)
  37. fn := f.Name()
  38. c.Assert(f.Close(), IsNil)
  39. c.Assert(s.FS.Remove(fn), IsNil)
  40. }
  41. func (s *TempFileSuite) TestRenameTempFile(c *C) {
  42. f, err := s.FS.TempFile("test-dir", "test-prefix")
  43. c.Assert(err, IsNil)
  44. fn := f.Name()
  45. c.Assert(f.Close(), IsNil)
  46. c.Assert(s.FS.Rename(fn, "other-path"), IsNil)
  47. }
  48. func (s *TempFileSuite) TestTempFileMany(c *C) {
  49. for i := 0; i < 1024; i++ {
  50. var fs []billy.File
  51. for j := 0; j < 100; j++ {
  52. f, err := s.FS.TempFile("test-dir", "test-prefix")
  53. c.Assert(err, IsNil)
  54. fs = append(fs, f)
  55. }
  56. for _, f := range fs {
  57. c.Assert(f.Close(), IsNil)
  58. c.Assert(s.FS.Remove(f.Name()), IsNil)
  59. }
  60. }
  61. }
  62. func (s *TempFileSuite) TestTempFileManyWithUtil(c *C) {
  63. for i := 0; i < 1024; i++ {
  64. var fs []billy.File
  65. for j := 0; j < 100; j++ {
  66. f, err := util.TempFile(s.FS, "test-dir", "test-prefix")
  67. c.Assert(err, IsNil)
  68. fs = append(fs, f)
  69. }
  70. for _, f := range fs {
  71. c.Assert(f.Close(), IsNil)
  72. c.Assert(s.FS.Remove(f.Name()), IsNil)
  73. }
  74. }
  75. }