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

135 lines
3.4 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"
2022-12-27 13:52:20 +02:00
"github.com/jesseduffield/lazygit/pkg/env"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
2022-08-09 12:27:44 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
2023-02-26 02:49:15 +02:00
// IntegrationTest describes an integration test that will be run against the lazygit gui.
2022-08-13 05:50:38 +02:00
// our unit tests will use this description to avoid a panic caused by attempting
// to get the test's name via it's file's path.
const unitTestDescription = "test test"
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(
2022-12-27 12:47:37 +02:00
testDriver *TestDriver,
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
2022-12-27 12:47:37 +02:00
Run func(t *TestDriver, 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 {
2022-08-13 05:50:38 +02:00
name := ""
if args.Description != unitTestDescription {
// this panics if we're in a unit test for our integration tests,
// so we're using "test test" as a sentinel value
name = testNameFromCurrentFilePath()
2022-08-13 05:50:38 +02:00
}
2022-08-11 13:28:55 +02:00
return &IntegrationTest{
2022-08-13 05:50:38 +02:00
name: name,
2022-08-09 12:27:44 +02:00
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)
}
func (self *IntegrationTest) Run(gui integrationTypes.GuiDriver) {
2022-12-27 13:52:20 +02:00
// we pass the --pass arg to lazygit when running an integration test, and that
// ends up stored in the following env var
repoPath := env.GetGitWorkTreeEnv()
shell := NewShell(repoPath, func(errorMsg string) { gui.Fail(errorMsg) })
2022-08-09 12:27:44 +02:00
keys := gui.Keys()
2022-12-27 12:47:37 +02:00
testDriver := NewTestDriver(gui, shell, keys, KeyPressDelay())
2022-08-09 12:27:44 +02:00
2022-12-27 12:47:37 +02:00
self.run(testDriver, keys)
if KeyPressDelay() > 0 {
// the dev would want to see the final state if they're running in slow mode
2022-12-27 12:47:37 +02:00
testDriver.Wait(2000)
}
2022-08-09 12:27:44 +02:00
}
func testNameFromCurrentFilePath() string {
2022-08-09 12:27:44 +02:00
path := utils.FilePath(3)
return TestNameFromFilePath(path)
}
func TestNameFromFilePath(path string) string {
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
}