Skip to content

Commit

Permalink
Remove deprecated packages (#298)
Browse files Browse the repository at this point in the history
* Remove archived `github.com/pkg/errors`
* Remove deprecated `ioutil`
* Small change for base64 corruption error check
  • Loading branch information
harryzcy committed Aug 9, 2023
1 parent 151d2dc commit 3c35f97
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 85 deletions.
22 changes: 10 additions & 12 deletions boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package enmime
import (
"bufio"
"bytes"
stderrors "errors"
"errors"
"fmt"
"io"
"io/ioutil"
"unicode"

"github.com/pkg/errors"
)

// This constant needs to be at least 76 for this package to work correctly. This is because
// \r\n--separator_of_len_70- would fill the buffer and it wouldn't be safe to consume a single byte
// from it.
const peekBufferSize = 4096

var errNoBoundaryTerminator = stderrors.New("expected boundary not present")
var errNoBoundaryTerminator = errors.New("expected boundary not present")

type boundaryReader struct {
finished bool // No parts remain when finished
Expand Down Expand Up @@ -84,7 +82,7 @@ func (b *boundaryReader) Read(dest []byte) (n int, err error) {
var cs []byte
cs, err = b.r.Peek(1)
if err != nil && err != io.EOF {
return 0, errors.WithStack(err)
return 0, fmt.Errorf("failed to read content: %w", err)
}
// Ensure that we can switch on the first byte of 'cs' without panic.
if len(cs) > 0 {
Expand Down Expand Up @@ -133,7 +131,7 @@ func (b *boundaryReader) Read(dest []byte) (n int, err error) {
}
return n, io.EOF
default:
return 0, errors.WithStack(err)
return 0, fmt.Errorf("failed to read content: %w", err)
}
}
case io.EOF:
Expand All @@ -155,11 +153,11 @@ func (b *boundaryReader) Read(dest []byte) (n int, err error) {
break
}

return 0, errors.WithStack(err)
return 0, fmt.Errorf("failed to read content: %w", err)
}

if err = b.buffer.WriteByte(next); err != nil {
return 0, errors.WithStack(err)
return 0, fmt.Errorf("failed to read content: %w", err)
}
}

Expand All @@ -178,7 +176,7 @@ func (b *boundaryReader) Next() (bool, error) {
}
if b.partsRead > 0 {
// Exhaust the current part to prevent errors when moving to the next part.
_, _ = io.Copy(ioutil.Discard, b)
_, _ = io.Copy(io.Discard, b)
}
for {
var line []byte = nil
Expand All @@ -196,7 +194,7 @@ func (b *boundaryReader) Next() (bool, error) {
if err == nil || err == io.EOF {
break
} else if err != bufio.ErrBufferFull || len(segment) == 0 {
return false, errors.WithStack(err)
return false, fmt.Errorf("failed to move over the boundary: %w", err)
}
}

Expand Down Expand Up @@ -224,7 +222,7 @@ func (b *boundaryReader) Next() (bool, error) {
continue
}
b.finished = true
return false, errors.WithMessagef(errNoBoundaryTerminator, "expecting boundary %q, got %q", string(b.prefix), string(line))
return false, fmt.Errorf("expecting boundary %q, got %q: %w", string(b.prefix), string(line), errNoBoundaryTerminator)
}
}

Expand Down
26 changes: 12 additions & 14 deletions boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package enmime
import (
"bufio"
"bytes"
"errors"
"io"
"io/ioutil"
"strings"
"testing"

"github.com/pkg/errors"
)

func TestBoundaryReader(t *testing.T) {
Expand Down Expand Up @@ -75,7 +73,7 @@ func TestBoundaryReader(t *testing.T) {
for _, tt := range ttable {
ir := bufio.NewReader(strings.NewReader(tt.input))
br := newBoundaryReader(ir, tt.boundary)
output, err := ioutil.ReadAll(br)
output, err := io.ReadAll(br)
if err != nil {
t.Fatalf("Got error: %v\ninput: %q", err, tt.input)
}
Expand All @@ -87,7 +85,7 @@ func TestBoundaryReader(t *testing.T) {
}

// Test the data remaining in reader is correct
rest, err := ioutil.ReadAll(ir)
rest, err := io.ReadAll(ir)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -135,7 +133,7 @@ func TestBoundaryReaderEOF(t *testing.T) {

ir := bufio.NewReader(strings.NewReader(input))
br := newBoundaryReader(ir, boundary)
output, err := ioutil.ReadAll(br)
output, err := io.ReadAll(br)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -215,7 +213,7 @@ func TestBoundaryReaderParts(t *testing.T) {
if !next {
t.Fatal("Next() = false, want: true")
}
output, err := ioutil.ReadAll(br)
output, err := io.ReadAll(br)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -355,7 +353,7 @@ func TestBoundaryReaderBufferBoundaryAbut(t *testing.T) {
if !next {
t.Fatal("Next() = false, want: true")
}
output, err := ioutil.ReadAll(br)
output, err := io.ReadAll(br)
if err != nil {
t.Fatalf("Got error: %v", err)
}
Expand All @@ -371,7 +369,7 @@ func TestBoundaryReaderBufferBoundaryAbut(t *testing.T) {
if !next {
t.Fatal("Next() = false, want: true")
}
output, err = ioutil.ReadAll(br)
output, err = io.ReadAll(br)
if err != nil {
t.Fatalf("Got error: %v", err)
}
Expand Down Expand Up @@ -411,7 +409,7 @@ func TestBoundaryReaderBufferBoundaryCross(t *testing.T) {
if !next {
t.Fatal("Next() = false, want: true")
}
output, err := ioutil.ReadAll(br)
output, err := io.ReadAll(br)
if err != nil {
t.Fatalf("Got error: %v", err)
}
Expand All @@ -427,7 +425,7 @@ func TestBoundaryReaderBufferBoundaryCross(t *testing.T) {
if !next {
t.Fatal("Next() = false, want: true")
}
output, err = ioutil.ReadAll(br)
output, err = io.ReadAll(br)
if err != nil {
t.Fatalf("Got error: %v", err)
}
Expand Down Expand Up @@ -464,15 +462,15 @@ func TestBoundaryReaderReadErrors(t *testing.T) {
if n != 0 {
t.Fatal("Read() should not have read any bytes, failed")
}
if errors.Cause(err) != bufio.ErrBufferFull {
if !errors.Is(err, bufio.ErrBufferFull) {
t.Fatal("Read() should have returned bufio.ErrBufferFull error, failed")
}
// Next method to return a non io.EOF error.
next, err := br.Next()
if next {
t.Fatal("Next() should have returned false, failed")
}
if errors.Cause(err) != bufio.ErrBufferFull {
if !errors.Is(err, bufio.ErrBufferFull) {
t.Fatal("Read() should have returned bufio.ErrBufferFull error, failed")
}
}
Expand Down Expand Up @@ -549,7 +547,7 @@ func BenchmarkBoundaryReader(b *testing.B) {
for i := 0; i < b.N; i++ {
ir := bufio.NewReader(strings.NewReader(input))
br := newBoundaryReader(ir, boundary)
_, err = io.Copy(ioutil.Discard, br)
_, err = io.Copy(io.Discard, br)
if err != nil {
b.Fatalf("Failed to read content: %+v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"math/rand"
"mime"
"net/mail"
"os"
"path/filepath"
"reflect"
"time"
Expand Down Expand Up @@ -247,7 +247,7 @@ func (p MailBuilder) AddFileAttachment(path string) MailBuilder {
if p.err != nil {
return p
}
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
p.err = err
return p
Expand Down Expand Up @@ -282,7 +282,7 @@ func (p MailBuilder) AddFileInline(path string) MailBuilder {
if p.err != nil {
return p
}
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
p.err = err
return p
Expand Down Expand Up @@ -317,7 +317,7 @@ func (p MailBuilder) AddFileOtherPart(path string) MailBuilder {
if p.err != nil {
return p
}
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
p.err = err
return p
Expand Down
3 changes: 1 addition & 2 deletions cmd/mime-extractor/mime-extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -35,7 +34,7 @@ func newDefaultExtractor() *extractor {
stdOut: os.Stdout,
exit: os.Exit,
wd: os.Getwd,
fileWrite: ioutil.WriteFile,
fileWrite: os.WriteFile,
}
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/mime-extractor/mime-extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -20,7 +20,7 @@ func TestExtractEmptyFilename(t *testing.T) {
b := &bytes.Buffer{}
testExtractor := &extractor{
errOut: b,
stdOut: ioutil.Discard,
stdOut: io.Discard,
}
exitCode := testExtractor.extract("", "")
if exitCode != 1 {
Expand All @@ -38,7 +38,7 @@ func TestExtractEmptyOutputDirectory(t *testing.T) {
}
testExtractor := &extractor{
errOut: b,
stdOut: ioutil.Discard,
stdOut: io.Discard,
wd: workingDirectoryFn,
}
exitCode := testExtractor.extract("some.file", "")
Expand All @@ -54,7 +54,7 @@ func TestExtractFailedToOpenFile(t *testing.T) {
b := &bytes.Buffer{}
testExtractor := &extractor{
errOut: b,
stdOut: ioutil.Discard,
stdOut: io.Discard,
wd: os.Getwd,
}
exitCode := testExtractor.extract("some.file", "")
Expand All @@ -70,7 +70,7 @@ func TestExtractFailedToParse(t *testing.T) {
s := &bytes.Buffer{}
testExtractor := &extractor{
errOut: s,
stdOut: ioutil.Discard,
stdOut: io.Discard,
wd: os.Getwd,
}
exitCode := testExtractor.extract(filepath.Join("..", "..", "testdata", "mail", "erroneous.raw"), "")
Expand All @@ -88,7 +88,7 @@ func TestExtractAttachmentWriteFail(t *testing.T) {
return fmt.Errorf("AttachmentWriteFail")
}
testExtractor := &extractor{
errOut: ioutil.Discard,
errOut: io.Discard,
stdOut: s,
wd: os.Getwd,
fileWrite: fw,
Expand All @@ -111,7 +111,7 @@ func TestExtractSuccess(t *testing.T) {
}
testExtractor := &extractor{
errOut: b,
stdOut: ioutil.Discard,
stdOut: io.Discard,
wd: os.Getwd,
fileWrite: fw,
}
Expand Down
4 changes: 1 addition & 3 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/jhillyerd/enmime/internal/coding"
"github.com/jhillyerd/enmime/internal/textproto"
"github.com/jhillyerd/enmime/mediatype"

"github.com/pkg/errors"
)

// Envelope is a simplified wrapper for MIME email messages.
Expand Down Expand Up @@ -158,7 +156,7 @@ func (p Parser) ReadEnvelope(r io.Reader) (*Envelope, error) {
// Read MIME parts from reader
root, err := p.ReadParts(r)
if err != nil {
return nil, errors.WithMessage(err, "Failed to ReadParts")
return nil, fmt.Errorf("failed to ReadParts: %w", err)
}
return p.EnvelopeFromPart(root)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/go-test/deep v1.0.7
github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
golang.org/x/text v0.8.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxm
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down
7 changes: 4 additions & 3 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/jhillyerd/enmime/internal/stringutil"
"github.com/jhillyerd/enmime/internal/textproto"
"github.com/jhillyerd/enmime/mediatype"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -194,7 +192,10 @@ line:
buf.Write([]byte{'\r', '\n'})
tr := textproto.NewReader(bufio.NewReader(buf))
header, err := tr.ReadEmailMIMEHeader()
return header, errors.WithStack(err)
if err != nil {
return nil, fmt.Errorf("failed to read header: %w", err)
}
return header, nil
}

// decodeToUTF8Base64Header decodes a MIME header per RFC 2047, reencoding to =?utf-8b?
Expand Down
10 changes: 4 additions & 6 deletions inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package enmime
import (
"bufio"
"bytes"
"errors"
"io"

"github.com/jhillyerd/enmime/internal/coding"
"github.com/jhillyerd/enmime/internal/textproto"

"github.com/pkg/errors"
)

var defaultHeadersList = []string{
Expand Down Expand Up @@ -40,10 +39,9 @@ func DecodeHeaders(b []byte, addtlHeaders ...string) (textproto.MIMEHeader, erro
b = ensureHeaderBoundary(b)
tr := textproto.NewReader(bufio.NewReader(bytes.NewReader(b)))
headers, err := tr.ReadMIMEHeader()
switch errors.Cause(err) {
case nil, io.EOF:
// carry on, io.EOF is expected
default:

// io.EOF is expected
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
headerList := defaultHeadersList
Expand Down
Loading

0 comments on commit 3c35f97

Please sign in to comment.