1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/integration/components/test.go

119 lines
2.8 KiB
Go
Raw Normal View History

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"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
2022-08-09 12:27:44 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
// 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
setupRepo func(shell *Shell)
2022-08-09 12:27:44 +02:00
setupConfig func(config *config.AppConfig)
run func(
shell *Shell,
input *Input,
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-11 13:28:55 +02:00
type NewIntegrationTestArgs struct {
// 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
// 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) {
shell := NewShell()
assert := NewAssert(gui)
2022-08-09 12:27:44 +02:00
keys := gui.Keys()
input := NewInput(gui, keys, assert, KeyPressDelay())
2022-08-09 12:27:44 +02:00
self.run(shell, input, assert, keys)
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)
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
}