1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-06 11:02:41 +03:00

store everything you need to know about a test in its directory

This commit is contained in:
Jesse Duffield
2020-10-07 18:48:34 +11:00
parent bb081ca764
commit 88f2a66a51
15 changed files with 71 additions and 47 deletions

View File

@@ -1,6 +1,7 @@
package gui package gui
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -8,6 +9,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"testing" "testing"
"github.com/creack/pty" "github.com/creack/pty"
@@ -39,46 +41,32 @@ import (
// original playback speed. Speed must be an integer. // original playback speed. Speed must be an integer.
type integrationTest struct { type integrationTest struct {
name string Name string `json:"name"`
fixture string Speed int `json:"speed"`
startSpeed int Description string `json:"description"`
} }
func tests() []integrationTest { func loadTests(t *testing.T, testDir string) []*integrationTest {
return []integrationTest{ paths, err := filepath.Glob(filepath.Join(testDir, "/*/test.json"))
{ assert.NoError(t, err)
name: "commit",
fixture: "newFile", tests := make([]*integrationTest, len(paths))
startSpeed: 20,
}, for i, path := range paths {
{ data, err := ioutil.ReadFile(path)
name: "squash", assert.NoError(t, err)
fixture: "manyCommits",
startSpeed: 6, test := &integrationTest{}
},
{ err = json.Unmarshal(data, test)
name: "patchBuilding", assert.NoError(t, err)
fixture: "updatedFile",
startSpeed: 3, test.Name = strings.TrimPrefix(filepath.Dir(path), testDir+"/")
},
{ tests[i] = test
name: "patchBuilding2",
fixture: "updatedFile",
startSpeed: 3,
},
{
name: "mergeConflicts",
fixture: "mergeConflicts",
},
{
name: "searching",
fixture: "newFile",
},
{
name: "searchingInStagingPanel",
fixture: "newFile2",
},
} }
return tests
} }
func generateSnapshot(t *testing.T, dir string) string { func generateSnapshot(t *testing.T, dir string) string {
@@ -169,9 +157,10 @@ func getTestSpeeds(testStartSpeed int, updateSnapshots bool) []int {
} }
func Test(t *testing.T) { func Test(t *testing.T) {
tests := tests()
rootDir := getRootDirectory() rootDir := getRootDirectory()
testDir := filepath.Join(rootDir, "test", "integration")
tests := loadTests(t, testDir)
record := os.Getenv("RECORD_EVENTS") != "" record := os.Getenv("RECORD_EVENTS") != ""
updateSnapshots := record || os.Getenv("UPDATE_SNAPSHOTS") != "" updateSnapshots := record || os.Getenv("UPDATE_SNAPSHOTS") != ""
@@ -179,17 +168,17 @@ func Test(t *testing.T) {
for _, test := range tests { for _, test := range tests {
test := test test := test
t.Run(test.name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
if runInParallel() { if runInParallel() {
t.Parallel() t.Parallel()
} }
speeds := getTestSpeeds(test.startSpeed, updateSnapshots) speeds := getTestSpeeds(test.Speed, updateSnapshots)
for i, speed := range speeds { for i, speed := range speeds {
t.Logf("%s: attempting test at speed %d\n", test.name, speed) t.Logf("%s: attempting test at speed %d\n", test.Name, speed)
testPath := filepath.Join(rootDir, "test", "integration", test.name) testPath := filepath.Join(testDir, test.Name)
actualDir := filepath.Join(testPath, "actual") actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected") expectedDir := filepath.Join(testPath, "expected")
t.Logf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir) t.Logf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir)
@@ -197,7 +186,7 @@ func Test(t *testing.T) {
prepareIntegrationTestDir(actualDir) prepareIntegrationTestDir(actualDir)
err := createFixture(rootDir, test.fixture, actualDir) err := createFixture(testPath, actualDir)
assert.NoError(t, err) assert.NoError(t, err)
runLazygit(t, testPath, rootDir, record, speed) runLazygit(t, testPath, rootDir, record, speed)
@@ -234,7 +223,7 @@ func Test(t *testing.T) {
}() }()
if expected == actual { if expected == actual {
t.Logf("%s: success at speed %d\n", test.name, speed) t.Logf("%s: success at speed %d\n", test.Name, speed)
break break
} }
@@ -247,9 +236,10 @@ func Test(t *testing.T) {
} }
} }
func createFixture(rootDir string, name string, actualDir string) error { func createFixture(testPath, actualDir string) error {
osCommand := oscommands.NewDummyOSCommand() osCommand := oscommands.NewDummyOSCommand()
cmd := exec.Command("bash", filepath.Join(rootDir, "test", "fixtures", fmt.Sprintf("%s.sh", name)), actualDir) bashScriptPath := filepath.Join(testPath, "setup.sh")
cmd := exec.Command("bash", bashScriptPath, actualDir)
if err := osCommand.RunExecutable(cmd); err != nil { if err := osCommand.RunExecutable(cmd); err != nil {
return err return err
@@ -311,6 +301,9 @@ func runLazygit(t *testing.T, testPath string, rootDir string, record bool, spee
cmd.Env = append(cmd.Env, fmt.Sprintf("REPLAY_SPEED=%d", speed)) cmd.Env = append(cmd.Env, fmt.Sprintf("REPLAY_SPEED=%d", speed))
if record { if record {
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Env = append( cmd.Env = append(
cmd.Env, cmd.Env,
fmt.Sprintf("RECORD_EVENTS_TO=%s", replayPath), fmt.Sprintf("RECORD_EVENTS_TO=%s", replayPath),

View File

@@ -0,0 +1 @@
{ "description": "stage a file and commit the change", "speed": 20 }

View File

@@ -0,0 +1 @@
{ "description": "" }

View File

@@ -0,0 +1 @@
{ "description": "", "speed": 3 }

View File

@@ -0,0 +1,24 @@
#!/bin/sh
cd $1
git init
git config user.email "CI@example.com"
git config user.name "CI"
echo test1 > myfile1
git add .
git commit -am "myfile1"
echo firstline > myfile2
echo secondline >> myfile2
echo thirdline >> myfile2
git add .
git commit -am "myfile2"
echo firstline2 > myfile2
echo secondline >> myfile2
echo thirdline2 >> myfile2
git commit -am "myfile2 update"
echo test3 > myfile3
git add .
git commit -am "myfile3"

View File

@@ -0,0 +1 @@
{ "description": "", "speed": 3 }

View File

@@ -0,0 +1 @@
{ "description": "" }

View File

@@ -0,0 +1 @@
{ "description": "" }

View File

@@ -0,0 +1 @@
{ "description": "" }