2022-08-12 01:19:39 +02:00
|
|
|
package components
|
2022-08-09 12:27:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2022-08-09 13:27:12 +02:00
|
|
|
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
|
2022-08-09 12:27:44 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-08-09 13:27:12 +02:00
|
|
|
// Test describes an integration tests that will be run against the lazygit gui.
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
type IntegrationTest struct {
|
2022-08-09 12:27:44 +02:00
|
|
|
name string
|
|
|
|
description string
|
|
|
|
extraCmdArgs string
|
|
|
|
skip bool
|
2022-08-09 13:27:12 +02:00
|
|
|
setupRepo func(shell *Shell)
|
2022-08-09 12:27:44 +02:00
|
|
|
setupConfig func(config *config.AppConfig)
|
|
|
|
run func(
|
2022-08-09 13:27:12 +02:00
|
|
|
shell *Shell,
|
2022-08-11 13:04:10 +02:00
|
|
|
input *Input,
|
2022-08-09 13:27:12 +02:00
|
|
|
assert *Assert,
|
2022-08-09 12:27:44 +02:00
|
|
|
keys config.KeybindingConfig,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
var _ integrationTypes.IntegrationTest = &IntegrationTest{}
|
2022-08-09 13:27:12 +02:00
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
type NewIntegrationTestArgs struct {
|
2022-08-11 13:04:10 +02:00
|
|
|
// Briefly describes what happens in the test and what it's testing for
|
|
|
|
Description string
|
|
|
|
// prepares a repo for testing
|
|
|
|
SetupRepo func(shell *Shell)
|
|
|
|
// takes a config and mutates. The mutated context will end up being passed to the gui
|
|
|
|
SetupConfig func(config *config.AppConfig)
|
|
|
|
// runs the test
|
|
|
|
Run func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig)
|
|
|
|
// additional args passed to lazygit
|
2022-08-09 12:27:44 +02:00
|
|
|
ExtraCmdArgs string
|
2022-08-11 13:04:10 +02:00
|
|
|
// for when a test is flakey
|
|
|
|
Skip bool
|
2022-08-09 12:27:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func NewIntegrationTest(args NewIntegrationTestArgs) *IntegrationTest {
|
|
|
|
return &IntegrationTest{
|
2022-08-09 12:27:44 +02:00
|
|
|
name: testNameFromFilePath(),
|
|
|
|
description: args.Description,
|
|
|
|
extraCmdArgs: args.ExtraCmdArgs,
|
|
|
|
skip: args.Skip,
|
|
|
|
setupRepo: args.SetupRepo,
|
|
|
|
setupConfig: args.SetupConfig,
|
|
|
|
run: args.Run,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) Name() string {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.name
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) Description() string {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.description
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) ExtraCmdArgs() string {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.extraCmdArgs
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) Skip() bool {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.skip
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) SetupConfig(config *config.AppConfig) {
|
2022-08-09 12:27:44 +02:00
|
|
|
self.setupConfig(config)
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) SetupRepo(shell *Shell) {
|
2022-08-09 12:27:44 +02:00
|
|
|
self.setupRepo(shell)
|
|
|
|
}
|
|
|
|
|
|
|
|
// I want access to all contexts, the model, the ability to press a key, the ability to log,
|
2022-08-11 13:28:55 +02:00
|
|
|
func (self *IntegrationTest) Run(gui integrationTypes.GuiAdapter) {
|
2022-08-09 13:27:12 +02:00
|
|
|
shell := NewShell()
|
|
|
|
assert := NewAssert(gui)
|
2022-08-09 12:27:44 +02:00
|
|
|
keys := gui.Keys()
|
2022-08-09 13:27:12 +02:00
|
|
|
input := NewInput(gui, keys, assert, KeyPressDelay())
|
2022-08-09 12:27:44 +02:00
|
|
|
|
|
|
|
self.run(shell, input, assert, keys)
|
2022-08-09 13:27:12 +02:00
|
|
|
|
|
|
|
if KeyPressDelay() > 0 {
|
|
|
|
// the dev would want to see the final state if they're running in slow mode
|
|
|
|
input.Wait(2000)
|
|
|
|
}
|
2022-08-09 12:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testNameFromFilePath() string {
|
|
|
|
path := utils.FilePath(3)
|
2022-08-09 13:27:12 +02:00
|
|
|
name := strings.Split(path, "integration/tests/")[1]
|
2022-08-09 12:27:44 +02:00
|
|
|
|
|
|
|
return name[:len(name)-len(".go")]
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is the delay in milliseconds between keypresses
|
|
|
|
// defaults to zero
|
|
|
|
func KeyPressDelay() int {
|
|
|
|
delayStr := os.Getenv("KEY_PRESS_DELAY")
|
|
|
|
if delayStr == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
delay, err := strconv.Atoi(delayStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return delay
|
|
|
|
}
|