Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use defer instead of os.Exit(m.Run()) #493

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading