2022-08-14 14:33:44 +10:00
|
|
|
package clients
|
2021-04-06 09:02:01 +10:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-08-14 19:15:21 +10:00
|
|
|
"regexp"
|
2022-08-14 14:33:44 +10:00
|
|
|
"strconv"
|
2022-08-14 19:15:21 +10:00
|
|
|
"strings"
|
2021-04-06 09:02:01 +10:00
|
|
|
|
2022-08-14 19:15:21 +10:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2022-08-12 09:24:39 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
2022-08-14 11:24:07 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
2021-04-06 09:02:01 +10:00
|
|
|
)
|
|
|
|
|
2022-08-11 21:17:01 +10:00
|
|
|
// see pkg/integration/README.md
|
|
|
|
|
2022-08-13 12:56:04 +10:00
|
|
|
// The purpose of this program is to run integration tests. It does this by
|
|
|
|
// building our injector program (in the sibling injector directory) and then for
|
|
|
|
// each test we're running, invoke the injector program with the test's name as
|
|
|
|
// an environment variable. Then the injector finds the test and passes it to
|
|
|
|
// the lazygit startup code.
|
|
|
|
|
|
|
|
// If invoked directly, you can specify tests to run by passing their names as positional arguments
|
2021-04-06 09:02:01 +10:00
|
|
|
|
2022-08-14 20:47:09 +10:00
|
|
|
func RunCLI(testNames []string, slow bool) {
|
|
|
|
keyPressDelay := tryConvert(os.Getenv("KEY_PRESS_DELAY"), 0)
|
|
|
|
if slow {
|
|
|
|
keyPressDelay = SLOW_KEY_PRESS_DELAY
|
|
|
|
}
|
|
|
|
|
2022-08-14 11:24:07 +10:00
|
|
|
err := components.RunTests(
|
2022-08-14 14:33:44 +10:00
|
|
|
getTestsToRun(testNames),
|
2021-04-06 09:02:01 +10:00
|
|
|
log.Printf,
|
|
|
|
runCmdInTerminal,
|
2022-08-22 18:51:34 +10:00
|
|
|
runAndPrintFatalError,
|
2022-08-14 11:24:07 +10:00
|
|
|
getModeFromEnv(),
|
2022-08-14 20:47:09 +10:00
|
|
|
keyPressDelay,
|
2021-04-06 09:02:01 +10:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 18:51:34 +10:00
|
|
|
func runAndPrintFatalError(test *components.IntegrationTest, f func() error) {
|
2022-08-14 14:33:44 +10:00
|
|
|
if err := f(); err != nil {
|
2022-08-14 20:47:09 +10:00
|
|
|
log.Fatalf(err.Error())
|
2022-08-14 14:33:44 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestsToRun(testNames []string) []*components.IntegrationTest {
|
2022-08-14 20:32:17 +10:00
|
|
|
allIntegrationTests := tests.GetTests()
|
2022-08-14 11:24:07 +10:00
|
|
|
var testsToRun []*components.IntegrationTest
|
|
|
|
|
2022-08-14 14:33:44 +10:00
|
|
|
if len(testNames) == 0 {
|
2022-08-14 20:32:17 +10:00
|
|
|
return allIntegrationTests
|
2022-08-14 11:24:07 +10:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:15:21 +10:00
|
|
|
testNames = slices.Map(testNames, func(name string) string {
|
|
|
|
// allowing full test paths to be passed for convenience
|
|
|
|
return strings.TrimSuffix(
|
|
|
|
regexp.MustCompile(`.*pkg/integration/tests/`).ReplaceAllString(name, ""),
|
|
|
|
".go",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-08-14 11:24:07 +10:00
|
|
|
outer:
|
2022-08-14 14:33:44 +10:00
|
|
|
for _, testName := range testNames {
|
2022-08-14 11:24:07 +10:00
|
|
|
// check if our given test name actually exists
|
2022-08-14 20:32:17 +10:00
|
|
|
for _, test := range allIntegrationTests {
|
2022-08-14 11:24:07 +10:00
|
|
|
if test.Name() == testName {
|
|
|
|
testsToRun = append(testsToRun, test)
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Fatalf("test %s not found. Perhaps you forgot to add it to `pkg/integration/integration_tests/tests.go`?", testName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return testsToRun
|
|
|
|
}
|
|
|
|
|
2021-04-06 09:02:01 +10:00
|
|
|
func runCmdInTerminal(cmd *exec.Cmd) error {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
2022-08-14 11:24:07 +10:00
|
|
|
|
|
|
|
func getModeFromEnv() components.Mode {
|
|
|
|
switch os.Getenv("MODE") {
|
|
|
|
case "", "ask":
|
|
|
|
return components.ASK_TO_UPDATE_SNAPSHOT
|
|
|
|
case "check":
|
|
|
|
return components.CHECK_SNAPSHOT
|
2022-08-14 14:33:44 +10:00
|
|
|
case "update":
|
2022-08-14 11:24:07 +10:00
|
|
|
return components.UPDATE_SNAPSHOT
|
|
|
|
case "sandbox":
|
|
|
|
return components.SANDBOX
|
|
|
|
default:
|
2022-08-14 14:33:44 +10:00
|
|
|
log.Fatalf("unknown test mode: %s, must be one of [ask, check, update, sandbox]", os.Getenv("MODE"))
|
2022-08-14 11:24:07 +10:00
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 14:33:44 +10:00
|
|
|
|
|
|
|
func tryConvert(numStr string, defaultVal int) int {
|
|
|
|
num, err := strconv.Atoi(numStr)
|
|
|
|
if err != nil {
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
|
|
|
|
return num
|
|
|
|
}
|