From c59e6b6451da5eaa9ce4c7e4d581fb44d17ca53a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 27 Mar 2024 19:22:24 +0100 Subject: [PATCH] Cleanup: don't mess with globals in tests without resetting them Changing globals in the init() function of a test file is a bad idea, as it affects all other tests that run after it. Do it explicitly in each test function that needs it, and take care of restoring the previous value afterwards. --- pkg/gui/presentation/branches_test.go | 5 +++++ pkg/gui/presentation/commits_test.go | 7 +++---- pkg/gui/presentation/files_test.go | 10 ++++++---- pkg/gui/presentation/graph/graph_test.go | 17 ++++++++++++----- pkg/gui/style/style_test.go | 11 ++++++----- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index 2414572ca..250b143e3 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -5,12 +5,14 @@ import ( "testing" "time" + "github.com/gookit/color" "github.com/jesseduffield/lazygit/pkg/commands/models" "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" ) func Test_getBranchDisplayStrings(t *testing.T) { @@ -223,6 +225,9 @@ func Test_getBranchDisplayStrings(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone) + defer color.ForceSetColorLevel(oldColorLevel) + c := utils.NewDummyCommon() for i, s := range scenarios { diff --git a/pkg/gui/presentation/commits_test.go b/pkg/gui/presentation/commits_test.go index 16f1de660..f1f075f45 100644 --- a/pkg/gui/presentation/commits_test.go +++ b/pkg/gui/presentation/commits_test.go @@ -16,10 +16,6 @@ import ( "github.com/xo/terminfo" ) -func init() { - color.ForceSetColorLevel(terminfo.ColorLevelNone) -} - func formatExpected(expected string) string { return strings.TrimSpace(strings.ReplaceAll(expected, "\t", "")) } @@ -385,6 +381,9 @@ func TestGetCommitListDisplayStrings(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone) + defer color.ForceSetColorLevel(oldColorLevel) + os.Setenv("TZ", "UTC") focusing := false diff --git a/pkg/gui/presentation/files_test.go b/pkg/gui/presentation/files_test.go index bbaa53947..6662c7732 100644 --- a/pkg/gui/presentation/files_test.go +++ b/pkg/gui/presentation/files_test.go @@ -13,10 +13,6 @@ import ( "github.com/xo/terminfo" ) -func init() { - color.ForceSetColorLevel(terminfo.ColorLevelNone) -} - func toStringSlice(str string) []string { return strings.Split(strings.TrimSpace(str), "\n") } @@ -66,6 +62,9 @@ M file1 }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone) + defer color.ForceSetColorLevel(oldColorLevel) + for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) { @@ -125,6 +124,9 @@ M file1 }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelNone) + defer color.ForceSetColorLevel(oldColorLevel) + for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) { diff --git a/pkg/gui/presentation/graph/graph_test.go b/pkg/gui/presentation/graph/graph_test.go index 834575d7b..a7fe5879b 100644 --- a/pkg/gui/presentation/graph/graph_test.go +++ b/pkg/gui/presentation/graph/graph_test.go @@ -15,11 +15,6 @@ import ( "github.com/xo/terminfo" ) -func init() { - // on CI we've got no color capability so we're forcing it here - color.ForceSetColorLevel(terminfo.ColorLevelMillions) -} - func TestRenderCommitGraph(t *testing.T) { tests := []struct { name string @@ -218,6 +213,9 @@ func TestRenderCommitGraph(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { @@ -452,6 +450,9 @@ func TestRenderPipeSet(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { @@ -523,6 +524,9 @@ func TestGetNextPipes(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + for _, test := range tests { getStyle := func(c *models.Commit) style.TextStyle { return style.FgDefault } pipes := getNextPipes(test.prevPipes, test.commit, getStyle) @@ -538,6 +542,9 @@ func TestGetNextPipes(t *testing.T) { } func BenchmarkRenderCommitGraph(b *testing.B) { + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + commits := generateCommits(50) getStyle := func(commit *models.Commit) style.TextStyle { return authors.AuthorStyle(commit.AuthorName) diff --git a/pkg/gui/style/style_test.go b/pkg/gui/style/style_test.go index c8157efd6..12fec4287 100644 --- a/pkg/gui/style/style_test.go +++ b/pkg/gui/style/style_test.go @@ -10,11 +10,6 @@ import ( "github.com/xo/terminfo" ) -func init() { - // on CI we've got no color capability so we're forcing it here - color.ForceSetColorLevel(terminfo.ColorLevelMillions) -} - func TestMerge(t *testing.T) { type scenario struct { name string @@ -162,6 +157,9 @@ func TestMerge(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) { @@ -210,6 +208,9 @@ func TestTemplateFuncMapAddColors(t *testing.T) { }, } + oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions) + defer color.ForceSetColorLevel(oldColorLevel) + for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) {