From cfdf0072c323ed90e0cf0d782e269690e966632f Mon Sep 17 00:00:00 2001 From: eddie cianci Date: Sun, 29 Dec 2013 22:31:30 -0500 Subject: [PATCH] create RecursiveTouch() and test --- .gitignore | 1 + touchp.go | 19 +++++++++++++++++++ touchp_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 touchp.go create mode 100644 touchp_test.go diff --git a/.gitignore b/.gitignore index a3b9bea..16a4110 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store /release +/test diff --git a/touchp.go b/touchp.go new file mode 100644 index 0000000..b0eac14 --- /dev/null +++ b/touchp.go @@ -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()) + } +} diff --git a/touchp_test.go b/touchp_test.go new file mode 100644 index 0000000..b4bbbf8 --- /dev/null +++ b/touchp_test.go @@ -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) + } +}