Skip to content

Commit

Permalink
a more complex custom command test
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Aug 14, 2022
1 parent 9c0d860 commit 53979f7
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 43 deletions.
8 changes: 8 additions & 0 deletions pkg/gui/gui_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
126 changes: 109 additions & 17 deletions pkg/integration/components/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
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) {
line := self.gui.CurrentContext().GetView().SelectedLine()
return strings.Contains(line, text), fmt.Sprintf("Expected selected line to contain '%s', but got '%s'", text, line)
value := getValue()
return matcher.context(context).test(value)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/integration/components/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/integration/components/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pkg/integration/tests/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -27,6 +27,6 @@ var Commit = components.NewIntegrationTest(components.NewIntegrationTestArgs{
input.Confirm()

assert.CommitCount(1)
assert.HeadCommitMessage(commitMessage)
assert.MatchHeadCommitMessage(Equals(commitMessage))
},
})
10 changes: 5 additions & 5 deletions pkg/integration/tests/commit/new_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
},
})
14 changes: 7 additions & 7 deletions pkg/integration/tests/custom_commands/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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"))
},
})
84 changes: 84 additions & 0 deletions pkg/integration/tests/custom_commands/multiple_prompts.go
Original file line number Diff line number Diff line change
@@ -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"))
},
})
Loading

0 comments on commit 53979f7

Please sign in to comment.