Skip to content

Commit

Permalink
fix(test): No os.Exit() calls in TestMain and no SA3000 warning (#…
Browse files Browse the repository at this point in the history
…883)

When upgrading golangci-lint in #870 I introduced a bunch of os.Exit
calls in test functions blindily following the SA300 warning. That's
bad, because it prevents the Go runtime to perform some clean-up after
`m.Run()` returns.
In fact, [Go 1.15
changelog](https://go.dev/doc/go1.15#testingpkgtesting) clearly states
that m.Run() does the right thing in regards to the program's exit code.
`staticcheck` running alone won't report that warning. `golangci-lint`
is failing to propagate the go version, thus confusing the downstream
linter.
  • Loading branch information
CarlosNihelton authored Aug 30, 2024
2 parents 069870c + 96114af commit fc45e52
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 20 deletions.
3 changes: 1 addition & 2 deletions common/grpc/logconnections/logconnections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"flag"
"os"
"testing"

"github.com/canonical/ubuntu-pro-for-wsl/common/grpc/logconnections"
Expand Down Expand Up @@ -109,5 +108,5 @@ func TestMain(m *testing.M) {
logrus.StandardLogger().SetLevel(logrus.DebugLevel)
}

os.Exit(m.Run())
m.Run()
}
4 changes: 1 addition & 3 deletions end-to-end/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestMain(m *testing.M) {
defer cleanup()
testImagePath = path

exitCode := m.Run()
m.Run()

if err := cleanupRegistry(); err != nil {
log.Printf("Cleanup: registry: %v\n", err)
Expand All @@ -111,8 +111,6 @@ func TestMain(m *testing.M) {
if out, err := cmd.CombinedOutput(); err != nil {
log.Printf("Cleanup: could not remove Appx: %v: %s", err, out)
}

os.Exit(exitCode)
}

func usePrebuiltProject(buildPath string) (err error) {
Expand Down
3 changes: 1 addition & 2 deletions storeapi/go-wrapper/microsoftstore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func TestMain(m *testing.M) {
}
}

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

func TestGenerateUserJWT(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions windows-agent/internal/distros/distro/distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

// globalStartupMu protects against multiple distros starting at the same time.
Expand Down
3 changes: 1 addition & 2 deletions windows-agent/internal/distros/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func init() {
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

func TestNew(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

const defaultLandscapeConfig = `
Expand Down
3 changes: 1 addition & 2 deletions windows-agent/internal/proservices/proservices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

func TestNew(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net"
"os"
"sync"
"sync/atomic"
"testing"
Expand All @@ -30,8 +29,7 @@ func TestMain(m *testing.M) {

task.Register[testTask]()

exit := m.Run()
defer os.Exit(exit)
m.Run()
}

func TestServe(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

os.Exit(m.Run())
m.Run()
}

func TestHelp(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion wsl-pro-service/internal/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)

os.Exit(m.Run())
m.Run()
}

func TestNew(t *testing.T) {
Expand Down

0 comments on commit fc45e52

Please sign in to comment.