Skip to content

Commit

Permalink
create RecursiveTouch() and test
Browse files Browse the repository at this point in the history
  • Loading branch information
defeated committed Dec 30, 2013
1 parent bbf86a4 commit cfdf007
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
/release
/test
19 changes: 19 additions & 0 deletions touchp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"os"
"path/filepath"
"time"
)

func RecursiveTouch(name string) {
dirs := filepath.Dir(name)
os.MkdirAll(dirs, 0755)

_, err := os.Stat(name)
if os.IsNotExist(err) {
os.Create(name)
} else {
os.Chtimes(name, time.Now(), time.Now())
}
}
27 changes: 27 additions & 0 deletions touchp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

// TODO: mock the filesystem instead? http://nf.wh3rd.net/10things/#8

import (
"os"
"testing"
)

const (
dir = "test"
)

func init() {
os.RemoveAll(dir)
os.Mkdir(dir, 0755)
}

func TestRecursiveTouchNew(t *testing.T) {
name := dir + "/new/file.ext"
RecursiveTouch(name)

_, err := os.Stat(name)
if err != nil {
t.Errorf("no such file or directory: '%v'", name)
}
}

0 comments on commit cfdf007

Please sign in to comment.