Skip to content

Commit

Permalink
use defer instead of os.Exit(m.Run())
Browse files Browse the repository at this point in the history
as of go1.15 `testing.M` returns the return code of `m.Run()`, so it is
possible to use `defer`
golang/go#34129
  • Loading branch information
pmenglund committed Feb 6, 2024
1 parent f37306e commit ad78e90
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -100,14 +100,15 @@ func TestMain(m *testing.M) {
log.Fatalf("Could not connect to database: %s", err)
}

code := m.Run()
// as of go1.15 testing.M returns the exit code of m.Run(), so it is safe to use defer here
defer func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}

// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}()

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

func TestSomething(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions examples/FakeGoogleCloudStorage.md
Expand Up @@ -63,8 +63,7 @@ func TestMain(m *testing.M) {
log.Fatalf("Could not connect to Docker: %s", err)
}

code := m.Run()
os.Exit(code)
m.Run()
}

func setUpGcloud() {
Expand Down
24 changes: 12 additions & 12 deletions examples/MongoDB.md
Expand Up @@ -66,20 +66,20 @@ func TestMain(m *testing.M) {
log.Fatalf("Could not connect to docker: %s", err)
}

// run tests
code := m.Run()

// When you're done, kill and remove the container
if err = pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
defer func() {
// When you're done, kill and remove the container
if err = pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}

// disconnect mongodb client
if err = dbClient.Disconnect(context.TODO()); err != nil {
panic(err)
}
// disconnect mongodb client
if err = dbClient.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()

os.Exit(code)
// run tests
m.Run()
}

```
14 changes: 7 additions & 7 deletions examples/Mountebank.md
Expand Up @@ -78,15 +78,15 @@ func TestMain(m *testing.M) {
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
//Run tests
code := m.Run()

// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
defer func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}()

os.Exit(code)
// run tests
m.Run()
}

func TestHandler(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions examples/PostgreSQL.md
Expand Up @@ -65,15 +65,15 @@ func TestMain(m *testing.M) {
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
//Run tests
code := m.Run()

// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
defer func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}()

os.Exit(code)
// run tests
m.Run()
}

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

0 comments on commit ad78e90

Please sign in to comment.