Skip to content

Commit e7da41f

Browse files
author
kuba--
committed
Add TempDir
Signed-off-by: kuba-- <[email protected]>
1 parent b4d81ed commit e7da41f

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
/coverage.txt
2+
/vendor
3+
Gopkg.lock
4+
Gopkg.toml
5+
go.sum

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module gopkg.in/src-d/go-billy.v4
2+
3+
require (
4+
github.com/kr/pretty v0.1.0 // indirect
5+
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9
6+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
7+
)

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
2+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6+
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
7+
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
9+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

test/fs.go

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func (s *FilesystemSuite) TestRemoveAllRelative(c *C) {
188188
}
189189

190190
func (s *FilesystemSuite) TestReadDir(c *C) {
191+
err := s.FS.MkdirAll("qux", 0644)
192+
c.Assert(err, IsNil)
193+
191194
files := []string{"foo", "bar", "qux/baz", "qux/qux"}
192195
for _, name := range files {
193196
err := util.WriteFile(s.FS, name, nil, 0644)

util/util.go

+39
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ func TempFile(fs billy.Basic, dir, prefix string) (f billy.File, err error) {
168168
return
169169
}
170170

171+
// TempDir creates a new temporary directory in the directory dir
172+
// with a name beginning with prefix and returns the path of the
173+
// new directory. If dir is the empty string, TempDir uses the
174+
// default directory for temporary files (see os.TempDir).
175+
// Multiple programs calling TempDir simultaneously
176+
// will not choose the same directory. It is the caller's responsibility
177+
// to remove the directory when no longer needed.
178+
func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
179+
// This implementation is based on stdlib ioutil.TempDir
180+
181+
if dir == "" {
182+
dir = os.TempDir()
183+
}
184+
185+
nconflict := 0
186+
for i := 0; i < 10000; i++ {
187+
try := filepath.Join(dir, prefix+nextSuffix())
188+
err = fs.MkdirAll(try, 0700)
189+
if os.IsExist(err) {
190+
if nconflict++; nconflict > 10 {
191+
randmu.Lock()
192+
rand = reseed()
193+
randmu.Unlock()
194+
}
195+
continue
196+
}
197+
if os.IsNotExist(err) {
198+
if _, err := os.Stat(dir); os.IsNotExist(err) {
199+
return "", err
200+
}
201+
}
202+
if err == nil {
203+
name = try
204+
}
205+
break
206+
}
207+
return
208+
}
209+
171210
type underlying interface {
172211
Underlying() billy.Basic
173212
}

util/util_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package util_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"regexp"
7+
"testing"
8+
9+
"gopkg.in/src-d/go-billy.v4/memfs"
10+
"gopkg.in/src-d/go-billy.v4/util"
11+
)
12+
13+
func TestTempFile(t *testing.T) {
14+
fs := memfs.New()
15+
16+
dir, err := util.TempDir(fs, "", "util_test")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer util.RemoveAll(fs, dir)
21+
22+
f, err := util.TempFile(fs, dir, "foo")
23+
if f == nil || err != nil {
24+
t.Errorf("TempFile(%q, `foo`) = %v, %v", dir, f, err)
25+
}
26+
}
27+
28+
func TestTempDir(t *testing.T) {
29+
fs := memfs.New()
30+
31+
dir := os.TempDir()
32+
name, err := util.TempDir(fs, dir, "util_test")
33+
if name == "" || err != nil {
34+
t.Errorf("TempDir(dir, `util_test`) = %v, %v", name, err)
35+
}
36+
if name != "" {
37+
util.RemoveAll(fs, name)
38+
re := regexp.MustCompile("^" + regexp.QuoteMeta(filepath.Join(dir, "util_test")) + "[0-9]+$")
39+
if !re.MatchString(name) {
40+
t.Errorf("TempDir(`"+dir+"`, `util_test`) created bad name %s", name)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)