1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +02:00

Add test for GenerateMenuCandidates from Custom Commands

This commit is contained in:
Elwardi
2021-07-22 15:44:16 +01:00
parent edfb0a26b2
commit 148bf2c070
3 changed files with 77 additions and 3 deletions

View File

@ -117,7 +117,8 @@ func (gui *Gui) menuPrompt(prompt config.CustomCommandPrompt, promptResponses []
return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true}) return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
} }
func (gui *Gui) generateMenuCandidates(commandOutput string, filter string, format string) ([]string, error) {
func (gui *Gui) GenerateMenuCandidates(commandOutput string, filter string, format string) ([]string, error) {
candidates := []string{} candidates := []string{}
reg, err := regexp.Compile(filter) reg, err := regexp.Compile(filter)
if err != nil { if err != nil {
@ -138,7 +139,7 @@ func (gui *Gui) generateMenuCandidates(commandOutput string, filter string, form
for groupIdx, group := range reg.SubexpNames() { for groupIdx, group := range reg.SubexpNames() {
// Record matched group with group ids // Record matched group with group ids
matchName := "group_" + strconv.Itoa(groupIdx) matchName := "group_" + strconv.Itoa(groupIdx)
tmplData[matchName] = group tmplData[matchName] = out[0][groupIdx]
// Record last named group non-empty matches as group matches // Record last named group non-empty matches as group matches
if group != "" { if group != "" {
tmplData[group] = out[0][groupIdx] tmplData[group] = out[0][groupIdx]
@ -176,7 +177,10 @@ func (gui *Gui) menuPromptFromCommand(prompt config.CustomCommandPrompt, promptR
} }
// Need to make a menu out of what the cmd has displayed // Need to make a menu out of what the cmd has displayed
candidates, err := gui.generateMenuCandidates(message, filter, prompt.Format) candidates, err := gui.GenerateMenuCandidates(message, filter, prompt.Format)
if err != nil {
return gui.surfaceError(err)
}
menuItems := make([]*menuItem, len(candidates)) menuItems := make([]*menuItem, len(candidates))
for i := range candidates { for i := range candidates {

21
pkg/gui/dummies.go Normal file
View File

@ -0,0 +1,21 @@
package gui
import (
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/updates"
)
// NewDummyGui creates a new dummy GUI for testing
func NewDummyUpdater() *updates.Updater {
DummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), config.NewDummyAppConfig(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog()))
return DummyUpdater
}
func NewDummyGui() *Gui {
DummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog()), config.NewDummyAppConfig(), NewDummyUpdater(), "", false)
return DummyGui
}

View File

@ -80,3 +80,52 @@ func runCmdHeadless(cmd *exec.Cmd) error {
return f.Close() return f.Close()
} }
func TestGuiGenerateMenuCandidates(t *testing.T) {
type scenario struct {
testName string
cmdOut string
filter string
format string
test func([]string, error)
}
scenarios := []scenario{
{
"Extract remote branch name",
"upstream/pr-1",
"upstream/(?P<branch>.*)",
"{{ .branch }}",
func(actual []string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1", actual[0])
},
},
{
"Multiple named groups",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .branch }}|{{ .remote }}",
func(actual []string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actual[0])
},
},
{
"Multiple named groups with group ids",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .group_2 }}|{{ .group_1 }}",
func(actual []string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actual[0])
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
s.test(NewDummyGui().GenerateMenuCandidates(s.cmdOut, s.filter, s.format))
})
}
}