From 6407b175a6d9693931e159769292634d36513b15 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 13 Feb 2024 23:27:03 +0100 Subject: [PATCH] test: more coverage --- tui/actions.go | 12 +-- tui/actions_test.go | 51 ----------- tui/export_test.go | 203 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 210 insertions(+), 56 deletions(-) create mode 100644 tui/export_test.go diff --git a/tui/actions.go b/tui/actions.go index c12c987c3..9926734d7 100644 --- a/tui/actions.go +++ b/tui/actions.go @@ -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 @@ -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() { @@ -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 @@ -469,9 +475,5 @@ func (ui *UI) exportAnalysis() { ui.showErrFromGo("Error writting to file", err) return } - - if ui.done != nil { - ui.done <- struct{}{} - } }() } diff --git a/tui/actions_test.go b/tui/actions_test.go index 388272814..c64212797 100644 --- a/tui/actions_test.go +++ b/tui/actions_test.go @@ -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() - } -} diff --git a/tui/export_test.go b/tui/export_test.go new file mode 100644 index 000000000..52fd22cc7 --- /dev/null +++ b/tui/export_test.go @@ -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")) +}