Skip to content

Commit

Permalink
add submodule integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jan 28, 2022
1 parent 1b09674 commit 1d90e1b
Show file tree
Hide file tree
Showing 182 changed files with 517 additions and 34 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ coverage.txt

# Binaries
lazygit
lazygit.exe

# Exceptions
!.gitignore
!.goreleaser.yml
!.circleci/
!.github/
# these are for our integration tests
!.git_keep
!.gitmodules_keep

test/git_server/data
test/integration/*/actual/
test/integration/*/actual_remote/
test/integration/*/used_config/
# these sample hooks waste too much space
test/integration/*/expected/.git_keep/hooks/
test/integration/*/expected_remote/hooks/
!.git_keep/
lazygit.exe
test/integration/*/expected/**/hooks/
test/integration/*/expected_remote/**/hooks/
125 changes: 95 additions & 30 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func RunTests(
expectedRepoDir := filepath.Join(testPath, "expected")
actualRemoteDir := filepath.Join(testPath, "actual_remote")
expectedRemoteDir := filepath.Join(testPath, "expected_remote")
otherRepoDir := filepath.Join(testPath, "other_repo")
logf("path: %s", testPath)

for i, speed := range speeds {
Expand All @@ -110,7 +111,8 @@ func RunTests(

findOrCreateDir(testPath)
prepareIntegrationTestDir(actualRepoDir)
removeRemoteDir(actualRemoteDir)
removeDir(otherRepoDir)
removeDir(actualRemoteDir)
err := createFixture(testPath, actualRepoDir)
if err != nil {
return err
Expand All @@ -128,16 +130,21 @@ func RunTests(
return err
}

// submodule tests currently make use of a repo called 'other_repo' but we don't want that
// to stick around. Long-term we should have an 'actual' folder which itself contains
// repos, and there we can put the 'repo' repo which is the main one, alongside
// any others that we use as part of the test (including remotes). Then we'll do snapshots for
// each of them.
removeDir(otherRepoDir)

if mode == UPDATE_SNAPSHOT || mode == RECORD {
// create/update snapshot
err = oscommands.CopyDir(actualRepoDir, expectedRepoDir)
if err != nil {
return err
}
err = os.Rename(
filepath.Join(expectedRepoDir, ".git"),
filepath.Join(expectedRepoDir, ".git_keep"),
)
if err != nil {

if err := renameGitDirs(expectedRepoDir); err != nil {
return err
}

Expand All @@ -148,11 +155,12 @@ func RunTests(
return err
}
} else {
removeRemoteDir(expectedRemoteDir)
removeDir(expectedRemoteDir)
}
}

if mode != SANDBOX {
logf("%s", "updated snapshot")
} else {
// compare result to snapshot
actualRepo, expectedRepo, err := generateSnapshots(actualRepoDir, expectedRepoDir)
if err != nil {
return err
Expand All @@ -174,7 +182,7 @@ func RunTests(
break
}

// if the snapshots and we haven't tried all playback speeds different we'll retry at a slower speed
// if the snapshot doesn't match and we haven't tried all playback speeds different we'll retry at a slower speed
if i == len(speeds)-1 {
// get the log file and print that
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
Expand All @@ -198,7 +206,7 @@ func RunTests(
return nil
}

func removeRemoteDir(dir string) {
func removeDir(dir string) {
err := os.RemoveAll(dir)
if err != nil {
panic(err)
Expand Down Expand Up @@ -334,6 +342,10 @@ func findOrCreateDir(path string) {
}
}

// note that we don't actually store this snapshot in the lazygit repo.
// Instead we store the whole expected git repo of our test, so that
// we can easily change what we want to compare without needing to regenerate
// snapshots for each test.
func generateSnapshot(dir string) (string, error) {
osCommand := oscommands.NewDummyOSCommand()

Expand All @@ -345,18 +357,24 @@ func generateSnapshot(dir string) (string, error) {
snapshot := ""

cmdStrs := []string{
fmt.Sprintf(`git -C %s status`, dir), // file tree
fmt.Sprintf(`git -C %s log --pretty=%%B -p -1`, dir), // log
fmt.Sprintf(`git -C %s tag -n`, dir), // tags
`status`, // file tree
`log --pretty=%B -p -1`, // log
`tag -n`, // tags
`stash list`, // stash
`submodule foreach 'git status'`, // submodule status
`submodule foreach 'git log --pretty=%B -p -1'`, // submodule log
`submodule foreach 'git tag -n'`, // submodule tags
`submodule foreach 'git stash list'`, // submodule stash
}

for _, cmdStr := range cmdStrs {
// ignoring error for now. If there's an error it could be that there are no results
output, _ := osCommand.Cmd.New(cmdStr).RunWithOutput()
output, _ := osCommand.Cmd.New(fmt.Sprintf("git -C %s %s", dir, cmdStr)).RunWithOutput()

snapshot += output + "\n"
snapshot += fmt.Sprintf("git %s:\n%s\n", cmdStr, output)
}

snapshot += "files in repo:\n"
err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -373,7 +391,12 @@ func generateSnapshot(dir string) (string, error) {
if err != nil {
return err
}
snapshot += string(bytes) + "\n"

relativePath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
snapshot += fmt.Sprintf("path: %s\ncontent:\n%s\n", relativePath, string(bytes))

return nil
})
Expand All @@ -391,24 +414,15 @@ func generateSnapshots(actualDir string, expectedDir string) (string, string, er
return "", "", err
}

// git refuses to track .git folders in subdirectories so we need to rename it
// to git_keep after running a test, and then change it back again
defer func() {
err = os.Rename(
filepath.Join(expectedDir, ".git"),
filepath.Join(expectedDir, ".git_keep"),
)

if err != nil {
if err := renameGitDirs(expectedDir); err != nil {
panic(err)
}
}()

// ignoring this error because we might not have a .git_keep file here yet.
_ = os.Rename(
filepath.Join(expectedDir, ".git_keep"),
filepath.Join(expectedDir, ".git"),
)
if err := restoreGitDirs(expectedDir); err != nil {
return "", "", err
}

expected, err := generateSnapshot(expectedDir)
if err != nil {
Expand All @@ -418,6 +432,57 @@ func generateSnapshots(actualDir string, expectedDir string) (string, string, er
return actual, expected, nil
}

func getPathsToRename(dir string, needle string) []string {
pathsToRename := []string{}

err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}

if f.Name() == needle {
pathsToRename = append(pathsToRename, path)
}

return nil
})
if err != nil {
panic(err)
}

return pathsToRename
}

// Git refuses to track .git and .gitmodules folders in subdirectories so we need to rename it
// to git_keep after running a test, and then change it back again
var untrackedGitDirs []string = []string{".git", ".gitmodules"}

func renameGitDirs(dir string) error {
for _, untrackedGitDir := range untrackedGitDirs {
for _, path := range getPathsToRename(dir, untrackedGitDir) {
err := os.Rename(path, path+"_keep")
if err != nil {
return err
}
}
}

return nil
}

func restoreGitDirs(dir string) error {
for _, untrackedGitDir := range untrackedGitDirs {
for _, path := range getPathsToRename(dir, untrackedGitDir+"_keep") {
err := os.Rename(path, strings.TrimSuffix(path, "_keep"))
if err != nil {
return err
}
}
}

return nil
}

func generateSnapshotsForRemote(actualDir string, expectedDir string) (string, string, error) {
actual, err := generateSnapshot(actualDir)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Empty file.
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/.git_keep/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
13 changes: 13 additions & 0 deletions test/integration/submoduleAdd/expected/.git_keep/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = [email protected]
name = CI
[submodule "blah"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleAdd/other_repo
active = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file not shown.
7 changes: 7 additions & 0 deletions test/integration/submoduleAdd/expected/.git_keep/info/exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
3 changes: 3 additions & 0 deletions test/integration/submoduleAdd/expected/.git_keep/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 a50a5125768001a3ea263ffb7cafbc421a508153 CI <[email protected]> 1534792759 +0100 commit (initial): myfile1
a50a5125768001a3ea263ffb7cafbc421a508153 42530e986dbb65877ed8d61ca0c816e425e5c62e CI <[email protected]> 1534792759 +0100 commit: myfile2
42530e986dbb65877ed8d61ca0c816e425e5c62e 6de70e35394a99cc437d1bc70b0852b70c5bb03d CI <[email protected]> 1617797593 +1000 commit: test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 a50a5125768001a3ea263ffb7cafbc421a508153 CI <[email protected]> 1534792759 +0100 commit (initial): myfile1
a50a5125768001a3ea263ffb7cafbc421a508153 42530e986dbb65877ed8d61ca0c816e425e5c62e CI <[email protected]> 1534792759 +0100 commit: myfile2
42530e986dbb65877ed8d61ca0c816e425e5c62e 6de70e35394a99cc437d1bc70b0852b70c5bb03d CI <[email protected]> 1617797593 +1000 commit: test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
worktree = ../../../haha
[remote "origin"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleAdd/other_repo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 42530e986dbb65877ed8d61ca0c816e425e5c62e Jesse Duffield <[email protected]> 1617797593 +1000 clone: from /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleAdd/other_repo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 42530e986dbb65877ed8d61ca0c816e425e5c62e Jesse Duffield <[email protected]> 1617797593 +1000 clone: from /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleAdd/other_repo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 42530e986dbb65877ed8d61ca0c816e425e5c62e Jesse Duffield <[email protected]> 1617797593 +1000 clone: from /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleAdd/other_repo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��M
�0@�s��J&N������  I�޾���/�Z�H����R��%d"��r@�X<-4dG��5�?���fxN�(_��&��� ���6�]���פ˟\ճ����5,�
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
42530e986dbb65877ed8d61ca0c816e425e5c62e refs/remotes/origin/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42530e986dbb65877ed8d61ca0c816e425e5c62e
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/remotes/origin/master
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��M
�0@�s��J&N������  I�޾���/�Z�H����R��%d"��r@�X<-4dG��5�?���fxN�(_��&��� ���6�]���פ˟\ճ����5,�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6de70e35394a99cc437d1bc70b0852b70c5bb03d
3 changes: 3 additions & 0 deletions test/integration/submoduleAdd/expected/.gitmodules_keep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "blah"]
path = haha
url = ../other_repo
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/haha/.git_keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gitdir: ../.git/modules/blah
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/haha/myfile1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/haha/myfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/myfile1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/expected/myfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
1 change: 1 addition & 0 deletions test/integration/submoduleAdd/recording.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":1858,"Mod":0,"Key":256,"Ch":93},{"Timestamp":4332,"Mod":0,"Key":256,"Ch":110},{"Timestamp":5067,"Mod":0,"Key":256,"Ch":46},{"Timestamp":5188,"Mod":0,"Key":256,"Ch":46},{"Timestamp":5323,"Mod":0,"Key":256,"Ch":47},{"Timestamp":5532,"Mod":0,"Key":256,"Ch":111},{"Timestamp":5652,"Mod":0,"Key":256,"Ch":116},{"Timestamp":5743,"Mod":0,"Key":256,"Ch":104},{"Timestamp":5862,"Mod":0,"Key":256,"Ch":101},{"Timestamp":5892,"Mod":0,"Key":256,"Ch":114},{"Timestamp":6102,"Mod":0,"Key":256,"Ch":95},{"Timestamp":6297,"Mod":0,"Key":256,"Ch":114},{"Timestamp":6312,"Mod":0,"Key":256,"Ch":101},{"Timestamp":6402,"Mod":0,"Key":256,"Ch":112},{"Timestamp":6432,"Mod":0,"Key":256,"Ch":111},{"Timestamp":6718,"Mod":0,"Key":13,"Ch":13},{"Timestamp":7632,"Mod":0,"Key":127,"Ch":127},{"Timestamp":7966,"Mod":0,"Key":127,"Ch":127},{"Timestamp":7982,"Mod":0,"Key":127,"Ch":127},{"Timestamp":7998,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8015,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8031,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8049,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8065,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8081,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8097,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8113,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8129,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8146,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8162,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8178,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8195,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8212,"Mod":0,"Key":127,"Ch":127},{"Timestamp":8397,"Mod":0,"Key":256,"Ch":98},{"Timestamp":8578,"Mod":0,"Key":256,"Ch":108},{"Timestamp":8698,"Mod":0,"Key":256,"Ch":97},{"Timestamp":8787,"Mod":0,"Key":256,"Ch":104},{"Timestamp":9012,"Mod":0,"Key":13,"Ch":13},{"Timestamp":9493,"Mod":0,"Key":127,"Ch":127},{"Timestamp":9717,"Mod":0,"Key":127,"Ch":127},{"Timestamp":9868,"Mod":0,"Key":127,"Ch":127},{"Timestamp":9973,"Mod":0,"Key":127,"Ch":127},{"Timestamp":10168,"Mod":0,"Key":256,"Ch":104},{"Timestamp":10228,"Mod":0,"Key":256,"Ch":97},{"Timestamp":10257,"Mod":0,"Key":256,"Ch":104},{"Timestamp":10393,"Mod":0,"Key":256,"Ch":97},{"Timestamp":10752,"Mod":0,"Key":13,"Ch":13},{"Timestamp":12612,"Mod":0,"Key":256,"Ch":91},{"Timestamp":13543,"Mod":0,"Key":256,"Ch":99},{"Timestamp":13797,"Mod":0,"Key":256,"Ch":116},{"Timestamp":13857,"Mod":0,"Key":256,"Ch":101},{"Timestamp":14037,"Mod":0,"Key":256,"Ch":115},{"Timestamp":14081,"Mod":0,"Key":256,"Ch":116},{"Timestamp":14368,"Mod":0,"Key":13,"Ch":13},{"Timestamp":15312,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":272,"Height":36}]}
24 changes: 24 additions & 0 deletions test/integration/submoduleAdd/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

set -e

cd $1

export GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST"
export GIT_AUTHOR_DATE="Mon 20 Aug 2018 20:19:19 BST"

git init

git config user.email "[email protected]"
git config user.name "CI"

echo test1 > myfile1
git add .
git commit -am "myfile1"
echo test2 > myfile2
git add .
git commit -am "myfile2"

cd ..
git clone --bare ./actual other_repo
cd actual
4 changes: 4 additions & 0 deletions test/integration/submoduleAdd/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "Add submodule",
"speed": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Empty file.
1 change: 1 addition & 0 deletions test/integration/submoduleEnter/expected/.git_keep/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
13 changes: 13 additions & 0 deletions test/integration/submoduleEnter/expected/.git_keep/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = [email protected]
name = CI
[submodule "other_repo"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/submoduleEnter/other_repo
active = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file not shown.
Loading

0 comments on commit 1d90e1b

Please sign in to comment.