diff --git a/pkg/gui/gui_driver.go b/pkg/gui/gui_driver.go index 860c6c9b8..7df2b95ba 100644 --- a/pkg/gui/gui_driver.go +++ b/pkg/gui/gui_driver.go @@ -71,3 +71,11 @@ func (self *GuiDriver) LogUI(message string) { func (self *GuiDriver) CheckedOutRef() *models.Branch { return self.gui.helpers.Refs.GetCheckedOutRef() } + +func (self *GuiDriver) MainView() *gocui.View { + return self.gui.mainView() +} + +func (self *GuiDriver) SecondaryView() *gocui.View { + return self.gui.secondaryView() +} diff --git a/pkg/integration/components/assert.go b/pkg/integration/components/assert.go index 584ad438b..dcfd00615 100644 --- a/pkg/integration/components/assert.go +++ b/pkg/integration/components/assert.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/types" integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" + "golang.org/x/exp/constraints" ) // through this struct we assert on the state of the lazygit gui @@ -19,6 +20,43 @@ func NewAssert(gui integrationTypes.GuiDriver) *Assert { return &Assert{gui: gui} } +// for making assertions on string values +type matcher[T any] struct { + testFn func(T) (bool, string) + prefix string +} + +func (self *matcher[T]) test(value T) (bool, string) { + ok, message := self.testFn(value) + if ok { + return true, "" + } + + if self.prefix != "" { + return false, self.prefix + " " + message + } + + return false, message +} + +func (self *matcher[T]) context(prefix string) *matcher[T] { + self.prefix = prefix + + return self +} + +func Contains(target string) *matcher[string] { + return &matcher[string]{testFn: func(value string) (bool, string) { + return strings.Contains(value, target), fmt.Sprintf("Expected '%s' to contain '%s'", value, target) + }} +} + +func Equals[T constraints.Ordered](target T) *matcher[T] { + return &matcher[T]{testFn: func(value T) (bool, string) { + return target == value, fmt.Sprintf("Expected '%T' to equal '%T'", value, target) + }} +} + func (self *Assert) WorkingTreeFileCount(expectedCount int) { self.assertWithRetries(func() (bool, string) { actualCount := len(self.gui.Model().Files) @@ -41,22 +79,16 @@ func (self *Assert) CommitCount(expectedCount int) { }) } -func (self *Assert) HeadCommitMessage(expectedMessage string) { +func (self *Assert) MatchHeadCommitMessage(matcher *matcher[string]) { self.assertWithRetries(func() (bool, string) { - if len(self.gui.Model().Commits) == 0 { - return false, "Expected at least one commit to be present" - } - - headCommit := self.gui.Model().Commits[0] - if headCommit.Name != expectedMessage { - return false, fmt.Sprintf( - "Expected commit message to be '%s', but got '%s'", - expectedMessage, headCommit.Name, - ) - } - - return true, "" + return len(self.gui.Model().Commits) == 0, "Expected at least one commit to be present" }) + + self.matchString(matcher, "Unexpected commit message.", + func() string { + return self.gui.Model().Commits[0].Name + }, + ) } func (self *Assert) CurrentViewName(expectedViewName string) { @@ -81,10 +113,70 @@ func (self *Assert) InListContext() { }) } -func (self *Assert) SelectedLineContains(text string) { +func (self *Assert) MatchSelectedLine(matcher *matcher[string]) { + self.matchString(matcher, "Unexpected selected line.", + func() string { + return self.gui.CurrentContext().GetView().SelectedLine() + }, + ) +} + +func (self *Assert) InPrompt() { self.assertWithRetries(func() (bool, string) { - line := self.gui.CurrentContext().GetView().SelectedLine() - return strings.Contains(line, text), fmt.Sprintf("Expected selected line to contain '%s', but got '%s'", text, line) + currentView := self.gui.CurrentContext().GetView() + return currentView.Name() == "confirmation" && currentView.Editable, fmt.Sprintf("Expected prompt popup to be focused") + }) +} + +func (self *Assert) InConfirm() { + self.assertWithRetries(func() (bool, string) { + currentView := self.gui.CurrentContext().GetView() + return currentView.Name() == "confirmation" && !currentView.Editable, fmt.Sprintf("Expected confirmation popup to be focused") + }) +} + +func (self *Assert) InAlert() { + // basically the same thing as a confirmation popup with the current implementation + self.assertWithRetries(func() (bool, string) { + currentView := self.gui.CurrentContext().GetView() + return currentView.Name() == "confirmation" && !currentView.Editable, fmt.Sprintf("Expected alert popup to be focused") + }) +} + +func (self *Assert) InMenu() { + self.assertWithRetries(func() (bool, string) { + return self.gui.CurrentContext().GetView().Name() == "menu", fmt.Sprintf("Expected popup menu to be focused") + }) +} + +func (self *Assert) MatchCurrentViewTitle(matcher *matcher[string]) { + self.matchString(matcher, "Unexpected current view title.", + func() string { + return self.gui.CurrentContext().GetView().Title + }, + ) +} + +func (self *Assert) MatchMainViewContent(matcher *matcher[string]) { + self.matchString(matcher, "Unexpected main view content.", + func() string { + return self.gui.MainView().Buffer() + }, + ) +} + +func (self *Assert) MatchSecondaryViewContent(matcher *matcher[string]) { + self.matchString(matcher, "Unexpected secondary view title.", + func() string { + return self.gui.SecondaryView().Buffer() + }, + ) +} + +func (self *Assert) matchString(matcher *matcher[string], context string, getValue func() string) { + self.assertWithRetries(func() (bool, string) { + value := getValue() + return matcher.context(context).test(value) }) } diff --git a/pkg/integration/components/input.go b/pkg/integration/components/input.go index d44b11830..63361e5c9 100644 --- a/pkg/integration/components/input.go +++ b/pkg/integration/components/input.go @@ -93,7 +93,7 @@ func (self *Input) PreviousItem() { func (self *Input) ContinueMerge() { self.PressKeys(self.keys.Universal.CreateRebaseOptionsMenu) - self.assert.SelectedLineContains("continue") + self.assert.MatchSelectedLine(Contains("continue")) self.Confirm() } diff --git a/pkg/integration/components/test_test.go b/pkg/integration/components/test_test.go index de8dac8e4..e180bfccb 100644 --- a/pkg/integration/components/test_test.go +++ b/pkg/integration/components/test_test.go @@ -3,6 +3,7 @@ package components import ( "testing" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -47,6 +48,14 @@ func (self *fakeGuiDriver) CheckedOutRef() *models.Branch { return nil } +func (self *fakeGuiDriver) MainView() *gocui.View { + return nil +} + +func (self *fakeGuiDriver) SecondaryView() *gocui.View { + return nil +} + func TestAssertionFailure(t *testing.T) { test := NewIntegrationTest(NewIntegrationTestArgs{ Description: unitTestDescription, diff --git a/pkg/integration/tests/commit/commit.go b/pkg/integration/tests/commit/commit.go index 12a68925d..0c3fc484c 100644 --- a/pkg/integration/tests/commit/commit.go +++ b/pkg/integration/tests/commit/commit.go @@ -2,19 +2,19 @@ package commit import ( "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/integration/components" + . "github.com/jesseduffield/lazygit/pkg/integration/components" ) -var Commit = components.NewIntegrationTest(components.NewIntegrationTestArgs{ +var Commit = NewIntegrationTest(NewIntegrationTestArgs{ Description: "Staging a couple files and committing", ExtraCmdArgs: "", Skip: false, SetupConfig: func(config *config.AppConfig) {}, - SetupRepo: func(shell *components.Shell) { + SetupRepo: func(shell *Shell) { shell.CreateFile("myfile", "myfile content") shell.CreateFile("myfile2", "myfile2 content") }, - Run: func(shell *components.Shell, input *components.Input, assert *components.Assert, keys config.KeybindingConfig) { + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { assert.CommitCount(0) input.Select() @@ -27,6 +27,6 @@ var Commit = components.NewIntegrationTest(components.NewIntegrationTestArgs{ input.Confirm() assert.CommitCount(1) - assert.HeadCommitMessage(commitMessage) + assert.MatchHeadCommitMessage(Equals(commitMessage)) }, }) diff --git a/pkg/integration/tests/commit/new_branch.go b/pkg/integration/tests/commit/new_branch.go index ad96938f5..ea784791d 100644 --- a/pkg/integration/tests/commit/new_branch.go +++ b/pkg/integration/tests/commit/new_branch.go @@ -2,21 +2,21 @@ package commit import ( "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/integration/components" + . "github.com/jesseduffield/lazygit/pkg/integration/components" ) -var NewBranch = components.NewIntegrationTest(components.NewIntegrationTestArgs{ +var NewBranch = NewIntegrationTest(NewIntegrationTestArgs{ Description: "Creating a new branch from a commit", ExtraCmdArgs: "", Skip: false, SetupConfig: func(config *config.AppConfig) {}, - SetupRepo: func(shell *components.Shell) { + SetupRepo: func(shell *Shell) { shell. EmptyCommit("commit 1"). EmptyCommit("commit 2"). EmptyCommit("commit 3") }, - Run: func(shell *components.Shell, input *components.Input, assert *components.Assert, keys config.KeybindingConfig) { + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { assert.CommitCount(3) input.SwitchToCommitsWindow() @@ -32,7 +32,7 @@ var NewBranch = components.NewIntegrationTest(components.NewIntegrationTestArgs{ input.Confirm() assert.CommitCount(2) - assert.HeadCommitMessage("commit 2") + assert.MatchHeadCommitMessage(Contains("commit 2")) assert.CurrentBranchName(branchName) }, }) diff --git a/pkg/integration/tests/custom_commands/basic.go b/pkg/integration/tests/custom_commands/basic.go index 5961dc0c5..8a7d67246 100644 --- a/pkg/integration/tests/custom_commands/basic.go +++ b/pkg/integration/tests/custom_commands/basic.go @@ -2,14 +2,14 @@ package custom_commands import ( "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/integration/components" + . "github.com/jesseduffield/lazygit/pkg/integration/components" ) -var Basic = components.NewIntegrationTest(components.NewIntegrationTestArgs{ +var Basic = NewIntegrationTest(NewIntegrationTestArgs{ Description: "Using a custom command to create a new file", ExtraCmdArgs: "", Skip: false, - SetupRepo: func(shell *components.Shell) {}, + SetupRepo: func(shell *Shell) {}, SetupConfig: func(cfg *config.AppConfig) { cfg.UserConfig.CustomCommands = []config.CustomCommand{ { @@ -20,15 +20,15 @@ var Basic = components.NewIntegrationTest(components.NewIntegrationTestArgs{ } }, Run: func( - shell *components.Shell, - input *components.Input, - assert *components.Assert, + shell *Shell, + input *Input, + assert *Assert, keys config.KeybindingConfig, ) { assert.WorkingTreeFileCount(0) input.PressKeys("a") assert.WorkingTreeFileCount(1) - assert.SelectedLineContains("myfile") + assert.MatchSelectedLine(Contains("myfile")) }, }) diff --git a/pkg/integration/tests/custom_commands/multiple_prompts.go b/pkg/integration/tests/custom_commands/multiple_prompts.go new file mode 100644 index 000000000..885dc8575 --- /dev/null +++ b/pkg/integration/tests/custom_commands/multiple_prompts.go @@ -0,0 +1,84 @@ +package custom_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var MultiplePrompts = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Using a custom command with multiple prompts", + ExtraCmdArgs: "", + Skip: false, + SetupRepo: func(shell *Shell) {}, + SetupConfig: func(cfg *config.AppConfig) { + cfg.UserConfig.CustomCommands = []config.CustomCommand{ + { + Key: "a", + Context: "files", + Command: `echo "{{index .PromptResponses 1}}" > {{index .PromptResponses 0}}`, + Prompts: []config.CustomCommandPrompt{ + { + Type: "input", + Title: "Enter a file name", + }, + { + Type: "menu", + Title: "Choose file content", + Options: []config.CustomCommandMenuOption{ + { + Name: "foo", + Description: "Foo", + Value: "FOO", + }, + { + Name: "bar", + Description: "Bar", + Value: "BAR", + }, + { + Name: "baz", + Description: "Baz", + Value: "BAZ", + }, + }, + }, + { + Type: "confirm", + Title: "Are you sure?", + Body: "Are you REALLY sure you want to make this file? Up to you buddy.", + }, + }, + }, + } + }, + Run: func( + shell *Shell, + input *Input, + assert *Assert, + keys config.KeybindingConfig, + ) { + assert.WorkingTreeFileCount(0) + + input.PressKeys("a") + + assert.InPrompt() + assert.MatchCurrentViewTitle(Equals("Enter a file name")) + input.Type("myfile") + input.Confirm() + + assert.InMenu() + assert.MatchCurrentViewTitle(Equals("Choose file content")) + assert.MatchSelectedLine(Contains("foo")) + input.NextItem() + assert.MatchSelectedLine(Contains("bar")) + input.Confirm() + + assert.InConfirm() + assert.MatchCurrentViewTitle(Equals("Are you sure?")) + input.Confirm() + + assert.WorkingTreeFileCount(1) + assert.MatchSelectedLine(Contains("myfile")) + assert.MatchMainViewContent(Contains("BAR")) + }, +}) diff --git a/pkg/integration/tests/interactive_rebase/one.go b/pkg/integration/tests/interactive_rebase/one.go index 3c785a727..014a4b8c9 100644 --- a/pkg/integration/tests/interactive_rebase/one.go +++ b/pkg/integration/tests/interactive_rebase/one.go @@ -2,37 +2,37 @@ package interactive_rebase import ( "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/integration/components" + . "github.com/jesseduffield/lazygit/pkg/integration/components" ) -var One = components.NewIntegrationTest(components.NewIntegrationTestArgs{ +var One = NewIntegrationTest(NewIntegrationTestArgs{ Description: "Begins an interactive rebase, then fixups, drops, and squashes some commits", ExtraCmdArgs: "", Skip: false, SetupConfig: func(config *config.AppConfig) {}, - SetupRepo: func(shell *components.Shell) { + SetupRepo: func(shell *Shell) { shell. CreateNCommits(5) // these will appears at commit 05, 04, 04, down to 01 }, - Run: func(shell *components.Shell, input *components.Input, assert *components.Assert, keys config.KeybindingConfig) { + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { input.SwitchToCommitsWindow() assert.CurrentViewName("commits") input.NavigateToListItemContainingText("commit 02") input.PressKeys(keys.Universal.Edit) - assert.SelectedLineContains("YOU ARE HERE") + assert.MatchSelectedLine(Contains("YOU ARE HERE")) input.PreviousItem() input.PressKeys(keys.Commits.MarkCommitAsFixup) - assert.SelectedLineContains("fixup") + assert.MatchSelectedLine(Contains("fixup")) input.PreviousItem() input.PressKeys(keys.Universal.Remove) - assert.SelectedLineContains("drop") + assert.MatchSelectedLine(Contains("drop")) input.PreviousItem() input.PressKeys(keys.Commits.SquashDown) - assert.SelectedLineContains("squash") + assert.MatchSelectedLine(Contains("squash")) input.ContinueRebase() diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go index 704c2fd5b..c681dd3b7 100644 --- a/pkg/integration/tests/tests.go +++ b/pkg/integration/tests/tests.go @@ -17,4 +17,5 @@ var Tests = []*components.IntegrationTest{ branch.Suggestions, interactive_rebase.One, custom_commands.Basic, + custom_commands.MultiplePrompts, } diff --git a/pkg/integration/types/types.go b/pkg/integration/types/types.go index 543212d59..b5ee2ca68 100644 --- a/pkg/integration/types/types.go +++ b/pkg/integration/types/types.go @@ -1,6 +1,7 @@ package types import ( + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -28,4 +29,9 @@ type GuiDriver interface { // logs in the actual UI (in the commands panel) LogUI(message string) CheckedOutRef() *models.Branch + // the view that appears to the right of the side panel + MainView() *gocui.View + // the other view that sometimes appears to the right of the side panel + // e.g. when we're showing both staged and unstaged changes + SecondaryView() *gocui.View } diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/FETCH_HEAD b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/FETCH_HEAD new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/HEAD b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/config b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/config new file mode 100644 index 000000000..8ae104545 --- /dev/null +++ b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/config @@ -0,0 +1,10 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true +[user] + email = CI@example.com + name = CI diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/description b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/info/exclude b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/info/exclude new file mode 100644 index 000000000..8e9f2071f --- /dev/null +++ b/test/integration_new/custom_commands/multiple_prompts/expected/repo/.git_keep/info/exclude @@ -0,0 +1,7 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ +.DS_Store diff --git a/test/integration_new/custom_commands/multiple_prompts/expected/repo/myfile b/test/integration_new/custom_commands/multiple_prompts/expected/repo/myfile new file mode 100644 index 000000000..ba578e48b --- /dev/null +++ b/test/integration_new/custom_commands/multiple_prompts/expected/repo/myfile @@ -0,0 +1 @@ +BAR