diff options
-rw-r--r-- | cmd/gdu/main.go | 7 | ||||
-rw-r--r-- | internal/testapp/app.go | 5 | ||||
-rw-r--r-- | pkg/device/dev_freebsd.go | 9 | ||||
-rw-r--r-- | pkg/device/dev_freebsd_test.go | 2 | ||||
-rw-r--r-- | pkg/device/dev_linux.go | 9 | ||||
-rw-r--r-- | pkg/device/dev_linux_test.go | 4 | ||||
-rw-r--r-- | stdout/stdout_test.go | 27 | ||||
-rw-r--r-- | tui/actions.go | 13 | ||||
-rw-r--r-- | tui/actions_test.go | 34 | ||||
-rw-r--r-- | tui/keys_test.go | 36 | ||||
-rw-r--r-- | tui/tui_test.go | 5 |
11 files changed, 105 insertions, 46 deletions
diff --git a/cmd/gdu/main.go b/cmd/gdu/main.go index e7f312c..799c97f 100644 --- a/cmd/gdu/main.go +++ b/cmd/gdu/main.go @@ -59,14 +59,17 @@ func runE(command *cobra.Command, args []string) error { af.LogFile = "nul" } - var termApp *tview.Application = nil + var termApp *tview.Application if !af.ShowVersion && !af.NonInteractive && istty { screen, err := tcell.NewScreen() if err != nil { return fmt.Errorf("Error creating screen: %w", err) } - screen.Init() + err = screen.Init() + if err != nil { + return fmt.Errorf("Error initializing screen: %w", err) + } defer screen.Clear() defer screen.Fini() diff --git a/internal/testapp/app.go b/internal/testapp/app.go index 4429edd..c38aa9d 100644 --- a/internal/testapp/app.go +++ b/internal/testapp/app.go @@ -12,7 +12,10 @@ import ( // CreateTestAppWithSimScreen returns app with simulation screen for tests func CreateTestAppWithSimScreen(width, height int) (*tview.Application, tcell.SimulationScreen) { screen := tcell.NewSimulationScreen("UTF-8") - screen.Init() + err := screen.Init() + if err != nil { + panic(err) + } screen.SetSize(width, height) app := tview.NewApplication() diff --git a/pkg/device/dev_freebsd.go b/pkg/device/dev_freebsd.go index 46094f8..ba3fbf6 100644 --- a/pkg/device/dev_freebsd.go +++ b/pkg/device/dev_freebsd.go @@ -37,7 +37,7 @@ func (t FreeBSDDevicesInfoGetter) GetDevicesInfo() (Devices, error) { return nil, err } - return processMounts(mounts) + return processMounts(mounts, false) } func readMountOutput(rdr io.Reader) (Devices, error) { @@ -74,13 +74,16 @@ func readMountOutput(rdr io.Reader) (Devices, error) { return mounts, nil } -func processMounts(mounts Devices) (Devices, error) { +func processMounts(mounts Devices, ignoreErrors bool) (Devices, error) { devices := Devices{} for _, mount := range mounts { if strings.HasPrefix(mount.Name, "/dev") || mount.Fstype == "zfs" { info := &syscall.Statfs_t{} - syscall.Statfs(mount.MountPoint, info) + err := syscall.Statfs(mount.MountPoint, info) + if err != nil && !ignoreErrors { + return nil, err + } mount.Size = int64(info.Bsize) * int64(info.Blocks) mount.Free = int64(info.Bsize) * int64(info.Bavail) diff --git a/pkg/device/dev_freebsd_test.go b/pkg/device/dev_freebsd_test.go index b6c3492..742d031 100644 --- a/pkg/device/dev_freebsd_test.go +++ b/pkg/device/dev_freebsd_test.go @@ -39,7 +39,7 @@ map -hosts on /net (autofs) argon:/usr/src on /usr/src (nfs) argon:/usr/obj on /usr/obj (nfs)`)) - devices, err := processMounts(mounts) + devices, err := processMounts(mounts, true) assert.Len(t, devices, 6) assert.Nil(t, err) } diff --git a/pkg/device/dev_linux.go b/pkg/device/dev_linux.go index d6534c1..509c05b 100644 --- a/pkg/device/dev_linux.go +++ b/pkg/device/dev_linux.go @@ -34,7 +34,7 @@ func (t LinuxDevicesInfoGetter) GetDevicesInfo() (Devices, error) { return nil, err } - return processMounts(mounts) + return processMounts(mounts, false) } func readMountsFile(file io.Reader) (Devices, error) { @@ -60,7 +60,7 @@ func readMountsFile(file io.Reader) (Devices, error) { return mounts, nil } -func processMounts(mounts Devices) (Devices, error) { +func processMounts(mounts Devices, ignoreErrors bool) (Devices, error) { devices := Devices{} for _, mount := range mounts { @@ -70,7 +70,10 @@ func processMounts(mounts Devices) (Devices, error) { if strings.HasPrefix(mount.Name, "/dev") || mount.Fstype == "zfs" { info := &syscall.Statfs_t{} - syscall.Statfs(mount.MountPoint, info) + err := syscall.Statfs(mount.MountPoint, info) + if err != nil && !ignoreErrors { + return nil, err + } mount.Size = int64(info.Bsize) * int64(info.Blocks) mount.Free = int64(info.Bsize) * int64(info.Bavail) diff --git a/pkg/device/dev_linux_test.go b/pkg/device/dev_linux_test.go index d955e84..9116021 100644 --- a/pkg/device/dev_linux_test.go +++ b/pkg/device/dev_linux_test.go @@ -26,7 +26,7 @@ func TestSnapMountsNotShown(t *testing.T) { /dev/loop3 /var/lib/snapd/snap/core20/904 squashfs ro,nodev,relatime 0 0 /dev/nvme0n1p1 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0`)) - devices, err := processMounts(mounts) + devices, err := processMounts(mounts, true) assert.Len(t, devices, 1) assert.Nil(t, err) } @@ -41,7 +41,7 @@ rootpool/home /home zfs rw,nodev,relatime,xattr,posixacl 0 0 /dev/loop3 /var/lib/snapd/snap/core20/904 squashfs ro,nodev,relatime 0 0 /dev/nvme0n1p1 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0`)) - devices, err := processMounts(mounts) + devices, err := processMounts(mounts, true) assert.Len(t, devices, 6) assert.Nil(t, err) } diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go index 930f029..25bb592 100644 --- a/stdout/stdout_test.go +++ b/stdout/stdout_test.go @@ -26,9 +26,11 @@ func TestAnalyzePath(t *testing.T) { ui := CreateStdoutUI(output, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) - ui.AnalyzePath("test_dir", nil) - ui.StartUILoop() + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) + err = ui.StartUILoop() + assert.Nil(t, err) assert.Contains(t, output.String(), "nested") } @@ -41,9 +43,11 @@ func TestAnalyzeSubdir(t *testing.T) { ui := CreateStdoutUI(output, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) - ui.AnalyzePath("test_dir/nested", nil) - ui.StartUILoop() + err := ui.AnalyzePath("test_dir/nested", nil) + assert.Nil(t, err) + err = ui.StartUILoop() + assert.Nil(t, err) assert.Contains(t, output.String(), "file2") } @@ -67,8 +71,9 @@ func TestAnalyzePathWithColors(t *testing.T) { ui := CreateStdoutUI(output, true, false, true) ui.SetIgnoreDirPaths([]string{"/xxx"}) - ui.AnalyzePath("test_dir/nested", nil) + err := ui.AnalyzePath("test_dir/nested", nil) + assert.Nil(t, err) assert.Contains(t, output.String(), "subnested") } @@ -78,8 +83,9 @@ func TestItemRows(t *testing.T) { ui := CreateStdoutUI(output, false, true, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) assert.Contains(t, output.String(), "TiB") assert.Contains(t, output.String(), "GiB") assert.Contains(t, output.String(), "MiB") @@ -94,8 +100,9 @@ func TestAnalyzePathWithProgress(t *testing.T) { ui := CreateStdoutUI(output, false, true, true) ui.SetIgnoreDirPaths([]string{"/xxx"}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) assert.Contains(t, output.String(), "nested") } @@ -103,8 +110,9 @@ func TestShowDevices(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) ui := CreateStdoutUI(output, false, true, false) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + assert.Nil(t, err) assert.Contains(t, output.String(), "Device") assert.Contains(t, output.String(), "xxx") } @@ -113,8 +121,9 @@ func TestShowDevicesWithColor(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) ui := CreateStdoutUI(output, true, true, true) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + assert.Nil(t, err) assert.Contains(t, output.String(), "Device") assert.Contains(t, output.String(), "xxx") } diff --git a/tui/actions.go b/tui/actions.go index 06131e7..09800ac 100644 --- a/tui/actions.go +++ b/tui/actions.go @@ -193,10 +193,19 @@ func (ui *UI) showFile() *tview.TextView { ui.currentDirLabel.SetText("[::b] --- " + selectedFile.GetPath() + " ---").SetDynamicColors(true) readNextPart := func(linesCount int) int { + var err error readLines := 0 for scanner.Scan() && readLines <= linesCount { - file.Write(scanner.Bytes()) - file.Write([]byte("\n")) + _, err = file.Write(scanner.Bytes()) + if err != nil { + ui.showErr("Error reading file", err) + return 0 + } + _, err = file.Write([]byte("\n")) + if err != nil { + ui.showErr("Error reading file", err) + return 0 + } readLines++ } return readLines diff --git a/tui/actions_test.go b/tui/actions_test.go index 8784c1b..c598cf2 100644 --- a/tui/actions_test.go +++ b/tui/actions_test.go @@ -16,7 +16,10 @@ func TestShowDevices(t *testing.T) { defer simScreen.Fini() ui := CreateUI(app, true, true) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + + assert.Nil(t, err) + ui.table.Draw(simScreen) simScreen.Show() @@ -33,7 +36,10 @@ func TestShowDevicesBW(t *testing.T) { defer simScreen.Fini() ui := CreateUI(app, false, false) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + + assert.Nil(t, err) + ui.table.Draw(simScreen) simScreen.Show() @@ -51,8 +57,9 @@ func TestDeviceSelected(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) ui.SetIgnoreDirPaths([]string{"/xxx"}) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + assert.Nil(t, err) assert.Equal(t, 3, ui.table.GetRowCount()) ui.deviceItemSelected(1, 0) @@ -108,7 +115,8 @@ func TestAnalyzePathWithParentDir(t *testing.T) { ui.PathChecker = testdir.MockedPathChecker ui.topDir = parentDir ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", parentDir) + err := ui.AnalyzePath("test_dir", parentDir) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -130,7 +138,8 @@ func TestViewDirContents(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -150,7 +159,8 @@ func TestViewContentsOfNotExistingFile(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -179,7 +189,8 @@ func TestViewFile(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -207,7 +218,8 @@ func TestShowInfo(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -236,7 +248,8 @@ func TestShowInfoBW(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, true, false) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -261,7 +274,8 @@ func TestExitViewFile(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer diff --git a/tui/keys_test.go b/tui/keys_test.go index 37888a0..625b0dd 100644 --- a/tui/keys_test.go +++ b/tui/keys_test.go @@ -64,7 +64,8 @@ func TestMoveLeftRight(t *testing.T) { app := testapp.CreateMockedApp(false) ui := CreateUI(app, true, true) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -109,7 +110,8 @@ func TestMoveRightOnDevice(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) ui.SetIgnoreDirPaths([]string{}) - ui.ListDevices(getDevicesInfoMock()) + err := ui.ListDevices(getDevicesInfoMock()) + assert.Nil(t, err) ui.table.Select(1, 0) @@ -134,7 +136,8 @@ func TestShowConfirm(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -168,7 +171,8 @@ func TestDelete(t *testing.T) { ui := CreateUI(app, false, true) ui.done = make(chan struct{}) ui.askBeforeDelete = false - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -201,7 +205,8 @@ func TestDeleteParent(t *testing.T) { ui := CreateUI(app, false, true) ui.done = make(chan struct{}) ui.askBeforeDelete = false - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -229,7 +234,8 @@ func TestEmptyDir(t *testing.T) { ui := CreateUI(app, false, true) ui.done = make(chan struct{}) ui.askBeforeDelete = false - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -263,7 +269,8 @@ func TestEmptyFile(t *testing.T) { ui := CreateUI(app, false, true) ui.done = make(chan struct{}) ui.askBeforeDelete = false - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -299,7 +306,8 @@ func TestSortByApparentSize(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -320,7 +328,8 @@ func TestShowFileCount(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -341,7 +350,8 @@ func TestShowFileCountBW(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -399,7 +409,8 @@ func TestSorting(t *testing.T) { ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.PathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer @@ -424,7 +435,8 @@ func TestShowFile(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.done = make(chan struct{}) - ui.AnalyzePath("test_dir", nil) + err := ui.AnalyzePath("test_dir", nil) + assert.Nil(t, err) <-ui.done // wait for analyzer diff --git a/tui/tui_test.go b/tui/tui_test.go index e768e92..6e71bee 100644 --- a/tui/tui_test.go +++ b/tui/tui_test.go @@ -194,7 +194,10 @@ func TestFileSelected(t *testing.T) { func TestBeforeDraw(t *testing.T) { screen := tcell.NewSimulationScreen("UTF-8") - screen.Init() + err := screen.Init() + + assert.Nil(t, err) + app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) |