Skip to content

Commit

Permalink
Merge pull request #4 from zikani03/show-file-counts
Browse files Browse the repository at this point in the history
Print directory and file counts
  • Loading branch information
zikani03 authored Feb 3, 2019
2 parents 6322a40 + f7b9c02 commit 7b7e8b5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
1 change: 1 addition & 0 deletions groupby.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 24 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
72 changes: 72 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 7b7e8b5

Please sign in to comment.