Skip to content

Commit 752526c

Browse files
Merge pull request #2470 from jesseduffield/migrate-staging-tests
2 parents 1b52a0d + c63fed2 commit 752526c

File tree

199 files changed

+540
-2476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+540
-2476
lines changed

pkg/integration/components/actions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ func (self *Actions) ContinueOnConflictsResolved() {
3131
Content(Contains("all merge conflicts resolved. Continue?")).
3232
Confirm()
3333
}
34+
35+
func (self *Actions) ConfirmDiscardLines() {
36+
self.t.ExpectPopup().Confirmation().
37+
Title(Equals("Unstage lines")).
38+
Content(Contains("Are you sure you want to delete the selected lines")).
39+
Confirm()
40+
}

pkg/integration/components/commit_message_panel_driver.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@ func (self *CommitMessagePanelDriver) AddNewline() *CommitMessagePanelDriver {
2828
}
2929

3030
func (self *CommitMessagePanelDriver) Clear() *CommitMessagePanelDriver {
31-
panic("Clear method not yet implemented!")
31+
// clearing multiple times in case there's multiple lines
32+
// (the clear button only clears a single line at a time)
33+
maxAttempts := 100
34+
for i := 0; i < maxAttempts+1; i++ {
35+
if self.getViewDriver().getView().Buffer() == "" {
36+
break
37+
}
38+
39+
self.t.press(ClearKey)
40+
if i == maxAttempts {
41+
panic("failed to clear commit message panel")
42+
}
43+
}
44+
45+
return self
3246
}
3347

3448
func (self *CommitMessagePanelDriver) Confirm() {

pkg/integration/components/test_driver.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ func (self *TestDriver) press(keyStr string) {
3737
self.gui.PressKey(keyStr)
3838
}
3939

40+
// Should only be used in specific cases where you're doing something weird!
41+
// E.g. invoking a global keybinding from within a popup.
42+
// You probably shouldn't use this function, and should instead go through a view like t.Views().Commit().Focus().Press(...)
43+
func (self *TestDriver) GlobalPress(keyStr string) {
44+
self.press(keyStr)
45+
}
46+
4047
func (self *TestDriver) typeContent(content string) {
4148
for _, char := range content {
4249
self.press(string(char))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package file
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var preCommitHook = `#!/bin/bash
9+
10+
if [[ -f bad ]]; then
11+
exit 1
12+
fi
13+
`
14+
15+
var RememberCommitMessageAfterFail = NewIntegrationTest(NewIntegrationTestArgs{
16+
Description: "Verify that the commit message is remembered after a failed attempt at committing",
17+
ExtraCmdArgs: "",
18+
Skip: false,
19+
SetupConfig: func(config *config.AppConfig) {
20+
},
21+
SetupRepo: func(shell *Shell) {
22+
shell.CreateFile(".git/hooks/pre-commit", preCommitHook)
23+
shell.RunCommand("chmod +x .git/hooks/pre-commit")
24+
25+
shell.CreateFileAndAdd("one", "one")
26+
27+
// the presence of this file will cause the pre-commit hook to fail
28+
shell.CreateFile("bad", "bad")
29+
},
30+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
31+
t.Views().Files().
32+
IsFocused().
33+
Lines(
34+
Contains("bad"),
35+
Contains("one"),
36+
).
37+
Press(keys.Files.CommitChanges).
38+
Tap(func() {
39+
t.ExpectPopup().CommitMessagePanel().Type("my message").Confirm()
40+
41+
t.ExpectPopup().Alert().Title(Equals("Error")).Content(Contains("Git command failed")).Confirm()
42+
}).
43+
Press(keys.Universal.Remove). // remove file that triggers pre-commit hook to fail
44+
Tap(func() {
45+
t.ExpectPopup().Menu().Title(Equals("bad")).Select(Contains("discard all changes")).Confirm()
46+
}).
47+
Lines(
48+
Contains("one"),
49+
).
50+
Press(keys.Files.CommitChanges).
51+
Tap(func() {
52+
t.ExpectPopup().CommitMessagePanel().
53+
InitialText(Equals("my message")). // it remembered the commit message
54+
Confirm()
55+
56+
t.Views().Commits().
57+
Lines(
58+
Contains("my message"),
59+
)
60+
})
61+
},
62+
})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package staging
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var DiffContextChange = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Change the number of diff context lines while in the staging panel",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
// need to be working with a few lines so that git perceives it as two separate hunks
15+
shell.CreateFileAndAdd("file1", "1a\n2a\n3a\n4a\n5a\n6a\n7a\n8a\n9a\n10a\n11a\n12a\n13a\n14a\n15a")
16+
shell.Commit("one")
17+
18+
shell.UpdateFile("file1", "1a\n2a\n3b\n4a\n5a\n6a\n7a\n8a\n9a\n10a\n11a\n12a\n13b\n14a\n15a")
19+
20+
// hunk looks like:
21+
// diff --git a/file1 b/file1
22+
// index 3653080..a6388b6 100644
23+
// --- a/file1
24+
// +++ b/file1
25+
// @@ -1,6 +1,6 @@
26+
// 1a
27+
// 2a
28+
// -3a
29+
// +3b
30+
// 4a
31+
// 5a
32+
// 6a
33+
// @@ -10,6 +10,6 @@
34+
// 10a
35+
// 11a
36+
// 12a
37+
// -13a
38+
// +13b
39+
// 14a
40+
// 15a
41+
// \ No newline at end of file
42+
},
43+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
44+
t.Views().Files().
45+
IsFocused().
46+
Lines(
47+
Contains("file1").IsSelected(),
48+
).
49+
PressEnter()
50+
51+
t.Views().Staging().
52+
IsFocused().
53+
Content(Contains("@@ -1,6 +1,6 @@").DoesNotContain(" 7a")).
54+
SelectedLine(Contains("-3a")).
55+
Press(keys.Universal.IncreaseContextInDiffView).
56+
// still on the same line
57+
SelectedLine(Contains("-3a")).
58+
// '7a' is now visible
59+
Content(Contains("@@ -1,7 +1,7 @@").Contains(" 7a")).
60+
Press(keys.Universal.DecreaseContextInDiffView).
61+
SelectedLine(Contains("-3a")).
62+
Content(Contains("@@ -1,6 +1,6 @@").DoesNotContain(" 7a")).
63+
Press(keys.Universal.DecreaseContextInDiffView).
64+
SelectedLine(Contains("-3a")).
65+
Content(Contains("@@ -1,5 +1,5 @@").DoesNotContain(" 6a")).
66+
Press(keys.Universal.DecreaseContextInDiffView).
67+
// arguably we should still be on -3a, but at the moment the logic puts us on on +3b
68+
SelectedLine(Contains("+3b")).
69+
Content(Contains("@@ -2,3 +2,3 @@").DoesNotContain(" 5a")).
70+
PressPrimaryAction().
71+
Content(DoesNotContain("+3b")).
72+
Press(keys.Universal.TogglePanel)
73+
74+
t.Views().StagingSecondary().
75+
IsFocused().
76+
Content(Contains("@@ -3,2 +3,3 @@\n 3a\n+3b\n 4a")).
77+
Press(keys.Universal.IncreaseContextInDiffView).
78+
Content(Contains("@@ -2,4 +2,5 @@\n 2a\n 3a\n+3b\n 4a\n 5a"))
79+
},
80+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package staging
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var DiscardAllChanges = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Discard all changes of a file in the staging panel, then assert we land in the staging panel of the next file",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.CreateFileAndAdd("file1", "one\ntwo\n")
15+
shell.CreateFileAndAdd("file2", "1\n2\n")
16+
shell.Commit("one")
17+
18+
shell.UpdateFile("file1", "one\ntwo\nthree\nfour\n")
19+
shell.UpdateFile("file2", "1\n2\n3\n4\n")
20+
},
21+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
22+
t.Views().Files().
23+
IsFocused().
24+
Lines(
25+
Contains("file1").IsSelected(),
26+
Contains("file2"),
27+
).
28+
PressEnter()
29+
30+
t.Views().Staging().
31+
IsFocused().
32+
SelectedLine(Contains("+three")).
33+
// discard the line
34+
Press(keys.Universal.Remove).
35+
Tap(func() {
36+
t.Actions().ConfirmDiscardLines()
37+
}).
38+
SelectedLine(Contains("+four")).
39+
// discard the other line
40+
Press(keys.Universal.Remove).
41+
Tap(func() {
42+
t.Actions().ConfirmDiscardLines()
43+
44+
// because there are no more changes in file1 we switch to file2
45+
t.Views().Files().
46+
Lines(
47+
Contains("file2").IsSelected(),
48+
)
49+
}).
50+
// assert we are still in the staging panel, but now looking at the changes of the other file
51+
IsFocused().
52+
SelectedLine(Contains("+3"))
53+
},
54+
})
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package staging
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var StageHunks = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Stage and unstage various hunks of a file in the staging panel",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
// need to be working with a few lines so that git perceives it as two separate hunks
15+
shell.CreateFileAndAdd("file1", "1a\n2a\n3a\n4a\n5a\n6a\n7a\n8a\n9a\n10a\n11a\n12a\n13a\n14a\n15a")
16+
shell.Commit("one")
17+
18+
shell.UpdateFile("file1", "1a\n2a\n3b\n4a\n5a\n6a\n7a\n8a\n9a\n10a\n11a\n12a\n13b\n14a\n15a")
19+
20+
// hunk looks like:
21+
// diff --git a/file1 b/file1
22+
// index 3653080..a6388b6 100644
23+
// --- a/file1
24+
// +++ b/file1
25+
// @@ -1,6 +1,6 @@
26+
// 1a
27+
// 2a
28+
// -3a
29+
// +3b
30+
// 4a
31+
// 5a
32+
// 6a
33+
// @@ -10,6 +10,6 @@
34+
// 10a
35+
// 11a
36+
// 12a
37+
// -13a
38+
// +13b
39+
// 14a
40+
// 15a
41+
// \ No newline at end of file
42+
},
43+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
44+
t.Views().Files().
45+
IsFocused().
46+
Lines(
47+
Contains("file1").IsSelected(),
48+
).
49+
PressEnter()
50+
51+
t.Views().Staging().
52+
IsFocused().
53+
SelectedLine(Contains("-3a")).
54+
Press(keys.Universal.NextBlock).
55+
SelectedLine(Contains("-13a")).
56+
Press(keys.Main.ToggleSelectHunk).
57+
// when in hunk mode, pressing up/down moves us up/down by a hunk
58+
SelectPreviousItem().
59+
SelectedLine(Contains("-3a")).
60+
SelectNextItem().
61+
SelectedLine(Contains("-13a")).
62+
// stage the second hunk
63+
PressPrimaryAction().
64+
Content(Contains("-3a\n+3b")).
65+
Tap(func() {
66+
t.Views().StagingSecondary().
67+
Content(Contains("-13a\n+13b"))
68+
}).
69+
Press(keys.Universal.TogglePanel)
70+
71+
t.Views().StagingSecondary().
72+
IsFocused().
73+
SelectedLine(Contains("-13a")).
74+
// after toggling panel, we're back to only having selected a single line
75+
PressPrimaryAction().
76+
SelectedLine(Contains("+13b")).
77+
PressPrimaryAction().
78+
IsEmpty()
79+
80+
t.Views().Staging().
81+
IsFocused().
82+
SelectedLine(Contains("-3a")).
83+
Press(keys.Main.ToggleSelectHunk).
84+
Press(keys.Universal.Remove).
85+
Tap(func() {
86+
t.Actions().ConfirmDiscardLines()
87+
}).
88+
SelectedLine(Contains("-13a")).
89+
Content(DoesNotContain("-3a").DoesNotContain("+3b"))
90+
},
91+
})

0 commit comments

Comments
 (0)