1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-28 05:46:16 +02:00

177 lines
4.2 KiB
Go
Raw Normal View History

2022-08-13 13:50:38 +10:00
package components
import (
"testing"
2022-08-14 20:13:39 +10:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2022-08-13 13:50:38 +10:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
"github.com/stretchr/testify/assert"
)
2023-02-26 11:49:15 +11:00
// this file is for testing our test code (meta, I know)
type coordinate struct {
x, y int
}
2022-08-13 13:50:38 +10:00
type fakeGuiDriver struct {
failureMessage string
pressedKeys []string
clickedCoordinates []coordinate
2022-08-13 13:50:38 +10:00
}
var _ integrationTypes.GuiDriver = &fakeGuiDriver{}
func (self *fakeGuiDriver) PressKey(key string) {
self.pressedKeys = append(self.pressedKeys, key)
}
func (self *fakeGuiDriver) Click(x, y int) {
self.clickedCoordinates = append(self.clickedCoordinates, coordinate{x: x, y: y})
}
2022-08-13 13:50:38 +10:00
func (self *fakeGuiDriver) Keys() config.KeybindingConfig {
return config.KeybindingConfig{}
}
func (self *fakeGuiDriver) CurrentContext() types.Context {
return nil
}
func (self *fakeGuiDriver) ContextForView(viewName string) types.Context {
return nil
}
2022-08-13 13:50:38 +10:00
func (self *fakeGuiDriver) Fail(message string) {
self.failureMessage = message
}
func (self *fakeGuiDriver) Log(message string) {
}
func (self *fakeGuiDriver) LogUI(message string) {
}
func (self *fakeGuiDriver) CheckedOutRef() *models.Branch {
return nil
}
2022-08-14 20:13:39 +10:00
func (self *fakeGuiDriver) MainView() *gocui.View {
return nil
}
func (self *fakeGuiDriver) SecondaryView() *gocui.View {
return nil
}
func (self *fakeGuiDriver) View(viewName string) *gocui.View {
return nil
}
func (self *fakeGuiDriver) SetCaption(string) {
}
func (self *fakeGuiDriver) SetCaptionPrefix(string) {
}
func (self *fakeGuiDriver) NextToast() *string {
return nil
}
func (self *fakeGuiDriver) CheckAllToastsAcknowledged() {}
2022-08-13 13:50:38 +10:00
func TestManualFailure(t *testing.T) {
test := NewIntegrationTest(NewIntegrationTestArgs{
Description: unitTestDescription,
2022-12-27 21:47:37 +11:00
Run: func(t *TestDriver, keys config.KeybindingConfig) {
2022-12-27 21:35:36 +11:00
t.Fail("blah")
2022-08-13 13:50:38 +10:00
},
})
driver := &fakeGuiDriver{}
test.Run(driver)
assert.Equal(t, "blah", driver.failureMessage)
}
func TestSuccess(t *testing.T) {
test := NewIntegrationTest(NewIntegrationTestArgs{
Description: unitTestDescription,
2022-12-27 21:47:37 +11:00
Run: func(t *TestDriver, keys config.KeybindingConfig) {
2022-12-27 21:35:36 +11:00
t.press("a")
t.press("b")
t.click(0, 1)
t.click(2, 3)
2022-08-13 13:50:38 +10:00
},
})
driver := &fakeGuiDriver{}
test.Run(driver)
assert.EqualValues(t, []string{"a", "b"}, driver.pressedKeys)
assert.EqualValues(t, []coordinate{{0, 1}, {2, 3}}, driver.clickedCoordinates)
2022-08-13 13:50:38 +10:00
assert.Equal(t, "", driver.failureMessage)
}
func TestGitVersionRestriction(t *testing.T) {
scenarios := []struct {
testName string
gitVersion GitVersionRestriction
expectedShouldRun bool
}{
{
2023-04-15 12:39:30 +02:00
testName: "AtLeast, current is newer",
gitVersion: AtLeast("2.24.9"),
expectedShouldRun: true,
},
{
2023-04-15 12:39:30 +02:00
testName: "AtLeast, current is same",
gitVersion: AtLeast("2.25.0"),
expectedShouldRun: true,
},
{
2023-04-15 12:39:30 +02:00
testName: "AtLeast, current is older",
gitVersion: AtLeast("2.26.0"),
expectedShouldRun: false,
},
{
testName: "Before, current is older",
gitVersion: Before("2.24.9"),
expectedShouldRun: false,
},
{
testName: "Before, current is same",
gitVersion: Before("2.25.0"),
expectedShouldRun: false,
},
{
testName: "Before, current is newer",
gitVersion: Before("2.26.0"),
expectedShouldRun: true,
},
{
testName: "Includes, current is included",
gitVersion: Includes("2.23.0", "2.25.0"),
expectedShouldRun: true,
},
{
testName: "Includes, current is not included",
gitVersion: Includes("2.23.0", "2.27.0"),
expectedShouldRun: false,
},
}
currentGitVersion := git_commands.GitVersion{Major: 2, Minor: 25, Patch: 0}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
test := NewIntegrationTest(NewIntegrationTestArgs{
Description: unitTestDescription,
GitVersion: s.gitVersion,
})
shouldRun := test.ShouldRunForGitVersion(&currentGitVersion)
assert.Equal(t, shouldRun, s.expectedShouldRun)
})
}
}