introduce gui adapter

This commit is contained in:
Jesse Duffield 2022-08-09 20:27:44 +10:00
parent 225c563c63
commit 46ae55f91e
20 changed files with 763 additions and 297 deletions

198
main.go
View file

@ -1,30 +1,19 @@
package main package main
import ( import (
"bytes"
"fmt"
"log"
"os" "os"
"path/filepath"
"runtime"
"runtime/debug" "runtime/debug"
"strings"
"github.com/integrii/flaggy" "github.com/integrii/flaggy"
"github.com/jesseduffield/lazygit/pkg/app" "github.com/jesseduffield/lazygit/pkg/app"
"github.com/jesseduffield/lazygit/pkg/app/daemon"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/integration"
"github.com/jesseduffield/lazygit/pkg/logs"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
yaml "github.com/jesseduffield/yaml"
"github.com/samber/lo" "github.com/samber/lo"
) )
const DEFAULT_VERSION = "unversioned" const DEFAULT_VERSION = "unversioned"
// These values may be set by the build script.
// we'll overwrite them if they haven't been set by the build script and if Go itself has set corresponding values in the binary
var ( var (
commit string commit string
version = DEFAULT_VERSION version = DEFAULT_VERSION
@ -33,8 +22,13 @@ var (
) )
func main() { func main() {
updateBuildInfo() cliArgs := parseCliArgsAndEnvVars()
buildInfo := getBuildInfo()
app.Start(cliArgs, buildInfo, nil)
}
func parseCliArgsAndEnvVars() *app.CliArgs {
flaggy.DefaultParser.ShowVersionWithVersionFlag = false flaggy.DefaultParser.ShowVersionWithVersionFlag = false
repoPath := "" repoPath := ""
@ -46,20 +40,20 @@ func main() {
gitArg := "" gitArg := ""
flaggy.AddPositionalValue(&gitArg, "git-arg", 1, false, "Panel to focus upon opening lazygit. Accepted values (based on git terminology): status, branch, log, stash. Ignored if --filter arg is passed.") flaggy.AddPositionalValue(&gitArg, "git-arg", 1, false, "Panel to focus upon opening lazygit. Accepted values (based on git terminology): status, branch, log, stash. Ignored if --filter arg is passed.")
versionFlag := false printVersionInfo := false
flaggy.Bool(&versionFlag, "v", "version", "Print the current version") flaggy.Bool(&printVersionInfo, "v", "version", "Print the current version")
debuggingFlag := false debug := false
flaggy.Bool(&debuggingFlag, "d", "debug", "Run in debug mode with logging (see --logs flag below). Use the LOG_LEVEL env var to set the log level (debug/info/warn/error)") flaggy.Bool(&debug, "d", "debug", "Run in debug mode with logging (see --logs flag below). Use the LOG_LEVEL env var to set the log level (debug/info/warn/error)")
logFlag := false tailLogs := false
flaggy.Bool(&logFlag, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)") flaggy.Bool(&tailLogs, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)")
configFlag := false printDefaultConfig := false
flaggy.Bool(&configFlag, "c", "config", "Print the default config") flaggy.Bool(&printDefaultConfig, "c", "config", "Print the default config")
configDirFlag := false printConfigDir := false
flaggy.Bool(&configDirFlag, "cd", "print-config-dir", "Print the config directory") flaggy.Bool(&printConfigDir, "cd", "print-config-dir", "Print the config directory")
useConfigDir := "" useConfigDir := ""
flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory") flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory")
@ -70,158 +64,68 @@ func main() {
gitDir := "" gitDir := ""
flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument") flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument")
customConfig := "" customConfigFile := ""
flaggy.String(&customConfig, "ucf", "use-config-file", "Comma separated list to custom config file(s)") flaggy.String(&customConfigFile, "ucf", "use-config-file", "Comma separated list to custom config file(s)")
flaggy.Parse() flaggy.Parse()
if os.Getenv("DEBUG") == "TRUE" { if os.Getenv("DEBUG") == "TRUE" {
debuggingFlag = true debug = true
} }
if repoPath != "" { return &app.CliArgs{
if workTree != "" || gitDir != "" { RepoPath: repoPath,
log.Fatal("--path option is incompatible with the --work-tree and --git-dir options") FilterPath: filterPath,
} GitArg: gitArg,
PrintVersionInfo: printVersionInfo,
absRepoPath, err := filepath.Abs(repoPath) Debug: debug,
if err != nil { TailLogs: tailLogs,
log.Fatal(err) PrintDefaultConfig: printDefaultConfig,
} PrintConfigDir: printConfigDir,
workTree = absRepoPath UseConfigDir: useConfigDir,
gitDir = filepath.Join(absRepoPath, ".git") WorkTree: workTree,
GitDir: gitDir,
CustomConfigFile: customConfigFile,
} }
if customConfig != "" {
os.Setenv("LG_CONFIG_FILE", customConfig)
}
if useConfigDir != "" {
os.Setenv("CONFIG_DIR", useConfigDir)
}
if workTree != "" {
env.SetGitWorkTreeEnv(workTree)
}
if gitDir != "" {
env.SetGitDirEnv(gitDir)
}
if versionFlag {
fmt.Printf("commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n", commit, date, buildSource, version, runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if configFlag {
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
err := encoder.Encode(config.GetDefaultConfig())
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("%s\n", buf.String())
os.Exit(0)
}
if configDirFlag {
fmt.Printf("%s\n", config.ConfigDir())
os.Exit(0)
}
if logFlag {
logs.TailLogs()
os.Exit(0)
}
if workTree != "" {
if err := os.Chdir(workTree); err != nil {
log.Fatal(err.Error())
}
}
tempDir, err := os.MkdirTemp("", "lazygit-*")
if err != nil {
log.Fatal(err.Error())
}
defer os.RemoveAll(tempDir)
appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag, tempDir)
if err != nil {
log.Fatal(err.Error())
}
if test, ok := integration.CurrentIntegrationTest(); ok {
test.SetupConfig(appConfig)
}
common, err := app.NewCommon(appConfig)
if err != nil {
log.Fatal(err)
}
if daemon.InDaemonMode() {
daemon.Handle(common)
return
}
parsedGitArg := parseGitArg(gitArg)
app.Run(appConfig, common, types.NewStartArgs(filterPath, parsedGitArg))
} }
func parseGitArg(gitArg string) types.GitArg { func getBuildInfo() *app.BuildInfo {
typedArg := types.GitArg(gitArg) buildInfo := &app.BuildInfo{
Commit: commit,
// using switch so that linter catches when a new git arg value is defined but not handled here Date: date,
switch typedArg { Version: version,
case types.GitArgNone, types.GitArgStatus, types.GitArgBranch, types.GitArgLog, types.GitArgStash: BuildSource: buildSource,
return typedArg
} }
permittedValues := []string{
string(types.GitArgStatus),
string(types.GitArgBranch),
string(types.GitArgLog),
string(types.GitArgStash),
}
log.Fatalf("Invalid git arg value: '%s'. Must be one of the following values: %s. e.g. 'lazygit status'. See 'lazygit --help'.",
gitArg,
strings.Join(permittedValues, ", "),
)
panic("unreachable")
}
func updateBuildInfo() {
// if the version has already been set by build flags then we'll honour that. // if the version has already been set by build flags then we'll honour that.
// chances are it's something like v0.31.0 which is more informative than a // chances are it's something like v0.31.0 which is more informative than a
// commit hash. // commit hash.
if version != DEFAULT_VERSION { if buildInfo.Version != DEFAULT_VERSION {
return return buildInfo
} }
buildInfo, ok := debug.ReadBuildInfo() goBuildInfo, ok := debug.ReadBuildInfo()
if !ok { if !ok {
return return buildInfo
} }
revision, ok := lo.Find(buildInfo.Settings, func(setting debug.BuildSetting) bool { revision, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
return setting.Key == "vcs.revision" return setting.Key == "vcs.revision"
}) })
if ok { if ok {
commit = revision.Value buildInfo.Commit = revision.Value
// if lazygit was built from source we'll show the version as the // if lazygit was built from source we'll show the version as the
// abbreviated commit hash // abbreviated commit hash
version = utils.ShortSha(revision.Value) buildInfo.Version = utils.ShortSha(revision.Value)
} }
// if version hasn't been set we assume that neither has the date // if version hasn't been set we assume that neither has the date
time, ok := lo.Find(buildInfo.Settings, func(setting debug.BuildSetting) bool { time, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool {
return setting.Key == "vcs.time" return setting.Key == "vcs.time"
}) })
if ok { if ok {
date = time.Value buildInfo.Date = time.Value
} }
return buildInfo
} }

View file

@ -24,6 +24,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui" "github.com/jesseduffield/lazygit/pkg/gui"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/updates"
) )
@ -38,7 +39,12 @@ type App struct {
Updater *updates.Updater // may only need this on the Gui Updater *updates.Updater // may only need this on the Gui
} }
func Run(config config.AppConfigurer, common *common.Common, startArgs types.StartArgs) { func Run(
config config.AppConfigurer,
common *common.Common,
startArgs types.StartArgs,
test integrationTypes.Test,
) {
app, err := NewApp(config, common) app, err := NewApp(config, common)
if err == nil { if err == nil {

163
pkg/app/run.go Normal file
View file

@ -0,0 +1,163 @@
package app
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/jesseduffield/lazygit/pkg/app/daemon"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/integration"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/jesseduffield/lazygit/pkg/logs"
"gopkg.in/yaml.v3"
)
type CliArgs struct {
RepoPath string
FilterPath string
GitArg string
PrintVersionInfo bool
Debug bool
TailLogs bool
PrintDefaultConfig bool
PrintConfigDir bool
UseConfigDir string
WorkTree string
GitDir string
CustomConfigFile string
}
type BuildInfo struct {
Commit string
Date string
Version string
BuildSource string
}
// only used when running integration tests
type TestConfig struct {
Test integrationTypes.Test
}
func Start(cliArgs *CliArgs, buildInfo *BuildInfo, test integrationTypes.Test) {
if cliArgs.RepoPath != "" {
if cliArgs.WorkTree != "" || cliArgs.GitDir != "" {
log.Fatal("--path option is incompatible with the --work-tree and --git-dir options")
}
absRepoPath, err := filepath.Abs(cliArgs.RepoPath)
if err != nil {
log.Fatal(err)
}
cliArgs.WorkTree = absRepoPath
cliArgs.GitDir = filepath.Join(absRepoPath, ".git")
}
if cliArgs.CustomConfigFile != "" {
os.Setenv("LG_CONFIG_FILE", cliArgs.CustomConfigFile)
}
if cliArgs.UseConfigDir != "" {
os.Setenv("CONFIG_DIR", cliArgs.UseConfigDir)
}
if cliArgs.WorkTree != "" {
env.SetGitWorkTreeEnv(cliArgs.WorkTree)
}
if cliArgs.GitDir != "" {
env.SetGitDirEnv(cliArgs.GitDir)
}
if cliArgs.PrintVersionInfo {
fmt.Printf("commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n", buildInfo.Commit, buildInfo.Date, buildInfo.BuildSource, buildInfo.Version, runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if cliArgs.PrintDefaultConfig {
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
err := encoder.Encode(config.GetDefaultConfig())
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("%s\n", buf.String())
os.Exit(0)
}
if cliArgs.PrintConfigDir {
fmt.Printf("%s\n", config.ConfigDir())
os.Exit(0)
}
if cliArgs.TailLogs {
logs.TailLogs()
os.Exit(0)
}
if cliArgs.WorkTree != "" {
if err := os.Chdir(cliArgs.WorkTree); err != nil {
log.Fatal(err.Error())
}
}
tempDir, err := os.MkdirTemp("", "lazygit-*")
if err != nil {
log.Fatal(err.Error())
}
defer os.RemoveAll(tempDir)
appConfig, err := config.NewAppConfig("lazygit", buildInfo.Version, buildInfo.Commit, buildInfo.Date, buildInfo.BuildSource, cliArgs.Debug, tempDir)
if err != nil {
log.Fatal(err.Error())
}
if test, ok := integration.CurrentIntegrationTest(); ok {
test.SetupConfig(appConfig)
}
common, err := NewCommon(appConfig)
if err != nil {
log.Fatal(err)
}
if daemon.InDaemonMode() {
daemon.Handle(common)
return
}
parsedGitArg := parseGitArg(cliArgs.GitArg)
Run(appConfig, common, types.NewStartArgs(cliArgs.FilterPath, parsedGitArg), test)
}
func parseGitArg(gitArg string) types.GitArg {
typedArg := types.GitArg(gitArg)
// using switch so that linter catches when a new git arg value is defined but not handled here
switch typedArg {
case types.GitArgNone, types.GitArgStatus, types.GitArgBranch, types.GitArgLog, types.GitArgStash:
return typedArg
}
permittedValues := []string{
string(types.GitArgStatus),
string(types.GitArgBranch),
string(types.GitArgLog),
string(types.GitArgStash),
}
log.Fatalf("Invalid git arg value: '%s'. Must be one of the following values: %s. e.g. 'lazygit status'. See 'lazygit --help'.",
gitArg,
strings.Join(permittedValues, ", "),
)
panic("unreachable")
}

View file

@ -27,8 +27,6 @@ type AppConfig struct {
IsNewRepo bool IsNewRepo bool
} }
// AppConfigurer interface allows individual app config structs to inherit Fields
// from AppConfig and still be used by lazygit.
type AppConfigurer interface { type AppConfigurer interface {
GetDebug() bool GetDebug() bool

View file

@ -0,0 +1,73 @@
package gui
import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
)
// this gives our integration test a way of interacting with the gui for sending keypresses
// and reading state.
type GuiAdapterImpl struct {
gui *Gui
}
var _ integrationTypes.GuiAdapter = &GuiAdapterImpl{}
func (self *GuiAdapterImpl) PressKey(keyStr string) {
key := keybindings.GetKey(keyStr)
var r rune
var tcellKey tcell.Key
switch v := key.(type) {
case rune:
r = v
tcellKey = tcell.KeyRune
case gocui.Key:
tcellKey = tcell.Key(v)
}
self.gui.g.ReplayedEvents.Keys <- gocui.NewTcellKeyEventWrapper(
tcell.NewEventKey(tcellKey, r, tcell.ModNone),
0,
)
}
func (self *GuiAdapterImpl) Keys() config.KeybindingConfig {
return self.gui.Config.GetUserConfig().Keybinding
}
func (self *GuiAdapterImpl) CurrentContext() types.Context {
return self.gui.c.CurrentContext()
}
func (self *GuiAdapterImpl) Model() *types.Model {
return self.gui.State.Model
}
func (self *GuiAdapterImpl) Fail(message string) {
self.gui.g.Close()
// need to give the gui time to close
time.Sleep(time.Millisecond * 100)
panic(message)
}
// logs to the normal place that you log to i.e. viewable with `lazygit --logs`
func (self *GuiAdapterImpl) Log(message string) {
self.gui.c.Log.Warn(message)
}
// logs in the actual UI (in the commands panel)
func (self *GuiAdapterImpl) LogUI(message string) {
self.gui.c.LogAction(message)
}
func (self *GuiAdapterImpl) CheckedOutRef() *models.Branch {
return self.gui.helpers.Refs.GetCheckedOutRef()
}

View file

@ -1,6 +1,10 @@
package custom_commands package custom_commands
import ( import (
"bytes"
"fmt"
"text/template"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
) )
@ -101,3 +105,30 @@ func (self *Resolver) resolveMenuOption(option *config.CustomCommandMenuOption,
Value: value, Value: value,
}, nil }, nil
} }
func main() {
fmt.Println(ResolveTemplate("old approach: {{index .PromptResponses 0}}, new approach: {{ .Form.a }}", CustomCommandObject{
PromptResponses: []string{"a"},
Form: map[string]string{"a": "B"},
}))
}
type CustomCommandObject struct {
// deprecated. Use Responses instead
PromptResponses []string
Form map[string]string
}
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, object); err != nil {
return "", err
}
return buf.String(), nil
}

View file

@ -10,6 +10,10 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
type IntegrationTest interface {
Run(guiAdapter *GuiAdapterImpl)
}
func (gui *Gui) handleTestMode() { func (gui *Gui) handleTestMode() {
if integration.PlayingIntegrationTest() { if integration.PlayingIntegrationTest() {
test, ok := integration.CurrentIntegrationTest() test, ok := integration.CurrentIntegrationTest()
@ -21,17 +25,7 @@ func (gui *Gui) handleTestMode() {
go func() { go func() {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
shell := &integration.ShellImpl{} test.Run(&GuiAdapterImpl{gui: gui})
assert := &AssertImpl{gui: gui}
keys := gui.Config.GetUserConfig().Keybinding
input := NewInputImpl(gui, keys, assert, integration.KeyPressDelay())
test.Run(
shell,
input,
assert,
gui.c.UserConfig.Keybinding,
)
gui.g.Update(func(*gocui.Gui) error { gui.g.Update(func(*gocui.Gui) error {
return gocui.ErrQuit return gocui.ErrQuit

View file

@ -2,9 +2,9 @@ package integration
import ( import (
"os" "os"
"strconv"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/integration/integration_tests"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
@ -18,35 +18,20 @@ func IntegrationTestName() string {
return os.Getenv("LAZYGIT_TEST_NAME") return os.Getenv("LAZYGIT_TEST_NAME")
} }
func PlayingIntegrationTest() bool {
return IntegrationTestName() != ""
}
func CurrentIntegrationTest() (types.Test, bool) { func CurrentIntegrationTest() (types.Test, bool) {
if !PlayingIntegrationTest() { if !PlayingIntegrationTest() {
return nil, false return nil, false
} }
return slices.Find(Tests, func(test types.Test) bool { return slices.Find(integration_tests.Tests, func(test types.Test) bool {
return test.Name() == IntegrationTestName() return test.Name() == IntegrationTestName()
}) })
} }
func PlayingIntegrationTest() bool {
return IntegrationTestName() != ""
}
// 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
}
// OLD integration test format stuff // OLD integration test format stuff
func Replaying() bool { func Replaying() bool {

View file

@ -0,0 +1,106 @@
package helpers
import (
"fmt"
"strings"
"time"
"github.com/jesseduffield/lazygit/pkg/gui/types"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
)
type AssertImpl struct {
gui integrationTypes.GuiAdapter
}
var _ integrationTypes.Assert = &AssertImpl{}
func (self *AssertImpl) WorkingTreeFileCount(expectedCount int) {
self.assertWithRetries(func() (bool, string) {
actualCount := len(self.gui.Model().Files)
return actualCount == expectedCount, fmt.Sprintf(
"Expected %d changed working tree files, but got %d",
expectedCount, actualCount,
)
})
}
func (self *AssertImpl) CommitCount(expectedCount int) {
self.assertWithRetries(func() (bool, string) {
actualCount := len(self.gui.Model().Commits)
return actualCount == expectedCount, fmt.Sprintf(
"Expected %d commits present, but got %d",
expectedCount, actualCount,
)
})
}
func (self *AssertImpl) HeadCommitMessage(expectedMessage string) {
self.assertWithRetries(func() (bool, string) {
if len(self.gui.Model().Commits) == 0 {
return false, "Expected at least one commit to be present"
}
headCommit := self.gui.Model().Commits[0]
if headCommit.Name != expectedMessage {
return false, fmt.Sprintf(
"Expected commit message to be '%s', but got '%s'",
expectedMessage, headCommit.Name,
)
}
return true, ""
})
}
func (self *AssertImpl) CurrentViewName(expectedViewName string) {
self.assertWithRetries(func() (bool, string) {
actual := self.gui.CurrentContext().GetView().Name()
return actual == expectedViewName, fmt.Sprintf("Expected current view name to be '%s', but got '%s'", expectedViewName, actual)
})
}
func (self *AssertImpl) CurrentBranchName(expectedViewName string) {
self.assertWithRetries(func() (bool, string) {
actual := self.gui.CheckedOutRef().Name
return actual == expectedViewName, fmt.Sprintf("Expected current branch name to be '%s', but got '%s'", expectedViewName, actual)
})
}
func (self *AssertImpl) InListContext() {
self.assertWithRetries(func() (bool, string) {
currentContext := self.gui.CurrentContext()
_, ok := currentContext.(types.IListContext)
return ok, fmt.Sprintf("Expected current context to be a list context, but got %s", currentContext.GetKey())
})
}
func (self *AssertImpl) SelectedLineContains(text string) {
self.assertWithRetries(func() (bool, string) {
line := self.gui.CurrentContext().GetView().SelectedLine()
return strings.Contains(line, text), fmt.Sprintf("Expected selected line to contain '%s', but got '%s'", text, line)
})
}
func (self *AssertImpl) assertWithRetries(test func() (bool, string)) {
waitTimes := []int{0, 1, 5, 10, 200, 500, 1000}
var message string
for _, waitTime := range waitTimes {
time.Sleep(time.Duration(waitTime) * time.Millisecond)
var ok bool
ok, message = test()
if ok {
return
}
}
self.Fail(message)
}
func (self *AssertImpl) Fail(message string) {
self.gui.Fail(message)
}

View file

@ -0,0 +1,153 @@
package helpers
import (
"fmt"
"strings"
"time"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
)
type InputImpl struct {
gui integrationTypes.GuiAdapter
keys config.KeybindingConfig
assert integrationTypes.Assert
pushKeyDelay int
}
func NewInputImpl(gui integrationTypes.GuiAdapter, keys config.KeybindingConfig, assert integrationTypes.Assert, pushKeyDelay int) *InputImpl {
return &InputImpl{
gui: gui,
keys: keys,
assert: assert,
pushKeyDelay: pushKeyDelay,
}
}
var _ integrationTypes.Input = &InputImpl{}
func (self *InputImpl) PressKeys(keyStrs ...string) {
for _, keyStr := range keyStrs {
self.pressKey(keyStr)
}
}
func (self *InputImpl) pressKey(keyStr string) {
self.Wait(self.pushKeyDelay)
self.gui.PressKey(keyStr)
}
func (self *InputImpl) SwitchToStatusWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[0])
}
func (self *InputImpl) SwitchToFilesWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[1])
}
func (self *InputImpl) SwitchToBranchesWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[2])
}
func (self *InputImpl) SwitchToCommitsWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[3])
}
func (self *InputImpl) SwitchToStashWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[4])
}
func (self *InputImpl) Type(content string) {
for _, char := range content {
self.pressKey(string(char))
}
}
func (self *InputImpl) Confirm() {
self.pressKey(self.keys.Universal.Confirm)
}
func (self *InputImpl) Cancel() {
self.pressKey(self.keys.Universal.Return)
}
func (self *InputImpl) Select() {
self.pressKey(self.keys.Universal.Select)
}
func (self *InputImpl) NextItem() {
self.pressKey(self.keys.Universal.NextItem)
}
func (self *InputImpl) PreviousItem() {
self.pressKey(self.keys.Universal.PrevItem)
}
func (self *InputImpl) ContinueMerge() {
self.PressKeys(self.keys.Universal.CreateRebaseOptionsMenu)
self.assert.SelectedLineContains("continue")
self.Confirm()
}
func (self *InputImpl) ContinueRebase() {
self.ContinueMerge()
}
func (self *InputImpl) Wait(milliseconds int) {
time.Sleep(time.Duration(milliseconds) * time.Millisecond)
}
func (self *InputImpl) LogUI(message string) {
self.gui.LogUI(message)
}
func (self *InputImpl) Log(message string) {
self.gui.LogUI(message)
}
// NOTE: this currently assumes that ViewBufferLines returns all the lines that can be accessed.
// If this changes in future, we'll need to update this code to first attempt to find the item
// in the current page and failing that, jump to the top of the view and iterate through all of it,
// looking for the item.
func (self *InputImpl) NavigateToListItemContainingText(text string) {
self.assert.InListContext()
currentContext := self.gui.CurrentContext().(types.IListContext)
view := currentContext.GetView()
// first we look for a duplicate on the current screen. We won't bother looking beyond that though.
matchCount := 0
matchIndex := -1
for i, line := range view.ViewBufferLines() {
if strings.Contains(line, text) {
matchCount++
matchIndex = i
}
}
if matchCount > 1 {
self.assert.Fail(fmt.Sprintf("Found %d matches for %s, expected only a single match", matchCount, text))
}
if matchCount == 1 {
selectedLineIdx := view.SelectedLineIdx()
if selectedLineIdx == matchIndex {
return
}
if selectedLineIdx < matchIndex {
for i := selectedLineIdx; i < matchIndex; i++ {
self.NextItem()
}
return
} else {
for i := selectedLineIdx; i > matchIndex; i-- {
self.PreviousItem()
}
return
}
}
self.assert.Fail(fmt.Sprintf("Could not find item containing text: %s", text))
}

View file

@ -1,4 +1,4 @@
package integration package helpers
import ( import (
"fmt" "fmt"

View file

@ -0,0 +1,107 @@
package helpers
import (
"os"
"strconv"
"strings"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type TestImpl struct {
name string
description string
extraCmdArgs string
skip bool
setupRepo func(shell types.Shell)
setupConfig func(config *config.AppConfig)
run func(
shell types.Shell,
input types.Input,
assert types.Assert,
keys config.KeybindingConfig,
)
}
type NewTestArgs struct {
Description string
SetupRepo func(shell types.Shell)
SetupConfig func(config *config.AppConfig)
Run func(shell types.Shell, input types.Input, assert types.Assert, keys config.KeybindingConfig)
ExtraCmdArgs string
Skip bool
}
func NewTest(args NewTestArgs) *TestImpl {
return &TestImpl{
name: testNameFromFilePath(),
description: args.Description,
extraCmdArgs: args.ExtraCmdArgs,
skip: args.Skip,
setupRepo: args.SetupRepo,
setupConfig: args.SetupConfig,
run: args.Run,
}
}
var _ types.Test = (*TestImpl)(nil)
func (self *TestImpl) Name() string {
return self.name
}
func (self *TestImpl) Description() string {
return self.description
}
func (self *TestImpl) ExtraCmdArgs() string {
return self.extraCmdArgs
}
func (self *TestImpl) Skip() bool {
return self.skip
}
func (self *TestImpl) SetupConfig(config *config.AppConfig) {
self.setupConfig(config)
}
func (self *TestImpl) SetupRepo(shell types.Shell) {
self.setupRepo(shell)
}
// I want access to all contexts, the model, the ability to press a key, the ability to log,
func (self *TestImpl) Run(
gui types.GuiAdapter,
) {
shell := &ShellImpl{}
assert := &AssertImpl{gui: gui}
keys := gui.Keys()
input := NewInputImpl(gui, keys, assert, KeyPressDelay())
self.run(shell, input, assert, keys)
}
func testNameFromFilePath() string {
path := utils.FilePath(3)
name := strings.Split(path, "integration/integration_tests/")[1]
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
}

View file

@ -10,15 +10,13 @@ import (
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/integration/helpers"
"github.com/jesseduffield/lazygit/pkg/integration/integration_tests" "github.com/jesseduffield/lazygit/pkg/integration/integration_tests"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
// this is the integration runner for the new and improved integration interface // this is the integration runner for the new and improved integration interface
// re-exporting this so that clients only need to import one package
var Tests = integration_tests.Tests
func RunTestsNew( func RunTestsNew(
logf func(format string, formatArgs ...interface{}), logf func(format string, formatArgs ...interface{}),
runCmd func(cmd *exec.Cmd) error, runCmd func(cmd *exec.Cmd) error,
@ -41,7 +39,7 @@ func RunTestsNew(
return err return err
} }
for _, test := range Tests { for _, test := range integration_tests.Tests {
test := test test := test
fnWrapper(test, func(t *testing.T) error { //nolint: thelper fnWrapper(test, func(t *testing.T) error { //nolint: thelper
@ -141,7 +139,7 @@ func createFixtureNew(test types.Test, actualDir string, rootDir string) error {
panic(err) panic(err)
} }
shell := &ShellImpl{} shell := &helpers.ShellImpl{}
shell.RunCommand("git init") shell.RunCommand("git init")
shell.RunCommand(`git config user.email "CI@example.com"`) shell.RunCommand(`git config user.email "CI@example.com"`)
shell.RunCommand(`git config user.name "CI"`) shell.RunCommand(`git config user.name "CI"`)

View file

@ -2,10 +2,11 @@ package branch
import ( import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/integration/helpers"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
var Suggestions = types.NewTest(types.NewTestArgs{ var Suggestions = helpers.NewTest(helpers.NewTestArgs{
Description: "Checking out a branch with name suggestions", Description: "Checking out a branch with name suggestions",
ExtraCmdArgs: "", ExtraCmdArgs: "",
Skip: false, Skip: false,

View file

@ -2,10 +2,11 @@ package commit
import ( import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/integration/helpers"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
var Commit = types.NewTest(types.NewTestArgs{ var Commit = helpers.NewTest(helpers.NewTestArgs{
Description: "Staging a couple files and committing", Description: "Staging a couple files and committing",
ExtraCmdArgs: "", ExtraCmdArgs: "",
Skip: false, Skip: false,

View file

@ -2,10 +2,11 @@ package commit
import ( import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/integration/helpers"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
var NewBranch = types.NewTest(types.NewTestArgs{ var NewBranch = helpers.NewTest(helpers.NewTestArgs{
Description: "Creating a new branch from a commit", Description: "Creating a new branch from a commit",
ExtraCmdArgs: "", ExtraCmdArgs: "",
Skip: false, Skip: false,

View file

@ -2,10 +2,11 @@ package interactive_rebase
import ( import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/integration/helpers"
"github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/integration/types"
) )
var One = types.NewTest(types.NewTestArgs{ var One = helpers.NewTest(helpers.NewTestArgs{
Description: "Begins an interactive rebase, then fixups, drops, and squashes some commits", Description: "Begins an interactive rebase, then fixups, drops, and squashes some commits",
ExtraCmdArgs: "", ExtraCmdArgs: "",
Skip: false, Skip: false,

View file

@ -1,12 +1,17 @@
package types package types
import ( import (
"strings" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
// TODO: refactor this so that we don't have code spread around so much. We want
// our TestImpl struct to take the dependencies it needs from the gui and then
// create the input, assert, shell structs itself. That way, we can potentially
// ditch these interfaces so that we don't need to keep updating them every time
// we add a method to the concrete struct.
type Test interface { type Test interface {
Name() string Name() string
Description() string Description() string
@ -16,7 +21,7 @@ type Test interface {
// so that they appear when lazygit runs // so that they appear when lazygit runs
SetupConfig(config *config.AppConfig) SetupConfig(config *config.AppConfig)
// this is called upon lazygit starting // this is called upon lazygit starting
Run(Shell, Input, Assert, config.KeybindingConfig) Run(GuiAdapter)
// e.g. '-debug' // e.g. '-debug'
ExtraCmdArgs() string ExtraCmdArgs() string
// for tests that are flakey and when we don't have time to fix them // for tests that are flakey and when we don't have time to fix them
@ -81,6 +86,7 @@ type Input interface {
} }
// through this interface we assert on the state of the lazygit gui // through this interface we assert on the state of the lazygit gui
// implementation is at pkg/gui/assert.go
type Assert interface { type Assert interface {
WorkingTreeFileCount(int) WorkingTreeFileCount(int)
CommitCount(int) CommitCount(int)
@ -93,80 +99,17 @@ type Assert interface {
Fail(errorMessage string) Fail(errorMessage string)
} }
type TestImpl struct { type GuiAdapter interface {
name string PressKey(string)
description string Keys() config.KeybindingConfig
extraCmdArgs string CurrentContext() types.Context
skip bool Model() *types.Model
setupRepo func(shell Shell) Fail(message string)
setupConfig func(config *config.AppConfig) // These two log methods are for the sake of debugging while testing. There's no need to actually
run func( // commit any logging.
shell Shell, // logs to the normal place that you log to i.e. viewable with `lazygit --logs`
input Input, Log(message string)
assert Assert, // logs in the actual UI (in the commands panel)
keys config.KeybindingConfig, LogUI(message string)
) CheckedOutRef() *models.Branch
}
type NewTestArgs struct {
Description string
SetupRepo func(shell Shell)
SetupConfig func(config *config.AppConfig)
Run func(shell Shell, input Input, assert Assert, keys config.KeybindingConfig)
ExtraCmdArgs string
Skip bool
}
func NewTest(args NewTestArgs) *TestImpl {
return &TestImpl{
name: testNameFromFilePath(),
description: args.Description,
extraCmdArgs: args.ExtraCmdArgs,
skip: args.Skip,
setupRepo: args.SetupRepo,
setupConfig: args.SetupConfig,
run: args.Run,
}
}
var _ Test = (*TestImpl)(nil)
func (self *TestImpl) Name() string {
return self.name
}
func (self *TestImpl) Description() string {
return self.description
}
func (self *TestImpl) ExtraCmdArgs() string {
return self.extraCmdArgs
}
func (self *TestImpl) Skip() bool {
return self.skip
}
func (self *TestImpl) SetupConfig(config *config.AppConfig) {
self.setupConfig(config)
}
func (self *TestImpl) SetupRepo(shell Shell) {
self.setupRepo(shell)
}
func (self *TestImpl) Run(
shell Shell,
input Input,
assert Assert,
keys config.KeybindingConfig,
) {
self.run(shell, input, assert, keys)
}
func testNameFromFilePath() string {
path := utils.FilePath(3)
name := strings.Split(path, "integration/integration_tests/")[1]
return name[:len(name)-len(".go")]
} }

View file

@ -8,11 +8,11 @@ import (
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/integration" "github.com/jesseduffield/lazygit/pkg/integration"
"github.com/jesseduffield/lazygit/pkg/integration/integration_tests"
"github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Deprecated: This file is part of the old way of doing things. See test/runner_new/main.go for the new way
// see docs/Integration_Tests.md // see docs/Integration_Tests.md
// This file can be invoked directly, but you might find it easier to go through // This file can be invoked directly, but you might find it easier to go through
// test/lazyintegration/main.go, which provides a convenient gui wrapper to integration tests. // test/lazyintegration/main.go, which provides a convenient gui wrapper to integration tests.
@ -23,15 +23,28 @@ import (
func main() { func main() {
mode := integration.GetModeFromEnv() mode := integration.GetModeFromEnv()
speedEnv := os.Getenv("SPEED")
includeSkipped := os.Getenv("INCLUDE_SKIPPED") == "true" includeSkipped := os.Getenv("INCLUDE_SKIPPED") == "true"
selectedTestName := os.Args[1] selectedTestName := os.Args[1]
err := integration.RunTests( // check if our given test name actually exists
if selectedTestName != "" {
found := false
for _, test := range integration_tests.Tests {
if test.Name() == selectedTestName {
found = true
break
}
}
if !found {
log.Fatalf("test %s not found. Perhaps you forgot to add it to `pkg/integration/integration_tests/tests.go`?", selectedTestName)
}
}
err := integration.RunTestsNew(
log.Printf, log.Printf,
runCmdInTerminal, runCmdInTerminal,
func(test *integration.Test, f func(*testing.T) error) { func(test types.Test, f func(*testing.T) error) {
if selectedTestName != "" && test.Name != selectedTestName { if selectedTestName != "" && test.Name() != selectedTestName {
return return
} }
if err := f(nil); err != nil { if err := f(nil); err != nil {
@ -39,7 +52,6 @@ func main() {
} }
}, },
mode, mode,
speedEnv,
func(_t *testing.T, expected string, actual string, prefix string) { //nolint:thelper func(_t *testing.T, expected string, actual string, prefix string) { //nolint:thelper
assert.Equal(MockTestingT{}, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual)) assert.Equal(MockTestingT{}, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual))
}, },

View file

@ -8,10 +8,11 @@ import (
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/integration" "github.com/jesseduffield/lazygit/pkg/integration"
"github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Deprecated: This file is part of the old way of doing things. See test/runner_new/main.go for the new way
// see docs/Integration_Tests.md // see docs/Integration_Tests.md
// This file can be invoked directly, but you might find it easier to go through // This file can be invoked directly, but you might find it easier to go through
// test/lazyintegration/main.go, which provides a convenient gui wrapper to integration tests. // test/lazyintegration/main.go, which provides a convenient gui wrapper to integration tests.
@ -22,28 +23,15 @@ import (
func main() { func main() {
mode := integration.GetModeFromEnv() mode := integration.GetModeFromEnv()
speedEnv := os.Getenv("SPEED")
includeSkipped := os.Getenv("INCLUDE_SKIPPED") == "true" includeSkipped := os.Getenv("INCLUDE_SKIPPED") == "true"
selectedTestName := os.Args[1] selectedTestName := os.Args[1]
// check if our given test name actually exists err := integration.RunTests(
if selectedTestName != "" {
found := false
for _, test := range integration.Tests {
if test.Name() == selectedTestName {
found = true
break
}
}
if !found {
log.Fatalf("test %s not found. Perhaps you forgot to add it to `pkg/integration/integration_tests/tests.go`?", selectedTestName)
}
}
err := integration.RunTestsNew(
log.Printf, log.Printf,
runCmdInTerminal, runCmdInTerminal,
func(test types.Test, f func(*testing.T) error) { func(test *integration.Test, f func(*testing.T) error) {
if selectedTestName != "" && test.Name() != selectedTestName { if selectedTestName != "" && test.Name != selectedTestName {
return return
} }
if err := f(nil); err != nil { if err := f(nil); err != nil {
@ -51,6 +39,7 @@ func main() {
} }
}, },
mode, mode,
speedEnv,
func(_t *testing.T, expected string, actual string, prefix string) { //nolint:thelper func(_t *testing.T, expected string, actual string, prefix string) { //nolint:thelper
assert.Equal(MockTestingT{}, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual)) assert.Equal(MockTestingT{}, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual))
}, },