1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-09 13:47:11 +02:00

Add worktree tests for removing/detaching

This commit is contained in:
Jesse Duffield 2023-07-24 15:29:23 +10:00
parent 277142fc4b
commit b93b9dae88
9 changed files with 183 additions and 8 deletions

View File

@ -42,20 +42,30 @@ func (self *GitCommandBuilder) Config(value string) *GitCommandBuilder {
return self
}
func (self *GitCommandBuilder) RepoPath(value string) *GitCommandBuilder {
// the -C arg will make git do a `cd` to the directory before doing anything else
func (self *GitCommandBuilder) Dir(path string) *GitCommandBuilder {
// repo path comes before the command
self.args = append([]string{"-C", value}, self.args...)
self.args = append([]string{"-C", path}, self.args...)
return self
}
func (self *GitCommandBuilder) WorktreePath(path string) *GitCommandBuilder {
// worktree path comes before the command
// Note, you may prefer to use the Dir method instead of this one
func (self *GitCommandBuilder) Worktree(path string) *GitCommandBuilder {
// worktree arg comes before the command
self.args = append([]string{"--work-tree", path}, self.args...)
return self
}
// Note, you may prefer to use the Dir method instead of this one
func (self *GitCommandBuilder) GitDir(path string) *GitCommandBuilder {
// git dir arg comes before the command
self.args = append([]string{"--git-dir", path}, self.args...)
return self
}
func (self *GitCommandBuilder) ToArgv() []string {
return append([]string{"git"}, self.args...)
}

View File

@ -45,7 +45,7 @@ func TestGitCommandBuilder(t *testing.T) {
expected: []string{"git", "-c", "user.email=bar", "-c", "user.name=foo", "push"},
},
{
input: NewGitCmd("push").RepoPath("a/b/c").ToArgv(),
input: NewGitCmd("push").Dir("a/b/c").ToArgv(),
expected: []string{"git", "-C", "a/b/c", "push"},
},
}

View File

@ -82,7 +82,7 @@ func (self *SubmoduleCommands) Stash(submodule *models.SubmoduleConfig) error {
}
cmdArgs := NewGitCmd("stash").
RepoPath(submodule.Path).
Dir(submodule.Path).
Arg("--include-untracked").
ToArgv()

View File

@ -54,9 +54,9 @@ func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
}
func (self *WorktreeCommands) Detach(worktreePath string) error {
cmdArgs := NewGitCmd("checkout").Arg("--detach").ToArgv()
cmdArgs := NewGitCmd("checkout").Arg("--detach").GitDir(filepath.Join(worktreePath, ".git")).ToArgv()
return self.cmd.New(cmdArgs).SetWd(worktreePath).Run()
return self.cmd.New(cmdArgs).Run()
}
func (self *WorktreeCommands) IsCurrentWorktree(path string) bool {

View File

@ -261,6 +261,16 @@ func (self *Shell) AddWorktree(base string, path string, newBranchName string) *
})
}
func (self *Shell) AddFileInWorktree(worktreePath string) *Shell {
self.CreateFile(filepath.Join(worktreePath, "content"), "content")
self.RunCommand([]string{
"git", "-C", worktreePath, "add", "content",
})
return self
}
func (self *Shell) MakeExecutable(path string) *Shell {
// 0755 sets the executable permission for owner, and read/execute permissions for group and others
err := os.Chmod(filepath.Join(self.dir, path), 0o755)

View File

@ -222,6 +222,9 @@ var tests = []*components.IntegrationTest{
undo.UndoDrop,
worktree.AddFromBranch,
worktree.Crud,
worktree.DetachWorktreeFromBranch,
worktree.ForceRemoveWorktree,
worktree.Rebase,
worktree.RemoveWorktreeFromBranch,
worktree.WorktreeInRepo,
}

View File

@ -0,0 +1,48 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var DetachWorktreeFromBranch = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Detach a worktree from 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")
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").IsSelected(),
Contains("newbranch (worktree)"),
).
NavigateToLine(Contains("newbranch")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Branch newbranch is checked out by worktree linked-worktree")).
Select(Equals("Detach worktree")).
Confirm()
}).
Lines(
Contains("mybranch"),
Contains("newbranch").DoesNotContain("(worktree").IsSelected(),
)
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)").IsSelected(),
Contains("linked-worktree"),
)
},
})

View File

@ -0,0 +1,46 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var ForceRemoveWorktree = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Force remove a dirty worktree",
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")
shell.AddFileInWorktree("../linked-worktree")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)").IsSelected(),
Contains("linked-worktree"),
).
NavigateToLine(Contains("linked-worktree")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Remove worktree")).
Content(Equals("Are you sure you want to remove worktree 'linked-worktree'?")).
Confirm()
t.ExpectPopup().Confirmation().
Title(Equals("Remove worktree")).
Content(Equals("'linked-worktree' contains modified or untracked files (to be honest, it could contain both). Are you sure you want to remove it?")).
Confirm()
}).
Lines(
Contains("repo (main)").IsSelected(),
)
},
})

View File

@ -0,0 +1,58 @@
package worktree
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var RemoveWorktreeFromBranch = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Remove a worktree from 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")
shell.EmptyCommit("commit 2")
shell.EmptyCommit("commit 3")
shell.AddWorktree("mybranch", "../linked-worktree", "newbranch")
shell.AddFileInWorktree("../linked-worktree")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Branches().
Focus().
Lines(
Contains("mybranch").IsSelected(),
Contains("newbranch (worktree)"),
).
NavigateToLine(Contains("newbranch")).
Press(keys.Universal.Remove).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Branch newbranch is checked out by worktree linked-worktree")).
Select(Equals("Remove worktree")).
Confirm()
t.ExpectPopup().Confirmation().
Title(Equals("Remove worktree")).
Content(Equals("Are you sure you want to remove worktree 'linked-worktree'?")).
Confirm()
t.ExpectPopup().Confirmation().
Title(Equals("Remove worktree")).
Content(Equals("'linked-worktree' contains modified or untracked files (to be honest, it could contain both). Are you sure you want to remove it?")).
Confirm()
}).
Lines(
Contains("mybranch"),
Contains("newbranch").DoesNotContain("(worktree").IsSelected(),
)
t.Views().Worktrees().
Focus().
Lines(
Contains("repo (main)").IsSelected(),
)
},
})