Skip to content

Commit

Permalink
check if the diven directory exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Mar 19, 2021
1 parent cef3f01 commit f728b7d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 12 deletions.
10 changes: 6 additions & 4 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...)
Expand All @@ -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
}
19 changes: 17 additions & 2 deletions cmd/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion common/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions internal/testdir/test_dir.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testdir

import (
"io/fs"
"os"
)

Expand All @@ -16,3 +17,7 @@ func CreateTestDir() func() {
}
}
}

func MockedPathChecker(path string) (fs.FileInfo, error) {
return nil, nil
}
18 changes: 15 additions & 3 deletions stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package stdout
import (
"fmt"
"io"
"io/fs"
"math"
"os"
"path/filepath"
"sort"
"sync"
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -166,6 +176,8 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) {
file.GetName())
}
}

return nil
}

// SetIgnoreDirPaths sets paths to ignore
Expand Down
12 changes: 12 additions & 0 deletions stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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... ")
Expand Down Expand Up @@ -102,6 +107,8 @@ func (ui *UI) AnalyzePath(path string, parentDir *analyze.Dir) {
ui.done <- struct{}{}
}
}()

return nil
}

func (ui *UI) deleteSelected() {
Expand Down
11 changes: 11 additions & 0 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions tui/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

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

Expand Down
2 changes: 2 additions & 0 deletions tui/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tui

import (
"fmt"
"io/fs"
"os"
"sort"
"time"

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit f728b7d

Please sign in to comment.