diff --git a/pkg/integration/components/shell.go b/pkg/integration/components/shell.go index 33b82fc58..5a3bbaa29 100644 --- a/pkg/integration/components/shell.go +++ b/pkg/integration/components/shell.go @@ -31,7 +31,11 @@ func NewShell(dir string, fail func(string)) *Shell { } func (self *Shell) RunCommand(args []string) *Shell { - output, err := self.runCommandWithOutput(args) + return self.RunCommandWithEnv(args, []string{}) +} + +func (self *Shell) RunCommandWithEnv(args []string, env []string) *Shell { + output, err := self.runCommandWithOutputAndEnv(args, env) if err != nil { self.fail(fmt.Sprintf("error running command: %v\n%s", args, output)) } @@ -49,8 +53,12 @@ func (self *Shell) RunCommandExpectError(args []string) *Shell { } func (self *Shell) runCommandWithOutput(args []string) (string, error) { + return self.runCommandWithOutputAndEnv(args, []string{}) +} + +func (self *Shell) runCommandWithOutputAndEnv(args []string, env []string) (string, error) { cmd := exec.Command(args[0], args[1:]...) - cmd.Env = os.Environ() + cmd.Env = append(os.Environ(), env...) cmd.Dir = self.dir output, err := cmd.CombinedOutput() @@ -164,6 +172,14 @@ func (self *Shell) EmptyCommitDaysAgo(message string, daysAgo int) *Shell { return self.RunCommand([]string{"git", "commit", "--allow-empty", "--date", fmt.Sprintf("%d days ago", daysAgo), "-m", message}) } +func (self *Shell) EmptyCommitWithDate(message string, date string) *Shell { + env := []string{ + "GIT_AUTHOR_DATE=" + date, + "GIT_COMMITTER_DATE=" + date, + } + return self.RunCommandWithEnv([]string{"git", "commit", "--allow-empty", "-m", message}, env) +} + func (self *Shell) Revert(ref string) *Shell { return self.RunCommand([]string{"git", "revert", ref}) } diff --git a/pkg/integration/tests/branch/sort_remote_branches.go b/pkg/integration/tests/branch/sort_remote_branches.go new file mode 100644 index 000000000..35e2f700a --- /dev/null +++ b/pkg/integration/tests/branch/sort_remote_branches.go @@ -0,0 +1,55 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var SortRemoteBranches = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Sort remote branches alphabetically or by date", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.NewBranch("first") + shell.EmptyCommitWithDate("commit", "2023-04-07 10:00:00") + shell.NewBranch("second") + shell.EmptyCommitWithDate("commit", "2023-04-07 12:00:00") + shell.NewBranch("third") + shell.EmptyCommitWithDate("commit", "2023-04-07 11:00:00") + shell.CloneIntoRemote("origin") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Remotes(). + Focus(). + Lines( + Contains("origin").IsSelected(), + ). + PressEnter() + + // sorted alphabetically by default + t.Views().RemoteBranches(). + IsFocused(). + Lines( + Contains("first").IsSelected(), + Contains("second"), + Contains("third"), + ). + SelectNextItem() // to test that the selection jumps back to the first when sorting + + t.Views().RemoteBranches(). + Press(keys.Branches.SortOrder) + + t.ExpectPopup().Menu().Title(Equals("Sort order")). + Select(Contains("-committerdate")). + Confirm() + + t.Views().RemoteBranches(). + IsFocused(). + Lines( + Contains("second").IsSelected(), + Contains("third"), + Contains("first"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 0aa61b463..9246aa82a 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -54,6 +54,7 @@ var tests = []*components.IntegrationTest{ branch.ResetToUpstream, branch.SetUpstream, branch.ShowDivergenceFromUpstream, + branch.SortRemoteBranches, branch.Suggestions, branch.UnsetUpstream, cherry_pick.CherryPick,