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

add tests for dealing with remotes

This commit is contained in:
Jesse Duffield 2021-10-22 20:18:40 +11:00
parent 6388af70ac
commit 2008c39516
663 changed files with 1663 additions and 72 deletions

4
.gitignore vendored
View File

@ -27,8 +27,10 @@ lazygit
test/git_server/data
test/integration/*/actual/
test/integration/*/actual_remote/
test/integration/*/used_config/
# these sample hooks waste too space space
# these sample hooks waste too much space
test/integration/*/expected/.git_keep/hooks/
test/integration/*/expected_remote/hooks/
!.git_keep/
lazygit.exe

View File

@ -0,0 +1,63 @@
package gui
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGuiGenerateMenuCandidates(t *testing.T) {
type scenario struct {
testName string
cmdOut string
filter string
valueFormat string
labelFormat string
test func([]commandMenuEntry, error)
}
scenarios := []scenario{
{
"Extract remote branch name",
"upstream/pr-1",
"(?P<remote>[a-z_]+)/(?P<branch>.*)",
"{{ .branch }}",
"Remote: {{ .remote }}",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)
},
},
{
"Multiple named groups with empty labelFormat",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .branch }}|{{ .remote }}",
"",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label)
},
},
{
"Multiple named groups with group ids",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .group_2 }}|{{ .group_1 }}",
"Remote: {{ .group_1 }}",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
s.test(NewDummyGui().GenerateMenuCandidates(s.cmdOut, s.filter, s.valueFormat, s.labelFormat))
})
}
}

View File

@ -56,8 +56,8 @@ func Test(t *testing.T) {
updateSnapshots,
record,
speedEnv,
func(t *testing.T, expected string, actual string) {
assert.Equal(t, expected, actual, fmt.Sprintf("expected:\n%s\nactual:\n%s\n", expected, actual))
func(t *testing.T, expected string, actual string, prefix string) {
assert.Equal(t, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual))
},
includeSkipped,
)
@ -81,59 +81,3 @@ func runCmdHeadless(cmd *exec.Cmd) error {
return f.Close()
}
func TestGuiGenerateMenuCandidates(t *testing.T) {
type scenario struct {
testName string
cmdOut string
filter string
valueFormat string
labelFormat string
test func([]commandMenuEntry, error)
}
scenarios := []scenario{
{
"Extract remote branch name",
"upstream/pr-1",
"(?P<remote>[a-z_]+)/(?P<branch>.*)",
"{{ .branch }}",
"Remote: {{ .remote }}",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)
},
},
{
"Multiple named groups with empty labelFormat",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .branch }}|{{ .remote }}",
"",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label)
},
},
{
"Multiple named groups with group ids",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .group_2 }}|{{ .group_1 }}",
"Remote: {{ .group_1 }}",
func(actualEntry []commandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "Remote: upstream", actualEntry[0].label)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
s.test(NewDummyGui().GenerateMenuCandidates(s.cmdOut, s.filter, s.valueFormat, s.labelFormat))
})
}
}

View File

@ -33,7 +33,7 @@ func RunTests(
updateSnapshots bool,
record bool,
speedEnv string,
onFail func(t *testing.T, expected string, actual string),
onFail func(t *testing.T, expected string, actual string, prefix string),
includeSkipped bool,
) error {
rootDir := GetRootDirectory()
@ -66,8 +66,10 @@ func RunTests(
fnWrapper(test, func(t *testing.T) error {
speeds := getTestSpeeds(test.Speed, updateSnapshots, speedEnv)
testPath := filepath.Join(testDir, test.Name)
actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected")
actualRepoDir := filepath.Join(testPath, "actual")
expectedRepoDir := filepath.Join(testPath, "expected")
actualRemoteDir := filepath.Join(testPath, "actual_remote")
expectedRemoteDir := filepath.Join(testPath, "expected_remote")
logf("path: %s", testPath)
// three retries at normal speed for the sake of flakey tests
@ -76,8 +78,9 @@ func RunTests(
logf("%s: attempting test at speed %f\n", test.Name, speed)
findOrCreateDir(testPath)
prepareIntegrationTestDir(actualDir)
err := createFixture(testPath, actualDir)
prepareIntegrationTestDir(actualRepoDir)
removeRemoteDir(actualRemoteDir)
err := createFixture(testPath, actualRepoDir)
if err != nil {
return err
}
@ -95,25 +98,46 @@ func RunTests(
}
if updateSnapshots {
err = oscommands.CopyDir(actualDir, expectedDir)
err = oscommands.CopyDir(actualRepoDir, expectedRepoDir)
if err != nil {
return err
}
err = os.Rename(
filepath.Join(expectedDir, ".git"),
filepath.Join(expectedDir, ".git_keep"),
filepath.Join(expectedRepoDir, ".git"),
filepath.Join(expectedRepoDir, ".git_keep"),
)
if err != nil {
return err
}
// see if we have a remote dir and if so, copy it over. Otherwise, delete the expected dir because we have no remote folder.
if folderExists(actualRemoteDir) {
err = oscommands.CopyDir(actualRemoteDir, expectedRemoteDir)
if err != nil {
return err
}
} else {
removeRemoteDir(expectedRemoteDir)
}
}
actual, expected, err := generateSnapshots(actualDir, expectedDir)
actualRepo, expectedRepo, err := generateSnapshots(actualRepoDir, expectedRepoDir)
if err != nil {
return err
}
if expected == actual {
actualRemote := "remote folder does not exist"
expectedRemote := "remote folder does not exist"
if folderExists(expectedRemoteDir) {
actualRemote, expectedRemote, err = generateSnapshotsForRemote(actualRemoteDir, expectedRemoteDir)
if err != nil {
return err
}
} else if folderExists(actualRemoteDir) {
actualRemote = "remote folder exists"
}
if expectedRepo == actualRepo && expectedRemote == actualRemote {
logf("%s: success at speed %f\n", test.Name, speed)
break
}
@ -126,7 +150,11 @@ func RunTests(
return err
}
logf("%s", string(bytes))
onFail(t, expected, actual)
if expectedRepo != actualRepo {
onFail(t, expectedRepo, actualRepo, "repo")
} else {
onFail(t, expectedRemote, actualRemote, "remote")
}
}
}
@ -137,6 +165,13 @@ func RunTests(
return nil
}
func removeRemoteDir(dir string) {
err := os.RemoveAll(dir)
if err != nil {
panic(err)
}
}
func prepareIntegrationTestDir(actualDir string) {
// remove contents of integration test directory
dir, err := ioutil.ReadDir(actualDir)
@ -351,6 +386,20 @@ func generateSnapshots(actualDir string, expectedDir string) (string, string, er
return actual, expected, nil
}
func generateSnapshotsForRemote(actualDir string, expectedDir string) (string, string, error) {
actual, err := generateSnapshot(actualDir)
if err != nil {
return "", "", err
}
expected, err := generateSnapshot(expectedDir)
if err != nil {
return "", "", err
}
return actual, expected, nil
}
func getLazygitCommand(testPath string, rootDir string, record bool, speed float64, extraCmdArgs string) (*exec.Cmd, error) {
osCommand := oscommands.NewDummyOSCommand()
@ -397,3 +446,8 @@ func getLazygitCommand(testPath string, rootDir string, record bool, speed float
return cmd, nil
}
func folderExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

View File

@ -0,0 +1 @@
myfile4

View File

@ -0,0 +1 @@
a9848fd98935937cd7d3909023ed1b588ccd4bfb branch 'master' of ../actual_remote

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -0,0 +1 @@
a9848fd98935937cd7d3909023ed1b588ccd4bfb

View File

@ -0,0 +1,16 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = CI@example.com
name = CI
[remote "origin"]
url = ../actual_remote
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

Binary file not shown.

View File

@ -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

View File

@ -0,0 +1,5 @@
0000000000000000000000000000000000000000 1fe60e6b7023a1b9751850f83ac5bda49ddd9278 CI <CI@example.com> 1634897551 +1100 commit (initial): myfile1
1fe60e6b7023a1b9751850f83ac5bda49ddd9278 66bd8d357f6226ec264478db3606bc1c4be87e63 CI <CI@example.com> 1634897551 +1100 commit: myfile2
66bd8d357f6226ec264478db3606bc1c4be87e63 a9848fd98935937cd7d3909023ed1b588ccd4bfb CI <CI@example.com> 1634897551 +1100 commit: myfile3
a9848fd98935937cd7d3909023ed1b588ccd4bfb 66bd8d357f6226ec264478db3606bc1c4be87e63 CI <CI@example.com> 1634897551 +1100 reset: moving to HEAD^
66bd8d357f6226ec264478db3606bc1c4be87e63 aed1af42535c9c6a27b9f660119452328fddd7cd CI <CI@example.com> 1634897551 +1100 commit: myfile4

View File

@ -0,0 +1,5 @@
0000000000000000000000000000000000000000 1fe60e6b7023a1b9751850f83ac5bda49ddd9278 CI <CI@example.com> 1634897551 +1100 commit (initial): myfile1
1fe60e6b7023a1b9751850f83ac5bda49ddd9278 66bd8d357f6226ec264478db3606bc1c4be87e63 CI <CI@example.com> 1634897551 +1100 commit: myfile2
66bd8d357f6226ec264478db3606bc1c4be87e63 a9848fd98935937cd7d3909023ed1b588ccd4bfb CI <CI@example.com> 1634897551 +1100 commit: myfile3
a9848fd98935937cd7d3909023ed1b588ccd4bfb 66bd8d357f6226ec264478db3606bc1c4be87e63 CI <CI@example.com> 1634897551 +1100 reset: moving to HEAD^
66bd8d357f6226ec264478db3606bc1c4be87e63 aed1af42535c9c6a27b9f660119452328fddd7cd CI <CI@example.com> 1634897551 +1100 commit: myfile4

View File

@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 66bd8d357f6226ec264478db3606bc1c4be87e63 CI <CI@example.com> 1634897551 +1100 fetch origin: storing head
66bd8d357f6226ec264478db3606bc1c4be87e63 a9848fd98935937cd7d3909023ed1b588ccd4bfb CI <CI@example.com> 1634897551 +1100 update by push
a9848fd98935937cd7d3909023ed1b588ccd4bfb aed1af42535c9c6a27b9f660119452328fddd7cd CI <CI@example.com> 1634897553 +1100 update by push

View File

@ -0,0 +1,5 @@
xŤÍA
�0@Ń®sŠŮJF'“E
®<FL&Tp�H
ööőÝ~üTU×H|k‡XáTläĹ2‘`ŕ°‹X<-Ôg¦“ëLü´w=`šá9Í/9Łî›<RŐ�{
�wáŽh­ąę5iň'7ú-ë&h~4ś,Ű

View File

@ -0,0 +1,3 @@
x�ÎA
Â0@Q×9ÅìÉ$“I
"BW=F2™bÁØR"èííÜ>þâËÚÚÒ:õ]Dm¢Ñzò9Ç^stRB©sfæ2¨ÔÌÉly×W‡Cjª>Ä™�cÇD1ÕâÙr*š¢²7ùÝëã×qºë'·í©YÛ �=¥!†€pF´ÖzLuý37í;/O%óÜ):e

View File

@ -0,0 +1 @@
aed1af42535c9c6a27b9f660119452328fddd7cd

View File

@ -0,0 +1 @@
aed1af42535c9c6a27b9f660119452328fddd7cd

View File

@ -0,0 +1 @@
test1

View File

@ -0,0 +1 @@
test2

View File

@ -0,0 +1 @@
test4

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -0,0 +1,8 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/forcePush/./actual

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -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

View File

@ -0,0 +1,5 @@
xŤÍA
�0@Ń®sŠŮJF'“E
®<FL&Tp�H
ööőÝ~üTU×H|k‡XáTläĹ2‘`ŕ°‹X<-Ôg¦“ëLü´w=`šá9Í/9Łî›<RŐ�{
�wáŽh­ąę5iň'7ú-ë&h~4ś,Ű

View File

@ -0,0 +1,3 @@
x�ÎA
Â0@Q×9ÅìÉ$“I
"BW=F2™bÁØR"èííÜ>þâËÚÚÒ:õ]Dm¢Ñzò9Ç^stRB©sfæ2¨ÔÌÉly×W‡Cjª>Ä™�cÇD1ÕâÙr*š¢²7ùÝëã×qºë'·í©YÛ �=¥!†€pF´ÖzLuý37í;/O%óÜ):e

View File

@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
66bd8d357f6226ec264478db3606bc1c4be87e63 refs/heads/master

View File

@ -0,0 +1 @@
aed1af42535c9c6a27b9f660119452328fddd7cd

View File

@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":1054,"Mod":0,"Key":256,"Ch":80},{"Timestamp":1736,"Mod":0,"Key":13,"Ch":13},{"Timestamp":2486,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":272,"Height":74}]}

View File

@ -0,0 +1,39 @@
#!/bin/sh
set -e
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 test2 > myfile2
git add .
git commit -am "myfile2"
cd ..
git clone --bare ./actual actual_remote
cd actual
git remote add origin ../actual_remote
git fetch origin
git branch --set-upstream-to=origin/master master
echo test3 > myfile3
git add .
git commit -am "myfile3"
git push origin master
git reset --hard HEAD^
echo test4 > myfile4
git add .
git commit -am "myfile4"

View File

@ -0,0 +1 @@
{ "description": "force push with lease if required", "speed": 10 }

View File

@ -0,0 +1 @@
myfile4

View File

@ -0,0 +1 @@
6ad6c42187d356f4eab4f004cca17863746adec1 branch 'master' of ../actual_remote

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -0,0 +1 @@
0c0f210a4e5ff3b58e4190501c2b755695f439fa

View File

@ -0,0 +1,16 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = CI@example.com
name = CI
[remote "origin"]
url = ../actual_remote
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

Binary file not shown.

View File

@ -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

View File

@ -0,0 +1,6 @@
0000000000000000000000000000000000000000 003527daa0801470151d8f93140a02fc306fea00 CI <CI@example.com> 1634896904 +1100 commit (initial): myfile1
003527daa0801470151d8f93140a02fc306fea00 0c0f210a4e5ff3b58e4190501c2b755695f439fa CI <CI@example.com> 1634896904 +1100 commit: myfile2
0c0f210a4e5ff3b58e4190501c2b755695f439fa 336826e035e431ac94eca7f3cb6dd3fb072f7a5a CI <CI@example.com> 1634896904 +1100 commit: myfile3
336826e035e431ac94eca7f3cb6dd3fb072f7a5a 6ad6c42187d356f4eab4f004cca17863746adec1 CI <CI@example.com> 1634896904 +1100 commit: myfile4
6ad6c42187d356f4eab4f004cca17863746adec1 0c0f210a4e5ff3b58e4190501c2b755695f439fa CI <CI@example.com> 1634896904 +1100 reset: moving to head^^
0c0f210a4e5ff3b58e4190501c2b755695f439fa 6ad6c42187d356f4eab4f004cca17863746adec1 CI <CI@example.com> 1634896905 +1100 pull --no-edit: Fast-forward

View File

@ -0,0 +1,6 @@
0000000000000000000000000000000000000000 003527daa0801470151d8f93140a02fc306fea00 CI <CI@example.com> 1634896904 +1100 commit (initial): myfile1
003527daa0801470151d8f93140a02fc306fea00 0c0f210a4e5ff3b58e4190501c2b755695f439fa CI <CI@example.com> 1634896904 +1100 commit: myfile2
0c0f210a4e5ff3b58e4190501c2b755695f439fa 336826e035e431ac94eca7f3cb6dd3fb072f7a5a CI <CI@example.com> 1634896904 +1100 commit: myfile3
336826e035e431ac94eca7f3cb6dd3fb072f7a5a 6ad6c42187d356f4eab4f004cca17863746adec1 CI <CI@example.com> 1634896904 +1100 commit: myfile4
6ad6c42187d356f4eab4f004cca17863746adec1 0c0f210a4e5ff3b58e4190501c2b755695f439fa CI <CI@example.com> 1634896904 +1100 reset: moving to head^^
0c0f210a4e5ff3b58e4190501c2b755695f439fa 6ad6c42187d356f4eab4f004cca17863746adec1 CI <CI@example.com> 1634896905 +1100 pull --no-edit: Fast-forward

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000 6ad6c42187d356f4eab4f004cca17863746adec1 CI <CI@example.com> 1634896904 +1100 fetch origin: storing head

View File

@ -0,0 +1,2 @@
x█мA
┐0@я╝s┼ый▄NгJ\y▄≤L╗Ю░")╢╥в#tШyПS5[к╔М╙─*╘`■eЙ3Ё▓≈Л╘▀T^╦об%╕{ГБ╖╫Йс ▐iУМ╫И-U{Iо>H@├+╒;К9iЗ'wЖ+К╕Д4М,щ

View File

@ -0,0 +1,2 @@
xŤÎA
Â0@Q×9Eö‚d2“I"BW=Ć4ť`ÁŘR"čííÜ~Ţâ—µµĄ[Čt껪ő•!’ “#Lhć�kdOŠ„ f“]_Ý"rň¬�‚”LZ$V,Ď3ÖÉE_Ł1ňîŹu·ĂhŻĂx׏´í©—˛¶›FJ™ł#{pÎő�ęú'7í[—§’ůôâ90

View File

@ -0,0 +1 @@
6ad6c42187d356f4eab4f004cca17863746adec1

View File

@ -0,0 +1 @@
6ad6c42187d356f4eab4f004cca17863746adec1

View File

@ -0,0 +1 @@
test1

View File

@ -0,0 +1 @@
test2

View File

@ -0,0 +1 @@
test3

View File

@ -0,0 +1 @@
test4

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -0,0 +1,8 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/pull/./actual

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -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

View File

@ -0,0 +1,2 @@
x█мA
┐0@я╝s┼ый▄NгJ\y▄≤L╗Ю░")╢╥в#tШyПS5[к╔М╙─*╘`■eЙ3Ё▓≈Л╘▀T^╦об%╕{ГБ╖╫Йс ▐iУМ╫И-U{Iо>H@├+╒;К9iЗ'wЖ+К╕Д4М,щ

View File

@ -0,0 +1,2 @@
xŤÎA
Â0@Q×9Eö‚d2“I"BW=Ć4ť`ÁŘR"čííÜ~Ţâ—µµĄ[Čt껪ő•!’ “#Lhć�kdOŠ„ f“]_Ý"rň¬�‚”LZ$V,Ď3ÖÉE_Ł1ňîŹu·ĂhŻĂx׏´í©—˛¶›FJ™ł#{pÎő�ęú'7í[—§’ůôâ90

View File

@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
6ad6c42187d356f4eab4f004cca17863746adec1 refs/heads/master

View File

@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":618,"Mod":0,"Key":256,"Ch":112},{"Timestamp":1225,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":272,"Height":74}]}

Some files were not shown because too many files have changed in this diff Show More