Repository: mailgun/mailgun-go
Pull request #395 (2025-03-01 13:32:31)

DE-1391 Enable more linters


Patch:
diff --git a/.golangci.yml b/.golangci.yml
index b74f6ac..921d6ea 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -18,11 +18,11 @@ linters:
     - gocyclo
     - noctx
     - gomodguard
+    - unused
     - nolintlint
 
 # TODO:
-#    - stylecheck # TODO(v5): enable
-#    - unused
+#    - stylecheck # TODO(v6): enable
 
 linters-settings:
   gocritic:
@@ -33,10 +33,9 @@ linters-settings:
       - performance
       - style
     disabled-checks:
-      - singleCaseSwitch
-      - hugeParam # TODO(v5): enable?
+      - singleCaseSwitch # redundant
+      - hugeParam # TODO(v6): allocations critical; enable?
       - sprintfQuotedString # noisy # TODO: enable
-      - exitAfterDefer # TODO: enable?
 
   errcheck:
     # List of functions to exclude from checking, where each entry is a single function to exclude.
diff --git a/events.go b/events.go
index cb05587..192c4ff 100644
--- a/events.go
+++ b/events.go
@@ -171,13 +171,11 @@ func (ei *EventIterator) fetch(ctx context.Context, url string) error {
 
 // EventPoller maintains the state necessary for polling events
 type EventPoller struct {
-	it            *EventIterator
-	opts          ListEventOptions
-	thresholdTime time.Time
-	beginTime     time.Time
-	sleepUntil    time.Time
-	mg            Mailgun
-	err           error
+	it        *EventIterator
+	opts      ListEventOptions
+	beginTime time.Time
+	mg        Mailgun
+	err       error
 }
 
 // PollEvents polls the events api and return new events as they occur
diff --git a/mailgun.go b/mailgun.go
index 484dfe2..36afe09 100644
--- a/mailgun.go
+++ b/mailgun.go
@@ -260,7 +260,6 @@ type MailgunImpl struct {
 	apiKey            string
 	webhookSigningKey string
 	client            *http.Client
-	baseURL           string
 	overrideHeaders   map[string]string
 }
 


Test patch:
diff --git a/acceptance_test.go b/acceptance_test.go
index 2b4602f..0b750f5 100644
--- a/acceptance_test.go
+++ b/acceptance_test.go
@@ -3,7 +3,6 @@ package mailgun
 import (
 	"fmt"
 	"os"
-	"testing"
 )
 
 // Return the variable missing which caused the test to be skipped
@@ -15,12 +14,3 @@ func SkipNetworkTest() string {
 	}
 	return ""
 }
-
-func spendMoney(t *testing.T, tFunc func()) {
-	ok := os.Getenv("MG_SPEND_MONEY")
-	if ok != "" {
-		tFunc()
-	} else {
-		t.Log("Money spending not allowed, not running function.")
-	}
-}
diff --git a/examples_test.go b/examples_test.go
index eacc5b3..bc153dd 100644
--- a/examples_test.go
+++ b/examples_test.go
@@ -24,7 +24,8 @@ func ExampleMailgunImpl_ValidateEmail() {
 
 	ev, err := mg.ValidateEmail(ctx, "joe@example.com", false)
 	if err != nil {
-		log.Fatal(err)
+		log.Println(err)
+		return
 	}
 	if ev.DidYouMean != "" {
 		log.Printf("The address is syntactically valid, but perhaps has a typo.")
@@ -43,7 +44,8 @@ func ExampleMailgunImpl_UpdateMailingList() {
 		Description: "Joe's status report list",
 	})
 	if err != nil {
-		log.Fatal(err)
+		log.Println(err)
+		return
 	}
 }
 
@@ -66,7 +68,8 @@ func ExampleMailgunImpl_Send_constructed() {
 	m.SetHTML("<html><body><h1>Testing some Mailgun Awesomeness!!</h1></body></html>")
 	_, id, err := mg.Send(ctx, m)
 	if err != nil {
-		log.Fatal(err)
+		log.Println(err)
+		return
 	}
 	log.Printf("Message id=%s", id)
 }
@@ -89,7 +92,8 @@ Testing some Mailgun MIME awesomeness!
 	m := mailgun.NewMIMEMessage("example.com", io.NopCloser(strings.NewReader(exampleMime)), "bargle.garf@example.com")
 	_, id, err := mg.Send(ctx, m)
 	if err != nil {
-		log.Fatal(err)
+		log.Println(err)
+		return
 	}
 	log.Printf("Message id=%s", id)
 }
@@ -107,7 +111,8 @@ func ExampleMailgunImpl_ListRoutes() {
 		}
 	}
 	if it.Err() != nil {
-		log.Fatal(it.Err())
+		log.Println(it.Err())
+		return
 	}
 }
 
diff --git a/mailgun_test.go b/mailgun_test.go
index ed62131..d576aa6 100644
--- a/mailgun_test.go
+++ b/mailgun_test.go
@@ -23,6 +23,9 @@ var server *mocks.Server
 func TestMain(m *testing.M) {
 	server = mocks.NewServer()
 	defer server.Stop()
+	// TODO: os.Exit will exit, and `defer server.Stop()` will not run
+	// switch to testify suite
+	//nolint:gocritic // ignored till switched to testify suite
 	os.Exit(m.Run())
 }
 
diff --git a/webhooks_test.go b/webhooks_test.go
index f2bea9d..e9d020b 100644
--- a/webhooks_test.go
+++ b/webhooks_test.go
@@ -1,16 +1,11 @@
 package mailgun_test
 
 import (
-	"bytes"
 	"context"
 	"crypto/hmac"
 	"crypto/sha256"
 	"encoding/hex"
 	"io"
-	"mime/multipart"
-	"net/http"
-	"net/url"
-	"strings"
 	"testing"
 
 	"github.com/mailgun/mailgun-go/v4"
@@ -102,36 +97,6 @@ func TestVerifyWebhookSignature(t *testing.T) {
 	}
 }
 
-func buildFormRequest(ctx context.Context, fields map[string]string) *http.Request {
-	values := url.Values{}
-
-	for k, v := range fields {
-		values.Add(k, v)
-	}
-
-	r := strings.NewReader(values.Encode())
-	req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/", r)
-	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
-
-	return req
-}
-
-func buildMultipartFormRequest(ctx context.Context, fields map[string]string) *http.Request {
-	buf := &bytes.Buffer{}
-	writer := multipart.NewWriter(buf)
-
-	for k, v := range fields {
-		_ = writer.WriteField(k, v)
-	}
-
-	writer.Close()
-
-	req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/", buf)
-	req.Header.Set("Content-type", writer.FormDataContentType())
-
-	return req
-}
-
 func getSignatureFields(key string, signed bool) map[string]string {
 	badSignature := hex.EncodeToString([]byte("badsignature"))