1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-19 21:28:28 +02:00

129 lines
3.1 KiB
Go
Raw Normal View History

2021-04-03 21:56:42 +11:00
package main
import (
"fmt"
"io/ioutil"
2021-04-05 10:20:02 +10:00
"log"
2021-04-03 21:56:42 +11:00
"os"
"path/filepath"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-04-05 20:37:18 +10:00
"github.com/jesseduffield/lazygit/pkg/integration"
2021-04-05 10:20:02 +10:00
"github.com/stretchr/testify/assert"
2021-04-03 21:56:42 +11:00
)
2021-04-05 20:37:18 +10:00
// This file can be invoked directly, but you might find it easier to go through
// test/lazyintegration/main.go, which provides a convenient gui wrapper to integration
// tests.
2021-04-03 21:56:42 +11:00
//
2021-04-05 20:37:18 +10:00
// If invoked directly, you can specify a test by passing it as the first argument.
// You can also specify that you want to record a test by passing RECORD_EVENTS=true
// as an env var.
2021-04-03 21:56:42 +11:00
2021-04-05 20:37:18 +10:00
func main() {
err := test()
2021-04-03 21:56:42 +11:00
if err != nil {
2021-04-05 20:37:18 +10:00
panic(err)
2021-04-03 21:56:42 +11:00
}
}
2021-04-05 20:37:18 +10:00
func test() error {
rootDir := integration.GetRootDirectory()
2021-04-03 21:56:42 +11:00
err := os.Chdir(rootDir)
if err != nil {
return err
}
testDir := filepath.Join(rootDir, "test", "integration")
osCommand := oscommands.NewDummyOSCommand()
2021-04-05 20:37:18 +10:00
err = osCommand.RunCommand("go build -o %s", integration.TempLazygitPath())
2021-04-03 21:56:42 +11:00
if err != nil {
return err
}
2021-04-05 20:37:18 +10:00
tests, err := integration.LoadTests(testDir)
2021-04-03 21:56:42 +11:00
if err != nil {
panic(err)
}
record := os.Getenv("RECORD_EVENTS") != ""
2021-04-05 20:37:18 +10:00
2021-04-03 21:56:42 +11:00
updateSnapshots := record || os.Getenv("UPDATE_SNAPSHOTS") != ""
selectedTestName := os.Args[1]
for _, test := range tests {
if selectedTestName != "" && test.Name != selectedTestName {
continue
}
2021-04-05 20:37:18 +10:00
speeds := integration.GetTestSpeeds(test.Speed, updateSnapshots)
testPath := filepath.Join(testDir, test.Name)
actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected")
configDir := filepath.Join(testPath, "used_config")
log.Printf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir)
2021-04-03 21:56:42 +11:00
for i, speed := range speeds {
2021-04-05 10:20:02 +10:00
log.Printf("%s: attempting test at speed %d\n", test.Name, speed)
2021-04-03 21:56:42 +11:00
2021-04-05 20:37:18 +10:00
integration.FindOrCreateDir(testPath)
integration.PrepareIntegrationTestDir(actualDir)
err := integration.CreateFixture(testPath, actualDir)
if err != nil {
return err
}
2021-04-03 21:56:42 +11:00
2021-04-05 20:37:18 +10:00
cmd, err := integration.GetLazygitCommand(testPath, rootDir, record, speed)
2021-04-03 21:56:42 +11:00
if err != nil {
2021-04-05 12:45:27 +10:00
return err
2021-04-03 21:56:42 +11:00
}
2021-04-05 20:37:18 +10:00
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
2021-04-05 12:45:27 +10:00
return err
}
2021-04-03 21:56:42 +11:00
if updateSnapshots {
err = oscommands.CopyDir(actualDir, expectedDir)
if err != nil {
return err
}
}
2021-04-05 20:37:18 +10:00
actual, expected, err := integration.GenerateSnapshots(actualDir, expectedDir)
2021-04-03 21:56:42 +11:00
if err != nil {
return err
}
if expected == actual {
2021-04-05 20:37:18 +10:00
fmt.Printf("%s: success at speed %d\n", test.Name, speed)
2021-04-03 21:56:42 +11:00
break
}
// if the snapshots and we haven't tried all playback speeds different we'll retry at a slower speed
if i == len(speeds)-1 {
2021-04-05 20:37:18 +10:00
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
if err != nil {
return err
}
fmt.Println(string(bytes))
2021-04-05 10:20:02 +10:00
assert.Equal(MockTestingT{}, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual))
os.Exit(1)
2021-04-03 21:56:42 +11:00
}
}
}
return nil
}
2021-04-05 10:20:02 +10:00
type MockTestingT struct{}
func (t MockTestingT) Errorf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}