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