Skip to content

Commit

Permalink
Merge pull request #648 from testwill/ioutil
Browse files Browse the repository at this point in the history
chore: remove refs to deprecated io/ioutil
  • Loading branch information
foxcpp authored Dec 21, 2023
2 parents c5f72ef + 8431eae commit 145bf3c
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 44 deletions.
3 changes: 1 addition & 2 deletions framework/buffer/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package buffer

import (
"io"
"io/ioutil"
)

// MemoryBuffer implements Buffer interface using byte slice.
Expand All @@ -43,7 +42,7 @@ func (mb MemoryBuffer) Remove() error {
// BufferInMemory is a convenience function which creates MemoryBuffer with
// contents of the passed io.Reader.
func BufferInMemory(r io.Reader) (Buffer, error) {
blob, err := ioutil.ReadAll(r)
blob, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions framework/config/tls/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"

"github.com/foxcpp/maddy/framework/config"
"github.com/foxcpp/maddy/framework/log"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TLSClientBlock(_ *config.Map, node config.Node) (interface{}, error) {
if len(rootCAPaths) != 0 {
pool := x509.NewCertPool()
for _, path := range rootCAPaths {
blob, err := ioutil.ReadFile(path)
blob, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions framework/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package log
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -199,7 +198,7 @@ func (l Logger) Write(s []byte) (int, error) {
// Write method of returned object will be no-op.
func (l Logger) DebugWriter() io.Writer {
if !l.Debug {
return ioutil.Discard
return io.Discard
}
l.Debug = true
return &l
Expand Down
4 changes: 2 additions & 2 deletions internal/modify/dkim/dkim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package dkim
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
Expand Down Expand Up @@ -106,7 +106,7 @@ func verifyTestMsg(t *testing.T, keysPath string, expectedDomains []string, hdr
domainsMap := make(map[string]bool)
zones := map[string]mockdns.Zone{}
for _, domain := range expectedDomains {
dnsRecord, err := ioutil.ReadFile(filepath.Join(keysPath, domain+".dns"))
dnsRecord, err := os.ReadFile(filepath.Join(keysPath, domain+".dns"))
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/modify/dkim/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
Expand All @@ -45,7 +44,7 @@ func (m *Modifier) loadOrGenerateKey(keyPath, newKeyAlgo string) (pkey crypto.Si
}
defer f.Close()

pemBlob, err := ioutil.ReadAll(f)
pemBlob, err := io.ReadAll(f)
if err != nil {
return nil, false, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/modify/dkim/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"crypto/ed25519"
"crypto/rsa"
"encoding/base64"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -44,7 +44,7 @@ func TestKeyLoad_new(t *testing.T) {
t.Fatal("newKey=false")
}

recordBlob, err := ioutil.ReadFile(filepath.Join(dir, "testkey.dns"))
recordBlob, err := os.ReadFile(filepath.Join(dir, "testkey.dns"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestKeyLoad_existing_pkcs8(t *testing.T) {

dir := t.TempDir()

if err := ioutil.WriteFile(filepath.Join(dir, "testkey.key"), []byte(pkeyEd25519), 0o600); err != nil {
if err := os.WriteFile(filepath.Join(dir, "testkey.key"), []byte(pkeyEd25519), 0o600); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func TestKeyLoad_existing_pkcs1(t *testing.T) {

dir := t.TempDir()

if err := ioutil.WriteFile(filepath.Join(dir, "testkey.key"), []byte(pkeyRSA), 0o600); err != nil {
if err := os.WriteFile(filepath.Join(dir, "testkey.key"), []byte(pkeyRSA), 0o600); err != nil {
t.Fatal(err)
}

Expand Down
9 changes: 4 additions & 5 deletions internal/table/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package table

import (
"io/ioutil"
"os"
"reflect"
"testing"
Expand All @@ -33,7 +32,7 @@ func TestReadFile(t *testing.T) {
test := func(file string, expected map[string][]string) {
t.Helper()

f, err := ioutil.TempFile("", "maddy-tests-")
f, err := os.CreateTemp("", "maddy-tests-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestFileReload(t *testing.T) {

const file = `cat: dog`

f, err := ioutil.TempFile("", "maddy-tests-")
f, err := os.CreateTemp("", "maddy-tests-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -139,7 +138,7 @@ func TestFileReload_Broken(t *testing.T) {

const file = `cat: dog`

f, err := ioutil.TempFile("", "maddy-tests-")
f, err := os.CreateTemp("", "maddy-tests-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -185,7 +184,7 @@ func TestFileReload_Removed(t *testing.T) {

const file = `cat: dog`

f, err := ioutil.TempFile("", "maddy-tests-")
f, err := os.CreateTemp("", "maddy-tests-")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/target/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -640,7 +639,7 @@ func (q *Queue) removeFromDisk(msgMeta *module.MsgMetadata) {
}

func (q *Queue) readDiskQueue() error {
dirInfo, err := ioutil.ReadDir(q.location)
dirInfo, err := os.ReadDir(q.location)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/target/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"crypto/sha1"
"encoding/hex"
"errors"
"io/ioutil"
"io"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -122,7 +122,7 @@ func (utd *unreliableTargetDelivery) Body(ctx context.Context, header textproto.
}

r, _ := body.Open()
utd.msg.Body, _ = ioutil.ReadAll(r)
utd.msg.Body, _ = io.ReadAll(r)

if len(utd.ut.bodyFailures) > utd.ut.passedMessages {
return utd.ut.bodyFailures[utd.ut.passedMessages]
Expand All @@ -133,7 +133,7 @@ func (utd *unreliableTargetDelivery) Body(ctx context.Context, header textproto.

func (utd *unreliableTargetDeliveryPartial) BodyNonAtomic(ctx context.Context, c module.StatusCollector, header textproto.Header, body buffer.Buffer) {
r, _ := body.Open()
utd.msg.Body, _ = ioutil.ReadAll(r)
utd.msg.Body, _ = io.ReadAll(r)

if len(utd.ut.bodyFailuresPartial) > utd.ut.passedMessages {
for rcpt, err := range utd.ut.bodyFailuresPartial[utd.ut.passedMessages] {
Expand Down Expand Up @@ -200,7 +200,7 @@ func checkQueueDir(t *testing.T, q *Queue, expectedIDs []string) {
expectedMap[id] = false
}

dir, err := ioutil.ReadDir(q.location)
dir, err := os.ReadDir(q.location)
if err != nil {
t.Fatalf("failed to read queue directory: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testutils/bench_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"context"
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"io"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -100,7 +100,7 @@ func RandomMsg(b *testing.B) (module.MsgMetadata, textproto.Header, buffer.Buffe
for i := 0; i < ExtraMessageHeaderFields; i++ {
hdr.Add("AAAAAAAAAAAA-"+strconv.Itoa(i), strings.Repeat("A", ExtraMessageHeaderFieldSize))
}
bodyBlob, _ := ioutil.ReadAll(body)
bodyBlob, _ := io.ReadAll(body)

return module.MsgMetadata{
DontTraceSender: true,
Expand Down
7 changes: 3 additions & 4 deletions internal/testutils/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"strings"
"testing"

Expand All @@ -38,7 +37,7 @@ func BodyFromStr(t *testing.T, literal string) (textproto.Header, buffer.MemoryB
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(bufr)
body, err := io.ReadAll(bufr)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -67,10 +66,10 @@ type FailingBuffer struct {
}

func (fb FailingBuffer) Open() (io.ReadCloser, error) {
r := ioutil.NopCloser(bytes.NewReader(fb.Blob))
r := io.NopCloser(bytes.NewReader(fb.Blob))

if fb.IOError != nil {
return ioutil.NopCloser(&errorReader{r, fb.IOError}), fb.OpenError
return io.NopCloser(&errorReader{r, fb.IOError}), fb.OpenError
}

return r, fb.OpenError
Expand Down
6 changes: 3 additions & 3 deletions internal/testutils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package testutils

import (
"io/ioutil"
"os"
"testing"
)

// Dir is a wrapper for ioutil.TempDir that
// Dir is a wrapper for os.MkdirTemp that
// fails the test on errors.
func Dir(t *testing.T) string {
dir, err := ioutil.TempDir("", "maddy-tests-")
dir, err := os.MkdirTemp("", "maddy-tests-")
if err != nil {
t.Fatalf("can't create test dir: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/testutils/smtp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"net"
"reflect"
"sort"
Expand Down Expand Up @@ -144,7 +143,7 @@ func (s *session) Data(r io.Reader) error {
return s.backend.DataErr
}

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return err
}
Expand All @@ -161,7 +160,7 @@ func (s *session) LMTPData(r io.Reader, status smtp.StatusCollector) error {
return s.backend.DataErr
}

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/testutils/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/hex"
"errors"
"io"
"io/ioutil"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -131,7 +130,7 @@ func (dtd *testTargetDeliveryPartial) BodyNonAtomic(ctx context.Context, c modul
}
defer body.Close()

dtd.msg.Body, err = ioutil.ReadAll(body)
dtd.msg.Body, err = io.ReadAll(body)
if err != nil {
for rcpt, err := range dtd.tgt.PartialBodyErr {
c.SetStatus(rcpt, err)
Expand All @@ -157,11 +156,11 @@ func (dtd *testTargetDelivery) Body(ctx context.Context, header textproto.Header

if dtd.tgt.DiscardMessages {
// Don't bother.
_, err = io.Copy(ioutil.Discard, body)
_, err = io.Copy(io.Discard, body)
return err
}

dtd.msg.Body, err = ioutil.ReadAll(body)
dtd.msg.Body, err = io.ReadAll(body)
return err
}

Expand Down
5 changes: 2 additions & 3 deletions tests/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -150,7 +149,7 @@ func (t *T) Run(waitListeners int) {
}

// Setup file system, create statedir, runtimedir, write out config.
testDir, err := ioutil.TempDir("", "maddy-tests-")
testDir, err := os.MkdirTemp("", "maddy-tests-")
if err != nil {
t.Fatal("Test configuration failed:", err)
}
Expand Down Expand Up @@ -182,7 +181,7 @@ func (t *T) Run(waitListeners int) {
configPreable := "state_dir " + filepath.Join(t.testDir, "statedir") + "\n" +
"runtime_dir " + filepath.Join(t.testDir, "runtime") + "\n\n"

err = ioutil.WriteFile(filepath.Join(t.testDir, "maddy.conf"), []byte(configPreable+t.cfg), os.ModePerm)
err = os.WriteFile(filepath.Join(t.testDir, "maddy.conf"), []byte(configPreable+t.cfg), os.ModePerm)
if err != nil {
t.Fatal("Test configuration failed:", err)
}
Expand Down

0 comments on commit 145bf3c

Please sign in to comment.