From 18283ad41bbb2ac17c8482ddaaf0f31ea9d38a26 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Dec 2021 21:08:36 +1100 Subject: add popup handler for easier testing --- pkg/gui/confirmation_panel.go | 34 ++------------- pkg/gui/context_test.go | 6 +-- pkg/gui/diff_context_size_test.go | 47 ++++++++++++++------- pkg/gui/dummies.go | 8 ++-- pkg/gui/gui.go | 3 ++ pkg/gui/popup_handler.go | 87 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 51 deletions(-) create mode 100644 pkg/gui/popup_handler.go (limited to 'pkg/gui') diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index e1b78bc3a..c540aa0d0 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -1,5 +1,3 @@ -// lots of this has been directly ported from one of the example files, will brush up later - package gui import ( @@ -7,7 +5,6 @@ import ( "strings" "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" @@ -44,30 +41,15 @@ type promptOpts struct { } func (gui *Gui) ask(opts askOpts) error { - return gui.createPopupPanel(createPopupPanelOpts{ - title: opts.title, - prompt: opts.prompt, - handleConfirm: opts.handleConfirm, - handleClose: opts.handleClose, - handlersManageFocus: opts.handlersManageFocus, - }) + return gui.PopupHandler.Ask(opts) } func (gui *Gui) prompt(opts promptOpts) error { - return gui.createPopupPanel(createPopupPanelOpts{ - title: opts.title, - prompt: opts.initialContent, - editable: true, - handleConfirmPrompt: opts.handleConfirm, - findSuggestionsFunc: opts.findSuggestionsFunc, - }) + return gui.PopupHandler.Prompt(opts) } func (gui *Gui) createLoaderPanel(prompt string) error { - return gui.createPopupPanel(createPopupPanelOpts{ - prompt: prompt, - hasLoader: true, - }) + return gui.PopupHandler.Loader(prompt) } func (gui *Gui) wrappedConfirmationFunction(handlersManageFocus bool, function func() error) func() error { @@ -339,15 +321,7 @@ func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View) } func (gui *Gui) createErrorPanel(message string) error { - coloredMessage := style.FgRed.Sprint(strings.TrimSpace(message)) - if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil { - return err - } - - return gui.ask(askOpts{ - title: gui.Tr.Error, - prompt: coloredMessage, - }) + return gui.PopupHandler.Error(message) } func (gui *Gui) surfaceError(err error) error { diff --git a/pkg/gui/context_test.go b/pkg/gui/context_test.go index abfcab4cd..7f03f7484 100644 --- a/pkg/gui/context_test.go +++ b/pkg/gui/context_test.go @@ -8,7 +8,7 @@ import ( ) func TestCanDeactivatePopupContextsWithoutViews(t *testing.T) { - contexts := []func(gui *Gui) Context { + contexts := []func(gui *Gui) Context{ func(gui *Gui) Context { return gui.State.Contexts.Credentials }, func(gui *Gui) Context { return gui.State.Contexts.Confirmation }, func(gui *Gui) Context { return gui.State.Contexts.CommitMessage }, @@ -20,7 +20,7 @@ func TestCanDeactivatePopupContextsWithoutViews(t *testing.T) { context := c(gui) gui.g = &gocui.Gui{} - gui.deactivateContext(context) + _ = gui.deactivateContext(context) // This really only checks a prerequisit, not the effect of deactivateContext view, _ := gui.g.View(context.GetViewName()) @@ -32,7 +32,7 @@ func TestCanDeactivateCommitFilesContextsWithoutViews(t *testing.T) { gui := NewDummyGui() gui.g = &gocui.Gui{} - gui.deactivateContext(gui.State.Contexts.CommitFiles) + _ = gui.deactivateContext(gui.State.Contexts.CommitFiles) // This really only checks a prerequisite, not the effect of deactivateContext view, _ := gui.g.View(gui.State.Contexts.CommitFiles.GetViewName()) diff --git a/pkg/gui/diff_context_size_test.go b/pkg/gui/diff_context_size_test.go index bd5758bf7..6007228a4 100644 --- a/pkg/gui/diff_context_size_test.go +++ b/pkg/gui/diff_context_size_test.go @@ -28,7 +28,7 @@ func setupGuiForTest(gui *Gui) { gui.Views.Main, _ = gui.prepareView("main") gui.Views.Secondary, _ = gui.prepareView("secondary") gui.GitCommand.PatchManager = &patch.PatchManager{} - gui.refreshLineByLinePanel(diffForTest, "", false, 11) + _, _ = gui.refreshLineByLinePanel(diffForTest, "", false, 11) } func TestIncreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) { @@ -47,9 +47,9 @@ func TestIncreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) { context := c(gui) setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 1 - gui.pushContextDirect(context) + _ = gui.pushContextDirect(context) - gui.IncreaseContextInDiffView() + _ = gui.IncreaseContextInDiffView() assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) } @@ -73,9 +73,9 @@ func TestDoesntIncreaseContextInDiffViewInContextWithoutDiff(t *testing.T) { context := c(gui) setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 1 - gui.pushContextDirect(context) + _ = gui.pushContextDirect(context) - gui.IncreaseContextInDiffView() + _ = gui.IncreaseContextInDiffView() assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) } @@ -97,9 +97,9 @@ func TestDecreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) { context := c(gui) setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 2 - gui.pushContextDirect(context) + _ = gui.pushContextDirect(context) - gui.DecreaseContextInDiffView() + _ = gui.DecreaseContextInDiffView() assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) } @@ -123,9 +123,9 @@ func TestDoesntDecreaseContextInDiffViewInContextWithoutDiff(t *testing.T) { context := c(gui) setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 2 - gui.pushContextDirect(context) + _ = gui.pushContextDirect(context) - gui.DecreaseContextInDiffView() + _ = gui.DecreaseContextInDiffView() assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey())) } @@ -135,11 +135,21 @@ func TestDoesntIncreaseContextInDiffViewInContextWhenInPatchBuildingMode(t *test gui := NewDummyGui() setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 2 - gui.pushContextDirect(gui.State.Contexts.CommitFiles) + _ = gui.pushContextDirect(gui.State.Contexts.CommitFiles) gui.GitCommand.PatchManager.Start("from", "to", false, false) - gui.IncreaseContextInDiffView() + errorCount := 0 + gui.PopupHandler = &TestPopupHandler{ + onError: func(message string) error { + assert.Equal(t, gui.Tr.CantChangeContextSizeError, message) + errorCount += 1 + return nil + }, + } + + _ = gui.IncreaseContextInDiffView() + assert.Equal(t, 1, errorCount) assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize) } @@ -147,10 +157,19 @@ func TestDoesntDecreaseContextInDiffViewInContextWhenInPatchBuildingMode(t *test gui := NewDummyGui() setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 2 - gui.pushContextDirect(gui.State.Contexts.CommitFiles) + _ = gui.pushContextDirect(gui.State.Contexts.CommitFiles) gui.GitCommand.PatchManager.Start("from", "to", false, false) - gui.DecreaseContextInDiffView() + errorCount := 0 + gui.PopupHandler = &TestPopupHandler{ + onError: func(message string) error { + assert.Equal(t, gui.Tr.CantChangeContextSizeError, message) + errorCount += 1 + return nil + }, + } + + _ = gui.DecreaseContextInDiffView() assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize) } @@ -160,7 +179,7 @@ func TestDecreasesContextInDiffViewNoFurtherThanOne(t *testing.T) { setupGuiForTest(gui) gui.Config.GetUserConfig().Git.DiffContextSize = 1 - gui.DecreaseContextInDiffView() + _ = gui.DecreaseContextInDiffView() assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize) } diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go index 0e37bbbc8..3c993836a 100644 --- a/pkg/gui/dummies.go +++ b/pkg/gui/dummies.go @@ -12,12 +12,12 @@ import ( // NewDummyGui creates a new dummy GUI for testing func NewDummyUpdater() *updates.Updater { newAppConfig := config.NewDummyAppConfig() - DummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language)) - return DummyUpdater + dummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language)) + return dummyUpdater } func NewDummyGui() *Gui { newAppConfig := config.NewDummyAppConfig() - DummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false) - return DummyGui + dummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false) + return dummyGui } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 35b497fc9..4a2248935 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -121,6 +121,8 @@ type Gui struct { ShowExtrasWindow bool suggestionsAsyncHandler *tasks.AsyncHandler + + PopupHandler PopupHandler } type listPanelState struct { @@ -455,6 +457,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom onRunCommand := gui.GetOnRunCommand() oSCommand.SetOnRunCommand(onRunCommand) gui.OnRunCommand = onRunCommand + gui.PopupHandler = &RealPopupHandler{gui: gui} authors.SetCustomAuthors(gui.Config.GetUserConfig().Gui.AuthorColors) diff --git a/pkg/gui/popup_handler.go b/pkg/gui/popup_handler.go new file mode 100644 index 000000000..9cacc3574 --- /dev/null +++ b/pkg/gui/popup_handler.go @@ -0,0 +1,87 @@ +package gui + +import ( + "strings" + + "github.com/jesseduffield/lazygit/pkg/gui/style" +) + +type PopupHandler interface { + Error(message string) error + Ask(opts askOpts) error + Prompt(opts promptOpts) error + Loader(message string) error +} + +type RealPopupHandler struct { + gui *Gui +} + +func (self *RealPopupHandler) Error(message string) error { + gui := self.gui + + coloredMessage := style.FgRed.Sprint(strings.TrimSpace(message)) + if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil { + return err + } + + return self.Ask(askOpts{ + title: gui.Tr.Error, + prompt: coloredMessage, + }) +} + +func (self *RealPopupHandler) Ask(opts askOpts) error { + gui := self.gui + + return gui.createPopupPanel(createPopupPanelOpts{ + title: opts.title, + prompt: opts.prompt, + handleConfirm: opts.handleConfirm, + handleClose: opts.handleClose, + handlersManageFocus: opts.handlersManageFocus, + }) +} + +func (self *RealPopupHandler) Prompt(opts promptOpts) error { + gui := self.gui + + return gui.createPopupPanel(createPopupPanelOpts{ + title: opts.title, + prompt: opts.initialContent, + editable: true, + handleConfirmPrompt: opts.handleConfirm, + findSuggestionsFunc: opts.findSuggestionsFunc, + }) +} + +func (self *RealPopupHandler) Loader(message string) error { + gui := self.gui + + return gui.createPopupPanel(createPopupPanelOpts{ + prompt: message, + hasLoader: true, + }) +} + +type TestPopupHandler struct { + onError func(message string) error + onAsk func(opts askOpts) error + onPrompt func(opts promptOpts) error +} + +func (self *TestPopupHandler) Error(message string) error { + return self.onError(message) +} + +func (self *TestPopupHandler) Ask(opts askOpts) error { + return self.onAsk(opts) +} + +func (self *TestPopupHandler) Prompt(opts promptOpts) error { + return self.onPrompt(opts) +} + +func (self *TestPopupHandler) Loader(message string) error { + return nil +} -- cgit v1.2.3