Skip to content

Commit e501c17

Browse files
authored
Merge pull request #36 from mcuadros/temporal
helper: new temporal helper
2 parents d48e2a7 + a7f984e commit e501c17

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

helper/temporal/temporal.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package temporal
2+
3+
import (
4+
"gopkg.in/src-d/go-billy.v3"
5+
"gopkg.in/src-d/go-billy.v3/util"
6+
)
7+
8+
// Temporal is a helper that implements billy.TempFile over any filesystem.
9+
type Temporal struct {
10+
billy.Filesystem
11+
defaultDir string
12+
}
13+
14+
// New creates a new filesystem wrapping up 'fs' the intercepts the calls to
15+
// the TempFile method. The param defaultDir is used as default directory were
16+
// the tempfiles are created.
17+
func New(fs billy.Filesystem, defaultDir string) billy.Filesystem {
18+
return &Temporal{
19+
Filesystem: fs,
20+
defaultDir: defaultDir,
21+
}
22+
}
23+
24+
func (h *Temporal) TempFile(dir, prefix string) (billy.File, error) {
25+
if dir == "" {
26+
dir = h.defaultDir
27+
}
28+
29+
return util.TempFile(h.Filesystem, dir, prefix)
30+
}

helper/temporal/temporal_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package temporal
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"gopkg.in/src-d/go-billy.v3/memfs"
8+
"gopkg.in/src-d/go-billy.v3/test"
9+
10+
. "gopkg.in/check.v1"
11+
)
12+
13+
func Test(t *testing.T) { TestingT(t) }
14+
15+
var _ = Suite(&TemporalSuite{})
16+
17+
type TemporalSuite struct {
18+
test.TempFileSuite
19+
}
20+
21+
func (s *TemporalSuite) SetUpTest(c *C) {
22+
s.FS = New(memfs.New(), "foo")
23+
}
24+
25+
func (s *TemporalSuite) TestTempFileDefaultPath(c *C) {
26+
fs := New(memfs.New(), "foo")
27+
f, err := fs.TempFile("", "bar")
28+
c.Assert(err, IsNil)
29+
c.Assert(f.Close(), IsNil)
30+
31+
c.Assert(strings.HasPrefix(f.Name(), fs.Join("foo", "bar")), Equals, true)
32+
}

test/tempfile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func (s *TempFileSuite) TestTempFile(c *C) {
2020
f, err := s.FS.TempFile("", "bar")
2121
c.Assert(err, IsNil)
2222
c.Assert(f.Close(), IsNil)
23-
c.Assert(strings.HasPrefix(f.Name(), "bar"), Equals, true)
23+
24+
c.Assert(strings.Index(f.Name(), "bar"), Not(Equals), -1)
2425
}
2526

2627
func (s *TempFileSuite) TestTempFileWithPath(c *C) {
@@ -36,7 +37,7 @@ func (s *TempFileSuite) TestTempFileFullWithPath(c *C) {
3637
c.Assert(err, IsNil)
3738
c.Assert(f.Close(), IsNil)
3839

39-
c.Assert(strings.HasPrefix(f.Name(), s.FS.Join("foo", "bar")), Equals, true)
40+
c.Assert(strings.Index(f.Name(), s.FS.Join("foo", "bar")), Not(Equals), -1)
4041
}
4142

4243
func (s *TempFileSuite) TestRemoveTempFile(c *C) {

0 commit comments

Comments
 (0)