From a6a68778ea66bd18508ed953d545d85ba17053ce Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 6 May 2025 09:37:18 +0200 Subject: [PATCH] Move NewDummyCommon to pkg/common It's too special for a utils package, and it als makes sense to put it right next to the thing that it is a dummy for. --- .../git_commands/commit_loader_test.go | 5 +-- pkg/commands/git_commands/deps_test.go | 3 +- .../git_commands/reflog_commit_loader_test.go | 3 +- .../git_commands/stash_loader_test.go | 4 +-- pkg/commands/git_commands/tag_loader_test.go | 4 +-- pkg/commands/oscommands/dummies.go | 12 +++---- pkg/common/dummies.go | 33 +++++++++++++++++++ pkg/gui/dummies.go | 6 ++-- pkg/gui/presentation/branches_test.go | 4 +-- pkg/gui/presentation/commits_test.go | 3 +- .../custom_commands/menu_generator_test.go | 4 +-- pkg/utils/dummies.go | 29 ---------------- 12 files changed, 58 insertions(+), 52 deletions(-) create mode 100644 pkg/common/dummies.go diff --git a/pkg/commands/git_commands/commit_loader_test.go b/pkg/commands/git_commands/commit_loader_test.go index 69d0de017..cc959b4ea 100644 --- a/pkg/commands/git_commands/commit_loader_test.go +++ b/pkg/commands/git_commands/commit_loader_test.go @@ -8,6 +8,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" @@ -296,7 +297,7 @@ func TestGetCommits(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.testName, func(t *testing.T) { - common := utils.NewDummyCommon() + common := common.NewDummyCommon() common.AppState = &config.AppState{} common.AppState.GitLogOrder = scenario.logOrder cmd := oscommands.NewDummyCmdObjBuilder(scenario.runner) @@ -516,7 +517,7 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) { } for _, scenario := range scenarios { t.Run(scenario.testName, func(t *testing.T) { - common := utils.NewDummyCommon() + common := common.NewDummyCommon() builder := &CommitLoader{ Common: common, diff --git a/pkg/commands/git_commands/deps_test.go b/pkg/commands/git_commands/deps_test.go index 78990a55e..bb079c063 100644 --- a/pkg/commands/git_commands/deps_test.go +++ b/pkg/commands/git_commands/deps_test.go @@ -9,7 +9,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/utils" "github.com/spf13/afero" ) @@ -32,7 +31,7 @@ func buildGitCommon(deps commonDeps) *GitCommon { gitCommon.Common = deps.common if gitCommon.Common == nil { - gitCommon.Common = utils.NewDummyCommonWithUserConfigAndAppState(deps.userConfig, deps.appState) + gitCommon.Common = common.NewDummyCommonWithUserConfigAndAppState(deps.userConfig, deps.appState) } if deps.fs != nil { diff --git a/pkg/commands/git_commands/reflog_commit_loader_test.go b/pkg/commands/git_commands/reflog_commit_loader_test.go index 3a90688a2..5cc7395c0 100644 --- a/pkg/commands/git_commands/reflog_commit_loader_test.go +++ b/pkg/commands/git_commands/reflog_commit_loader_test.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" "github.com/sanity-io/litter" @@ -181,7 +182,7 @@ func TestGetReflogCommits(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.testName, func(t *testing.T) { builder := &ReflogCommitLoader{ - Common: utils.NewDummyCommon(), + Common: common.NewDummyCommon(), cmd: oscommands.NewDummyCmdObjBuilder(scenario.runner), } diff --git a/pkg/commands/git_commands/stash_loader_test.go b/pkg/commands/git_commands/stash_loader_test.go index 4f1f171fe..99e6a57d2 100644 --- a/pkg/commands/git_commands/stash_loader_test.go +++ b/pkg/commands/git_commands/stash_loader_test.go @@ -5,7 +5,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/stretchr/testify/assert" ) @@ -50,7 +50,7 @@ func TestGetStashEntries(t *testing.T) { t.Run(s.testName, func(t *testing.T) { cmd := oscommands.NewDummyCmdObjBuilder(s.runner) - loader := NewStashLoader(utils.NewDummyCommon(), cmd) + loader := NewStashLoader(common.NewDummyCommon(), cmd) assert.EqualValues(t, s.expectedStashEntries, loader.GetStashEntries(s.filterPath)) }) diff --git a/pkg/commands/git_commands/tag_loader_test.go b/pkg/commands/git_commands/tag_loader_test.go index 1d24e9dd1..e8f19fcd4 100644 --- a/pkg/commands/git_commands/tag_loader_test.go +++ b/pkg/commands/git_commands/tag_loader_test.go @@ -5,7 +5,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/stretchr/testify/assert" ) @@ -46,7 +46,7 @@ func TestGetTags(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.testName, func(t *testing.T) { loader := &TagLoader{ - Common: utils.NewDummyCommon(), + Common: common.NewDummyCommon(), cmd: oscommands.NewDummyCmdObjBuilder(scenario.runner), } diff --git a/pkg/commands/oscommands/dummies.go b/pkg/commands/oscommands/dummies.go index 3f72a6f06..9490a970d 100644 --- a/pkg/commands/oscommands/dummies.go +++ b/pkg/commands/oscommands/dummies.go @@ -8,7 +8,7 @@ import ( // NewDummyOSCommand creates a new dummy OSCommand for testing func NewDummyOSCommand() *OSCommand { - osCmd := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) + osCmd := NewOSCommand(common.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) return osCmd } @@ -23,9 +23,9 @@ type OSCommandDeps struct { } func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand { - common := deps.Common - if common == nil { - common = utils.NewDummyCommon() + cmn := deps.Common + if cmn == nil { + cmn = common.NewDummyCommon() } platform := deps.Platform @@ -34,7 +34,7 @@ func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand { } return &OSCommand{ - Common: common, + Common: cmn, Platform: platform, getenvFn: deps.GetenvFn, removeFileFn: deps.RemoveFileFn, @@ -59,7 +59,7 @@ var dummyPlatform = &Platform{ } func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand { - osCommand := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) + osCommand := NewOSCommand(common.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) osCommand.Cmd = NewDummyCmdObjBuilder(runner) return osCommand diff --git a/pkg/common/dummies.go b/pkg/common/dummies.go new file mode 100644 index 000000000..c689bca09 --- /dev/null +++ b/pkg/common/dummies.go @@ -0,0 +1,33 @@ +package common + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/spf13/afero" +) + +func NewDummyCommon() *Common { + tr := i18n.EnglishTranslationSet() + cmn := &Common{ + Log: utils.NewDummyLog(), + Tr: tr, + Fs: afero.NewOsFs(), + } + cmn.SetUserConfig(config.GetDefaultConfig()) + return cmn +} + +func NewDummyCommonWithUserConfigAndAppState(userConfig *config.UserConfig, appState *config.AppState) *Common { + tr := i18n.EnglishTranslationSet() + cmn := &Common{ + Log: utils.NewDummyLog(), + Tr: tr, + AppState: appState, + // TODO: remove dependency on actual filesystem in tests and switch to using + // in-memory for everything + Fs: afero.NewOsFs(), + } + cmn.SetUserConfig(userConfig) + return cmn +} diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go index 2350d215e..979f818e1 100644 --- a/pkg/gui/dummies.go +++ b/pkg/gui/dummies.go @@ -3,20 +3,20 @@ package gui import ( "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/updates" - "github.com/jesseduffield/lazygit/pkg/utils" ) func NewDummyUpdater() *updates.Updater { newAppConfig := config.NewDummyAppConfig() - dummyUpdater, _ := updates.NewUpdater(utils.NewDummyCommon(), newAppConfig, oscommands.NewDummyOSCommand()) + dummyUpdater, _ := updates.NewUpdater(common.NewDummyCommon(), newAppConfig, oscommands.NewDummyOSCommand()) return dummyUpdater } // NewDummyGui creates a new dummy GUI for testing func NewDummyGui() *Gui { newAppConfig := config.NewDummyAppConfig() - dummyGui, _ := NewGui(utils.NewDummyCommon(), newAppConfig, &git_commands.GitVersion{Major: 2, Minor: 0, Patch: 0}, NewDummyUpdater(), false, "", nil) + dummyGui, _ := NewGui(common.NewDummyCommon(), newAppConfig, &git_commands.GitVersion{Major: 2, Minor: 0, Patch: 0}, NewDummyUpdater(), false, "", nil) return dummyGui } diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index 02377e8bc..01910f119 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -8,9 +8,9 @@ import ( "github.com/gookit/color" "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/gui/presentation/icons" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/xo/terminfo" @@ -320,7 +320,7 @@ func Test_getBranchDisplayStrings(t *testing.T) { oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone) defer color.ForceSetColorLevel(oldColorLevel) - c := utils.NewDummyCommon() + c := common.NewDummyCommon() SetCustomBranches(c.UserConfig().Gui.BranchColorPatterns, true) for i, s := range scenarios { diff --git a/pkg/gui/presentation/commits_test.go b/pkg/gui/presentation/commits_test.go index 595c3d750..1536d420a 100644 --- a/pkg/gui/presentation/commits_test.go +++ b/pkg/gui/presentation/commits_test.go @@ -10,6 +10,7 @@ import ( "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" "github.com/stefanhaller/git-todo-parser/todo" @@ -539,7 +540,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) { } } - common := utils.NewDummyCommon() + common := common.NewDummyCommon() for _, s := range scenarios { if !focusing || s.focus { diff --git a/pkg/gui/services/custom_commands/menu_generator_test.go b/pkg/gui/services/custom_commands/menu_generator_test.go index 81480d1d8..d5f15028e 100644 --- a/pkg/gui/services/custom_commands/menu_generator_test.go +++ b/pkg/gui/services/custom_commands/menu_generator_test.go @@ -3,7 +3,7 @@ package custom_commands import ( "testing" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/stretchr/testify/assert" ) @@ -82,7 +82,7 @@ func TestMenuGenerator(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { - s.test(NewMenuGenerator(utils.NewDummyCommon()).call(s.cmdOut, s.filter, s.valueFormat, s.labelFormat)) + s.test(NewMenuGenerator(common.NewDummyCommon()).call(s.cmdOut, s.filter, s.valueFormat, s.labelFormat)) }) } } diff --git a/pkg/utils/dummies.go b/pkg/utils/dummies.go index 1a2348b7a..2d9e73b03 100644 --- a/pkg/utils/dummies.go +++ b/pkg/utils/dummies.go @@ -3,11 +3,7 @@ package utils import ( "io" - "github.com/jesseduffield/lazygit/pkg/common" - "github.com/jesseduffield/lazygit/pkg/config" - "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/sirupsen/logrus" - "github.com/spf13/afero" ) // NewDummyLog creates a new dummy Log for testing @@ -16,28 +12,3 @@ func NewDummyLog() *logrus.Entry { log.Out = io.Discard return log.WithField("test", "test") } - -func NewDummyCommon() *common.Common { - tr := i18n.EnglishTranslationSet() - cmn := &common.Common{ - Log: NewDummyLog(), - Tr: tr, - Fs: afero.NewOsFs(), - } - cmn.SetUserConfig(config.GetDefaultConfig()) - return cmn -} - -func NewDummyCommonWithUserConfigAndAppState(userConfig *config.UserConfig, appState *config.AppState) *common.Common { - tr := i18n.EnglishTranslationSet() - cmn := &common.Common{ - Log: NewDummyLog(), - Tr: tr, - AppState: appState, - // TODO: remove dependency on actual filesystem in tests and switch to using - // in-memory for everything - Fs: afero.NewOsFs(), - } - cmn.SetUserConfig(userConfig) - return cmn -}