Skip to content

Commit

Permalink
fix: check is currentDir is set
Browse files Browse the repository at this point in the history
fixes #88
  • Loading branch information
dundee committed Aug 27, 2021
1 parent 5b3a81b commit 49eb695
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 11 deletions.
8 changes: 8 additions & 0 deletions tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func (ui *UI) deleteSelected(shouldEmpty bool) {
}

func (ui *UI) showFile() *tview.TextView {
if ui.currentDir == nil {
return nil
}

row, column := ui.table.GetSelection()
selectedFile := ui.table.GetCell(row, column).GetReference().(analyze.Item)
if selectedFile.IsDir() {
Expand Down Expand Up @@ -305,6 +309,10 @@ func (ui *UI) showFile() *tview.TextView {
}

func (ui *UI) showInfo() {
if ui.currentDir == nil {
return
}

var content, numberColor string
row, column := ui.table.GetSelection()
selectedFile := ui.table.GetCell(row, column).GetReference().(analyze.Item)
Expand Down
28 changes: 28 additions & 0 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ func TestViewDirContents(t *testing.T) {
assert.Nil(t, res)
}

func TestViewFileWithoutCurrentDir(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
ui.done = make(chan struct{})

res := ui.showFile() // no current directory
assert.Nil(t, res)
}

func TestViewContentsOfNotExistingFile(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()
Expand Down Expand Up @@ -317,6 +330,21 @@ func TestShowInfoBW(t *testing.T) {
assert.True(t, ui.pages.HasPage("info"))
}

func TestShowInfoWithoutCurrentDir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true)
ui.done = make(chan struct{})

// pressing `i` will do nothing
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'i', 0))
assert.False(t, ui.pages.HasPage("info"))
}

func TestExitViewFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down
4 changes: 4 additions & 0 deletions tui/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (ui *UI) hideFilterInput() {
}

func (ui *UI) showFilterInput() {
if ui.currentDir == nil {
return
}

if ui.filteringInput == nil {
ui.filteringInput = tview.NewInputField()

Expand Down
14 changes: 14 additions & 0 deletions tui/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func TestFiltering(t *testing.T) {
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // filtering reset
}

func TestFilteringWithoutCurrentDir(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(false)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
ui.done = make(chan struct{})

ui.showFilterInput()

assert.False(t, ui.filtering)
}

func TestSwitchToTable(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down
27 changes: 17 additions & 10 deletions tui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey {
ui.app.Stop()
return nil
case 'b':
ui.app.Stop()
if err := os.Chdir(ui.currentDirPath); err != nil {
panic(err)
}
if err := ui.exec(getShellBin(), nil, os.Environ()); err != nil {
panic(err)
}
ui.spawnShell()
return nil
case '?':
ui.showHelp()
Expand Down Expand Up @@ -121,10 +115,8 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey {
ui.setSorting("mtime")
case '/':
ui.showFilterInput()
default:
return key
}
return nil
return key
}

func (ui *UI) handleLeft() {
Expand Down Expand Up @@ -166,3 +158,18 @@ func (ui *UI) handleDelete(shouldEmpty bool) {
ui.deleteSelected(shouldEmpty)
}
}

func (ui *UI) spawnShell() {
if ui.currentDir == nil {
return
}

ui.app.Stop()
if err := os.Chdir(ui.currentDirPath); err != nil {
ui.showErr("Error changing directory", err)
return
}
if err := ui.exec(getShellBin(), nil, os.Environ()); err != nil {
ui.showErr("Error executing shell", err)
}
}
75 changes: 74 additions & 1 deletion tui/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"bytes"
"errors"
"testing"

"github.com/dundee/gdu/v5/internal/testanalyze"
Expand Down Expand Up @@ -206,6 +207,78 @@ func TestSpawnShell(t *testing.T) {
assert.True(t, called)
}

func TestSpawnShellWithoutDir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(false)
buff := &bytes.Buffer{}
ui := CreateUI(app, simScreen, buff, true, true)
var called = false
ui.exec = func(argv0 string, argv, envv []string) error {
called = true
return nil
}

ui.done = make(chan struct{})

key := ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'b', 0))
assert.Nil(t, key)
assert.False(t, called)
}

func TestSpawnShellWithWrongDir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(false)
buff := &bytes.Buffer{}
ui := CreateUI(app, simScreen, buff, true, true)
var called = false
ui.exec = func(argv0 string, argv, envv []string) error {
called = true
return nil
}

ui.done = make(chan struct{})
ui.currentDir = &analyze.Dir{}
ui.currentDirPath = "/xxxxx"

key := ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'b', 0))
assert.Nil(t, key)
assert.False(t, called)
assert.True(t, ui.pages.HasPage("error"))
}

func TestSpawnShellWithError(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(false)
buff := &bytes.Buffer{}
ui := CreateUI(app, simScreen, buff, true, true)
var called = false
ui.exec = func(argv0 string, argv, envv []string) error {
called = true
return errors.New("wrong shell")
}

ui.done = make(chan struct{})
ui.currentDir = &analyze.Dir{}
ui.currentDirPath = "."

key := ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'b', 0))
assert.Nil(t, key)
assert.True(t, called)
assert.True(t, ui.pages.HasPage("error"))
}

func TestShowConfirm(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()
Expand Down Expand Up @@ -241,7 +314,7 @@ func TestDeleteEmpty(t *testing.T) {
ui.done = make(chan struct{})

key := ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'd', 0))
assert.Nil(t, key)
assert.NotNil(t, key)
}

func TestDelete(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions tui/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func TestSetSorting(t *testing.T) {
assert.Equal(t, "asc", ui.sortOrder)
}

func TestSetSortingWithoutCurrentDir(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true)

// calling setSorting does nothing if we have not current dir
ui.setSorting("itemCount")
assert.Equal(t, "size", ui.sortBy)
}

func getAnalyzedPathWithSorting(sortBy string, sortOrder string, apparentSize bool) *UI {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()
Expand Down
4 changes: 4 additions & 0 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ func (ui *UI) showErr(msg string, err error) {
}

func (ui *UI) setSorting(newOrder string) {
if ui.currentDir == nil {
return
}

if newOrder == ui.sortBy {
if ui.sortOrder == "asc" {
ui.sortOrder = "desc"
Expand Down

0 comments on commit 49eb695

Please sign in to comment.