1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-24 08:52:21 +02:00

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.
This commit is contained in:
Stefan Haller 2024-03-27 19:22:24 +01:00
parent 1cedfa463b
commit c59e6b6451
5 changed files with 32 additions and 18 deletions

View File

@ -5,12 +5,14 @@ import (
"testing" "testing"
"time" "time"
"github.com/gookit/color"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons" "github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/xo/terminfo"
) )
func Test_getBranchDisplayStrings(t *testing.T) { 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() c := utils.NewDummyCommon()
for i, s := range scenarios { for i, s := range scenarios {

View File

@ -16,10 +16,6 @@ import (
"github.com/xo/terminfo" "github.com/xo/terminfo"
) )
func init() {
color.ForceSetColorLevel(terminfo.ColorLevelNone)
}
func formatExpected(expected string) string { func formatExpected(expected string) string {
return strings.TrimSpace(strings.ReplaceAll(expected, "\t", "")) 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") os.Setenv("TZ", "UTC")
focusing := false focusing := false

View File

@ -13,10 +13,6 @@ import (
"github.com/xo/terminfo" "github.com/xo/terminfo"
) )
func init() {
color.ForceSetColorLevel(terminfo.ColorLevelNone)
}
func toStringSlice(str string) []string { func toStringSlice(str string) []string {
return strings.Split(strings.TrimSpace(str), "\n") 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 { for _, s := range scenarios {
s := s s := s
t.Run(s.name, func(t *testing.T) { 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 { for _, s := range scenarios {
s := s s := s
t.Run(s.name, func(t *testing.T) { t.Run(s.name, func(t *testing.T) {

View File

@ -15,11 +15,6 @@ import (
"github.com/xo/terminfo" "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) { func TestRenderCommitGraph(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -218,6 +213,9 @@ func TestRenderCommitGraph(t *testing.T) {
}, },
} }
oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions)
defer color.ForceSetColorLevel(oldColorLevel)
for _, test := range tests { for _, test := range tests {
test := test test := test
t.Run(test.name, func(t *testing.T) { 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 { for _, test := range tests {
test := test test := test
t.Run(test.name, func(t *testing.T) { 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 { for _, test := range tests {
getStyle := func(c *models.Commit) style.TextStyle { return style.FgDefault } getStyle := func(c *models.Commit) style.TextStyle { return style.FgDefault }
pipes := getNextPipes(test.prevPipes, test.commit, getStyle) pipes := getNextPipes(test.prevPipes, test.commit, getStyle)
@ -538,6 +542,9 @@ func TestGetNextPipes(t *testing.T) {
} }
func BenchmarkRenderCommitGraph(b *testing.B) { func BenchmarkRenderCommitGraph(b *testing.B) {
oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions)
defer color.ForceSetColorLevel(oldColorLevel)
commits := generateCommits(50) commits := generateCommits(50)
getStyle := func(commit *models.Commit) style.TextStyle { getStyle := func(commit *models.Commit) style.TextStyle {
return authors.AuthorStyle(commit.AuthorName) return authors.AuthorStyle(commit.AuthorName)

View File

@ -10,11 +10,6 @@ import (
"github.com/xo/terminfo" "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) { func TestMerge(t *testing.T) {
type scenario struct { type scenario struct {
name string name string
@ -162,6 +157,9 @@ func TestMerge(t *testing.T) {
}, },
} }
oldColorLevel := color.ForceSetColorLevel(terminfo.ColorLevelMillions)
defer color.ForceSetColorLevel(oldColorLevel)
for _, s := range scenarios { for _, s := range scenarios {
s := s s := s
t.Run(s.name, func(t *testing.T) { 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 { for _, s := range scenarios {
s := s s := s
t.Run(s.name, func(t *testing.T) { t.Run(s.name, func(t *testing.T) {