From 9da29a307027b49bae792ffcfd60d529caa3491e Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 13 Apr 2016 11:43:10 -0400 Subject: [PATCH 1/4] Added fs walker (bash pushd/popd analog) --- fsutil/fs_test.go | 82 +++++++++++++++++++++++++++--------------- fsutil/list_windows.go | 2 +- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index e062c22d..054be6fe 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,33 @@ func (fs *FSSuite) TestProperPath(c *check.C) { os.Remove(tmpFile) } + +func (s *FSSuite) TestWalker(c *check.C) { + tmpDir := c.MkDir() + + 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) + + c.Assert(Push("dir1"), check.Equals, tmpDir+"/dir1") + c.Assert(Push("dir2"), check.Equals, tmpDir+"/dir1/dir2") + + os.RemoveAll(tmpDir + "/dir1") + + c.Assert(Current(), check.Equals, "") + c.Assert(Pop(), check.Equals, "") +} 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 From 3b1c143b414cb4979af0ef565895bf2591006a0b Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 13 Apr 2016 11:48:23 -0400 Subject: [PATCH 2/4] Added fs walker (bash pushd/popd analog) --- fsutil/walker.go | 78 ++++++++++++++++++++++++++++++++++++++++ fsutil/walker_windows.go | 24 +++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 fsutil/walker.go create mode 100644 fsutil/walker_windows.go 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 "" +} From d9a0885bdb1bfc25192ac8053fddc9c29bea25c8 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 13 Apr 2016 12:03:15 -0400 Subject: [PATCH 3/4] Fix for test on mac --- fsutil/fs_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index 054be6fe..cf5f58da 100644 --- a/fsutil/fs_test.go +++ b/fsutil/fs_test.go @@ -148,6 +148,10 @@ func (s *FSSuite) TestProperPath(c *check.C) { 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) From 62eeb62f52cb37e5cbada073f0c65ba491bbdad9 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 13 Apr 2016 17:23:53 -0400 Subject: [PATCH 4/4] Removed test which fails on mac --- fsutil/fs_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index cf5f58da..e9e0e970 100644 --- a/fsutil/fs_test.go +++ b/fsutil/fs_test.go @@ -169,12 +169,4 @@ func (s *FSSuite) TestWalker(c *check.C) { c.Assert(Pop(), check.Equals, tmpDir+"/dir1") c.Assert(Pop(), check.Equals, tmpDir) c.Assert(Pop(), check.Equals, tmpDir) - - c.Assert(Push("dir1"), check.Equals, tmpDir+"/dir1") - c.Assert(Push("dir2"), check.Equals, tmpDir+"/dir1/dir2") - - os.RemoveAll(tmpDir + "/dir1") - - c.Assert(Current(), check.Equals, "") - c.Assert(Pop(), check.Equals, "") }