diff options
author | Daniel Milde <daniel@milde.cz> | 2021-03-19 09:09:07 +0100 |
---|---|---|
committer | Daniel Milde <daniel@milde.cz> | 2021-03-19 09:09:07 +0100 |
commit | f728b7d068b346a155e29fe5ea3049fe52a08f3d (patch) | |
tree | c5a1fe4364ad5d12298344f1c4500238f09bdba4 | |
parent | cef3f01f690d91e0e8acbafecec6545864e96ab0 (diff) |
check if the diven directory existsv4.8.1
-rw-r--r-- | cmd/app/app.go | 10 | ||||
-rw-r--r-- | cmd/app/app_test.go | 19 | ||||
-rw-r--r-- | cmd/root.go | 1 | ||||
-rw-r--r-- | common/ui.go | 2 | ||||
-rw-r--r-- | internal/testdir/test_dir.go | 5 | ||||
-rw-r--r-- | stdout/stdout.go | 18 | ||||
-rw-r--r-- | stdout/stdout_test.go | 12 | ||||
-rw-r--r-- | tui/actions.go | 9 | ||||
-rw-r--r-- | tui/actions_test.go | 11 | ||||
-rw-r--r-- | tui/keys_test.go | 3 | ||||
-rw-r--r-- | tui/sort_test.go | 2 | ||||
-rw-r--r-- | tui/tui.go | 4 | ||||
-rw-r--r-- | tui/tui_test.go | 1 |
13 files changed, 85 insertions, 12 deletions
diff --git a/cmd/app/app.go b/cmd/app/app.go index 3451a13..aad9420 100644 --- a/cmd/app/app.go +++ b/cmd/app/app.go @@ -51,7 +51,7 @@ func (a *App) Run() error { f, err := os.OpenFile(a.Flags.LogFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { - return fmt.Errorf("Error opening log file: %w", err) + return fmt.Errorf("opening log file: %w", err) } defer f.Close() log.SetOutput(f) @@ -101,7 +101,7 @@ func (a *App) setNoCross(path string) error { if a.Flags.NoCross { mounts, err := a.Getter.GetMounts() if err != nil { - return fmt.Errorf("Error loading mount points: %w", err) + return fmt.Errorf("loading mount points: %w", err) } paths := device.GetNestedMountpointsPaths(path, mounts) a.Flags.IgnoreDirs = append(a.Flags.IgnoreDirs, paths...) @@ -112,10 +112,12 @@ func (a *App) setNoCross(path string) error { func (a *App) runAction(ui common.UI, path string) error { if a.Flags.ShowDisks { if err := ui.ListDevices(a.Getter); err != nil { - return fmt.Errorf("Error loading mount points: %w", err) + return fmt.Errorf("loading mount points: %w", err) } } else { - ui.AnalyzePath(path, nil) + if err := ui.AnalyzePath(path, nil); err != nil { + return fmt.Errorf("scanning dir: %w", err) + } } return nil } diff --git a/cmd/app/app_test.go b/cmd/app/app_test.go index 8b1a9f7..fdd8e33 100644 --- a/cmd/app/app_test.go +++ b/cmd/app/app_test.go @@ -66,6 +66,21 @@ func TestAnalyzePathWithGui(t *testing.T) { assert.Nil(t, err) } +func TestAnalyzePathWithErr(t *testing.T) { + fin := testdir.CreateTestDir() + defer fin() + + out, err := runApp( + &Flags{LogFile: "/dev/null"}, + []string{"xxx"}, + false, + testdev.DevicesInfoGetterMock{}, + ) + + assert.Equal(t, "", out) + assert.Contains(t, err.Error(), "no such file or directory") +} + func TestNoCross(t *testing.T) { fin := testdir.CreateTestDir() defer fin() @@ -92,7 +107,7 @@ func TestNoCrossWithErr(t *testing.T) { device.LinuxDevicesInfoGetter{MountsPath: "/xxxyyy"}, ) - assert.Equal(t, "Error loading mount points: open /xxxyyy: no such file or directory", err.Error()) + assert.Equal(t, "loading mount points: open /xxxyyy: no such file or directory", err.Error()) assert.Empty(t, out) } @@ -122,7 +137,7 @@ func TestListDevicesWithErr(t *testing.T) { device.LinuxDevicesInfoGetter{MountsPath: "/xxxyyy"}, ) - assert.Equal(t, "Error loading mount points: open /xxxyyy: no such file or directory", err.Error()) + assert.Equal(t, "loading mount points: open /xxxyyy: no such file or directory", err.Error()) } func TestListDevicesWithGui(t *testing.T) { diff --git a/cmd/root.go b/cmd/root.go index 694992f..028361c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,7 +83,6 @@ func runE(command *cobra.Command, args []string) error { // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } diff --git a/common/ui.go b/common/ui.go index 8cecf69..a5d616d 100644 --- a/common/ui.go +++ b/common/ui.go @@ -8,7 +8,7 @@ import ( // UI is common interface for both terminal UI and text output type UI interface { ListDevices(getter device.DevicesInfoGetter) error - AnalyzePath(path string, parentDir *analyze.Dir) + AnalyzePath(path string, parentDir *analyze.Dir) error SetIgnoreDirPaths(paths []string) StartUILoop() error } diff --git a/internal/testdir/test_dir.go b/internal/testdir/test_dir.go index fd99a7b..e7fe7ea 100644 --- a/internal/testdir/test_dir.go +++ b/internal/testdir/test_dir.go @@ -1,6 +1,7 @@ package testdir import ( + "io/fs" "os" ) @@ -16,3 +17,7 @@ func CreateTestDir() func() { } } } + +func MockedPathChecker(path string) (fs.FileInfo, error) { + return nil, nil +} diff --git a/stdout/stdout.go b/stdout/stdout.go index 2cf5b60..647e243 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -3,7 +3,9 @@ package stdout import ( "fmt" "io" + "io/fs" "math" + "os" "path/filepath" "sort" "sync" @@ -25,6 +27,7 @@ type UI struct { red *color.Color orange *color.Color blue *color.Color + pathChecker func(string) (fs.FileInfo, error) } // CreateStdoutUI creates UI for stdout @@ -35,6 +38,7 @@ func CreateStdoutUI(output io.Writer, useColors bool, showProgress bool, showApp showProgress: showProgress, showApparentSize: showApparentSize, analyzer: analyze.CreateAnalyzer(), + pathChecker: os.Stat, } ui.red = color.New(color.FgRed).Add(color.Bold) @@ -112,11 +116,17 @@ func (ui *UI) ListDevices(getter device.DevicesInfoGetter) error { } // AnalyzePath analyzes recursively disk usage in given path -func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) { +func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) error { + var ( + dir *analyze.Dir + wait sync.WaitGroup + ) abspath, _ := filepath.Abs(path) - var dir *analyze.Dir - var wait sync.WaitGroup + _, err := ui.pathChecker(abspath) + if err != nil { + return err + } if ui.showProgress { wait.Add(1) @@ -166,6 +176,8 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) { file.GetName()) } } + + return nil } // SetIgnoreDirPaths sets paths to ignore diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go index 16bf507..c4808ad 100644 --- a/stdout/stdout_test.go +++ b/stdout/stdout_test.go @@ -26,6 +26,17 @@ func TestAnalyzePath(t *testing.T) { assert.Contains(t, output.String(), "nested") } +func TestAnalyzePathWithErr(t *testing.T) { + buff := make([]byte, 10) + output := bytes.NewBuffer(buff) + + ui := CreateStdoutUI(output, false, false, false) + ui.SetIgnoreDirPaths([]string{"/xxx"}) + err := ui.AnalyzePath("aaa", nil) + + assert.Contains(t, err.Error(), "no such file or directory") +} + func TestAnalyzePathWithColors(t *testing.T) { fin := testdir.CreateTestDir() defer fin() @@ -45,6 +56,7 @@ func TestItemRows(t *testing.T) { ui := CreateStdoutUI(output, false, true, false) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.AnalyzePath("test_dir", nil) assert.Contains(t, output.String(), "TiB") diff --git a/tui/actions.go b/tui/actions.go index ea78de2..ea10fa2 100644 --- a/tui/actions.go +++ b/tui/actions.go @@ -56,9 +56,14 @@ func (ui *UI) ListDevices(getter device.DevicesInfoGetter) error { } // AnalyzePath analyzes recursively disk usage in given path -func (ui *UI) AnalyzePath(path string, parentDir *analyze.Dir) { +func (ui *UI) AnalyzePath(path string, parentDir *analyze.Dir) error { abspath, _ := filepath.Abs(path) + _, err := ui.pathChecker(abspath) + if err != nil { + return err + } + ui.progress = tview.NewTextView().SetText("Scanning...") ui.progress.SetBorder(true).SetBorderPadding(2, 2, 2, 2) ui.progress.SetTitle(" Scanning... ") @@ -102,6 +107,8 @@ func (ui *UI) AnalyzePath(path string, parentDir *analyze.Dir) { ui.done <- struct{}{} } }() + + return nil } func (ui *UI) deleteSelected() { diff --git a/tui/actions_test.go b/tui/actions_test.go index 59eb7bf..336a7fe 100644 --- a/tui/actions_test.go +++ b/tui/actions_test.go @@ -96,6 +96,14 @@ func TestAnalyzePath(t *testing.T) { assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa") } +func TestAnalyzePathWithErr(t *testing.T) { + app := testapp.CreateMockedApp(true) + ui := CreateUI(app, false, true) + err := ui.AnalyzePath("xxx", nil) + + assert.Contains(t, err.Error(), "no such file or directory") +} + func TestAnalyzePathBW(t *testing.T) { ui := getAnalyzedPathMockedApp(t, false, true, true) @@ -115,6 +123,7 @@ func TestAnalyzePathWithParentDir(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.topDir = parentDir ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", parentDir) @@ -137,6 +146,7 @@ func TestViewDirContents(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", nil) @@ -156,6 +166,7 @@ func TestViewContentsOfNotExistingFile(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", nil) diff --git a/tui/keys_test.go b/tui/keys_test.go index 993b83d..95a035e 100644 --- a/tui/keys_test.go +++ b/tui/keys_test.go @@ -132,6 +132,7 @@ func TestShowConfirm(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, true, true) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", nil) @@ -224,6 +225,7 @@ func TestSortByApparentSize(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, false) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", nil) @@ -281,6 +283,7 @@ func TestSorting(t *testing.T) { app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, true) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.AnalyzePath("test_dir", nil) diff --git a/tui/sort_test.go b/tui/sort_test.go index d8b367c..3a79e7b 100644 --- a/tui/sort_test.go +++ b/tui/sort_test.go @@ -5,6 +5,7 @@ import ( "github.com/dundee/gdu/v4/internal/testanalyze" "github.com/dundee/gdu/v4/internal/testapp" + "github.com/dundee/gdu/v4/internal/testdir" "github.com/stretchr/testify/assert" ) @@ -114,6 +115,7 @@ func getAnalyzedPathWithSorting(sortBy string, sortOrder string, apparentSize bo app := testapp.CreateMockedApp(true) ui := CreateUI(app, false, apparentSize) ui.analyzer = &testanalyze.MockedAnalyzer{} + ui.pathChecker = testdir.MockedPathChecker ui.done = make(chan struct{}) ui.sortBy = sortBy ui.sortOrder = sortOrder @@ -2,6 +2,8 @@ package tui import ( "fmt" + "io/fs" + "os" "sort" "time" @@ -65,6 +67,7 @@ type UI struct { showApparentSize bool done chan struct{} remover func(*analyze.Dir, analyze.Item) error + pathChecker func(string) (fs.FileInfo, error) } // CreateUI creates the whole UI app @@ -77,6 +80,7 @@ func CreateUI(app common.TermApplication, useColors bool, showApparentSize bool) showApparentSize: showApparentSize, analyzer: analyze.CreateAnalyzer(), remover: analyze.RemoveItemFromDir, + pathChecker: os.Stat, } app.SetBeforeDrawFunc(func(screen tcell.Screen) bool { diff --git a/tui/tui_test.go b/tui/tui_test.go index 313d334..171a682 100644 --- a/tui/tui_test.go +++ b/tui/tui_test.go @@ -334,6 +334,7 @@ func getDevicesInfoMock() device.DevicesInfoGetter { func getAnalyzedPathMockedApp(t *testing.T, useColors, apparentSize bool, mockedAnalyzer bool) *UI { app := testapp.CreateMockedApp(true) ui := CreateUI(app, useColors, apparentSize) + ui.pathChecker = testdir.MockedPathChecker if mockedAnalyzer { ui.analyzer = &testanalyze.MockedAnalyzer{} |