mirror of
				https://github.com/jesseduffield/lazygit.git
				synced 2025-10-30 23:57:43 +02:00 
			
		
		
		
	commands/git : add several tests, do some cleanup
This commit is contained in:
		| @@ -230,7 +230,7 @@ func (c *GitCommand) GetCommitsToPush() []string { | |||||||
|  |  | ||||||
| // RenameCommit renames the topmost commit with the given name | // RenameCommit renames the topmost commit with the given name | ||||||
| func (c *GitCommand) RenameCommit(name string) error { | func (c *GitCommand) RenameCommit(name string) error { | ||||||
| 	return c.OSCommand.RunCommand("git commit --allow-empty --amend -m " + c.OSCommand.Quote(name)) | 	return c.OSCommand.RunCommand(fmt.Sprintf("git commit --allow-empty --amend -m %s", c.OSCommand.Quote(name))) | ||||||
| } | } | ||||||
|  |  | ||||||
| // Fetch fetch git repo | // Fetch fetch git repo | ||||||
| @@ -240,23 +240,23 @@ func (c *GitCommand) Fetch() error { | |||||||
|  |  | ||||||
| // ResetToCommit reset to commit | // ResetToCommit reset to commit | ||||||
| func (c *GitCommand) ResetToCommit(sha string) error { | func (c *GitCommand) ResetToCommit(sha string) error { | ||||||
| 	return c.OSCommand.RunCommand("git reset " + sha) | 	return c.OSCommand.RunCommand(fmt.Sprintf("git reset %s", sha)) | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewBranch create new branch | // NewBranch create new branch | ||||||
| func (c *GitCommand) NewBranch(name string) error { | func (c *GitCommand) NewBranch(name string) error { | ||||||
| 	return c.OSCommand.RunCommand("git checkout -b " + name) | 	return c.OSCommand.RunCommand(fmt.Sprintf("git checkout -b %s", name)) | ||||||
| } | } | ||||||
|  |  | ||||||
| // DeleteBranch delete branch | // DeleteBranch delete branch | ||||||
| func (c *GitCommand) DeleteBranch(branch string, force bool) error { | func (c *GitCommand) DeleteBranch(branch string, force bool) error { | ||||||
| 	var command string | 	command := "git branch -d" | ||||||
|  |  | ||||||
| 	if force { | 	if force { | ||||||
| 		command = "git branch -D " | 		command = "git branch -D" | ||||||
| 	} else { |  | ||||||
| 		command = "git branch -d " |  | ||||||
| 	} | 	} | ||||||
| 	return c.OSCommand.RunCommand(command + branch) |  | ||||||
|  | 	return c.OSCommand.RunCommand(fmt.Sprintf("%s %s", command, branch)) | ||||||
| } | } | ||||||
|  |  | ||||||
| // ListStash list stash | // ListStash list stash | ||||||
| @@ -266,7 +266,7 @@ func (c *GitCommand) ListStash() (string, error) { | |||||||
|  |  | ||||||
| // Merge merge | // Merge merge | ||||||
| func (c *GitCommand) Merge(branchName string) error { | func (c *GitCommand) Merge(branchName string) error { | ||||||
| 	return c.OSCommand.RunCommand("git merge --no-edit " + branchName) | 	return c.OSCommand.RunCommand(fmt.Sprintf("git merge --no-edit %s", branchName)) | ||||||
| } | } | ||||||
|  |  | ||||||
| // AbortMerge abort merge | // AbortMerge abort merge | ||||||
|   | |||||||
| @@ -633,6 +633,103 @@ func TestGitCommandGetCommitsToPush(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestGitCommandRenameCommit(t *testing.T) { | ||||||
|  | 	gitCmd := newDummyGitCommand() | ||||||
|  | 	gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 		assert.EqualValues(t, "git", cmd) | ||||||
|  | 		assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "-m", "test"}, args) | ||||||
|  |  | ||||||
|  | 		return exec.Command("echo") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	assert.NoError(t, gitCmd.RenameCommit("test")) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestGitCommandResetToCommit(t *testing.T) { | ||||||
|  | 	gitCmd := newDummyGitCommand() | ||||||
|  | 	gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 		assert.EqualValues(t, "git", cmd) | ||||||
|  | 		assert.EqualValues(t, []string{"reset", "78976bc"}, args) | ||||||
|  |  | ||||||
|  | 		return exec.Command("echo") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	assert.NoError(t, gitCmd.ResetToCommit("78976bc")) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestGitCommandNewBranch(t *testing.T) { | ||||||
|  | 	gitCmd := newDummyGitCommand() | ||||||
|  | 	gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 		assert.EqualValues(t, "git", cmd) | ||||||
|  | 		assert.EqualValues(t, []string{"checkout", "-b", "test"}, args) | ||||||
|  |  | ||||||
|  | 		return exec.Command("echo") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	assert.NoError(t, gitCmd.NewBranch("test")) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestGitCommandDeleteBranch(t *testing.T) { | ||||||
|  | 	type scenario struct { | ||||||
|  | 		testName string | ||||||
|  | 		branch   string | ||||||
|  | 		force    bool | ||||||
|  | 		command  func(string, ...string) *exec.Cmd | ||||||
|  | 		test     func(error) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	scenarios := []scenario{ | ||||||
|  | 		{ | ||||||
|  | 			"Delete a branch", | ||||||
|  | 			"test", | ||||||
|  | 			false, | ||||||
|  | 			func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 				assert.EqualValues(t, "git", cmd) | ||||||
|  | 				assert.EqualValues(t, []string{"branch", "-d", "test"}, args) | ||||||
|  |  | ||||||
|  | 				return exec.Command("echo") | ||||||
|  | 			}, | ||||||
|  | 			func(err error) { | ||||||
|  | 				assert.NoError(t, err) | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			"Force delete a branch", | ||||||
|  | 			"test", | ||||||
|  | 			true, | ||||||
|  | 			func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 				assert.EqualValues(t, "git", cmd) | ||||||
|  | 				assert.EqualValues(t, []string{"branch", "-D", "test"}, args) | ||||||
|  |  | ||||||
|  | 				return exec.Command("echo") | ||||||
|  | 			}, | ||||||
|  | 			func(err error) { | ||||||
|  | 				assert.NoError(t, err) | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, s := range scenarios { | ||||||
|  | 		t.Run(s.testName, func(t *testing.T) { | ||||||
|  | 			gitCmd := newDummyGitCommand() | ||||||
|  | 			gitCmd.OSCommand.command = s.command | ||||||
|  | 			s.test(gitCmd.DeleteBranch(s.branch, s.force)) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestGitCommandMerge(t *testing.T) { | ||||||
|  | 	gitCmd := newDummyGitCommand() | ||||||
|  | 	gitCmd.OSCommand.command = func(cmd string, args ...string) *exec.Cmd { | ||||||
|  | 		assert.EqualValues(t, "git", cmd) | ||||||
|  | 		assert.EqualValues(t, []string{"merge", "--no-edit", "test"}, args) | ||||||
|  |  | ||||||
|  | 		return exec.Command("echo") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	assert.NoError(t, gitCmd.Merge("test")) | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestGitCommandDiff(t *testing.T) { | func TestGitCommandDiff(t *testing.T) { | ||||||
| 	gitCommand := newDummyGitCommand() | 	gitCommand := newDummyGitCommand() | ||||||
| 	assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) | 	assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh")) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user