Skip to content

Commit

Permalink
test: more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Feb 13, 2024
1 parent 346d94c commit 6407b17
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 56 deletions.
12 changes: 7 additions & 5 deletions tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func (ui *UI) openItem() {
}
}

func (ui *UI) confirmExport() {
func (ui *UI) confirmExport() *tview.Form {
form := tview.NewForm().
AddInputField("File name", "export.json", 30, nil, func(v string) {
ui.exportName = v
Expand All @@ -423,6 +423,7 @@ func (ui *UI) confirmExport() {
flex := modal(form, 50, 7)
ui.pages.AddPage("export", flex, true, true)
ui.app.SetFocus(form)
return form
}

func (ui *UI) exportAnalysis() {
Expand All @@ -441,6 +442,11 @@ func (ui *UI) exportAnalysis() {
ui.app.SetFocus(ui.table)
}
})
if ui.done != nil {
defer func() {
ui.done <- struct{}{}
}()
}

var buff bytes.Buffer

Expand Down Expand Up @@ -469,9 +475,5 @@ func (ui *UI) exportAnalysis() {
ui.showErrFromGo("Error writting to file", err)
return
}

if ui.done != nil {
ui.done <- struct{}{}
}
}()
}
51 changes: 0 additions & 51 deletions tui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,54 +492,3 @@ func TestExitViewFile(t *testing.T) {

assert.False(t, ui.pages.HasPage("file"))
}

func TestExportAnalysis(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]fs.Item, 0, 1),
}
currentDir := &analyze.Dir{
File: &analyze.File{
Name: "sub",
Parent: parentDir,
},
}

simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

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

assert.Equal(t, "sub", ui.currentDir.GetName())

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'E', 0))

assert.True(t, ui.pages.HasPage("export"))

ui.keyPressed(tcell.NewEventKey(tcell.KeyEnter, 0, 0))

assert.True(t, ui.pages.HasPage("export"))

// we cannot send Enter to the form from here

ui.exportAnalysis()

assert.True(t, ui.pages.HasPage("exporting"))

<-ui.done

assert.FileExists(t, "export.json")
err := os.Remove("export.json")
assert.NoError(t, err)

for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
f()
}
}
203 changes: 203 additions & 0 deletions tui/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package tui

import (
"bytes"
"os"
"testing"

"github.com/dundee/gdu/v5/internal/testanalyze"
"github.com/dundee/gdu/v5/internal/testapp"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/fs"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/stretchr/testify/assert"
)

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

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

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'E', 0))

assert.True(t, ui.pages.HasPage("export"))

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'n', 0))
ui.keyPressed(tcell.NewEventKey(tcell.KeyEnter, 0, 0))

assert.True(t, ui.pages.HasPage("export"))
}

func TestExportAnalysis(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]fs.Item, 0, 1),
}
currentDir := &analyze.Dir{
File: &analyze.File{
Name: "sub",
Parent: parentDir,
},
}

simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

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

ui.exportAnalysis()

assert.True(t, ui.pages.HasPage("exporting"))

<-ui.done

assert.FileExists(t, "export.json")
err := os.Remove("export.json")
assert.NoError(t, err)

for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
f()
}
}

func TestExportAnalysisEsc(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]fs.Item, 0, 1),
}
currentDir := &analyze.Dir{
File: &analyze.File{
Name: "sub",
Parent: parentDir,
},
}

simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

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

form := ui.confirmExport()
formInputFn := form.GetInputCapture()

assert.True(t, ui.pages.HasPage("export"))

formInputFn(tcell.NewEventKey(tcell.KeyEsc, 0, 0))

assert.False(t, ui.pages.HasPage("export"))
}

func TestExportAnalysisWithName(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]fs.Item, 0, 1),
}
currentDir := &analyze.Dir{
File: &analyze.File{
Name: "sub",
Parent: parentDir,
},
}

simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

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

form := ui.confirmExport()
// formInputFn := form.GetInputCapture()
item := form.GetFormItemByLabel("File name")
inputFn := item.(*tview.InputField).InputHandler()

// send 'n' to input
inputFn(tcell.NewEventKey(tcell.KeyRune, 'n', 0), nil)
assert.Equal(t, "export.jsonn", ui.exportName)

assert.True(t, ui.pages.HasPage("export"))

form.GetButton(0).InputHandler()(tcell.NewEventKey(tcell.KeyEnter, 0, 0), nil)

assert.True(t, ui.pages.HasPage("exporting"))

<-ui.done

assert.FileExists(t, "export.jsonn")
err := os.Remove("export.jsonn")
assert.NoError(t, err)

for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
f()
}
}

func TestExportAnalysisWithoutRights(t *testing.T) {
parentDir := &analyze.Dir{
File: &analyze.File{
Name: "parent",
},
Files: make([]fs.Item, 0, 1),
}
currentDir := &analyze.Dir{
File: &analyze.File{
Name: "sub",
Parent: parentDir,
},
}

_, err := os.Create("export.json")
assert.NoError(t, err)
err = os.Chmod("export.json", 0)
assert.NoError(t, err)
defer func() {
err = os.Chmod("export.json", 0755)
assert.Nil(t, err)
err = os.Remove("export.json")
assert.NoError(t, err)
}()

simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

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

ui.exportAnalysis()

<-ui.done

for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
f()
}

assert.True(t, ui.pages.HasPage("error"))
}

0 comments on commit 6407b17

Please sign in to comment.