From ccd39bb8ae582e00f345b8a578853e2dcf23b136 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 18 Aug 2024 11:18:54 +0200 Subject: [PATCH 1/3] Fix wrong test assertion text If a `t.FileSystem().FileContent("file.txt", Equals("bla"))` assertion fails because the file doesn't exist, the error would say Expected path 'file.txt' to not exist, but it does which is very confusing. --- pkg/integration/components/file_system.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/integration/components/file_system.go b/pkg/integration/components/file_system.go index 74f179fdc..feea9a5b1 100644 --- a/pkg/integration/components/file_system.go +++ b/pkg/integration/components/file_system.go @@ -30,7 +30,7 @@ func (self *FileSystem) FileContent(path string, matcher *TextMatcher) { self.assertWithRetries(func() (bool, string) { _, err := os.Stat(path) if os.IsNotExist(err) { - return false, fmt.Sprintf("Expected path '%s' to not exist, but it does", path) + return false, fmt.Sprintf("Expected path '%s' to exist, but it does not", path) } output, err := os.ReadFile(path) From 283ed29f10d2bcfce6b62be622489bc0e9850672 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 18 Aug 2024 11:21:42 +0200 Subject: [PATCH 2/3] Add a test that shows how per-repo config file replaces customCommands We want to add to the global customCommands instead of replacing them. --- .../custom_commands_in_per_repo_config.go | 63 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 64 insertions(+) create mode 100644 pkg/integration/tests/config/custom_commands_in_per_repo_config.go diff --git a/pkg/integration/tests/config/custom_commands_in_per_repo_config.go b/pkg/integration/tests/config/custom_commands_in_per_repo_config.go new file mode 100644 index 000000000..4cff322ef --- /dev/null +++ b/pkg/integration/tests/config/custom_commands_in_per_repo_config.go @@ -0,0 +1,63 @@ +package config + +import ( + "path/filepath" + + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CustomCommandsInPerRepoConfig = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Custom commands in per-repo config add to the global ones instead of replacing them", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(cfg *config.AppConfig) { + otherRepo, _ := filepath.Abs("../other") + cfg.GetAppState().RecentRepos = []string{otherRepo} + + cfg.GetUserConfig().CustomCommands = []config.CustomCommand{ + { + Key: "X", + Context: "global", + Command: "printf 'global X' > file.txt", + }, + { + Key: "Y", + Context: "global", + Command: "printf 'global Y' > file.txt", + }, + } + }, + SetupRepo: func(shell *Shell) { + shell.CloneNonBare("other") + shell.CreateFile("../other/.git/lazygit.yml", ` +customCommands: + - key: Y + context: global + command: printf 'local Y' > file.txt + - key: Z + context: global + command: printf 'local Z' > file.txt`) + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.GlobalPress(keys.Universal.OpenRecentRepos) + t.ExpectPopup().Menu().Title(Equals("Recent repositories")). + Lines( + Contains("other").IsSelected(), + Contains("Cancel"), + ).Confirm() + t.Views().Status().Content(Contains("other → master")) + + t.GlobalPress("X") + /* EXPECTED: + t.FileSystem().FileContent("../other/file.txt", Equals("global X")) + ACTUAL: */ + t.FileSystem().PathNotPresent("../other/file.txt") + + t.GlobalPress("Y") + t.FileSystem().FileContent("../other/file.txt", Equals("local Y")) + + t.GlobalPress("Z") + t.FileSystem().FileContent("../other/file.txt", Equals("local Z")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 8f547078a..de074232f 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -110,6 +110,7 @@ var tests = []*components.IntegrationTest{ commit.Staged, commit.StagedWithoutHooks, commit.Unstaged, + config.CustomCommandsInPerRepoConfig, config.RemoteNamedStar, conflicts.Filter, conflicts.ResolveExternally, From 30f43a245b76d43bcfb3ec3f4c7d73c953c325c5 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 18 Aug 2024 11:27:08 +0200 Subject: [PATCH 3/3] Fix loading customCommands from per-repo config file Any newly loaded custom command coming from the per-repo config file should add to the global ones (or override an existing one in the global one), rather than replace all global ones. We can achieve this by simply prepending the newly loaded commands to the existing ones. We don't have to take care of removing duplicate key assignments; it is already possible to add two custom commands with the same key to the global config file, the first one wins. --- pkg/config/app_config.go | 4 ++++ .../tests/config/custom_commands_in_per_repo_config.go | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 936dcb64a..d68620867 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -196,10 +196,14 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e return nil, err } + existingCustomCommands := base.CustomCommands + if err := yaml.Unmarshal(content, base); err != nil { return nil, fmt.Errorf("The config at `%s` couldn't be parsed, please inspect it before opening up an issue.\n%w", path, err) } + base.CustomCommands = append(base.CustomCommands, existingCustomCommands...) + if err := base.Validate(); err != nil { return nil, fmt.Errorf("The config at `%s` has a validation error.\n%w", path, err) } diff --git a/pkg/integration/tests/config/custom_commands_in_per_repo_config.go b/pkg/integration/tests/config/custom_commands_in_per_repo_config.go index 4cff322ef..81f8724aa 100644 --- a/pkg/integration/tests/config/custom_commands_in_per_repo_config.go +++ b/pkg/integration/tests/config/custom_commands_in_per_repo_config.go @@ -49,10 +49,7 @@ customCommands: t.Views().Status().Content(Contains("other → master")) t.GlobalPress("X") - /* EXPECTED: t.FileSystem().FileContent("../other/file.txt", Equals("global X")) - ACTUAL: */ - t.FileSystem().PathNotPresent("../other/file.txt") t.GlobalPress("Y") t.FileSystem().FileContent("../other/file.txt", Equals("local Y"))