Skip to content
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
3 changes: 2 additions & 1 deletion charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func (u UnknownCharsetError) Error() string {
// IsUnknownCharset returns a boolean indicating whether the error is known to
// report that the charset advertised by the entity is unknown.
func IsUnknownCharset(err error) bool {
return errors.As(err, new(UnknownCharsetError))
_, ok := errors.AsType[UnknownCharsetError](err)
return ok
}

// CharsetReader, if non-nil, defines a function to generate charset-conversion
Expand Down
2 changes: 1 addition & 1 deletion charset/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func charsetEncoding(charset string) (encoding.Encoding, error) {
enc, err = htmlindex.Get(charset)
}
if err != nil {
return nil, fmt.Errorf("charset %q: %v", charset, err)
return nil, fmt.Errorf("charset %q: %w", charset, err)
Comment thread
thunderkatz marked this conversation as resolved.
Resolved
}
// See https://github.com/golang/go/issues/19421
if enc == nil {
Expand Down
20 changes: 18 additions & 2 deletions charset/charset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package charset

import (
"bytes"
"errors"
"io"
"io/ioutil"

"strings"
"testing"

"golang.org/x/text/encoding/htmlindex"
)

var testCharsets = []struct {
Expand Down Expand Up @@ -70,7 +73,7 @@ func TestCharsetReader(t *testing.T) {
if test.decoded != "" {
if err != nil {
t.Errorf("Expected no error when creating reader for charset %q, but got: %v", test.charset, err)
} else if b, err := ioutil.ReadAll(r); err != nil {
} else if b, err := io.ReadAll(r); err != nil {
t.Errorf("Expected no error when reading charset %q, but got: %v", test.charset, err)
} else if s := string(b); s != test.decoded {
t.Errorf("Expected decoded text to be %q but got %q", test.decoded, s)
Expand Down Expand Up @@ -121,3 +124,16 @@ func TestDisabledCharsetWriter(t *testing.T) {
t.Errorf("Writer(): expected disabled charset to return an error")
}
}

func TestUnknownCharsetWrapsCause(t *testing.T) {
const name = "no-such-charset"
_, want := htmlindex.Get(name)
if want == nil {
t.Fatalf("htmlindex.Get(%q): expected an error", name)
}

_, err := charsetEncoding(name)
if !errors.Is(err, want) {
t.Errorf("charsetEncoding(%q) = %v, want an error wrapping %v", name, err, want)
}
}
10 changes: 5 additions & 5 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (u UnknownEncodingError) Error() string {
// IsUnknownEncoding returns a boolean indicating whether the error is known to
// report that the encoding advertised by the entity is unknown.
func IsUnknownEncoding(err error) bool {
return errors.As(err, new(UnknownEncodingError))
_, ok := errors.AsType[UnknownEncodingError](err)
return ok
}

func encodingReader(enc string, r io.Reader) (io.Reader, error) {
Expand Down Expand Up @@ -78,7 +79,7 @@ type whitespaceReplacingReader struct {
func (r *whitespaceReplacingReader) Read(p []byte) (int, error) {
n, err := r.wrapped.Read(p)

for i := 0; i < n; i++ {
for i := range n {
if p[i] == ' ' || p[i] == '\t' {
p[i] = '\n'
}
Expand All @@ -101,8 +102,7 @@ func (w *lineWrapper) Write(b []byte) (int, error) {
var l []byte
l, b = cutLine(b, w.maxLineLen-w.curLineLen)

lf := bytes.HasSuffix(l, []byte("\n"))
l = bytes.TrimSuffix(l, []byte("\n"))
l, lf := bytes.CutSuffix(l, []byte("\n"))

n, err := w.w.Write(l)
if err != nil {
Expand Down Expand Up @@ -141,7 +141,7 @@ func (w *lineWrapper) Write(b []byte) (int, error) {
}

func cutLine(b []byte, max int) ([]byte, []byte) {
for i := 0; i < len(b); i++ {
for i := range b {
if b[i] == '\r' && i == max {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package message
import (
"bytes"
"io"
"io/ioutil"

"strings"
"testing"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestDecode(t *testing.T) {
r, err := encodingReader(test.enc, strings.NewReader(test.encoded))
if err != nil {
t.Errorf("Expected no error when creating decoder for encoding %q, but got: %v", test.enc, err)
} else if b, err := ioutil.ReadAll(r); err != nil {
} else if b, err := io.ReadAll(r); err != nil {
t.Errorf("Expected no error when reading encoding %q, but got: %v", test.enc, err)
} else if s := string(b); s != test.decoded {
t.Errorf("Expected decoded text to be %q but got %q", test.decoded, s)
Expand Down
23 changes: 11 additions & 12 deletions entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"golang.org/x/text/encoding/ianaindex"
"io"
"io/ioutil"
"math"
"reflect"
"strings"
Expand All @@ -27,7 +26,7 @@ func TestNewEntity(t *testing.T) {
e := testMakeEntity()

expected := "cc sava"
if b, err := ioutil.ReadAll(e.Body); err != nil {
if b, err := io.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading entity body, got", err)
} else if s := string(b); s != expected {
t.Errorf("Expected %q as entity body but got %q", expected, s)
Expand Down Expand Up @@ -129,7 +128,7 @@ func testMultipart(t *testing.T, e *Entity) {
if mediaType := p.Header.Get("Content-Type"); mediaType != expectedType {
t.Errorf("Expected part Content-Type to be %q, got %q", expectedType, mediaType)
}
if b, err := ioutil.ReadAll(p.Body); err != nil {
if b, err := io.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, got", err)
} else if s := string(b); s != expectedBody {
t.Errorf("Expected %q as part body but got %q", expectedBody, s)
Expand All @@ -150,7 +149,7 @@ func TestNewMultipart(t *testing.T) {
func TestNewMultipart_read(t *testing.T) {
e := testMakeMultipart()

if b, err := ioutil.ReadAll(e.Body); err != nil {
if b, err := io.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading multipart body, got", err)
} else if s := string(b); s != testMultipartBody {
t.Errorf("Expected %q as multipart body but got %q", testMultipartBody, s)
Expand All @@ -172,9 +171,9 @@ func TestRead_single(t *testing.T) {
t.Fatalf("Read() = %v", err)
}

b, err := ioutil.ReadAll(e.Body)
b, err := io.ReadAll(e.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll() = %v", err)
t.Fatalf("io.ReadAll() = %v", err)
}

expected := "Message body"
Expand Down Expand Up @@ -475,11 +474,11 @@ func TestNew_unknownTransferEncoding(t *testing.T) {
if !IsUnknownEncoding(err) {
t.Fatal("New(unknown transfer encoding): expected an error that verifies IsUnknownEncoding")
}
if !errors.As(err, &UnknownEncodingError{}) {
t.Fatal("New(unknown transfer encoding): expected an error that verifies errors.As(err, &EncodingError{})")
if _, ok := errors.AsType[UnknownEncodingError](err); !ok {
t.Fatal("New(unknown transfer encoding): expected an error that verifies errors.AsType[UnknownEncodingError]")
}

if b, err := ioutil.ReadAll(e.Body); err != nil {
if b, err := io.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading entity body, got", err)
} else if s := string(b); s != expected {
t.Errorf("Expected %q as entity body but got %q", expected, s)
Expand All @@ -501,7 +500,7 @@ func TestNew_unknownCharset(t *testing.T) {
t.Fatal("New(unknown charset): expected an error that verifies IsUnknownCharset")
}

if b, err := ioutil.ReadAll(e.Body); err != nil {
if b, err := io.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading entity body, got", err)
} else if s := string(b); s != expected {
t.Errorf("Expected %q as entity body but got %q", expected, s)
Expand Down Expand Up @@ -537,7 +536,7 @@ func TestNew_paddedBase64(t *testing.T) {
t.Fatal("New(padded Base64): expected no error, got", err)
}

if b, err := ioutil.ReadAll(e.Body); err != nil {
if b, err := io.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading entity body, got", err)
} else if s := string(b); s != expected {
t.Errorf("Expected %q as entity body but got %q", expected, s)
Expand Down Expand Up @@ -565,7 +564,7 @@ func walkCollect(e *Entity) ([]testWalkPart, error) {
err := e.Walk(func(path []int, part *Entity, err error) error {
var body string
if part.MultipartReader() == nil {
b, err := ioutil.ReadAll(part.Body)
b, err := io.ReadAll(part.Body)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions mail/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *headerParser) consumeComment() (string, bool) {
// '(' already consumed.
depth := 1

var comment string
var comment strings.Builder
for {
if p.empty() || depth == 0 {
break
Expand All @@ -85,13 +85,13 @@ func (p *headerParser) consumeComment() (string, bool) {
}

if depth > 0 {
comment += p.s[:1]
comment.WriteString(p.s[:1])
}

p.s = p.s[1:]
}

return comment, depth == 0
return comment.String(), depth == 0
}

func (p *headerParser) parseAtomText(dot bool) (string, error) {
Expand Down
9 changes: 4 additions & 5 deletions mail/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mail_test

import (
"io"
"io/ioutil"
"log"
"strings"
"testing"
Expand Down Expand Up @@ -31,7 +30,7 @@ func ExampleReader() {

switch h := p.Header.(type) {
case *mail.InlineHeader:
b, _ := ioutil.ReadAll(p.Body)
b, _ := io.ReadAll(p.Body)
log.Printf("Got text: %v\n", string(b))
case *mail.AttachmentHeader:
filename, _ := h.Filename()
Expand Down Expand Up @@ -92,7 +91,7 @@ func testReader(t *testing.T, r io.Reader) {
expectedBody = "I'm Mitsuha."
}

if b, err := ioutil.ReadAll(p.Body); err != nil {
if b, err := io.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
Expand Down Expand Up @@ -131,7 +130,7 @@ func TestReader_nonMultipart(t *testing.T) {
}

expectedBody := "Who are you?"
if b, err := ioutil.ReadAll(p.Body); err != nil {
if b, err := io.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
Expand Down Expand Up @@ -185,7 +184,7 @@ func TestReader_nested(t *testing.T) {
}

expectedBody := "I forgot."
if b, err := ioutil.ReadAll(p.Body); err != nil {
if b, err := io.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
Expand Down
4 changes: 2 additions & 2 deletions textproto/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func BenchmarkTextprotoReadHeader(b *testing.B) {
r := bufio.NewReader(strings.NewReader(blob))

var err error
for i := 0; i < b.N; i++ {
for b.Loop() {
r.Reset(strings.NewReader(blob))
hdr, err = ReadHeader(r)
if err != nil {
Expand All @@ -254,7 +254,7 @@ func BenchmarkStdlibReadHeader(b *testing.B) {
tr := textproto.NewReader(r)

var err error
for i := 0; i < b.N; i++ {
for b.Loop() {
r.Reset(strings.NewReader(blob))
mimeHdr, err = tr.ReadMIMEHeader()
if err != nil {
Expand Down
Loading
Loading