1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2026-06-09 22:05:16 +02:00
Files
lazygit/pkg/integration/tests/custom_commands/conditional_prompts.go
Stefan Haller fbcf562e29 Convert custom command Key fields to Keybinding
CustomCommand.Key and CustomCommandMenuOption.Key are user-configured
keybindings just like the built-in ones. Converting them to the Keybinding type
lets a user assign multiple keys to the same custom command, e.g. `key: [a, b]`,
the same way they would for any other keybinding.

The validator iterates over the elements rather than checking a single string,
the binding registration goes through GetValidatedKeyBindingKeys to register
every alternate, and the existing error messages use .String() so a multi-key
binding renders sensibly.

CustomCommandPrompt.Key (a form field name, not a keybinding) stays a plain
string.
2026-05-25 15:32:47 +02:00

97 lines
2.6 KiB
Go

package custom_commands
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var ConditionalPrompts = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Using a custom command with conditional prompts that are skipped based on form values",
ExtraCmdArgs: []string{},
Skip: false,
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("initial commit")
},
SetupConfig: func(cfg *config.AppConfig) {
cfg.GetUserConfig().CustomCommands = []config.CustomCommand{
{
Key: config.Keybinding{"a"},
Context: "files",
Command: `echo "{{.Form.Choice}}{{if .Form.Detail}} {{.Form.Detail}}{{end}}" > result.txt`,
Prompts: []config.CustomCommandPrompt{
{
Key: "Choice",
Type: "menu",
Title: "Choose an option",
Options: []config.CustomCommandMenuOption{
{
Name: "first",
Description: "First option",
Value: "FIRST",
Key: config.Keybinding{"1"},
},
{
Name: "second",
Description: "Second option",
Value: "SECOND",
Key: config.Keybinding{"H"},
},
},
},
{
Key: "Detail",
Type: "input",
Title: "Enter detail for second option",
Condition: `{{ eq .Form.Choice "SECOND" }}`,
},
},
},
}
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
// Test 1: Select "first" via key — conditional prompt should be skipped
t.Views().Files().
IsFocused().
Press(config.Keybinding{"a"})
t.ExpectPopup().Menu().
Title(Equals("Choose an option"))
t.Views().Menu().Press(config.Keybinding{"1"})
// Detail prompt should be skipped, file should be created directly
t.Views().Files().
Focus().
Lines(
Contains("result.txt").IsSelected(),
)
t.FileSystem().FileContent("result.txt", Equals("FIRST\n"))
// Test 2: Select "second" via key — conditional prompt should appear
t.Shell().DeleteFile("result.txt")
t.GlobalPress(keys.Files.RefreshFiles)
t.Views().Files().
IsEmpty().
IsFocused().
Press(config.Keybinding{"a"})
t.ExpectPopup().Menu().
Title(Equals("Choose an option"))
t.Views().Menu().Press(config.Keybinding{"H"})
// Detail prompt should appear because Choice == "SECOND"
t.ExpectPopup().Prompt().Title(Equals("Enter detail for second option")).Type("extra").Confirm()
t.Views().Files().
Focus().
Lines(
Contains("result.txt").IsSelected(),
)
t.FileSystem().FileContent("result.txt", Equals("SECOND extra\n"))
},
})