From bd1ffe1dd1bbe21e0d1d9558314cfbee2e5b9dc1 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Wed, 30 Dec 2020 23:54:54 +0100 Subject: [PATCH] split into packages --- Makefile | 5 +- analyze/dev.go | 9 +++ dev_linux.go => analyze/dev_linux.go | 10 +-- dev_other.go => analyze/dev_other.go | 2 +- dir.go => analyze/dir.go | 48 +++++++-------- analyze/dir_test.go | 42 +++++++++++++ file.go => analyze/file.go | 40 ++++++------ analyze/file_test.go | 91 ++++++++++++++++++++++++++++ analyze/utils.go | 16 +++++ cli.go => cli/cli.go | 85 +++++++++++++------------- cli_test.go => cli/cli_test.go | 37 +++++------ dev.go | 9 --- dir_test.go | 53 ---------------- file_test.go | 91 ---------------------------- go.mod | 2 +- main.go | 3 +- 16 files changed, 276 insertions(+), 267 deletions(-) create mode 100644 analyze/dev.go rename dev_linux.go => analyze/dev_linux.go (82%) rename dev_other.go => analyze/dev_other.go (91%) rename dir.go => analyze/dir.go (69%) create mode 100644 analyze/dir_test.go rename file.go => analyze/file.go (63%) create mode 100644 analyze/file_test.go create mode 100644 analyze/utils.go rename cli.go => cli/cli.go (82%) rename cli_test.go => cli/cli_test.go (90%) delete mode 100644 dev.go delete mode 100644 dir_test.go delete mode 100644 file_test.go diff --git a/Makefile b/Makefile index 2c7cb4f92..106537710 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ VERSION := $(shell git describe --tags) +PACKAGES := $(shell go list ./...) run: go run . @@ -12,10 +13,10 @@ build: cd build; GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w -X 'main.AppVersion=$(VERSION)'" -o gdu-darwin-amd64 ..; tar czf gdu-darwin-amd64.tgz gdu-darwin-amd64 test: - go test -v + go test -v $(PACKAGES) coverage: - go test -v -race -coverprofile=coverage.txt -covermode=atomic + go test -v -race -coverprofile=coverage.txt -covermode=atomic $(PACKAGES) clean: -rm coverage.txt diff --git a/analyze/dev.go b/analyze/dev.go new file mode 100644 index 000000000..b84822def --- /dev/null +++ b/analyze/dev.go @@ -0,0 +1,9 @@ +package analyze + +// Device struct +type Device struct { + Name string + MountPoint string + Size int64 + Free int64 +} diff --git a/dev_linux.go b/analyze/dev_linux.go similarity index 82% rename from dev_linux.go rename to analyze/dev_linux.go index 7b6778693..0c9c411df 100644 --- a/dev_linux.go +++ b/analyze/dev_linux.go @@ -1,6 +1,6 @@ // +build linux,amd64 -package main +package analyze import ( "bufio" @@ -34,10 +34,10 @@ func GetDevicesInfo() []*Device { syscall.Statfs(parts[1], info) device := &Device{ - name: parts[0], - mountPoint: parts[1], - size: info.Bsize * int64(info.Blocks), - free: info.Bsize * int64(info.Bavail), + Name: parts[0], + MountPoint: parts[1], + Size: info.Bsize * int64(info.Blocks), + Free: info.Bsize * int64(info.Bavail), } devices = append(devices, device) } diff --git a/dev_other.go b/analyze/dev_other.go similarity index 91% rename from dev_other.go rename to analyze/dev_other.go index aded48b07..2a8f22935 100644 --- a/dev_other.go +++ b/analyze/dev_other.go @@ -1,6 +1,6 @@ // +build windows darwin linux,arm -package main +package analyze // GetDevicesInfo returns usage info about mounted devices (by calling Statfs syscall) func GetDevicesInfo() []*Device { diff --git a/dir.go b/analyze/dir.go similarity index 69% rename from dir.go rename to analyze/dir.go index 17dbeb170..1c3aec380 100644 --- a/dir.go +++ b/analyze/dir.go @@ -1,4 +1,4 @@ -package main +package analyze import ( "io/ioutil" @@ -10,11 +10,11 @@ import ( // CurrentProgress struct type CurrentProgress struct { - mutex *sync.Mutex - currentItemName string - itemCount int - totalSize int64 - done bool + Mutex *sync.Mutex + CurrentItemName string + ItemCount int + TotalSize int64 + Done bool } // ShouldBeIgnored whether path should be ignored @@ -44,11 +44,11 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel } dir := File{ - name: filepath.Base(path), - path: path, - isDir: true, - itemCount: 1, - files: make([]*File, 0, len(files)), + Name: filepath.Base(path), + Path: path, + IsDir: true, + ItemCount: 1, + Files: make([]*File, 0, len(files)), } var mutex sync.Mutex @@ -66,34 +66,34 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel go func() { concurrencyLimitChannel <- true file = processDir(entryPath, progress, concurrencyLimitChannel, wait, ignore) - file.parent = &dir + file.Parent = &dir mutex.Lock() - dir.files = append(dir.files, file) + dir.Files = append(dir.Files, file) mutex.Unlock() <-concurrencyLimitChannel wait.Done() }() } else { file = &File{ - name: f.Name(), - path: entryPath, - size: f.Size(), - itemCount: 1, - parent: &dir, + Name: f.Name(), + Path: entryPath, + Size: f.Size(), + ItemCount: 1, + Parent: &dir, } totalSize += f.Size() mutex.Lock() - dir.files = append(dir.files, file) + dir.Files = append(dir.Files, file) mutex.Unlock() } } - progress.mutex.Lock() - progress.currentItemName = path - progress.itemCount += len(files) - progress.totalSize += totalSize - progress.mutex.Unlock() + progress.Mutex.Lock() + progress.CurrentItemName = path + progress.ItemCount += len(files) + progress.TotalSize += totalSize + progress.Mutex.Unlock() return &dir } diff --git a/analyze/dir_test.go b/analyze/dir_test.go new file mode 100644 index 000000000..8c3848c44 --- /dev/null +++ b/analyze/dir_test.go @@ -0,0 +1,42 @@ +package analyze + +import ( + "sync" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestProcessDir(t *testing.T) { + fin := CreateTestDir() + defer fin() + + dir := ProcessDir("test_dir", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return false }) + + // test dir info + assert.Equal(t, "test_dir", dir.Name) + assert.Equal(t, int64(7), dir.Size) + assert.Equal(t, 5, dir.ItemCount) + assert.True(t, dir.IsDir) + + // test dir tree + assert.Equal(t, "nested", dir.Files[0].Name) + assert.Equal(t, "subnested", dir.Files[0].Files[1].Name) + + // test file + assert.Equal(t, "file2", dir.Files[0].Files[0].Name) + assert.Equal(t, int64(2), dir.Files[0].Files[0].Size) + + assert.Equal(t, "file", dir.Files[0].Files[1].Files[0].Name) + assert.Equal(t, int64(5), dir.Files[0].Files[1].Files[0].Size) + + // test parent link + assert.Equal(t, "test_dir", dir.Files[0].Files[1].Files[0].Parent.Parent.Parent.Name) +} + +func TestIgnoreDir(t *testing.T) { + dir := ProcessDir("/proc", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return true }) + + assert.Equal(t, "proc", dir.Name) + assert.Equal(t, 1, dir.ItemCount) +} diff --git a/file.go b/analyze/file.go similarity index 63% rename from file.go rename to analyze/file.go index 627b0bebd..c7d175d65 100644 --- a/file.go +++ b/analyze/file.go @@ -1,4 +1,4 @@ -package main +package analyze import ( "os" @@ -6,13 +6,13 @@ import ( // File struct type File struct { - name string - path string - size int64 - itemCount int - isDir bool - files Files - parent *File + Name string + Path string + Size int64 + ItemCount int + IsDir bool + Files Files + Parent *File } // Files - slice of pointers to File @@ -39,37 +39,37 @@ func (s Files) Remove(file *File) Files { // RemoveFile removes file from dir func (f *File) RemoveFile(file *File) { - error := os.RemoveAll(file.path) + error := os.RemoveAll(file.Path) if error != nil { panic(error) } - f.files = f.files.Remove(file) + f.Files = f.Files.Remove(file) cur := f for { - cur.itemCount -= file.itemCount - cur.size -= file.size + cur.ItemCount -= file.ItemCount + cur.Size -= file.Size - if cur.parent == nil { + if cur.Parent == nil { break } - cur = cur.parent + cur = cur.Parent } } // UpdateStats recursively updates size and item count func (f *File) UpdateStats() { - if !f.isDir { + if !f.IsDir { return } var totalSize int64 var itemCount int - for _, entry := range f.files { + for _, entry := range f.Files { entry.UpdateStats() - totalSize += entry.size - itemCount += entry.itemCount + totalSize += entry.Size + itemCount += entry.ItemCount } - f.itemCount = itemCount + 1 - f.size = totalSize + f.ItemCount = itemCount + 1 + f.Size = totalSize } diff --git a/analyze/file_test.go b/analyze/file_test.go new file mode 100644 index 000000000..364ab67b9 --- /dev/null +++ b/analyze/file_test.go @@ -0,0 +1,91 @@ +package analyze + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFind(t *testing.T) { + dir := File{ + Name: "xxx", + Size: 5, + ItemCount: 2, + } + + file := &File{ + Name: "yyy", + Size: 2, + ItemCount: 1, + Parent: &dir, + } + file2 := &File{ + Name: "zzz", + Size: 3, + ItemCount: 1, + Parent: &dir, + } + dir.Files = []*File{file, file2} + + assert.Equal(t, 0, dir.Files.Find(file)) + assert.Equal(t, 1, dir.Files.Find(file2)) +} + +func TestRemove(t *testing.T) { + dir := File{ + Name: "xxx", + Size: 5, + ItemCount: 2, + } + + file := &File{ + Name: "yyy", + Size: 2, + ItemCount: 1, + Parent: &dir, + } + file2 := &File{ + Name: "zzz", + Size: 3, + ItemCount: 1, + Parent: &dir, + } + dir.Files = []*File{file, file2} + + dir.Files = dir.Files.Remove(file) + + assert.Equal(t, 1, len(dir.Files)) + assert.Equal(t, file2, dir.Files[0]) +} + +func TestRemoveFile(t *testing.T) { + dir := &File{ + Name: "xxx", + Size: 5, + ItemCount: 3, + } + + subdir := &File{ + Name: "yyy", + Size: 4, + ItemCount: 2, + Parent: dir, + } + file := &File{ + Name: "zzz", + Size: 3, + ItemCount: 1, + Parent: subdir, + } + dir.Files = []*File{subdir} + subdir.Files = []*File{file} + + subdir.RemoveFile(file) + + assert.Equal(t, 0, len(subdir.Files)) + assert.Equal(t, 1, subdir.ItemCount) + assert.Equal(t, int64(1), subdir.Size) + assert.Equal(t, 1, len(dir.Files)) + assert.Equal(t, 2, dir.ItemCount) + assert.Equal(t, int64(2), dir.Size) +} diff --git a/analyze/utils.go b/analyze/utils.go new file mode 100644 index 000000000..1346b1264 --- /dev/null +++ b/analyze/utils.go @@ -0,0 +1,16 @@ +package analyze + +import ( + "io/ioutil" + "os" +) + +// CreateTestDir creates test dir structure +func CreateTestDir() func() { + os.MkdirAll("test_dir/nested/subnested", os.ModePerm) + ioutil.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0644) + ioutil.WriteFile("test_dir/nested/file2", []byte("go"), 0644) + return func() { + os.RemoveAll("test_dir") + } +} diff --git a/cli.go b/cli/cli.go similarity index 82% rename from cli.go rename to cli/cli.go index 49323454d..c49632fb7 100644 --- a/cli.go +++ b/cli/cli.go @@ -1,4 +1,4 @@ -package main +package cli import ( "fmt" @@ -9,6 +9,7 @@ import ( "sync" "time" + "github.com/dundee/gdu/analyze" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -30,7 +31,7 @@ type UI struct { progress *tview.TextView help *tview.Flex table *tview.Table - currentDir *File + currentDir *analyze.File topDirPath string currentDirPath string askBeforeDelete bool @@ -77,7 +78,7 @@ func CreateUI(screen tcell.Screen) *UI { // ListDevices lists mounted devices and shows their disk usage func (ui *UI) ListDevices() { - devices := GetDevicesInfo() + devices := analyze.GetDevicesInfo() ui.table.SetCell(0, 0, tview.NewTableCell("Device name").SetSelectable(false)) ui.table.SetCell(0, 1, tview.NewTableCell("Size").SetSelectable(false)) @@ -87,12 +88,12 @@ func (ui *UI) ListDevices() { ui.table.SetCell(0, 5, tview.NewTableCell("Mount point").SetSelectable(false)) for i, device := range devices { - ui.table.SetCell(i+1, 0, tview.NewTableCell(device.name).SetReference(devices[i])) - ui.table.SetCell(i+1, 1, tview.NewTableCell(formatSize(device.size))) - ui.table.SetCell(i+1, 2, tview.NewTableCell(formatSize(device.size-device.free))) + ui.table.SetCell(i+1, 0, tview.NewTableCell(device.Name).SetReference(devices[i])) + ui.table.SetCell(i+1, 1, tview.NewTableCell(formatSize(device.Size))) + ui.table.SetCell(i+1, 2, tview.NewTableCell(formatSize(device.Size-device.Free))) ui.table.SetCell(i+1, 3, tview.NewTableCell(getDeviceUsagePart(device))) - ui.table.SetCell(i+1, 4, tview.NewTableCell(formatSize(device.free))) - ui.table.SetCell(i+1, 5, tview.NewTableCell(device.mountPoint)) + ui.table.SetCell(i+1, 4, tview.NewTableCell(formatSize(device.Free))) + ui.table.SetCell(i+1, 5, tview.NewTableCell(device.MountPoint)) } ui.table.Select(1, 0) @@ -119,16 +120,16 @@ func (ui *UI) AnalyzePath(path string) { ui.pages.AddPage("progress", flex, true, true) ui.table.SetSelectedFunc(ui.fileItemSelected) - progress := &CurrentProgress{ - mutex: &sync.Mutex{}, - done: false, - itemCount: 0, - totalSize: int64(0), + progress := &analyze.CurrentProgress{ + Mutex: &sync.Mutex{}, + Done: false, + ItemCount: 0, + TotalSize: int64(0), } go ui.updateProgress(progress) go func() { - ui.currentDir = ProcessDir(ui.topDirPath, progress, ui.ShouldBeIgnored) + ui.currentDir = analyze.ProcessDir(ui.topDirPath, progress, ui.ShouldBeIgnored) ui.app.QueueUpdateDraw(func() { ui.showDir() @@ -160,7 +161,7 @@ func (ui *UI) ShouldBeIgnored(path string) bool { } func (ui *UI) showDir() { - ui.currentDirPath = ui.currentDir.path + ui.currentDirPath = ui.currentDir.Path ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---") ui.table.Clear() @@ -168,31 +169,31 @@ func (ui *UI) showDir() { rowIndex := 0 if ui.currentDirPath != ui.topDirPath { cell := tview.NewTableCell(" /..") - cell.SetReference(ui.currentDir.parent) + cell.SetReference(ui.currentDir.Parent) ui.table.SetCell(0, 0, cell) rowIndex++ } - sort.Slice(ui.currentDir.files, func(i, j int) bool { - return ui.currentDir.files[i].size > ui.currentDir.files[j].size + sort.Slice(ui.currentDir.Files, func(i, j int) bool { + return ui.currentDir.Files[i].Size > ui.currentDir.Files[j].Size }) - for i, item := range ui.currentDir.files { + for i, item := range ui.currentDir.Files { cell := tview.NewTableCell(formatFileRow(item)) - cell.SetReference(ui.currentDir.files[i]) + cell.SetReference(ui.currentDir.Files[i]) ui.table.SetCell(rowIndex, 0, cell) rowIndex++ } - ui.footer.SetText("Apparent size: " + formatSize(ui.currentDir.size) + " Items: " + fmt.Sprint(ui.currentDir.itemCount)) + ui.footer.SetText("Apparent size: " + formatSize(ui.currentDir.Size) + " Items: " + fmt.Sprint(ui.currentDir.ItemCount)) ui.table.Select(0, 0) ui.table.ScrollToBeginning() ui.app.SetFocus(ui.table) } func (ui *UI) fileItemSelected(row, column int) { - selectedDir := ui.table.GetCell(row, column).GetReference().(*File) - if !selectedDir.isDir { + selectedDir := ui.table.GetCell(row, column).GetReference().(*analyze.File) + if !selectedDir.IsDir { return } @@ -201,15 +202,15 @@ func (ui *UI) fileItemSelected(row, column int) { } func (ui *UI) deviceItemSelected(row, column int) { - selectedDevice := ui.table.GetCell(row, column).GetReference().(*Device) - ui.AnalyzePath(selectedDevice.mountPoint) + selectedDevice := ui.table.GetCell(row, column).GetReference().(*analyze.Device) + ui.AnalyzePath(selectedDevice.MountPoint) } func (ui *UI) confirmDeletion() { row, column := ui.table.GetSelection() - selectedFile := ui.table.GetCell(row, column).GetReference().(*File) + selectedFile := ui.table.GetCell(row, column).GetReference().(*analyze.File) modal := tview.NewModal(). - SetText("Are you sure you want to delete \"" + selectedFile.name + "\""). + SetText("Are you sure you want to delete \"" + selectedFile.Name + "\""). AddButtons([]string{"yes", "no", "don't ask me again"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonIndex == 1 { @@ -226,7 +227,7 @@ func (ui *UI) confirmDeletion() { func (ui *UI) deleteSelected() { row, column := ui.table.GetSelection() - selectedFile := ui.table.GetCell(row, column).GetReference().(*File) + selectedFile := ui.table.GetCell(row, column).GetReference().(*analyze.File) ui.currentDir.RemoveFile(selectedFile) ui.showDir() } @@ -273,24 +274,24 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey { return key } -func (ui *UI) updateProgress(progress *CurrentProgress) { +func (ui *UI) updateProgress(progress *analyze.CurrentProgress) { for { - progress.mutex.Lock() + progress.Mutex.Lock() - if progress.done { + if progress.Done { return } ui.app.QueueUpdateDraw(func() { ui.progress.SetText("Total items: " + - fmt.Sprint(progress.itemCount) + + fmt.Sprint(progress.ItemCount) + " size: " + "size: " + - formatSize(progress.totalSize) + + formatSize(progress.TotalSize) + "\nCurrent item: " + - progress.currentItemName) + progress.CurrentItemName) }) - progress.mutex.Unlock() + progress.Mutex.Unlock() time.Sleep(100 * time.Millisecond) } @@ -326,9 +327,9 @@ func formatSize(size int64) string { return fmt.Sprintf("%d B", size) } -func formatFileRow(item *File) string { - part := int(float64(item.size) / float64(item.parent.size) * 10.0) - row := fmt.Sprintf("%10s", formatSize(item.size)) +func formatFileRow(item *analyze.File) string { + part := int(float64(item.Size) / float64(item.Parent.Size) * 10.0) + row := fmt.Sprintf("%10s", formatSize(item.Size)) row += " [" for i := 0; i < 10; i++ { if part > i { @@ -339,15 +340,15 @@ func formatFileRow(item *File) string { } row += "] " - if item.isDir { + if item.IsDir { row += "/" } - row += item.name + row += item.Name return row } -func getDeviceUsagePart(item *Device) string { - part := int(float64(item.size-item.free) / float64(item.size) * 10.0) +func getDeviceUsagePart(item *analyze.Device) string { + part := int(float64(item.Size-item.Free) / float64(item.Size) * 10.0) row := "[" for i := 0; i < 10; i++ { if part > i { diff --git a/cli_test.go b/cli/cli_test.go similarity index 90% rename from cli_test.go rename to cli/cli_test.go index db3f99d6b..32017ef0d 100644 --- a/cli_test.go +++ b/cli/cli_test.go @@ -1,4 +1,4 @@ -package main +package cli import ( "path/filepath" @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/dundee/gdu/analyze" "github.com/gdamore/tcell/v2" "github.com/stretchr/testify/assert" ) @@ -19,19 +20,19 @@ func TestFooter(t *testing.T) { ui := CreateUI(simScreen) - dir := File{ - name: "xxx", - size: 5, - itemCount: 2, + dir := analyze.File{ + Name: "xxx", + Size: 5, + ItemCount: 2, } - file := File{ - name: "yyy", - size: 2, - itemCount: 1, - parent: &dir, + file := analyze.File{ + Name: "yyy", + Size: 2, + ItemCount: 1, + Parent: &dir, } - dir.files = []*File{&file} + dir.Files = []*analyze.File{&file} ui.currentDir = &dir ui.showDir() @@ -57,10 +58,10 @@ func TestUpdateProgress(t *testing.T) { simScreen.Init() simScreen.SetSize(15, 15) - progress := &CurrentProgress{mutex: &sync.Mutex{}, done: true} + progress := &analyze.CurrentProgress{Mutex: &sync.Mutex{}, Done: true} ui := CreateUI(simScreen) - progress.currentItemName = "xxx" + progress.CurrentItemName = "xxx" ui.updateProgress(progress) assert.True(t, true) } @@ -87,7 +88,7 @@ func TestHelp(t *testing.T) { } func TestDeleteDir(t *testing.T) { - fin := CreateTestDir() + fin := analyze.CreateTestDir() defer fin() simScreen := tcell.NewSimulationScreen("UTF-8") @@ -118,7 +119,7 @@ func TestDeleteDir(t *testing.T) { } func TestShowConfirm(t *testing.T) { - fin := CreateTestDir() + fin := analyze.CreateTestDir() defer fin() simScreen := tcell.NewSimulationScreen("UTF-8") @@ -179,7 +180,7 @@ func printScreen(simScreen tcell.SimulationScreen) { } func TestKeys(t *testing.T) { - fin := CreateTestDir() + fin := analyze.CreateTestDir() defer fin() simScreen := tcell.NewSimulationScreen("UTF-8") @@ -219,7 +220,7 @@ func TestKeys(t *testing.T) { } func TestSetIgnorePaths(t *testing.T) { - fin := CreateTestDir() + fin := analyze.CreateTestDir() defer fin() simScreen := tcell.NewSimulationScreen("UTF-8") @@ -243,6 +244,6 @@ func TestSetIgnorePaths(t *testing.T) { dir := ui.currentDir - assert.Equal(t, 3, dir.itemCount) + assert.Equal(t, 3, dir.ItemCount) } diff --git a/dev.go b/dev.go deleted file mode 100644 index 8ce0c4ed1..000000000 --- a/dev.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -// Device struct -type Device struct { - name string - mountPoint string - size int64 - free int64 -} diff --git a/dir_test.go b/dir_test.go deleted file mode 100644 index b2d1dcbc8..000000000 --- a/dir_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "io/ioutil" - "os" - "sync" - "testing" - - "github.com/stretchr/testify/assert" -) - -func CreateTestDir() func() { - os.MkdirAll("test_dir/nested/subnested", os.ModePerm) - ioutil.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0644) - ioutil.WriteFile("test_dir/nested/file2", []byte("go"), 0644) - return func() { - os.RemoveAll("test_dir") - } -} - -func TestProcessDir(t *testing.T) { - fin := CreateTestDir() - defer fin() - - dir := ProcessDir("test_dir", &CurrentProgress{mutex: &sync.Mutex{}}, func(_ string) bool { return false }) - - // test dir info - assert.Equal(t, "test_dir", dir.name) - assert.Equal(t, int64(7), dir.size) - assert.Equal(t, 5, dir.itemCount) - assert.True(t, dir.isDir) - - // test dir tree - assert.Equal(t, "nested", dir.files[0].name) - assert.Equal(t, "subnested", dir.files[0].files[1].name) - - // test file - assert.Equal(t, "file2", dir.files[0].files[0].name) - assert.Equal(t, int64(2), dir.files[0].files[0].size) - - assert.Equal(t, "file", dir.files[0].files[1].files[0].name) - assert.Equal(t, int64(5), dir.files[0].files[1].files[0].size) - - // test parent link - assert.Equal(t, "test_dir", dir.files[0].files[1].files[0].parent.parent.parent.name) -} - -func TestIgnoreDir(t *testing.T) { - dir := ProcessDir("/proc", &CurrentProgress{mutex: &sync.Mutex{}}, func(_ string) bool { return true }) - - assert.Equal(t, "proc", dir.name) - assert.Equal(t, 1, dir.itemCount) -} diff --git a/file_test.go b/file_test.go deleted file mode 100644 index d7ccd17ea..000000000 --- a/file_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFind(t *testing.T) { - dir := File{ - name: "xxx", - size: 5, - itemCount: 2, - } - - file := &File{ - name: "yyy", - size: 2, - itemCount: 1, - parent: &dir, - } - file2 := &File{ - name: "zzz", - size: 3, - itemCount: 1, - parent: &dir, - } - dir.files = []*File{file, file2} - - assert.Equal(t, 0, dir.files.Find(file)) - assert.Equal(t, 1, dir.files.Find(file2)) -} - -func TestRemove(t *testing.T) { - dir := File{ - name: "xxx", - size: 5, - itemCount: 2, - } - - file := &File{ - name: "yyy", - size: 2, - itemCount: 1, - parent: &dir, - } - file2 := &File{ - name: "zzz", - size: 3, - itemCount: 1, - parent: &dir, - } - dir.files = []*File{file, file2} - - dir.files = dir.files.Remove(file) - - assert.Equal(t, 1, len(dir.files)) - assert.Equal(t, file2, dir.files[0]) -} - -func TestRemoveFile(t *testing.T) { - dir := &File{ - name: "xxx", - size: 5, - itemCount: 3, - } - - subdir := &File{ - name: "yyy", - size: 4, - itemCount: 2, - parent: dir, - } - file := &File{ - name: "zzz", - size: 3, - itemCount: 1, - parent: subdir, - } - dir.files = []*File{subdir} - subdir.files = []*File{file} - - subdir.RemoveFile(file) - - assert.Equal(t, 0, len(subdir.files)) - assert.Equal(t, 1, subdir.itemCount) - assert.Equal(t, int64(1), subdir.size) - assert.Equal(t, 1, len(dir.files)) - assert.Equal(t, 2, dir.itemCount) - assert.Equal(t, int64(2), dir.size) -} diff --git a/go.mod b/go.mod index 3fddaa998..abb1c8b26 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Dundee/gdu +module github.com/dundee/gdu go 1.15 diff --git a/main.go b/main.go index 7ff77faeb..65787436a 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "runtime" "strings" + "github.com/dundee/gdu/cli" "github.com/gdamore/tcell/v2" ) @@ -39,7 +40,7 @@ func main() { } screen.Init() - ui := CreateUI(screen) + ui := cli.CreateUI(screen) ui.SetIgnorePaths(strings.Split(*ignorePath, ",")) args := flag.Args()