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"
"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 {

View File

@ -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

View File

@ -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) {

View File

@ -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)

View File

@ -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) {