Add worktree integration tests

This commit is contained in:
Jesse Duffield
2023-07-17 22:03:51 +10:00
parent 18a508b29c
commit 277142fc4b
12 changed files with 385 additions and 18 deletions

View File

@@ -0,0 +1,60 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var AddFromBranch = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Add a worktree via the branches view, then switch back to the main worktree via the branches view",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.NewBranch("mybranch")
shell.CreateFileAndAdd("README.md", "hello world")
shell.Commit("initial commit")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Lines(
Contains("mybranch"),
).
Press(keys.Worktrees.ViewWorktreeOptions).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Worktree")).
Select(Contains(`Create worktree from mybranch`).DoesNotContain("detached")).
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New worktree path")).
Type("../linked-worktree").
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New branch name")).
Type("newbranch").
Confirm()
}).
// confirm we're still focused on the branches view
IsFocused().
Lines(
Contains("newbranch").IsSelected(),
Contains("mybranch (worktree)"),
).
NavigateToLine(Contains("mybranch")).
Press(keys.Universal.Select).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Switch to worktree")).
Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")).
Confirm()
}).
Lines(
Contains("mybranch").IsSelected(),
Contains("newbranch (worktree)"),
)
},
})

View File

@@ -0,0 +1,120 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var Crud = NewIntegrationTest(NewIntegrationTestArgs{
Description: "From the worktrees view, add a work tree, switch to it, switch back, and remove it",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.NewBranch("mybranch")
shell.CreateFileAndAdd("README.md", "hello world")
shell.Commit("initial commit")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Lines(
Contains("mybranch"),
)
t.Views().Status().
Lines(
Contains("repo → mybranch"),
)
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)"),
).
Press(keys.Universal.New).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Worktree")).
Select(Contains(`Create worktree from ref`).DoesNotContain(("detached"))).
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New worktree base ref")).
InitialText(Equals("mybranch")).
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New worktree path")).
Type("../linked-worktree").
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New branch name (leave blank to checkout mybranch)")).
Type("newbranch").
Confirm()
}).
Lines(
Contains("linked-worktree").IsSelected(),
Contains("repo (main)"),
).
// confirm we're still in the same view
IsFocused()
// status panel includes the worktree if it's a linked worktree
t.Views().Status().
Lines(
Contains("repo(linked-worktree) → newbranch"),
)
t.Views().Branches().
Lines(
Contains("newbranch"),
Contains("mybranch"),
)
t.Views().Worktrees().
// confirm we can't remove the current worktree
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Alert().
Title(Equals("Error")).
Content(Equals("You cannot remove the current worktree!")).
Confirm()
}).
// confirm we cannot remove the main worktree
NavigateToLine(Contains("repo (main)")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Alert().
Title(Equals("Error")).
Content(Equals("You cannot remove the main worktree!")).
Confirm()
}).
// switch back to main worktree
Press(keys.Universal.Select).
Lines(
Contains("repo (main)").IsSelected(),
Contains("linked-worktree"),
)
t.Views().Branches().
Lines(
Contains("mybranch"),
Contains("newbranch"),
)
t.Views().Worktrees().
// remove linked worktree
NavigateToLine(Contains("linked-worktree")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Remove worktree")).
Content(Contains("Are you sure you want to remove worktree 'linked-worktree'?")).
Confirm()
}).
Lines(
Contains("repo (main)").IsSelected(),
)
},
})

View File

@@ -0,0 +1,70 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
// This is important because `git worktree list` will show a worktree being in a detached head state (which is true)
// when it's in the middle of a rebase, but it won't tell you about the branch it's on.
// Even so, if you attempt to check out that branch from another worktree git won't let you, so we need to
// keep track of the association ourselves.
var Rebase = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Verify that when you start a rebase in a worktree, Lazygit still associates the worktree with the branch",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.NewBranch("mybranch")
shell.CreateFileAndAdd("README.md", "hello world")
shell.Commit("initial commit")
shell.EmptyCommit("commit 2")
shell.EmptyCommit("commit 3")
shell.AddWorktree("mybranch", "../linked-worktree", "newbranch")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Lines(
Contains("mybranch"),
Contains("newbranch (worktree)"),
)
t.Views().Commits().
Focus().
NavigateToLine(Contains("commit 2")).
Press(keys.Universal.Edit)
t.Views().Information().Content(Contains("Rebasing"))
t.Views().Branches().
Focus().
// switch to linked worktree
NavigateToLine(Contains("newbranch")).
Press(keys.Universal.Select).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Switch to worktree")).
Content(Equals("This branch is checked out by worktree linked-worktree. Do you want to switch to that worktree?")).
Confirm()
t.Views().Information().Content(DoesNotContain("Rebasing"))
}).
Lines(
Contains("newbranch").IsSelected(),
Contains("mybranch (worktree)"),
).
// switch back to main worktree
NavigateToLine(Contains("mybranch")).
Press(keys.Universal.Select).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Switch to worktree")).
Content(Equals("This branch is checked out by worktree repo. Do you want to switch to that worktree?")).
Confirm()
t.Views().Information().Content(Contains("Rebasing"))
})
},
})

View File

@@ -0,0 +1,85 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var WorktreeInRepo = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Add a worktree inside the repo, then remove the directory and confirm the worktree is removed",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.NewBranch("mybranch")
shell.CreateFileAndAdd("README.md", "hello world")
shell.Commit("initial commit")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Lines(
Contains("mybranch"),
)
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)"),
).
Press(keys.Universal.New).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Worktree")).
Select(Contains(`Create worktree from ref`).DoesNotContain(("detached"))).
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New worktree base ref")).
InitialText(Equals("mybranch")).
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New worktree path")).
Type("linked-worktree").
Confirm()
t.ExpectPopup().Prompt().
Title(Equals("New branch name (leave blank to checkout mybranch)")).
Type("newbranch").
Confirm()
}).
Lines(
Contains("linked-worktree").IsSelected(),
Contains("repo (main)"),
).
// switch back to main worktree
NavigateToLine(Contains("repo (main)")).
Press(keys.Universal.Select).
Lines(
Contains("repo (main)").IsSelected(),
Contains("linked-worktree"),
)
t.Views().Files().
Focus().
Lines(
Contains("linked-worktree"),
).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("linked-worktree")).
Select(Contains("Discard all changes")).
Confirm()
}).
IsEmpty()
// confirm worktree appears as missing
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)").IsSelected(),
Contains("linked-worktree (missing)"),
)
},
})