Skip to content

Commit

Permalink
Use t.Cleanup instead of returning a function.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFaer committed Jan 12, 2024
1 parent 1e0d092 commit 854dcea
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions keepsorted/keep_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ var (

// initZerolog initializes zerolog to log as part of the test.
// It returns a function that restores zerolog to its state before this function was called.
func initZerolog(t testing.TB) func() {
func initZerolog(t testing.TB) {
oldLogger := log.Logger
log.Logger = log.Output(zerolog.NewTestWriter(t))
return func() { log.Logger = oldLogger }
t.Cleanup(func() { log.Logger = oldLogger })
}

func defaultOptionsWith(f func(*blockOptions)) blockOptions {
Expand Down Expand Up @@ -180,7 +180,7 @@ foo
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
got, gotAlreadyFixed := New("keep-sorted-test").Fix(tc.in, nil)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("Fix diff (-want +got):\n%s", diff)
Expand Down Expand Up @@ -301,7 +301,7 @@ baz
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
var mod []LineRange
if tc.modifiedLines != nil {
for _, l := range tc.modifiedLines {
Expand Down Expand Up @@ -636,7 +636,7 @@ dog
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
if tc.include == nil {
tc.include = func(start, end int) bool { return true }
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func TestLineSorting(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
got, gotAlreadySorted := block{lines: tc.in, opts: tc.opts}.sorted()
if gotAlreadySorted != tc.wantAlreadySorted {
t.Errorf("alreadySorted mismatch: got %t want %t", gotAlreadySorted, tc.wantAlreadySorted)
Expand Down Expand Up @@ -1354,7 +1354,7 @@ func TestLineGrouping(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
var in []string
for _, lg := range tc.want {
in = append(in, lg.comment...)
Expand Down Expand Up @@ -1445,7 +1445,7 @@ func TestBlockOptions(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
defer initZerolog(t)() // Note the extra parens.
initZerolog(t)
got, err := New("keep-sorted-test").parseBlockOptions(tc.in)
if err != nil {
if tc.wantErr == "" {
Expand Down

0 comments on commit 854dcea

Please sign in to comment.