diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index e062c22d..e9e0e970 100644 --- a/fsutil/fs_test.go +++ b/fsutil/fs_test.go @@ -17,9 +17,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // -type FSSuite struct { - TempDir string -} +type FSSuite struct{} // ////////////////////////////////////////////////////////////////////////////////// // @@ -31,35 +29,33 @@ var _ = check.Suite(&FSSuite{}) // ////////////////////////////////////////////////////////////////////////////////// // -func (fs *FSSuite) SetUpSuite(c *check.C) { - fs.TempDir = c.MkDir() -} +func (s *FSSuite) TestList(c *check.C) { + tmpDir := c.MkDir() -func (fs *FSSuite) TestList(c *check.C) { - os.Mkdir(fs.TempDir+"/.dir0", 0755) + os.Mkdir(tmpDir+"/.dir0", 0755) - os.Create(fs.TempDir + "/.file0") - os.Create(fs.TempDir + "/file1.mp3") - os.Create(fs.TempDir + "/file2.jpg") + os.Create(tmpDir + "/.file0") + os.Create(tmpDir + "/file1.mp3") + os.Create(tmpDir + "/file2.jpg") - os.Mkdir(fs.TempDir+"/dir1", 0755) - os.Mkdir(fs.TempDir+"/dir2", 0755) + os.Mkdir(tmpDir+"/dir1", 0755) + os.Mkdir(tmpDir+"/dir2", 0755) - os.Create(fs.TempDir + "/dir1/file3.mp3") - os.Create(fs.TempDir + "/dir2/file4.wav") + os.Create(tmpDir + "/dir1/file3.mp3") + os.Create(tmpDir + "/dir2/file4.wav") - os.Mkdir(fs.TempDir+"/dir1/dir3", 0755) + os.Mkdir(tmpDir+"/dir1/dir3", 0755) - listing1 := List(fs.TempDir, false) - listing2 := List(fs.TempDir, true) - listing3 := ListAll(fs.TempDir, false) - listing4 := ListAll(fs.TempDir, true) - listing5 := ListAllDirs(fs.TempDir, false) - listing6 := ListAllDirs(fs.TempDir, true) - listing7 := ListAllFiles(fs.TempDir, false) - listing8 := ListAllFiles(fs.TempDir, true) - listing9 := ListAllFiles(fs.TempDir, true, &ListingFilter{MatchPatterns: []string{"*.mp3", "*.wav"}}) - listing10 := ListAllFiles(fs.TempDir, true, &ListingFilter{NotMatchPatterns: []string{"*.mp3"}}) + listing1 := List(tmpDir, false) + listing2 := List(tmpDir, true) + listing3 := ListAll(tmpDir, false) + listing4 := ListAll(tmpDir, true) + listing5 := ListAllDirs(tmpDir, false) + listing6 := ListAllDirs(tmpDir, true) + listing7 := ListAllFiles(tmpDir, false) + listing8 := ListAllFiles(tmpDir, true) + listing9 := ListAllFiles(tmpDir, true, &ListingFilter{MatchPatterns: []string{"*.mp3", "*.wav"}}) + listing10 := ListAllFiles(tmpDir, true, &ListingFilter{NotMatchPatterns: []string{"*.mp3"}}) sort.Strings(listing1) sort.Strings(listing2) @@ -133,8 +129,8 @@ func (fs *FSSuite) TestList(c *check.C) { ) } -func (fs *FSSuite) TestProperPath(c *check.C) { - tmpFile := fs.TempDir + "/test.txt" +func (s *FSSuite) TestProperPath(c *check.C) { + tmpFile := c.MkDir() + "/test.txt" os.OpenFile(tmpFile, os.O_CREATE, 0644) @@ -148,3 +144,29 @@ func (fs *FSSuite) TestProperPath(c *check.C) { os.Remove(tmpFile) } + +func (s *FSSuite) TestWalker(c *check.C) { + tmpDir := c.MkDir() + + os.Chdir(tmpDir) + + tmpDir, _ = os.Getwd() + + os.MkdirAll(tmpDir+"/dir1/dir2/dir3/dir4", 0755) + os.Chdir(tmpDir) + + c.Assert(Current(), check.Equals, tmpDir) + c.Assert(Pop(), check.Equals, tmpDir) + + dirStack = nil + + c.Assert(Push("dir1"), check.Equals, tmpDir+"/dir1") + c.Assert(Push("dir9"), check.Equals, "") + c.Assert(Push("dir2/dir3"), check.Equals, tmpDir+"/dir1/dir2/dir3") + c.Assert(Push("dir4"), check.Equals, tmpDir+"/dir1/dir2/dir3/dir4") + c.Assert(Push("dir9"), check.Equals, "") + c.Assert(Pop(), check.Equals, tmpDir+"/dir1/dir2/dir3") + c.Assert(Pop(), check.Equals, tmpDir+"/dir1") + c.Assert(Pop(), check.Equals, tmpDir) + c.Assert(Pop(), check.Equals, tmpDir) +} diff --git a/fsutil/list_windows.go b/fsutil/list_windows.go index a67d389a..5fcbc165 100644 --- a/fsutil/list_windows.go +++ b/fsutil/list_windows.go @@ -1,4 +1,4 @@ -// +build !darwin !linux +// +build windows package fsutil diff --git a/fsutil/walker.go b/fsutil/walker.go new file mode 100644 index 00000000..0b9179c5 --- /dev/null +++ b/fsutil/walker.go @@ -0,0 +1,78 @@ +// +build !windows + +package fsutil + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2009-2016 Essential Kaos // +// Essential Kaos Open Source License // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "os" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +var dirStack []string + +// ////////////////////////////////////////////////////////////////////////////////// // + +// Push change current working directory and add previous working directory to stack +func Push(dir string) string { + if dirStack == nil { + dirStack = append(dirStack, Current()) + } + + err := os.Chdir(dir) + + if err != nil { + return "" + } + + wd, _ := os.Getwd() + + dirStack = append(dirStack, wd) + + return wd +} + +// Pop change current working directory to previous in stack +func Pop() string { + if dirStack == nil { + dirStack = append(dirStack, Current()) + } + + dl := len(dirStack) + + switch dl { + + case 0, 1: + // nop + + default: + err := os.Chdir(dirStack[dl-2]) + + if err != nil { + return "" + } + + dirStack = dirStack[0 : dl-1] + } + + wd, _ := os.Getwd() + + return wd +} + +// Current return current working directory +func Current() string { + wd, err := os.Getwd() + + if err != nil { + return "" + } + + return wd +} diff --git a/fsutil/walker_windows.go b/fsutil/walker_windows.go new file mode 100644 index 00000000..87a24db1 --- /dev/null +++ b/fsutil/walker_windows.go @@ -0,0 +1,24 @@ +// +build windows + +package fsutil + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2009-2016 Essential Kaos // +// Essential Kaos Open Source License // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +func Push(dir string) string { + return "" +} + +// Pop change current working directory to previous in stack +func Pop() string { + return "" +} + +// Current return current working directory +func Current() string { + return "" +}