1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

more refactoring of popup stuff

This commit is contained in:
Jesse Duffield
2022-12-27 11:34:41 +11:00
parent 8052ac4fd6
commit 926ed7b9b2
23 changed files with 173 additions and 67 deletions

View File

@ -0,0 +1,48 @@
package components
type AlertAsserter struct {
assert *Assert
input *Input
hasCheckedTitle bool
hasCheckedContent bool
}
func (self *AlertAsserter) getViewAsserter() *ViewAsserter {
return self.assert.View("confirmation")
}
// asserts that the alert view has the expected title
func (self *AlertAsserter) Title(expected *matcher) *AlertAsserter {
self.getViewAsserter().Title(expected)
self.hasCheckedTitle = true
return self
}
// asserts that the alert view has the expected content
func (self *AlertAsserter) Content(expected *matcher) *AlertAsserter {
self.getViewAsserter().Content(expected)
self.hasCheckedContent = true
return self
}
func (self *AlertAsserter) Confirm() {
self.checkNecessaryChecksCompleted()
self.input.Confirm()
}
func (self *AlertAsserter) Cancel() {
self.checkNecessaryChecksCompleted()
self.input.Press(self.input.keys.Universal.Return)
}
func (self *AlertAsserter) checkNecessaryChecksCompleted() {
if !self.hasCheckedContent || !self.hasCheckedTitle {
self.assert.Fail("You must both check the content and title of a confirmation popup by calling Title()/Content() before calling Confirm()/Cancel().")
}
}

View File

@ -29,20 +29,16 @@ func (self *ConfirmationAsserter) Content(expected *matcher) *ConfirmationAssert
return self
}
func (self *ConfirmationAsserter) Confirm() *ConfirmationAsserter {
func (self *ConfirmationAsserter) Confirm() {
self.checkNecessaryChecksCompleted()
self.input.Confirm()
return self
}
func (self *ConfirmationAsserter) Cancel() *ConfirmationAsserter {
func (self *ConfirmationAsserter) Cancel() {
self.checkNecessaryChecksCompleted()
self.input.Press(self.input.keys.Universal.Return)
return self
}
func (self *ConfirmationAsserter) checkNecessaryChecksCompleted() {

View File

@ -227,28 +227,14 @@ func (self *Input) Prompt() *PromptAsserter {
return &PromptAsserter{assert: self.assert, input: self}
}
// type some text into a prompt, then switch to the suggestions panel and expect the first
// item to match the given matcher, then confirm that item.
func (self *Input) Typeahead(title *matcher, textToType string, expectedFirstOption *matcher) {
self.assert.InPrompt()
self.assert.CurrentView().Title(title)
self.Type(textToType)
self.Press(self.keys.Universal.TogglePanel)
self.assert.CurrentView().Name("suggestions")
self.assert.CurrentView().SelectedLine(expectedFirstOption)
self.Confirm()
func (self *Input) Alert() *AlertAsserter {
self.assert.InAlert()
return &AlertAsserter{assert: self.assert, input: self}
}
func (self *Input) Menu(title *matcher, optionToSelect *matcher) {
func (self *Input) Menu() *MenuAsserter {
self.assert.InMenu()
self.assert.CurrentView().Title(title)
self.NavigateToListItem(optionToSelect)
self.Confirm()
}
func (self *Input) Alert(title *matcher, content *matcher) {
self.assert.InListContext()
self.assert.CurrentView().Title(title)
self.assert.CurrentView().Content(content)
self.Confirm()
return &MenuAsserter{assert: self.assert, input: self}
}

View File

@ -0,0 +1,44 @@
package components
type MenuAsserter struct {
assert *Assert
input *Input
hasCheckedTitle bool
}
func (self *MenuAsserter) getViewAsserter() *ViewAsserter {
return self.assert.View("menu")
}
// asserts that the popup has the expected title
func (self *MenuAsserter) Title(expected *matcher) *MenuAsserter {
self.getViewAsserter().Title(expected)
self.hasCheckedTitle = true
return self
}
func (self *MenuAsserter) Confirm() {
self.checkNecessaryChecksCompleted()
self.input.Confirm()
}
func (self *MenuAsserter) Cancel() {
self.checkNecessaryChecksCompleted()
self.input.Press(self.input.keys.Universal.Return)
}
func (self *MenuAsserter) Select(option *matcher) *MenuAsserter {
self.input.NavigateToListItem(option)
return self
}
func (self *MenuAsserter) checkNecessaryChecksCompleted() {
if !self.hasCheckedTitle {
self.assert.Fail("You must check the title of a menu popup by calling Title() before calling Confirm()/Cancel().")
}
}

View File

@ -26,22 +26,6 @@ func (self *PromptAsserter) InitialText(expected *matcher) *PromptAsserter {
return self
}
func (self *PromptAsserter) Confirm() *PromptAsserter {
self.checkNecessaryChecksCompleted()
self.input.Confirm()
return self
}
func (self *PromptAsserter) Cancel() *PromptAsserter {
self.checkNecessaryChecksCompleted()
self.input.Press(self.input.keys.Universal.Return)
return self
}
func (self *PromptAsserter) Type(value string) *PromptAsserter {
self.input.Type(value)
@ -52,8 +36,48 @@ func (self *PromptAsserter) Clear() *PromptAsserter {
panic("Clear method not yet implemented!")
}
func (self *PromptAsserter) Confirm() {
self.checkNecessaryChecksCompleted()
self.input.Confirm()
}
func (self *PromptAsserter) Cancel() {
self.checkNecessaryChecksCompleted()
self.input.Press(self.input.keys.Universal.Return)
}
func (self *PromptAsserter) checkNecessaryChecksCompleted() {
if !self.hasCheckedTitle {
self.assert.Fail("You must check the title of a prompt popup by calling Title() before calling Confirm()/Cancel().")
}
}
func (self *PromptAsserter) SuggestionLines(matchers ...*matcher) *PromptAsserter {
self.assert.View("suggestions").Lines(matchers...)
return self
}
func (self *PromptAsserter) SuggestionTopLines(matchers ...*matcher) *PromptAsserter {
self.assert.View("suggestions").TopLines(matchers...)
return self
}
func (self *PromptAsserter) SelectFirstSuggestion() *PromptAsserter {
self.input.Press(self.input.keys.Universal.TogglePanel)
self.assert.CurrentView().Name("suggestions")
return self
}
func (self *PromptAsserter) SelectSuggestion(matcher *matcher) *PromptAsserter {
self.input.Press(self.input.keys.Universal.TogglePanel)
self.assert.CurrentView().Name("suggestions")
self.input.NavigateToListItem(matcher)
return self
}

View File

@ -22,12 +22,12 @@ var Basic = NewIntegrationTest(NewIntegrationTestArgs{
) {
markCommitAsBad := func() {
input.Press(keys.Commits.ViewBisectOptions)
input.Menu(Equals("Bisect"), MatchesRegexp(`mark .* as bad`))
input.Menu().Title(Equals("Bisect")).Select(MatchesRegexp(`mark .* as bad`)).Confirm()
}
markCommitAsGood := func() {
input.Press(keys.Commits.ViewBisectOptions)
input.Menu(Equals("Bisect"), MatchesRegexp(`mark .* as good`))
input.Menu().Title(Equals("Bisect")).Select(MatchesRegexp(`mark .* as good`)).Confirm()
}
assert.AtLeastOneCommit()
@ -62,7 +62,7 @@ var Basic = NewIntegrationTest(NewIntegrationTestArgs{
markCommitAsGood()
// commit 5 is the culprit because we marked 4 as good and 5 as bad.
input.Alert(Equals("Bisect complete"), MatchesRegexp("(?s)commit 05.*Do you want to reset"))
input.Alert().Title(Equals("Bisect complete")).Content(MatchesRegexp("(?s)commit 05.*Do you want to reset")).Confirm()
assert.CurrentView().Name("commits").Content(Contains("commit 04"))
assert.View("information").Content(DoesNotContain("bisecting"))

View File

@ -40,9 +40,9 @@ var FromOtherBranch = NewIntegrationTest(NewIntegrationTestArgs{
input.NextItem()
input.Press(keys.Commits.ViewBisectOptions)
input.Menu(Equals("Bisect"), MatchesRegexp(`mark .* as good`))
input.Menu().Title(Equals("Bisect")).Select(MatchesRegexp(`mark .* as good`)).Confirm()
input.Alert(Equals("Bisect complete"), MatchesRegexp(`(?s)commit 08.*Do you want to reset`))
input.Alert().Title(Equals("Bisect complete")).Content(MatchesRegexp("(?s)commit 08.*Do you want to reset")).Confirm()
assert.View("information").Content(DoesNotContain("bisecting"))

View File

@ -30,7 +30,7 @@ var CheckoutByName = NewIntegrationTest(NewIntegrationTestArgs{
input.Prompt().Title(Equals("Branch name:")).Type("new-branch").Confirm()
input.Alert(Equals("Branch not found"), Equals("Branch not found. Create a new branch named new-branch?"))
input.Alert().Title(Equals("Branch not found")).Content(Equals("Branch not found. Create a new branch named new-branch?")).Confirm()
assert.CurrentView().Name("localBranches").
Lines(

View File

@ -26,7 +26,7 @@ var Delete = NewIntegrationTest(NewIntegrationTestArgs{
)
input.Press(keys.Universal.Remove)
input.Alert(Equals("Error"), Contains("You cannot delete the checked out branch!"))
input.Alert().Title(Equals("Error")).Content(Contains("You cannot delete the checked out branch!")).Confirm()
input.NextItem()

View File

@ -36,7 +36,7 @@ var Reset = NewIntegrationTest(NewIntegrationTestArgs{
input.Press(keys.Commits.ViewResetOptions)
input.Menu(Contains("reset to other-branch"), Contains("hard reset"))
input.Menu().Title(Contains("reset to other-branch")).Select(Contains("hard reset")).Confirm()
// ensure that we've returned from the menu before continuing
assert.CurrentView().Name("localBranches")

View File

@ -27,7 +27,12 @@ var Suggestions = NewIntegrationTest(NewIntegrationTestArgs{
// we expect the first suggestion to be the branch we want because it most
// closely matches what we typed in
input.Typeahead(Equals("Branch name:"), "branch-to", Contains("branch-to-checkout"))
input.Prompt().
Title(Equals("Branch name:")).
Type("branch-to").
SuggestionTopLines(Contains("branch-to-checkout")).
SelectFirstSuggestion().
Confirm()
assert.CurrentBranchName("branch-to-checkout")
},

View File

@ -58,7 +58,10 @@ var CherryPick = NewIntegrationTest(NewIntegrationTestArgs{
)
input.Press(keys.Commits.PasteCommits)
input.Alert(Equals("Cherry-Pick"), Contains("Are you sure you want to cherry-pick the copied commits onto this branch?"))
input.Alert().
Title(Equals("Cherry-Pick")).
Content(Contains("Are you sure you want to cherry-pick the copied commits onto this branch?")).
Confirm()
assert.CurrentView().Name("commits").Lines(
Contains("four"),

View File

@ -45,7 +45,7 @@ var CherryPickConflicts = NewIntegrationTest(NewIntegrationTestArgs{
)
input.Press(keys.Commits.PasteCommits)
input.Alert(Equals("Cherry-Pick"), Contains("Are you sure you want to cherry-pick the copied commits onto this branch?"))
input.Alert().Title(Equals("Cherry-Pick")).Content(Contains("Are you sure you want to cherry-pick the copied commits onto this branch?")).Confirm()
input.Confirmation().
Title(Equals("Auto-merge failed")).

View File

@ -67,7 +67,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
input.Prompt().Title(Equals("Enter a file name")).Type("my file").Confirm()
input.Menu(Equals("Choose file content"), Contains("bar"))
input.Menu().Title(Equals("Choose file content")).Select(Contains("bar")).Confirm()
input.Confirmation().
Title(Equals("Are you sure?")).

View File

@ -53,7 +53,7 @@ var MenuFromCommand = NewIntegrationTest(NewIntegrationTestArgs{
input.Press("a")
input.Menu(Equals("Choose commit message"), Contains("bar"))
input.Menu().Title(Equals("Choose commit message")).Select(Contains("bar")).Confirm()
input.Prompt().Title(Equals("Description")).Type(" my branch").Confirm()

View File

@ -60,7 +60,7 @@ var MenuFromCommandsOutput = NewIntegrationTest(NewIntegrationTestArgs{
SelectedLine(Equals("branch"))
input.Confirm()
input.Menu(Equals("Branch:"), Equals("master"))
input.Menu().Title(Equals("Branch:")).Select(Equals("master")).Confirm()
assert.CurrentBranchName("master")
},

View File

@ -65,7 +65,7 @@ var MultiplePrompts = NewIntegrationTest(NewIntegrationTestArgs{
input.Prompt().Title(Equals("Enter a file name")).Type("myfile").Confirm()
input.Menu(Equals("Choose file content"), Contains("bar"))
input.Menu().Title(Equals("Choose file content")).Select(Contains("bar")).Confirm()
input.Confirmation().
Title(Equals("Are you sure?")).

View File

@ -29,7 +29,7 @@ var Diff = NewIntegrationTest(NewIntegrationTestArgs{
Contains("branch-b"),
)
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), Contains(`diff branch-a`))
input.Menu().Title(Equals("Diffing")).Select(Contains(`diff branch-a`)).Confirm()
assert.CurrentView().Name("localBranches")
@ -51,7 +51,7 @@ var Diff = NewIntegrationTest(NewIntegrationTestArgs{
assert.CurrentView().Name("localBranches")
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), Contains("reverse diff direction"))
input.Menu().Title(Equals("Diffing")).Select(Contains("reverse diff direction")).Confirm()
assert.View("information").Content(Contains("showing output for: git diff branch-a branch-b -R"))
assert.MainView().Content(Contains("-second line"))
},

View File

@ -30,7 +30,7 @@ var DiffAndApplyPatch = NewIntegrationTest(NewIntegrationTestArgs{
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), Equals("diff branch-a"))
input.Menu().Title(Equals("Diffing")).Select(Equals("diff branch-a")).Confirm()
assert.CurrentView().Name("localBranches")
@ -52,13 +52,13 @@ var DiffAndApplyPatch = NewIntegrationTest(NewIntegrationTestArgs{
input.PrimaryAction()
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), Contains("exit diff mode"))
input.Menu().Title(Equals("Diffing")).Select(Contains("exit diff mode")).Confirm()
assert.View("information").Content(DoesNotContain("building patch"))
input.Press(keys.Universal.CreatePatchOptionsMenu)
// adding the regex '$' here to distinguish the menu item from the 'apply patch in reverse' item
input.Menu(Equals("Patch Options"), MatchesRegexp("apply patch$"))
input.Menu().Title(Equals("Patch Options")).Select(MatchesRegexp("apply patch$")).Confirm()
input.SwitchToFilesView()

View File

@ -28,7 +28,7 @@ var DiffCommits = NewIntegrationTest(NewIntegrationTestArgs{
)
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), MatchesRegexp(`diff \w+`))
input.Menu().Title(Equals("Diffing")).Select(MatchesRegexp(`diff \w+`)).Confirm()
assert.NotInPopup()
@ -41,7 +41,7 @@ var DiffCommits = NewIntegrationTest(NewIntegrationTestArgs{
assert.MainView().Content(Contains("-second line\n-third line"))
input.Press(keys.Universal.DiffingMenu)
input.Menu(Equals("Diffing"), Contains("reverse diff direction"))
input.Menu().Title(Equals("Diffing")).Select(Contains("reverse diff direction")).Confirm()
assert.NotInPopup()
assert.MainView().Content(Contains("+second line\n+third line"))

View File

@ -84,7 +84,7 @@ var DiscardChanges = NewIntegrationTest(NewIntegrationTestArgs{
for _, file := range files {
assert.CurrentView().SelectedLine(Contains(file.status + " " + file.label))
input.Press(keys.Universal.Remove)
input.Menu(Equals(file.menuTitle), Contains("discard all changes"))
input.Menu().Title(Equals(file.menuTitle)).Select(Contains("discard all changes")).Confirm()
}
}

View File

@ -21,7 +21,7 @@ var Stash = NewIntegrationTest(NewIntegrationTestArgs{
input.Press(keys.Files.ViewStashOptions)
input.Menu(Equals("Stash options"), MatchesRegexp("stash all changes$"))
input.Menu().Title(Equals("Stash options")).Select(MatchesRegexp("stash all changes$")).Confirm()
input.Prompt().Title(Equals("Stash changes")).Type("my stashed file").Confirm()

View File

@ -22,7 +22,7 @@ var StashIncludingUntrackedFiles = NewIntegrationTest(NewIntegrationTestArgs{
input.Press(keys.Files.ViewStashOptions)
input.Menu(Equals("Stash options"), Contains("stash all changes including untracked files"))
input.Menu().Title(Equals("Stash options")).Select(Contains("stash all changes including untracked files")).Confirm()
input.Prompt().Title(Equals("Stash changes")).Type("my stashed file").Confirm()