Skip to content

Commit

Permalink
Fix filtering and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r-darwish committed Mar 10, 2025
1 parent 0bb899e commit 92af3e3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
21 changes: 19 additions & 2 deletions pkg/gui/controllers/helpers/mode_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ type ModeStatus struct {
Reset func() error
}

func (self *ModeHelper) getFilter() string {
filtering := self.c.Modes().Filtering

if path := filtering.GetPath(); path != "" {
return path
}

if author := filtering.GetAuthor(); author != "" {
return author
}

if changes := filtering.GetChanges(); changes != "" {
return changes
}

return ""
}

func (self *ModeHelper) Statuses() []ModeStatus {
return []ModeStatus{
{
Expand All @@ -72,12 +90,11 @@ func (self *ModeHelper) Statuses() []ModeStatus {
{
IsActive: self.c.Modes().Filtering.Active,
Description: func() string {
filterContent := lo.Ternary(self.c.Modes().Filtering.GetPath() != "", self.c.Modes().Filtering.GetPath(), self.c.Modes().Filtering.GetAuthor())
return self.withResetButton(
fmt.Sprintf(
"%s '%s'",
self.c.Tr.FilteringBy,
filterContent,
self.getFilter(),
),
style.FgRed,
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/gui/modes/filtering/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ func New(path string, author string) Filtering {
}

func (m *Filtering) Active() bool {
return m.path != "" || m.author != ""
return m.path != "" || m.author != "" || m.changes != ""
}

func (m *Filtering) Reset() {
m.path = ""
m.author = ""
m.changes = ""
}

func (m *Filtering) SetPath(path string) {
Expand Down
62 changes: 62 additions & 0 deletions pkg/integration/tests/filter_by_changes/type_changes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package filter_by_changes

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var TypeChanges = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Filter commits by author using the typed in author",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
},
SetupRepo: func(shell *Shell) {
shell.CreateNCommits(4)
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Status().
Focus().
Press(keys.Universal.FilteringMenu)

t.ExpectPopup().Menu().
Title(Equals("Filtering")).
Select(Contains("Enter changes to filter by")).
Confirm()

t.ExpectPopup().Prompt().
Title(Equals("Enter changes:")).
Type("file01").
Confirm()

t.Views().Commits().
IsFocused().
Lines(
Contains("commit 01"),
)

t.Views().Information().Content(Contains("Filtering by 'file01'"))

t.Views().Status().
Focus().
Press(keys.Universal.FilteringMenu)

t.ExpectPopup().Menu().
Title(Equals("Filtering")).
Select(Contains("Enter changes to filter by")).
Confirm()

t.ExpectPopup().Prompt().
Title(Equals("Enter changes:")).
Type("file02").
Confirm()

t.Views().Commits().
IsFocused().
Lines(
Contains("commit 02"),
)

t.Views().Information().Content(Contains("Filtering by 'file02'"))
},
})
2 changes: 2 additions & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/integration/tests/file"
"github.com/jesseduffield/lazygit/pkg/integration/tests/filter_and_search"
"github.com/jesseduffield/lazygit/pkg/integration/tests/filter_by_author"
"github.com/jesseduffield/lazygit/pkg/integration/tests/filter_by_changes"
"github.com/jesseduffield/lazygit/pkg/integration/tests/filter_by_path"
"github.com/jesseduffield/lazygit/pkg/integration/tests/interactive_rebase"
"github.com/jesseduffield/lazygit/pkg/integration/tests/misc"
Expand Down Expand Up @@ -212,6 +213,7 @@ var tests = []*components.IntegrationTest{
filter_by_path.KeepSameCommitSelectedOnExit,
filter_by_path.SelectFile,
filter_by_path.TypeFile,
filter_by_changes.TypeChanges,
interactive_rebase.AdvancedInteractiveRebase,
interactive_rebase.AmendCommitWithConflict,
interactive_rebase.AmendFirstCommit,
Expand Down

0 comments on commit 92af3e3

Please sign in to comment.