diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-12-27 21:47:37 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-12-27 21:47:37 +1100 |
commit | c5050ecabd57702c7bfb02a38729ed8a4f23b2c5 (patch) | |
tree | 00407f650f067e09589f7175330912435ff21928 | |
parent | 78b495f50a080822121852dfdf27b481dbbd8879 (diff) |
move shell into test driver
39 files changed, 108 insertions, 131 deletions
diff --git a/pkg/integration/components/runner.go b/pkg/integration/components/runner.go index 85ea3f330..964ad98a5 100644 --- a/pkg/integration/components/runner.go +++ b/pkg/integration/components/runner.go @@ -126,7 +126,7 @@ func buildLazygit() error { } func createFixture(test *IntegrationTest, paths Paths) error { - shell := NewShell(paths.ActualRepo()) + shell := NewShell(paths.ActualRepo(), func(errorMsg string) { panic(errorMsg) }) shell.RunCommand("git init -b master") shell.RunCommand(`git config user.email "CI@example.com"`) shell.RunCommand(`git config user.name "CI"`) diff --git a/pkg/integration/components/shell.go b/pkg/integration/components/shell.go index d0f0345dc..3fd63c8cc 100644 --- a/pkg/integration/components/shell.go +++ b/pkg/integration/components/shell.go @@ -15,119 +15,122 @@ import ( type Shell struct { // working directory the shell is invoked in dir string + // when running the shell outside the gui we can directly panic on failure, + // but inside the gui we need to close the gui before panicking + fail func(string) } -func NewShell(dir string) *Shell { - return &Shell{dir: dir} +func NewShell(dir string, fail func(string)) *Shell { + return &Shell{dir: dir, fail: fail} } -func (s *Shell) RunCommand(cmdStr string) *Shell { +func (self *Shell) RunCommand(cmdStr string) *Shell { args := str.ToArgv(cmdStr) cmd := secureexec.Command(args[0], args[1:]...) cmd.Env = os.Environ() - cmd.Dir = s.dir + cmd.Dir = self.dir output, err := cmd.CombinedOutput() if err != nil { - panic(fmt.Sprintf("error running command: %s\n%s", cmdStr, string(output))) + self.fail(fmt.Sprintf("error running command: %s\n%s", cmdStr, string(output))) } - return s + return self } -func (s *Shell) RunShellCommand(cmdStr string) *Shell { +func (self *Shell) RunShellCommand(cmdStr string) *Shell { cmd := secureexec.Command("sh", "-c", cmdStr) cmd.Env = os.Environ() - cmd.Dir = s.dir + cmd.Dir = self.dir output, err := cmd.CombinedOutput() if err != nil { - panic(fmt.Sprintf("error running shell command: %s\n%s", cmdStr, string(output))) + self.fail(fmt.Sprintf("error running shell command: %s\n%s", cmdStr, string(output))) } - return s + return self } -func (s *Shell) RunShellCommandExpectError(cmdStr string) *Shell { +func (self *Shell) RunShellCommandExpectError(cmdStr string) *Shell { cmd := secureexec.Command("sh", "-c", cmdStr) cmd.Env = os.Environ() - cmd.Dir = s.dir + cmd.Dir = self.dir output, err := cmd.CombinedOutput() if err == nil { - panic(fmt.Sprintf("Expected error running shell command: %s\n%s", cmdStr, string(output))) + self.fail(fmt.Sprintf("Expected error running shell command: %s\n%s", cmdStr, string(output))) } - return s + return self } -func (s *Shell) CreateFile(path string, content string) *Shell { - fullPath := filepath.Join(s.dir, path) +func (self *Shell) CreateFile(path string, content string) *Shell { + fullPath := filepath.Join(self.dir, path) err := os.WriteFile(fullPath, []byte(content), 0o644) if err != nil { - panic(fmt.Sprintf("error creating file: %s\n%s", fullPath, err)) + self.fail(fmt.Sprintf("error creating file: %s\n%s", fullPath, err)) } - return s + return self } -func (s *Shell) CreateDir(path string) *Shell { - fullPath := filepath.Join(s.dir, path) +func (self *Shell) CreateDir(path string) *Shell { + fullPath := filepath.Join(self.dir, path) if err := os.MkdirAll(fullPath, 0o755); err != nil { - panic(fmt.Sprintf("error creating directory: %s\n%s", fullPath, err)) + self.fail(fmt.Sprintf("error creating directory: %s\n%s", fullPath, err)) } - return s + return self } -func (s *Shell) UpdateFile(path string, content string) *Shell { - fullPath := filepath.Join(s.dir, path) +func (self *Shell) UpdateFile(path string, content string) *Shell { + fullPath := filepath.Join(self.dir, path) err := os.WriteFile(fullPath, []byte(content), 0o644) if err != nil { - panic(fmt.Sprintf("error updating file: %s\n%s", fullPath, err)) + self.fail(fmt.Sprintf("error updating file: %s\n%s", fullPath, err)) } - return s + return self } -func (s *Shell) NewBranch(name string) *Shell { - return s.RunCommand("git checkout -b " + name) +func (self *Shell) NewBranch(name string) *Shell { + return self.RunCommand("git checkout -b " + name) } -func (s *Shell) Checkout(name string) *Shell { - return s.RunCommand("git checkout " + name) +func (self *Shell) Checkout(name string) *Shell { + return self.RunCommand("git checkout " + name) } -func (s *Shell) Merge(name string) *Shell { - return s.RunCommand("git merge --commit --no-ff " + name) +func (self *Shell) Merge(name string) *Shell { + return self.RunCommand("git merge --commit --no-ff " + name) } -func (s *Shell) GitAdd(path string) *Shell { - return s.RunCommand(fmt.Sprintf("git add \"%s\"", path)) +func (self *Shell) GitAdd(path string) *Shell { + return self.RunCommand(fmt.Sprintf("git add \"%s\"", path)) } -func (s *Shell) GitAddAll() *Shell { - return s.RunCommand("git add -A") +func (self *Shell) GitAddAll() *Shell { + return self.RunCommand("git add -A") } -func (s *Shell) Commit(message string) *Shell { - return s.RunCommand(fmt.Sprintf("git commit -m \"%s\"", message)) +func (self *Shell) Commit(message string) *Shell { + return self.RunCommand(fmt.Sprintf("git commit -m \"%s\"", message)) } -func (s *Shell) EmptyCommit(message string) *Shell { - return s.RunCommand(fmt.Sprintf("git commit --allow-empty -m \"%s\"", message)) +func (self *Shell) EmptyCommit(message string) *Shell { + return self.RunCommand(fmt.Sprintf("git commit --allow-empty -m \"%s\"", message)) } // convenience method for creating a file and adding it -func (s *Shell) CreateFileAndAdd(fileName string, fileContents string) *Shell { - return s. +func (self *Shell) CreateFileAndAdd(fileName string, fileContents string) *Shell { + return self. CreateFile(fileName, fileContents). GitAdd(fileName) } // convenience method for updating a file and adding it -func (s *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell { - return s. +func (self *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell { + return self. UpdateFile(fileName, fileContents). GitAdd(fileName) } @@ -135,24 +138,24 @@ func (s *Shell) UpdateFileAndAdd(fileName string, fileContents string) *Shell { // creates commits 01, 02, 03, ..., n with a new file in each // The reason for padding with zeroes is so that it's easier to do string // matches on the commit messages when there are many of them -func (s *Shell) CreateNCommits(n int) *Shell { +func (self *Shell) CreateNCommits(n int) *Shell { for i := 1; i <= n; i++ { - s.CreateFileAndAdd( + self.CreateFileAndAdd( fmt.Sprintf("file%02d.txt", i), fmt.Sprintf("file%02d content", i), ). Commit(fmt.Sprintf("commit %02d", i)) } - return s + return self } -func (s *Shell) StashWithMessage(message string) *Shell { - s.RunCommand(fmt.Sprintf(`git stash -m "%s"`, message)) - return s +func (self *Shell) StashWithMessage(message string) *Shell { + self.RunCommand(fmt.Sprintf(`git stash -m "%s"`, message)) + return self } -func (s *Shell) SetConfig(key string, value string) *Shell { - s.RunCommand(fmt.Sprintf(`git config --local "%s" %s`, key, value)) - return s +func (self *Shell) SetConfig(key string, value string) *Shell { + self.RunCommand(fmt.Sprintf(`git config --local "%s" %s`, key, value)) + return self } diff --git a/pkg/integration/components/test.go b/pkg/integration/components/test.go index 927dfb36c..0b36a12d9 100644 --- a/pkg/integration/components/test.go +++ b/pkg/integration/components/test.go @@ -24,8 +24,7 @@ type IntegrationTest struct { setupRepo func(shell *Shell) setupConfig func(config *config.AppConfig) run func( - shell *Shell, - testController *TestDriver, + testDriver *TestDriver, keys config.KeybindingConfig, ) } @@ -40,7 +39,7 @@ type NewIntegrationTestArgs struct { // 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, t *TestDriver, keys config.KeybindingConfig) + Run func(t *TestDriver, keys config.KeybindingConfig) // additional args passed to lazygit ExtraCmdArgs string // for when a test is flakey @@ -92,15 +91,15 @@ func (self *IntegrationTest) SetupRepo(shell *Shell) { // I want access to all contexts, the model, the ability to press a key, the ability to log, func (self *IntegrationTest) Run(gui integrationTypes.GuiDriver) { - shell := NewShell("/tmp/lazygit-test") + shell := NewShell("/tmp/lazygit-test", func(errorMsg string) { gui.Fail(errorMsg) }) keys := gui.Keys() - testController := NewTestController(gui, keys, KeyPressDelay()) + testDriver := NewTestDriver(gui, shell, keys, KeyPressDelay()) - self.run(shell, testController, keys) + self.run(testDriver, keys) if KeyPressDelay() > 0 { // the dev would want to see the final state if they're running in slow mode - testController.Wait(2000) + testDriver.Wait(2000) } } diff --git a/pkg/integration/components/test_driver.go b/pkg/integration/components/test_driver.go index 7b3fb0889..f2604d211 100644 --- a/pkg/integration/components/test_driver.go +++ b/pkg/integration/components/test_driver.go @@ -16,14 +16,16 @@ type TestDriver struct { keys config.KeybindingConfig pushKeyDelay int *assertionHelper + shell *Shell } -func NewTestController(gui integrationTypes.GuiDriver, keys config.KeybindingConfig, pushKeyDelay int) *TestDriver { +func NewTestDriver(gui integrationTypes.GuiDriver, shell *Shell, keys config.KeybindingConfig, pushKeyDelay int) *TestDriver { return &TestDriver{ gui: gui, keys: keys, pushKeyDelay: pushKeyDelay, assertionHelper: &assertionHelper{gui: gui}, + shell: shell, } } @@ -67,6 +69,11 @@ func (self *TestDriver) Log(message string) { self.gui.LogUI(message) } +// allows the user to run shell commands during the test to emulate background activity +func (self *TestDriver) Shell() *Shell { + return self.shell +} + // this will look for a list item in the current panel and if it finds it, it will // enter the keypresses required to navigate to it. // The test will fail if: diff --git a/pkg/integration/components/test_test.go b/pkg/integration/components/test_test.go index 79d2b3955..ccab35dc8 100644 --- a/pkg/integration/components/test_test.go +++ b/pkg/integration/components/test_test.go @@ -63,7 +63,7 @@ func (self *fakeGuiDriver) View(viewName string) *gocui.View { func TestAssertionFailure(t *testing.T) { test := NewIntegrationTest(NewIntegrationTestArgs{ Description: unitTestDescription, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.press("a") t.press("b") t.Model().CommitCount(2) @@ -78,7 +78,7 @@ func TestAssertionFailure(t *testing.T) { func TestManualFailure(t *testing.T) { test := NewIntegrationTest(NewIntegrationTestArgs{ Description: unitTestDescription, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Fail("blah") }, }) @@ -90,7 +90,7 @@ func TestManualFailure(t *testing.T) { func TestSuccess(t *testing.T) { test := NewIntegrationTest(NewIntegrationTestArgs{ Description: unitTestDescription, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.press("a") t.press("b") t.Model().CommitCount(0) diff --git a/pkg/integration/tests/bisect/basic.go b/pkg/integration/tests/bisect/basic.go index cbb37aac7..8294f61a4 100644 --- a/pkg/integration/tests/bisect/basic.go +++ b/pkg/integration/tests/bisect/basic.go @@ -14,11 +14,7 @@ var Basic = NewIntegrationTest(NewIntegrationTestArgs{ CreateNCommits(10) }, SetupConfig: func(cfg *config.AppConfig) {}, - Run: func( - shell *Shell, - t *TestDriver, - keys config.KeybindingConfig, - ) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { markCommitAsBad := func() { t.Views().Commits(). Press(keys.Commits.ViewBisectOptions) diff --git a/pkg/integration/tests/bisect/from_other_branch.go b/pkg/integration/tests/bisect/from_other_branch.go index 8759e28f3..13e7e8055 100644 --- a/pkg/integration/tests/bisect/from_other_branch.go +++ b/pkg/integration/tests/bisect/from_other_branch.go @@ -18,11 +18,7 @@ var FromOtherBranch = NewIntegrationTest(NewIntegrationTestArgs{ RunCommand("git bisect start other~2 other~5") }, SetupConfig: func(cfg *config.AppConfig) {}, - Run: func( - shell *Shell, - t *TestDriver, - keys config.KeybindingConfig, - ) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Information().Content(Contains("bisecting")) t.Model().AtLeastOneCommit() diff --git a/pkg/integration/tests/branch/checkout_by_name.go b/pkg/integration/tests/branch/checkout_by_name.go index 7a8733d26..c786f8970 100644 --- a/pkg/integration/tests/branch/checkout_by_name.go +++ b/pkg/integration/tests/branch/checkout_by_name.go @@ -17,7 +17,7 @@ var CheckoutByName = NewIntegrationTest(NewIntegrationTestArgs{ Checkout("master"). EmptyCommit("blah") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Lines( diff --git a/pkg/integration/tests/branch/delete.go b/pkg/integration/tests/branch/delete.go index 6410576e8..520923cd6 100644 --- a/pkg/integration/tests/branch/delete.go +++ b/pkg/integration/tests/branch/delete.go @@ -16,7 +16,7 @@ var Delete = NewIntegrationTest(NewIntegrationTestArgs{ NewBranch("branch-one"). NewBranch("branch-two") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Lines( diff --git a/pkg/integration/tests/branch/rebase.go b/pkg/integration/tests/branch/rebase.go index b90810b82..137bada27 100644 --- a/pkg/integration/tests/branch/rebase.go +++ b/pkg/integration/tests/branch/rebase.go @@ -14,7 +14,7 @@ var Rebase = NewIntegrationTest(NewIntegrationTestArgs{ SetupRepo: func(shell *Shell) { shared.MergeConflictsSetup(shell) }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Commits().TopLines( Contains("first change"), Contains("original"), diff --git a/pkg/integration/tests/branch/rebase_and_drop.go b/pkg/integration/tests/branch/rebase_and_drop.go index 610bb1c19..dc1cfddea 100644 --- a/pkg/integration/tests/branch/rebase_and_drop.go +++ b/pkg/integration/tests/branch/rebase_and_drop.go @@ -17,7 +17,7 @@ var RebaseAndDrop = NewIntegrationTest(NewIntegrationTestArgs{ shell.EmptyCommit("to remove") shell.EmptyCommit("to keep") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Lines( diff --git a/pkg/integration/tests/branch/reset.go b/pkg/integration/tests/branch/reset.go index bfd1b950f..65a10e31b 100644 --- a/pkg/integration/tests/branch/reset.go +++ b/pkg/integration/tests/branch/reset.go @@ -20,7 +20,7 @@ var Reset = NewIntegrationTest(NewIntegrationTestArgs{ shell.Checkout("current-branch") shell.EmptyCommit("current-branch commit") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Commits().Lines( Contains("current-branch commit"), Contains("root commit"), diff --git a/pkg/integration/tests/branch/suggestions.go b/pkg/integration/tests/branch/suggestions.go index 438bbf9be..65c64fa6d 100644 --- a/pkg/integration/tests/branch/suggestions.go +++ b/pkg/integration/tests/branch/suggestions.go @@ -20,7 +20,7 @@ var Suggestions = NewIntegrationTest(NewIntegrationTestArgs{ NewBranch("other-new-branch-2"). NewBranch("other-new-branch-3") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Press(keys.Branches.CheckoutBranchByName) diff --git a/pkg/integration/tests/cherry_pick/cherry_pick.go b/pkg/integration/tests/cherry_pick/cherry_pick.go index 5fa2d47ac..a9d7dd9c7 100644 --- a/pkg/integration/tests/cherry_pick/cherry_pick.go +++ b/pkg/integration/tests/cherry_pick/cherry_pick.go @@ -23,7 +23,7 @@ var CherryPick = NewIntegrationTest(NewIntegrationTestArgs{ EmptyCommit("four"). Checkout("first-branch") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Lines( diff --git a/pkg/integration/tests/cherry_pick/cherry_pick_conflicts.go b/pkg/integration/tests/cherry_pick/cherry_pick_conflicts.go index 5de63baa6..5eef30b50 100644 --- a/pkg/integration/tests/cherry_pick/cherry_pick_conflicts.go +++ b/pkg/integration/tests/cherry_pick/cherry_pick_conflicts.go @@ -14,7 +14,7 @@ var CherryPickConflicts = NewIntegrationTest(NewIntegrationTestArgs{ SetupRepo: func(shell *Shell) { shared.MergeConflictsSetup(shell) }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Views().Branches(). Focus(). Lines( diff --git a/pkg/integration/tests/commit/commit.go b/pkg/integration/tests/commit/commit.go index 951529cea..5950545e7 100644 --- a/pkg/integration/tests/commit/commit.go +++ b/pkg/integration/tests/commit/commit.go @@ -14,7 +14,7 @@ var Commit = NewIntegrationTest(NewIntegrationTestArgs{ shell.CreateFile("myfile", "myfile content") shell.CreateFile("myfile2", "myfile2 content") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(0) t.Views().Files(). diff --git a/pkg/integration/tests/commit/commit_multiline.go b/pkg/integration/tests/commit/commit_multiline.go index 823412f68..11876e09a 100644 --- a/pkg/integration/tests/commit/commit_multiline.go +++ b/pkg/integration/tests/commit/commit_multiline.go @@ -13,7 +13,7 @@ var CommitMultiline = NewIntegrationTest(NewIntegrationTestArgs{ SetupRepo: func(shell *Shell) { shell.CreateFile("myfile", "myfile content") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(0) t.Views().Files(). diff --git a/pkg/integration/tests/commit/new_branch.go b/pkg/integration/tests/commit/new_branch.go index f2deecdc9..6f528ff4c 100644 --- a/pkg/integration/tests/commit/new_branch.go +++ b/pkg/integration/tests/commit/new_branch.go @@ -16,7 +16,7 @@ var NewBranch = NewIntegrationTest(NewIntegrationTestArgs{ EmptyCommit("commit 2"). EmptyCommit("commit 3") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(3) t.Views().Commits(). diff --git a/pkg/integration/tests/commit/revert.go b/pkg/integration/tests/commit/revert.go index c2f3b9a52..bee657380 100644 --- a/pkg/integration/tests/commit/revert.go +++ b/pkg/integration/tests/commit/revert.go @@ -15,7 +15,7 @@ var Revert = NewIntegrationTest(NewIntegrationTestArgs{ shell.GitAddAll() shell.Commit("first commit") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(1) t.Views().Commits(). diff --git a/pkg/integration/tests/commit/staged.go b/pkg/integration/tests/commit/staged.go index 559231f53..d0698192a 100644 --- a/pkg/integration/tests/commit/staged.go +++ b/pkg/integration/tests/commit/staged.go @@ -15,7 +15,7 @@ var Staged = NewIntegrationTest(NewIntegrationTestArgs{ CreateFile("myfile", "myfile content\nwith a second line"). CreateFile("myfile2", "myfile2 content") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(0) t.Views().Files(). diff --git a/pkg/integration/tests/commit/staged_without_hooks.go b/pkg/integration/tests/commit/staged_without_hooks.go index be0d43904..8a6b0d3d0 100644 --- a/pkg/integration/tests/commit/staged_without_hooks.go +++ b/pkg/integration/tests/commit/staged_without_hooks.go @@ -15,7 +15,7 @@ var StagedWithoutHooks = NewIntegrationTest(NewIntegrationTestArgs{ CreateFile("myfile", "myfile content\nwith a second line"). CreateFile("myfile2", "myfile2 content") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(0) // stage the file diff --git a/pkg/integration/tests/commit/unstaged.go b/pkg/integration/tests/commit/unstaged.go index 69eb4e814..3e0d68617 100644 --- a/pkg/integration/tests/commit/unstaged.go +++ b/pkg/integration/tests/commit/unstaged.go @@ -17,7 +17,7 @@ var Unstaged = NewIntegrationTest(NewIntegrationTestArgs{ CreateFile("myfile", "myfile content\nwith a second line"). CreateFile("myfile2", "myfile2 content") }, - Run: func(shell *Shell, t *TestDriver, keys config.KeybindingConfig) { + Run: func(t *TestDriver, keys config.KeybindingConfig) { t.Model().CommitCount(0) t.Views().Files(). diff --git a/pkg/i |