1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

various changes to improve integration tests

This commit is contained in:
Jesse Duffield
2022-09-09 21:11:05 -07:00
parent 7b757d1cfe
commit 7af7af27c6
103 changed files with 2883 additions and 1525 deletions

View File

@ -111,6 +111,13 @@ func (self *Assert) CurrentViewName(expectedViewName string) {
})
}
func (self *Assert) CurrentWindowName(expectedWindowName string) {
self.assertWithRetries(func() (bool, string) {
actual := self.gui.CurrentContext().GetView().Name()
return actual == expectedWindowName, fmt.Sprintf("Expected current window name to be '%s', but got '%s'", expectedWindowName, actual)
})
}
func (self *Assert) CurrentBranchName(expectedViewName string) {
self.assertWithRetries(func() (bool, string) {
actual := self.gui.CheckedOutRef().Name
@ -210,7 +217,7 @@ func (self *Assert) matchString(matcher *matcher, context string, getValue func(
}
func (self *Assert) assertWithRetries(test func() (bool, string)) {
waitTimes := []int{0, 1, 5, 10, 200, 500, 1000}
waitTimes := []int{0, 1, 5, 10, 200, 500, 1000, 2000}
var message string
for _, waitTime := range waitTimes {

View File

@ -42,22 +42,27 @@ func (self *Input) pressKey(keyStr string) {
func (self *Input) SwitchToStatusWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[0])
self.assert.CurrentWindowName("status")
}
func (self *Input) SwitchToFilesWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[1])
self.assert.CurrentWindowName("files")
}
func (self *Input) SwitchToBranchesWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[2])
self.assert.CurrentWindowName("localBranches")
}
func (self *Input) SwitchToCommitsWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[3])
self.assert.CurrentWindowName("commits")
}
func (self *Input) SwitchToStashWindow() {
self.pressKey(self.keys.Universal.JumpToBlock[4])
self.assert.CurrentWindowName("stash")
}
func (self *Input) Type(content string) {
@ -132,38 +137,43 @@ func (self *Input) NavigateToListItemContainingText(text string) {
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 {
self.assert.MatchSelectedLine(Contains(text))
return
}
if selectedLineIdx < matchIndex {
for i := selectedLineIdx; i < matchIndex; i++ {
self.NextItem()
}
self.assert.MatchSelectedLine(Contains(text))
return
} else {
for i := selectedLineIdx; i > matchIndex; i-- {
self.PreviousItem()
}
self.assert.MatchSelectedLine(Contains(text))
return
}
}
var matchIndex int
self.assert.Fail(fmt.Sprintf("Could not find item containing text: %s", text))
self.assert.assertWithRetries(func() (bool, string) {
matchCount := 0
matchIndex = -1
// first we look for a duplicate on the current screen. We won't bother looking beyond that though.
for i, line := range view.ViewBufferLines() {
if strings.Contains(line, text) {
matchCount++
matchIndex = i
}
}
if matchCount > 1 {
return false, fmt.Sprintf("Found %d matches for %s, expected only a single match", matchCount, text)
} else if matchCount == 0 {
return false, fmt.Sprintf("Could not find item containing text: %s", text)
} else {
return true, ""
}
})
selectedLineIdx := view.SelectedLineIdx()
if selectedLineIdx == matchIndex {
self.assert.MatchSelectedLine(Contains(text))
return
}
if selectedLineIdx < matchIndex {
for i := selectedLineIdx; i < matchIndex; i++ {
self.NextItem()
}
self.assert.MatchSelectedLine(Contains(text))
return
} else {
for i := selectedLineIdx; i > matchIndex; i-- {
self.PreviousItem()
}
self.assert.MatchSelectedLine(Contains(text))
return
}
}

View File

@ -58,11 +58,11 @@ func RunTests(
for _, test := range tests {
test := test
paths := NewPaths(
filepath.Join(testDir, test.Name()),
)
testWrapper(test, func() error { //nolint: thelper
paths := NewPaths(
filepath.Join(testDir, test.Name()),
)
return runTest(test, paths, projectRootDir, logf, runCmd, mode, keyPressDelay)
})
}
@ -126,16 +126,7 @@ func buildLazygit() error {
}
func createFixture(test *IntegrationTest, paths Paths) error {
originalDir, err := os.Getwd()
if err != nil {
return err
}
if err := os.Chdir(paths.ActualRepo()); err != nil {
panic(err)
}
shell := NewShell()
shell := NewShell(paths.ActualRepo())
shell.RunCommand("git init -b master")
shell.RunCommand(`git config user.email "CI@example.com"`)
shell.RunCommand(`git config user.name "CI"`)
@ -143,10 +134,6 @@ func createFixture(test *IntegrationTest, paths Paths) error {
test.SetupRepo(shell)
if err := os.Chdir(originalDir); err != nil {
panic(err)
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/jesseduffield/lazygit/pkg/secureexec"
"github.com/mgutz/str"
@ -12,16 +13,20 @@ import (
// this is for running shell commands, mostly for the sake of setting up the repo
// but you can also run the commands from within lazygit to emulate things happening
// in the background.
type Shell struct{}
type Shell struct {
// working directory the shell is invoked in
dir string
}
func NewShell() *Shell {
return &Shell{}
func NewShell(dir string) *Shell {
return &Shell{dir: dir}
}
func (s *Shell) RunCommand(cmdStr string) *Shell {
args := str.ToArgv(cmdStr)
cmd := secureexec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
cmd.Dir = s.dir
output, err := cmd.CombinedOutput()
if err != nil {
@ -32,18 +37,20 @@ func (s *Shell) RunCommand(cmdStr string) *Shell {
}
func (s *Shell) CreateFile(path string, content string) *Shell {
err := ioutil.WriteFile(path, []byte(content), 0o644)
fullPath := filepath.Join(s.dir, path)
err := ioutil.WriteFile(fullPath, []byte(content), 0o644)
if err != nil {
panic(fmt.Sprintf("error creating file: %s\n%s", path, err))
panic(fmt.Sprintf("error creating file: %s\n%s", fullPath, err))
}
return s
}
func (s *Shell) UpdateFile(path string, content string) *Shell {
err := ioutil.WriteFile(path, []byte(content), 0o644)
fullPath := filepath.Join(s.dir, path)
err := ioutil.WriteFile(fullPath, []byte(content), 0o644)
if err != nil {
panic(fmt.Sprintf("error updating file: %s\n%s", path, err))
panic(fmt.Sprintf("error updating file: %s\n%s", fullPath, err))
}
return s

View File

@ -174,7 +174,7 @@ func (self *Snapshotter) compareSnapshots() error {
}
self.logf("%s", string(bytes))
return errors.New(getDiff(f.Name(), actualRepo, expectedRepo))
return errors.New(getDiff(f.Name(), expectedRepo, actualRepo))
}
}

View File

@ -93,7 +93,7 @@ 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()
shell := NewShell("/tmp/lazygit-test")
assert := NewAssert(gui)
keys := gui.Keys()
input := NewInput(gui, keys, assert, KeyPressDelay())

View File

@ -56,6 +56,10 @@ func (self *fakeGuiDriver) SecondaryView() *gocui.View {
return nil
}
func (self *fakeGuiDriver) View(viewName string) *gocui.View {
return nil
}
func TestAssertionFailure(t *testing.T) {
test := NewIntegrationTest(NewIntegrationTestArgs{
Description: unitTestDescription,