Skip to content

Commit

Permalink
Merge pull request #21 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.5.0
  • Loading branch information
andyone committed Feb 1, 2016
2 parents ab417d9 + 822e79d commit 4ccdf09
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

#### v1.5.0

* `[tmp]` Improved error handling
* `[tmp]` Changed name pattern of temporary files and directories

#### v1.4.5

* `[pid]` Fixed bug with pid file creation
Expand Down
26 changes: 21 additions & 5 deletions tmp/tmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package tmp
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"os"
"path"

Expand All @@ -33,12 +34,26 @@ var Dir = "/tmp"
// ////////////////////////////////////////////////////////////////////////////////// //

// NewTemp create new Temp structure
func NewTemp(args ...string) *Temp {
if len(args) == 0 {
return &Temp{Dir: Dir}
func NewTemp(args ...string) (*Temp, error) {
tempDir := path.Clean(Dir)

if len(args) != 0 {
tempDir = path.Clean(args[0])
}

if !fsutil.IsExist(tempDir) {
return nil, fmt.Errorf("Directory %s is not exist", tempDir)
}

if !fsutil.IsDir(tempDir) {
return nil, fmt.Errorf("%s is not a directory", tempDir)
}

if !fsutil.IsWritable(tempDir) {
return nil, fmt.Errorf("Directory %s is not writable", tempDir)
}

return &Temp{Dir: path.Clean(args[0])}
return &Temp{Dir: tempDir}, nil
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -110,12 +125,13 @@ func (t *Temp) Clean() {

// ////////////////////////////////////////////////////////////////////////////////// //

// getTempName return name of temporary file
func getTempName(dir, name string) string {
var result string

for {
if name != "" {
result = path.Join(dir, "_"+name+"_"+rand.String(12))
result = path.Join(dir, "_"+rand.String(12)+"_"+name)
} else {
result = path.Join(dir, "_tmp_"+rand.String(12))
}
Expand Down
36 changes: 27 additions & 9 deletions tmp/tmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,34 @@ func (ts *TmpSuite) SetUpSuite(c *C) {
}

func (ts *TmpSuite) TestMk(c *C) {
t := NewTemp()
t, err := NewTemp()

c.Assert(err, IsNil)
c.Assert(t, NotNil)
c.Assert(t.Dir, Equals, "/tmp")

t.Clean()
}

func (ts *TmpSuite) TestErrors(c *C) {
t := NewTemp("/")
t, err := NewTemp("/")

c.Assert(t, NotNil)
c.Assert(t.Dir, Equals, "/")
c.Assert(err, NotNil)
c.Assert(t, IsNil)

t, err = NewTemp("/tmpz")

c.Assert(err, NotNil)
c.Assert(t, IsNil)

os.Create(ts.TempDir + "/test_")

t, err = NewTemp(ts.TempDir + "/test_")

c.Assert(err, NotNil)
c.Assert(t, IsNil)

t = &Temp{Dir: "/"}

tmpDir, err := t.MkDir("test")

Expand All @@ -64,8 +79,9 @@ func (ts *TmpSuite) TestErrors(c *C) {
}

func (ts *TmpSuite) TestMkDir(c *C) {
t := NewTemp(ts.TempDir)
t, err := NewTemp(ts.TempDir)

c.Assert(err, IsNil)
c.Assert(t, NotNil)
c.Assert(t.Dir, Equals, ts.TempDir)

Expand All @@ -85,8 +101,9 @@ func (ts *TmpSuite) TestMkDir(c *C) {
}

func (ts *TmpSuite) TestMkFile(c *C) {
t := NewTemp(ts.TempDir)
t, err := NewTemp(ts.TempDir)

c.Assert(err, IsNil)
c.Assert(t, NotNil)
c.Assert(t.Dir, Equals, ts.TempDir)

Expand All @@ -107,15 +124,16 @@ func (ts *TmpSuite) TestMkFile(c *C) {
}

func (ts *TmpSuite) TestMkName(c *C) {
t := NewTemp(ts.TempDir)
t, err := NewTemp(ts.TempDir)

c.Assert(err, IsNil)
c.Assert(t, NotNil)
c.Assert(t.Dir, Equals, ts.TempDir)

c.Assert(t.MkName(), Not(Equals), "")
c.Assert(t.MkName("1234"), Not(Equals), "")

ln := len(ts.TempDir + "/_1234")
ln := len(ts.TempDir + "/")

c.Assert(t.MkName("1234")[:ln], Equals, ts.TempDir+"/_1234")
c.Assert(t.MkName("1234.json")[ln+14:], Equals, "1234.json")
}

0 comments on commit 4ccdf09

Please sign in to comment.