From 5e475355bfab17bc8281a230798cba19c7c6ac6c Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 13 Aug 2022 13:50:38 +1000 Subject: [PATCH] add tests for my tests --- pkg/integration/README.md | 2 +- pkg/integration/components/test.go | 13 ++- pkg/integration/components/test_test.go | 105 ++++++++++++++++++ .../{integration_test.go => go_test.go} | 0 .../{integration_test.go => go_test.go} | 0 pkg/integration/integration.go | 2 +- 6 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 pkg/integration/components/test_test.go rename pkg/integration/deprecated/{integration_test.go => go_test.go} (100%) rename pkg/integration/{integration_test.go => go_test.go} (100%) diff --git a/pkg/integration/README.md b/pkg/integration/README.md index 978a20600..01d5786b1 100644 --- a/pkg/integration/README.md +++ b/pkg/integration/README.md @@ -39,7 +39,7 @@ There are three ways to invoke a test: 1. go run pkg/integration/cmd/runner/main.go [...] 2. go run pkg/integration/cmd/tui/main.go -3. go test pkg/integration/integration_test.go +3. go test pkg/integration/go_test.go The first, the test runner, is for directly running a test from the command line. If you pass no arguments, it runs all tests. The second, the TUI, is for running tests from a terminal UI where it's easier to find a test and run it without having to copy it's name and paste it into the terminal. This is the easiest approach by far. diff --git a/pkg/integration/components/test.go b/pkg/integration/components/test.go index 2dc60c51f..a5973c07a 100644 --- a/pkg/integration/components/test.go +++ b/pkg/integration/components/test.go @@ -12,6 +12,10 @@ import ( // Test describes an integration tests that will be run against the lazygit gui. +// our unit tests will use this description to avoid a panic caused by attempting +// to get the test's name via it's file's path. +const unitTestDescription = "test test" + type IntegrationTest struct { name string description string @@ -45,8 +49,15 @@ type NewIntegrationTestArgs struct { } func NewIntegrationTest(args NewIntegrationTestArgs) *IntegrationTest { + name := "" + if args.Description != unitTestDescription { + // this panics if we're in a unit test for our integration tests, + // so we're using "test test" as a sentinel value + name = testNameFromFilePath() + } + return &IntegrationTest{ - name: testNameFromFilePath(), + name: name, description: args.Description, extraCmdArgs: args.ExtraCmdArgs, skip: args.Skip, diff --git a/pkg/integration/components/test_test.go b/pkg/integration/components/test_test.go new file mode 100644 index 000000000..3be317424 --- /dev/null +++ b/pkg/integration/components/test_test.go @@ -0,0 +1,105 @@ +package components + +import ( + "testing" + + "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" +) + +type fakeGuiDriver struct { + failureMessage string + pressedKeys []string +} + +var _ integrationTypes.GuiDriver = &fakeGuiDriver{} + +type GuiDriver interface { + PressKey(string) + Keys() config.KeybindingConfig + CurrentContext() types.Context + Model() *types.Model + Fail(message string) + // These two log methods are for the sake of debugging while testing. There's no need to actually + // commit any logging. + // logs to the normal place that you log to i.e. viewable with `lazygit --logs` + Log(message string) + // logs in the actual UI (in the commands panel) + LogUI(message string) + CheckedOutRef() *models.Branch +} + +func (self *fakeGuiDriver) PressKey(key string) { + self.pressedKeys = append(self.pressedKeys, key) +} + +func (self *fakeGuiDriver) Keys() config.KeybindingConfig { + return config.KeybindingConfig{} +} + +func (self *fakeGuiDriver) CurrentContext() types.Context { + return nil +} + +func (self *fakeGuiDriver) Model() *types.Model { + return &types.Model{Commits: []*models.Commit{}} +} + +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 +} + +func TestAssertionFailure(t *testing.T) { + test := NewIntegrationTest(NewIntegrationTestArgs{ + Description: unitTestDescription, + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { + input.PressKeys("a") + input.PressKeys("b") + assert.CommitCount(2) + }, + }) + driver := &fakeGuiDriver{} + test.Run(driver) + assert.EqualValues(t, []string{"a", "b"}, driver.pressedKeys) + assert.Equal(t, "Expected 2 commits present, but got 0", driver.failureMessage) +} + +func TestManualFailure(t *testing.T) { + test := NewIntegrationTest(NewIntegrationTestArgs{ + Description: unitTestDescription, + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { + assert.Fail("blah") + }, + }) + driver := &fakeGuiDriver{} + test.Run(driver) + assert.Equal(t, "blah", driver.failureMessage) +} + +func TestSuccess(t *testing.T) { + test := NewIntegrationTest(NewIntegrationTestArgs{ + Description: unitTestDescription, + Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) { + input.PressKeys("a") + input.PressKeys("b") + assert.CommitCount(0) + }, + }) + driver := &fakeGuiDriver{} + test.Run(driver) + assert.EqualValues(t, []string{"a", "b"}, driver.pressedKeys) + assert.Equal(t, "", driver.failureMessage) +} diff --git a/pkg/integration/deprecated/integration_test.go b/pkg/integration/deprecated/go_test.go similarity index 100% rename from pkg/integration/deprecated/integration_test.go rename to pkg/integration/deprecated/go_test.go diff --git a/pkg/integration/integration_test.go b/pkg/integration/go_test.go similarity index 100% rename from pkg/integration/integration_test.go rename to pkg/integration/go_test.go diff --git a/pkg/integration/integration.go b/pkg/integration/integration.go index 68dade273..fd7b0fee6 100644 --- a/pkg/integration/integration.go +++ b/pkg/integration/integration.go @@ -58,7 +58,7 @@ func RunTests( testDir := filepath.Join(rootDir, "test", "integration_new") osCommand := oscommands.NewDummyOSCommand() - err = osCommand.Cmd.New("go build pkg/integration/cmd/intector.go -o " + tempLazygitPath()).Run() + err = osCommand.Cmd.New(fmt.Sprintf("go build -o %s pkg/integration/cmd/injector.go", tempLazygitPath())).Run() if err != nil { return err }