From 1eefc22fb30640b6a9d9631df6b91c80356b5e14 Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 1 Feb 2019 01:24:32 +0200 Subject: [PATCH 1/2] Print directory and file count when using -dry-run Dry run prints the number of directories and files in the directory we're grouping in. --- groupby.go | 1 + tree.go | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/groupby.go b/groupby.go index 6853cc5..afeb893 100644 --- a/groupby.go +++ b/groupby.go @@ -188,6 +188,7 @@ func main() { printingVisitor := NewPrintingVisitor() if dryRun { tree.Visit(printingVisitor) + fmt.Printf("\n%d directories, %d files\n", tree.Directories(), tree.Files()) os.Exit(-1) return } diff --git a/tree.go b/tree.go index ee0bfd2..ffc8caa 100644 --- a/tree.go +++ b/tree.go @@ -9,8 +9,10 @@ import ( ) type Tree struct { - Root *Node - MaxDepth int + Root *Node + MaxDepth int + directoryCount int + fileCount int } func NewTree(directory string, maxDepth int) *Tree { @@ -20,8 +22,10 @@ func NewTree(directory string, maxDepth int) *Tree { log.Fatal(err) } return &Tree{ - Root: NewNode(dirPath, year, month, day), - MaxDepth: maxDepth, + Root: NewNode(dirPath, year, month, day), + MaxDepth: maxDepth, + directoryCount: 0, + fileCount: 0, } } @@ -58,6 +62,12 @@ func (t *Tree) AddEntry(file os.FileInfo) { return } + if file.IsDir() { + t.directoryCount++ + } else { + t.fileCount++ + } + year, month, day := GetFileInfoYMD(file) var node = NewNode(file.Name(), year, month, day) @@ -102,3 +112,13 @@ func (t *Tree) AddEntry(file os.FileInfo) { func (t *Tree) Visit(visitor NodeVisitor) { t.Root.Visit(visitor, 0) } + +// Directories returns number of directories in the tree +func (t *Tree) Directories() int { + return t.directoryCount +} + +// Files returns number of files in the tree +func (t *Tree) Files() int { + return t.fileCount +} From f7b9c021a0b573b16dcdecebc26e3fa8f1d3357b Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 1 Feb 2019 01:53:51 +0200 Subject: [PATCH 2/2] Add test Directories() and Files() functions --- tree_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tree_test.go diff --git a/tree_test.go b/tree_test.go new file mode 100644 index 0000000..095c13b --- /dev/null +++ b/tree_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "os" + "testing" + "time" +) + +// fileInfo for testing the tree +type fileInfo struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (f fileInfo) Name() string { + return f.name +} + +func (f fileInfo) Size() int64 { + return f.size +} + +func (f fileInfo) Mode() os.FileMode { + return f.mode +} + +func (f fileInfo) ModTime() time.Time { + return f.modTime +} + +func (f fileInfo) IsDir() bool { + return f.mode.IsDir() +} + +func (f fileInfo) Sys() interface{} { + return nil +} + +func TestAddEntryIncrementsDirectoryAndFileCounts(t *testing.T) { + pano := time.Now() + fileMode := os.FileMode(0644) + files := []fileInfo{ + fileInfo{"dir1", 1024, os.ModeDir, pano}, + fileInfo{"dir2", 1024, os.ModeDir, pano}, + fileInfo{"dir3", 1024, os.ModeDir, pano}, + fileInfo{"file1", 1024, fileMode, pano}, + fileInfo{"file2", 1024, fileMode, pano}, + fileInfo{"file3", 1024, fileMode, pano}, + fileInfo{"file4", 1024, fileMode, pano}, + } + + tree := &Tree{ + Root: NewNode("/", pano.Year(), pano.Month(), pano.Day()), + MaxDepth: 1, + directoryCount: 0, + fileCount: 0, + } + + for _, f := range files { + tree.AddEntry(f) + } + + if tree.Directories() != 3 { + t.Errorf("Tree's Directories() is incorrect. Got '%d', Expected '%d'", tree.Directories(), 3) + } + + if tree.Files() != 4 { + t.Errorf("Tree's Files() is incorrect. Got '%d', Expected '%d'", tree.Files(), 4) + } +}