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

DE-1331 Get rid of github.com/facebookgo/ensure and update README #336

Merged
merged 30 commits into from
Nov 2, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ all: test

.PHONY: test
test:
export GO111MODULE=on; go test . -race -count=1
go test . -race -count=1

.PHONY: godoc
godoc:
Expand Down
44 changes: 14 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

Go library for interacting with the [Mailgun](https://mailgun.com/) [API](https://documentation.mailgun.com/en/latest/api_reference.html).

## Installation

If you are using [Go Modules](https://go.dev/wiki/Modules) make sure you
include the `/v4` at the end of your import paths
```bash
$ go get github.com/mailgun/mailgun-go/v4
```

## Usage
```go
package main
Expand All @@ -20,11 +28,11 @@ import (

// Your available domain names can be found here:
// (https://app.mailgun.com/app/domains)
var yourDomain string = "your-domain-name" // e.g. mg.yourcompany.com
var yourDomain = "your-domain-name" // e.g. mg.yourcompany.com

// You can find the Private API Key in your Account Menu, under "Settings":
// (https://app.mailgun.com/app/account/security)
var privateAPIKey string = "your-private-key"
var privateAPIKey = "your-private-key"

func main() {
// Create an instance of the Mailgun Client
Expand Down Expand Up @@ -267,11 +275,11 @@ import (

// Your available domain names can be found here:
// (https://app.mailgun.com/app/domains)
var yourDomain string = "your-domain-name" // e.g. mg.yourcompany.com
var yourDomain = "your-domain-name" // e.g. mg.yourcompany.com

// You can find the Private API Key in your Account Menu, under "Settings":
// (https://app.mailgun.com/app/account/security)
var privateAPIKey string = "your-private-key"
var privateAPIKey = "your-private-key"

func main() {
// Create an instance of the Mailgun Client
Expand Down Expand Up @@ -326,11 +334,11 @@ import (

// Your available domain names can be found here:
// (https://app.mailgun.com/app/domains)
var yourDomain string = "your-domain-name" // e.g. mg.yourcompany.com
var yourDomain = "your-domain-name" // e.g. mg.yourcompany.com

// You can find the Private API Key in your Account Menu, under "Settings":
// (https://app.mailgun.com/app/account/security)
var privateAPIKey string = "your-private-key"
var privateAPIKey = "your-private-key"

func main() {
// Create an instance of the Mailgun Client
Expand Down Expand Up @@ -373,30 +381,6 @@ European customers will need to change the default API Base to access your domai
mg := mailgun.NewMailgun("your-domain.com", "private-api-key")
mg.SetAPIBase(mailgun.APIBaseEU)
```
## Installation

If you are using [golang modules](https://github.com/golang/go/wiki/Modules) make sure you
include the `/v4` at the end of your import paths
```bash
$ go get github.com/mailgun/mailgun-go/v4
```

If you are **not** using golang modules, you can drop the `/v4` at the end of the import path.
As long as you are using the latest 1.10 or 1.11 golang release, import paths that end in `/v4`
in your code should work fine even if you do not have golang modules enabled for your project.
```bash
$ go get github.com/mailgun/mailgun-go
```

**NOTE for go dep users**

Using version 3 of the mailgun-go library with go dep currently results in the following error
```
"github.com/mailgun/mailgun-go/v4/events", which contains malformed code: no package exists at ...
```
This is a known bug in go dep. You can follow the PR to fix this bug [here](https://github.com/golang/dep/pull/1963)
Until this bug is fixed, the best way to use version 3 of the mailgun-go library is to use the golang community
supported [golang modules](https://github.com/golang/go/wiki/Modules).

## Testing

Expand Down
20 changes: 11 additions & 9 deletions attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"strings"
"testing"

"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func createAttachment(t *testing.T) string {
t.Helper()
name := "/tmp/" + randomString(10, "attachment-")
f, err := os.Create(name)
ensure.Nil(t, err)
require.NoError(t, err)

_, err = f.Write([]byte(randomString(100, "")))
ensure.Nil(t, err)
ensure.Nil(t, f.Close())
require.NoError(t, err)
require.Nil(t, f.Close())
return name
}

Expand All @@ -37,19 +38,20 @@ func TestMultipleAttachments(t *testing.T) {
m.AddAttachment(createAttachment(t))

msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)
require.NoError(t, err)

id = strings.Trim(id, "<>")
t.Logf("New Email: %s Id: %s\n", msg, id)

e, err := findAcceptedMessage(mg, id)
ensure.NotNil(t, e)
require.NoError(t, err)
require.NotNil(t, e)

ensure.DeepEqual(t, e.ID, id)
ensure.DeepEqual(t, len(e.Message.Attachments), 2)
assert.Equal(t, e.ID, id)
assert.Len(t, e.Message.Attachments, 2)
for _, f := range e.Message.Attachments {
t.Logf("attachment: %v\n", f)
ensure.DeepEqual(t, f.Size, 100)
assert.Equal(t, 100, f.Size)
}
}

Expand Down
31 changes: 15 additions & 16 deletions bounces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"time"

"github.com/mailgun/mailgun-go/v4"

"github.com/facebookgo/ensure"
"github.com/stretchr/testify/require"
)

func TestGetBounces(t *testing.T) {
Expand All @@ -27,7 +26,7 @@ func TestGetBounces(t *testing.T) {
t.Logf("Bounce: %+v\n", bounce)
}
}
ensure.Nil(t, it.Err())
require.NoError(t, it.Err())
}

func TestGetSingleBounce(t *testing.T) {
Expand All @@ -38,11 +37,11 @@ func TestGetSingleBounce(t *testing.T) {
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(64, "")),
os.Getenv("MG_DOMAIN"))
_, err := mg.GetBounce(ctx, exampleEmail)
ensure.NotNil(t, err)
require.NotNil(t, err)

ure, ok := err.(*mailgun.UnexpectedResponseError)
ensure.True(t, ok)
ensure.DeepEqual(t, ure.Actual, http.StatusNotFound)
var ure *mailgun.UnexpectedResponseError
require.ErrorAs(t, err, &ure)
require.Equal(t, http.StatusNotFound, ure.Actual)
}

func TestAddDelBounces(t *testing.T) {
Expand All @@ -54,7 +53,7 @@ func TestAddDelBounces(t *testing.T) {
it := mg.ListBounces(nil)
var page []mailgun.Bounce
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
require.True(t, len(page) != 0)
for _, bounce := range page {
t.Logf("Bounce Address: %s\n", bounce.Address)
if bounce.Address == address {
Expand All @@ -73,7 +72,7 @@ func TestAddDelBounces(t *testing.T) {

// Add the bounce for our address.
err := mg.AddBounce(ctx, exampleEmail, "550", "TestAddDelBounces-generated error")
ensure.Nil(t, err)
require.NoError(t, err)

// Give API some time to refresh cache
time.Sleep(time.Second)
Expand All @@ -84,23 +83,23 @@ func TestAddDelBounces(t *testing.T) {
}

bounce, err := mg.GetBounce(ctx, exampleEmail)
ensure.Nil(t, err)
require.NoError(t, err)
if bounce.Address != exampleEmail {
t.Fatalf("Expected at least one bounce for %s", exampleEmail)
}
t.Logf("Bounce Created At: %s", bounce.CreatedAt)

// Delete it. This should put us back the way we were.
err = mg.DeleteBounce(ctx, exampleEmail)
ensure.Nil(t, err)
require.NoError(t, err)

// Make sure we're back to the way we were.
if findBounce(exampleEmail) {
t.Fatalf("Un-expected bounce for address %s in list of bounces", exampleEmail)
}

_, err = mg.GetBounce(ctx, exampleEmail)
ensure.NotNil(t, err)
require.NotNil(t, err)
}

func TestAddDelBounceList(t *testing.T) {
Expand All @@ -113,7 +112,7 @@ func TestAddDelBounceList(t *testing.T) {
it := mg.ListBounces(nil)
var page []mailgun.Bounce
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
require.True(t, len(page) != 0)
for _, bounce := range page {
t.Logf("Bounce Address: %s\n", bounce.Address)
if bounce.Address == address {
Expand Down Expand Up @@ -149,15 +148,15 @@ func TestAddDelBounceList(t *testing.T) {

// Add the bounce for our address.
err = mg.AddBounces(ctx, bounces)
ensure.Nil(t, err)
require.NoError(t, err)

for _, expect := range bounces {
if !findBounce(expect.Address) {
t.Fatalf("Expected bounce for address %s in list of bounces", expect.Address)
}

bounce, err := mg.GetBounce(ctx, expect.Address)
ensure.Nil(t, err)
require.NoError(t, err)
if bounce.Address != expect.Address {
t.Fatalf("Expected at least one bounce for %s", expect.Address)
}
Expand All @@ -169,7 +168,7 @@ func TestAddDelBounceList(t *testing.T) {

// Delete the bounce list. This should put us back the way we were.
err = mg.DeleteBounceList(ctx)
ensure.Nil(t, err)
require.NoError(t, err)

it := mg.ListBounces(nil)
var page []mailgun.Bounce
Expand Down
11 changes: 5 additions & 6 deletions credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"testing"

"github.com/mailgun/mailgun-go/v4"

"github.com/facebookgo/ensure"
"github.com/stretchr/testify/require"
)

func TestGetCredentials(t *testing.T) {
Expand All @@ -25,7 +24,7 @@ func TestGetCredentials(t *testing.T) {
t.Logf("%s\t%s\t\n", c.Login, c.CreatedAt)
}
}
ensure.Nil(t, it.Err())
require.NoError(t, it.Err())
}

func TestCreateDeleteCredentials(t *testing.T) {
Expand All @@ -37,7 +36,7 @@ func TestCreateDeleteCredentials(t *testing.T) {
randomLogin := fmt.Sprintf("%s@%s", randomID, testDomain)

ctx := context.Background()
ensure.Nil(t, mg.CreateCredential(ctx, randomLogin, randomPassword))
ensure.Nil(t, mg.ChangeCredentialPassword(ctx, randomID, randomString(16, "pw2")))
ensure.Nil(t, mg.DeleteCredential(ctx, randomID))
require.NoError(t, mg.CreateCredential(ctx, randomLogin, randomPassword))
require.NoError(t, mg.ChangeCredentialPassword(ctx, randomID, randomString(16, "pw2")))
require.NoError(t, mg.DeleteCredential(ctx, randomID))
}
Loading