diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-09 21:27:12 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-13 13:55:08 +1000 |
commit | ba96baee32f5d02173312b357327eb7478492f89 (patch) | |
tree | fbd0c91d5799f9519d617f7041518c0c7e14d2bc | |
parent | d890238c7bcbdd62e7158df0c1f3f0e5c0b05b66 (diff) |
move code from main into app package to allow test to be injected
68 files changed, 1080 insertions, 466 deletions
@@ -6,6 +6,8 @@ import ( "github.com/integrii/flaggy" "github.com/jesseduffield/lazygit/pkg/app" + "github.com/jesseduffield/lazygit/pkg/integration" + integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" ) @@ -24,8 +26,9 @@ var ( func main() { cliArgs := parseCliArgsAndEnvVars() buildInfo := getBuildInfo() + integrationTest := getIntegrationTest() - app.Start(cliArgs, buildInfo, nil) + app.Start(cliArgs, buildInfo, integrationTest) } func parseCliArgsAndEnvVars() *app.CliArgs { @@ -129,3 +132,20 @@ func getBuildInfo() *app.BuildInfo { return buildInfo } + +func getIntegrationTest() integrationTypes.IntegrationTest { + integrationTestName := os.Getenv("LAZYGIT_TEST_NAME") + if integrationTestName == "" { + return nil + } + + // unsetting so that if we run lazygit in as a 'daemon' we don't think we're trying to run a test again + os.Unsetenv("LAZYGIT_TEST_NAME") + for _, candidateTest := range integration.Tests { + if candidateTest.Name() == integrationTestName { + return candidateTest + } + } + + panic("Could not find integration test with name: " + integrationTestName) +} diff --git a/pkg/app/app.go b/pkg/app/app.go index a45ffb118..3a1c127de 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -14,6 +14,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/generics/slices" + appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" @@ -22,7 +23,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/gui" - "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/updates" ) @@ -41,7 +41,7 @@ type App struct { func Run( config config.AppConfigurer, common *common.Common, - startArgs types.StartArgs, + startArgs appTypes.StartArgs, ) { app, err := NewApp(config, common) @@ -217,7 +217,7 @@ func (app *App) setupRepo() (bool, error) { return false, nil } -func (app *App) Run(startArgs types.StartArgs) error { +func (app *App) Run(startArgs appTypes.StartArgs) error { err := app.Gui.RunAndHandleError(startArgs) return err } diff --git a/pkg/app/run.go b/pkg/app/entry_point.go index b1e2c396f..551b959a6 100644 --- a/pkg/app/run.go +++ b/pkg/app/entry_point.go @@ -10,9 +10,9 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/app/daemon" + appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/env" - "github.com/jesseduffield/lazygit/pkg/gui/types" integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/logs" "gopkg.in/yaml.v3" @@ -40,12 +40,7 @@ type BuildInfo struct { BuildSource string } -// only used when running integration tests -type TestConfig struct { - Test integrationTypes.Test -} - -func Start(cliArgs *CliArgs, buildInfo *BuildInfo, test integrationTypes.Test) { +func Start(cliArgs *CliArgs, buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTest) { if cliArgs.RepoPath != "" { if cliArgs.WorkTree != "" || cliArgs.GitDir != "" { log.Fatal("--path option is incompatible with the --work-tree and --git-dir options") @@ -118,8 +113,8 @@ func Start(cliArgs *CliArgs, buildInfo *BuildInfo, test integrationTypes.Test) { log.Fatal(err.Error()) } - if test != nil { - test.SetupConfig(appConfig) + if integrationTest != nil { + integrationTest.SetupConfig(appConfig) } common, err := NewCommon(appConfig) @@ -134,23 +129,23 @@ func Start(cliArgs *CliArgs, buildInfo *BuildInfo, test integrationTypes.Test) { parsedGitArg := parseGitArg(cliArgs.GitArg) - Run(appConfig, common, types.NewStartArgs(cliArgs.FilterPath, parsedGitArg, test)) + Run(appConfig, common, appTypes.NewStartArgs(cliArgs.FilterPath, parsedGitArg, integrationTest)) } -func parseGitArg(gitArg string) types.GitArg { - typedArg := types.GitArg(gitArg) +func parseGitArg(gitArg string) appTypes.GitArg { + typedArg := appTypes.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: + case appTypes.GitArgNone, appTypes.GitArgStatus, appTypes.GitArgBranch, appTypes.GitArgLog, appTypes.GitArgStash: return typedArg } permittedValues := []string{ - string(types.GitArgStatus), - string(types.GitArgBranch), - string(types.GitArgLog), - string(types.GitArgStash), + string(appTypes.GitArgStatus), + string(appTypes.GitArgBranch), + string(appTypes.GitArgLog), + string(appTypes.GitArgStash), } log.Fatalf("Invalid git arg value: '%s'. Must be one of the following values: %s. e.g. 'lazygit status'. See 'lazygit --help'.", diff --git a/pkg/gui/types/main_args.go b/pkg/app/types/types.go index 7d9b9fbb7..002111087 100644 --- a/pkg/gui/types/main_args.go +++ b/pkg/app/types/types.go @@ -1,4 +1,8 @@ -package types +package app + +import ( + integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" +) // StartArgs is the struct that represents some things we want to do on program start type StartArgs struct { @@ -7,7 +11,7 @@ type StartArgs struct { // GitArg determines what context we open in GitArg GitArg // integration test (only relevant when invoking lazygit in the context of an integration test) - Test Test + IntegrationTest integrationTypes.IntegrationTest } type GitArg string @@ -20,10 +24,10 @@ const ( GitArgStash GitArg = "stash" ) -func NewStartArgs(filterPath string, gitArg GitArg, test Test) StartArgs { +func NewStartArgs(filterPath string, gitArg GitArg, test integrationTypes.IntegrationTest) StartArgs { return StartArgs{ - FilterPath: filterPath, - GitArg: gitArg, - Test: test, + FilterPath: filterPath, + GitArg: gitArg, + IntegrationTest: test, } } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 11c8af78b..8be5a4a4d 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -10,6 +10,7 @@ import ( "time" "github.com/jesseduffield/gocui" + appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_config" @@ -31,6 +32,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/services/custom_commands" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" + integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/updates" @@ -213,7 +215,7 @@ const ( COMPLETE ) -func (gui *Gui) onNewRepo(startArgs types.StartArgs, reuseState bool) error { +func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, reuseState bool) error { var err error gui.git, err = commands.NewGitCommand( gui.Common, @@ -245,7 +247,7 @@ func (gui *Gui) onNewRepo(startArgs types.StartArgs, reuseState bool) error { // it gets a bit confusing to land back in the status panel when visiting a repo // you've already switched from. There's no doubt some easy way to make the UX // optimal for all cases but I'm too lazy to think about what that is right now -func (gui *Gui) resetState(startArgs types.StartArgs, reuseState bool) { +func (gui *Gui) resetState(startArgs appTypes.StartArgs, reuseState bool) { currentDir, err := os.Getwd() if reuseState { @@ -300,28 +302,28 @@ func (gui *Gui) resetState(startArgs types.StartArgs, reuseState bool) { gui.RepoStateMap[Repo(currentDir)] = gui.State } -func initialScreenMode(startArgs types.StartArgs) WindowMaximisation { - if startArgs.FilterPath != "" || startArgs.GitArg != types.GitArgNone { +func initialScreenMode(startArgs appTypes.StartArgs) WindowMaximisation { + if startArgs.FilterPath != "" || startArgs.GitArg != appTypes.GitArgNone { return SCREEN_HALF } else { return SCREEN_NORMAL } } -func initialContext(contextTree *context.ContextTree, startArgs types.StartArgs) types.IListContext { +func initialContext(contextTree *context.ContextTree, startArgs appTypes.StartArgs) types.IListContext { var initialContext types.IListContext = contextTree.Files if startArgs.FilterPath != "" { initialContext = contextTree.LocalCommits - } else if startArgs.GitArg != types.GitArgNone { + } else if startArgs.GitArg != appTypes.GitArgNone { switch startArgs.GitArg { - case types.GitArgStatus: + case appTypes.GitArgStatus: initialContext = contextTree.Files - case types.GitArgBranch: + case appTypes.GitArgBranch: initialContext = contextTree.Branches - case types.GitArgLog: + case appTypes.GitArgLog: initialContext = contextTree.LocalCommits - case types.GitArgStash: + case appTypes.GitArgStash: initialContext = contextTree.Stash default: panic("unhandled git arg") @@ -417,7 +419,7 @@ var RuneReplacements = map[rune]string{ graph.CommitSymbol: "o", } -func (gui *Gui) initGocui(headless bool, test types.Test) (*gocui.Gui, error) { +func (gui *Gui) initGocui(headless bool, test integrationTypes.IntegrationTest) (*gocui.Gui, error) { recordEvents := RecordingEvents() playMode := gocui.NORMAL if recordEvents { @@ -476,8 +478,8 @@ func (gui *Gui) viewTabMap() map[string][]context.TabView { } // Run: setup the gui with keybindings and start the mainloop -func (gui *Gui) Run(startArgs types.StartArgs) error { - g, err := gui.initGocui(Headless(), startArgs.Test) +func (gui *Gui) Run(startArgs appTypes.StartArgs) error { + g, err := gui.initGocui(Headless(), startArgs.IntegrationTest) if err != nil { return err } @@ -492,7 +494,7 @@ func (gui *Gui) Run(startArgs types.StartArgs) error { }) deadlock.Opts.Disable = !gui.Debug - gui.handleTestMode(startArgs.Test) + gui.handleTestMode(startArgs.IntegrationTest) gui.g.OnSearchEscape = gui.onSearchEscape if err := gui.Config.ReloadUserConfig(); err != nil { @@ -553,7 +555,7 @@ func (gui *Gui) Run(startArgs types.StartArgs) error { return gui.g.MainLoop() } -func (gui *Gui) RunAndHandleError(startArgs types.StartArgs) error { +func (gui *Gui) RunAndHandleError(startArgs appTypes.StartArgs) error { gui.stopChan = make(chan struct{}) return utils.SafeWithError(func() error { if err := gui.Run(startArgs); err != nil { diff --git a/pkg/gui/gui_adapter_impl.go b/pkg/gui/gui_adapter.go index 427b8eb47..5566f8b0f 100644 --- a/pkg/gui/gui_adapter_impl.go +++ b/pkg/gui/gui_adapter.go @@ -9,17 +9,18 @@ import ( "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 g |