Skip to content

Commit cfdf007

Browse files
committed
create RecursiveTouch() and test
1 parent bbf86a4 commit cfdf007

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
/release
3+
/test

touchp.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"time"
7+
)
8+
9+
func RecursiveTouch(name string) {
10+
dirs := filepath.Dir(name)
11+
os.MkdirAll(dirs, 0755)
12+
13+
_, err := os.Stat(name)
14+
if os.IsNotExist(err) {
15+
os.Create(name)
16+
} else {
17+
os.Chtimes(name, time.Now(), time.Now())
18+
}
19+
}

touchp_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
// TODO: mock the filesystem instead? http://nf.wh3rd.net/10things/#8
4+
5+
import (
6+
"os"
7+
"testing"
8+
)
9+
10+
const (
11+
dir = "test"
12+
)
13+
14+
func init() {
15+
os.RemoveAll(dir)
16+
os.Mkdir(dir, 0755)
17+
}
18+
19+
func TestRecursiveTouchNew(t *testing.T) {
20+
name := dir + "/new/file.ext"
21+
RecursiveTouch(name)
22+
23+
_, err := os.Stat(name)
24+
if err != nil {
25+
t.Errorf("no such file or directory: '%v'", name)
26+
}
27+
}

0 commit comments

Comments
 (0)