Skip to content

Commit

Permalink
chore: remove unneeded sprintf calls
Browse files Browse the repository at this point in the history
Signed-off-by: James Hillyerd <james@hillyerd.com>
  • Loading branch information
jhillyerd committed Feb 15, 2024
1 parent 4c2e65e commit 0845e31
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/mime-extractor/mime-extractor_test.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"bytes"
"fmt"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestExtractFailedToParse(t *testing.T) {
func TestExtractAttachmentWriteFail(t *testing.T) {
s := &bytes.Buffer{}
fw := func(filename string, data []byte, perm os.FileMode) error {
return fmt.Errorf("AttachmentWriteFail")
return errors.New("AttachmentWriteFail")
}
testExtractor := &extractor{
errOut: io.Discard,
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Expand Up @@ -168,7 +168,7 @@ func FormatPart(w io.Writer, p *enmime.Part, indent string) {
}
disposition := ""
if p.Disposition != "" {
disposition = fmt.Sprintf(", disposition: %s", p.Disposition)
disposition = ", disposition: " + p.Disposition
}
filename := ""
if p.FileName != "" {
Expand Down
10 changes: 5 additions & 5 deletions envelope.go
Expand Up @@ -69,7 +69,7 @@ func (e *Envelope) GetHeaderValues(name string) []string {
// If the header exists already, all existing values are replaced.
func (e *Envelope) SetHeader(name string, value []string) error {
if name == "" {
return fmt.Errorf("provide non-empty header name")
return errors.New("provide non-empty header name")
}

for i, v := range value {
Expand All @@ -86,7 +86,7 @@ func (e *Envelope) SetHeader(name string, value []string) error {
// If the header does not exist already, it will be created.
func (e *Envelope) AddHeader(name string, value string) error {
if name == "" {
return fmt.Errorf("provide non-empty header name")
return errors.New("provide non-empty header name")
}

e.header.Add(name, mime.BEncoding.Encode("utf-8", value))
Expand All @@ -96,7 +96,7 @@ func (e *Envelope) AddHeader(name string, value string) error {
// DeleteHeader deletes given header.
func (e *Envelope) DeleteHeader(name string) error {
if name == "" {
return fmt.Errorf("provide non-empty header name")
return errors.New("provide non-empty header name")
}

e.header.Del(name)
Expand All @@ -106,7 +106,7 @@ func (e *Envelope) DeleteHeader(name string) error {
// AddressList returns a mail.Address slice with RFC 2047 encoded names converted to UTF-8
func (e *Envelope) AddressList(key string) ([]*mail.Address, error) {
if e.header == nil {
return nil, fmt.Errorf("no headers available")
return nil, errors.New("no headers available")
}
if !AddressHeaders[strings.ToLower(key)] {
return nil, fmt.Errorf("%s is not an address header", key)
Expand Down Expand Up @@ -272,7 +272,7 @@ func parseMultiPartBody(root *Part, e *Envelope) error {
}
boundary := params[hpBoundary]
if boundary == "" {
return fmt.Errorf("unable to locate boundary param in Content-Type header")
return errors.New("unable to locate boundary param in Content-Type header")
}

// Locate text body
Expand Down
6 changes: 3 additions & 3 deletions envelope_test.go
Expand Up @@ -30,7 +30,7 @@ func TestParseHeaderOnly(t *testing.T) {
t.Errorf("Expected no HTML body, got %q", e.HTML)
}
if e.Root == nil {
t.Errorf("Expected a root part")
t.Error("Expected a root part")
}
if len(e.Root.Header) != 7 {
t.Errorf("Expected 7 headers, got %d", len(e.Root.Header))
Expand Down Expand Up @@ -998,11 +998,11 @@ func TestAttachmentOnly(t *testing.T) {
}
// Check, if root header is set
if len(e.Root.Header) < 1 {
t.Errorf("No root header defined, but must be set from binary only part.")
t.Error("No root header defined, but must be set from binary only part.")
}
// Check, that the root part has content
if len(e.Root.Content) == 0 {
t.Errorf("Root part of envelope has no content.")
t.Error("Root part of envelope has no content.")
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions inspect_test.go
Expand Up @@ -16,21 +16,21 @@ func TestDecodeRFC2047(t *testing.T) {
t.Run("rfc2047 basic", func(t *testing.T) {
s := enmime.DecodeRFC2047("=?UTF-8?Q?Miros=C5=82aw_Marczak?=")
if s != "Mirosław Marczak" {
t.Errorf("Wrong decoded result")
t.Error("Wrong decoded result")
}
})

t.Run("rfc2047 unknown", func(t *testing.T) {
s := enmime.DecodeRFC2047("=?ABC-1?Q?FooBar?=")
if s != "=?ABC-1?Q?FooBar?=" {
t.Errorf("Expected unmodified result for unknown charset")
t.Error("Expected unmodified result for unknown charset")
}
})

t.Run("rfc2047 pass-through", func(t *testing.T) {
s := enmime.DecodeRFC2047("Hello World")
if s != "Hello World" {
t.Errorf("Expected unmodified result")
t.Error("Expected unmodified result")
}
})
}
Expand All @@ -47,7 +47,7 @@ func TestDecodeHeaders(t *testing.T) {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Errorf("Error decoding RFC2047 header value")
t.Error("Error decoding RFC2047 header value")
}
})

Expand All @@ -62,7 +62,7 @@ func TestDecodeHeaders(t *testing.T) {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Errorf("Error decoding RFC2047 header value")
t.Error("Error decoding RFC2047 header value")
}
})

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestDecodeHeaders(t *testing.T) {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("From"), "WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984") {
t.Errorf("Error decoding recursive RFC2047 header value")
t.Error("Error decoding recursive RFC2047 header value")
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/coding/headerext.go
Expand Up @@ -61,10 +61,10 @@ func RFC2047Decode(s string) string {

// Add quotes as needed.
if !strings.HasPrefix(value, "\"") {
value = fmt.Sprintf("\"%s", value)
value = `"` + value
}
if !strings.HasSuffix(value, "\"") {
value = fmt.Sprintf("%s\"", value)
value += `"`
}

return fmt.Sprintf("%s=%s", key, value)
Expand Down
7 changes: 2 additions & 5 deletions internal/stringutil/uuid_test.go
Expand Up @@ -4,15 +4,12 @@ import (
"testing"

"github.com/jhillyerd/enmime/internal/stringutil"
"github.com/stretchr/testify/assert"
)

func TestUUID(t *testing.T) {
id1 := stringutil.UUID(nil)
id2 := stringutil.UUID(nil)

if id1 == id2 {
t.Errorf("Random UUID should not equal another random UUID")
t.Logf("id1: %q", id1)
t.Logf("id2: %q", id2)
}
assert.NotEqual(t, id1, id2, "Random UUID should not equal another random UUID")
}
2 changes: 1 addition & 1 deletion internal/textproto/reader.go
Expand Up @@ -127,7 +127,7 @@ func (r *Reader) ReadContinuedLineBytes() ([]byte, error) {
// error is returned from readContinuedLineSlice.
func (r *Reader) readContinuedLineSlice(validateFirstLine func([]byte) error) ([]byte, error) {
if validateFirstLine == nil {
return nil, fmt.Errorf("missing validateFirstLine func")
return nil, errors.New("missing validateFirstLine func")
}

// Read the first line.
Expand Down

0 comments on commit 0845e31

Please sign in to comment.