Skip to content

Commit

Permalink
chore: stop using deprecated ioutil package (#467)
Browse files Browse the repository at this point in the history
This package was deprecated in Go 1.16. This chore breaks compatability
with Go 1.15 which is out of support.
  • Loading branch information
blgm committed Aug 23, 2021
1 parent afd6b76 commit 07f405d
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 76 deletions.
5 changes: 2 additions & 3 deletions gexec/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -222,11 +221,11 @@ func temporaryDirectory() (string, error) {
mu.Lock()
defer mu.Unlock()
if tmpDir == "" {
tmpDir, err = ioutil.TempDir("", "gexec_artifacts")
tmpDir, err = os.MkdirTemp("", "gexec_artifacts")
if err != nil {
return "", err
}
}

return ioutil.TempDir(tmpDir, "g")
return os.MkdirTemp(tmpDir, "g")
}
9 changes: 4 additions & 5 deletions gexec/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gexec_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -99,7 +98,7 @@ var _ = Describe(".BuildIn", func() {
BeforeEach(func() {
var err error
original = os.Getenv("GOPATH")
gopath, err = ioutil.TempDir("", "")
gopath, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
Expand Down Expand Up @@ -235,7 +234,7 @@ var _ = Describe(".CompiledTestIn", func() {
BeforeEach(func() {
var err error
original = os.Getenv("GOPATH")
gopath, err = ioutil.TempDir("", "")
gopath, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
Expand Down Expand Up @@ -278,7 +277,7 @@ var _ = Describe(".CompiledTestIn", func() {

func copyFile(source, directory, basename string) {
Expect(os.MkdirAll(directory, 0755)).To(Succeed())
content, err := ioutil.ReadFile(source)
content, err := os.ReadFile(source)
Expect(err).NotTo(HaveOccurred())
Expect(ioutil.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed())
}
9 changes: 4 additions & 5 deletions gexec/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package gexec_test

import (
"io"
"io/ioutil"
"os/exec"
"syscall"
"time"
Expand Down Expand Up @@ -326,8 +325,8 @@ var _ = Describe("Session", func() {

When("discarding the output of the command", func() {
BeforeEach(func() {
outWriter = ioutil.Discard
errWriter = ioutil.Discard
outWriter = io.Discard
errWriter = io.Discard
})

It("executes succesfuly", func() {
Expand Down Expand Up @@ -387,8 +386,8 @@ var _ = Describe("Session", func() {

When("discarding the output of the command", func() {
BeforeEach(func() {
outWriter = ioutil.Discard
errWriter = ioutil.Discard
outWriter = io.Discard
errWriter = io.Discard
})

It("executes succesfuly", func() {
Expand Down
8 changes: 4 additions & 4 deletions ghttp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -117,7 +117,7 @@ func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.Handl
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
return CombineHandlers(
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
req.Body.Close()
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(body).Should(Equal(expectedBody), "Body Mismatch")
Expand All @@ -133,7 +133,7 @@ func (g GHTTPWithGomega) VerifyJSON(expectedJSON string) http.HandlerFunc {
return CombineHandlers(
g.VerifyMimeType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
req.Body.Close()
g.gomega.Expect(err).ShouldNot(HaveOccurred())
g.gomega.Expect(body).Should(MatchJSON(expectedJSON), "JSON Mismatch")
Expand Down Expand Up @@ -182,7 +182,7 @@ func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.Ha
return CombineHandlers(
g.VerifyContentType("application/x-protobuf"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
g.gomega.Expect(err).ShouldNot(HaveOccurred())
req.Body.Close()

Expand Down
3 changes: 1 addition & 2 deletions ghttp/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ package ghttp
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand Down Expand Up @@ -269,7 +268,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} else {
s.rwMutex.Unlock()
if s.GetAllowUnhandledRequests() {
ioutil.ReadAll(req.Body)
io.ReadAll(req.Body)
req.Body.Close()
w.WriteHeader(s.GetUnhandledRequestStatusCode())
} else {
Expand Down
25 changes: 12 additions & 13 deletions ghttp/test_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ghttp_test
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -60,7 +59,7 @@ var _ = Describe("TestServer", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(200))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
Expect(err).ShouldNot(HaveOccurred())

Expand All @@ -70,7 +69,7 @@ var _ = Describe("TestServer", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(200))

body2, err := ioutil.ReadAll(resp.Body)
body2, err := io.ReadAll(resp.Body)
resp.Body.Close()
Expect(err).ShouldNot(HaveOccurred())

Expand Down Expand Up @@ -102,7 +101,7 @@ var _ = Describe("TestServer", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(data).Should(BeEmpty())
})
Expand Down Expand Up @@ -792,7 +791,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(Equal([]byte("sweet")))

Expand All @@ -801,7 +800,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusOK))

body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(Equal([]byte("sour")))
})
Expand All @@ -820,7 +819,7 @@ var _ = Describe("TestServer", func() {
Expect(err).ShouldNot(HaveOccurred())

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
Expect(ioutil.ReadAll(resp.Body)).Should(Equal([]byte("sweet")))
Expect(io.ReadAll(resp.Body)).Should(Equal([]byte("sweet")))
Expect(resp.Header.Get("X-Custom-Header")).Should(Equal("my header"))
})
})
Expand Down Expand Up @@ -854,7 +853,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(Equal([]byte("tasty")))

Expand All @@ -863,7 +862,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(Equal([]byte("treat")))
})
Expand All @@ -881,7 +880,7 @@ var _ = Describe("TestServer", func() {

Expect(err).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(BeEmpty())

Expand All @@ -905,7 +904,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON("[1,2,3]"))
})
Expand Down Expand Up @@ -990,7 +989,7 @@ var _ = Describe("TestServer", func() {

Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
Expect(body).Should(MatchJSON(`{"Key": "Jim", "Value": "Codes"}`))
})
Expand Down Expand Up @@ -1071,7 +1070,7 @@ var _ = Describe("TestServer", func() {
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

var received protobuf.SimpleMessage
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
Expect(err).ShouldNot(HaveOccurred())
err = proto.Unmarshal(body, &received)
Expect(err).ShouldNot(HaveOccurred())
Expand Down
19 changes: 9 additions & 10 deletions gmeasure/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -66,16 +65,16 @@ func (cache ExperimentCache) readHeader(filename string) (CachedExperimentHeader
List returns a list of all Cached Experiments found in the cache.
*/
func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
out := []CachedExperimentHeader{}
infos, err := ioutil.ReadDir(cache.Path)
var out []CachedExperimentHeader
entries, err := os.ReadDir(cache.Path)
if err != nil {
return out, err
}
for _, info := range infos {
if filepath.Ext(info.Name()) != CACHE_EXT {
for _, entry := range entries {
if filepath.Ext(entry.Name()) != CACHE_EXT {
continue
}
header, err := cache.readHeader(info.Name())
header, err := cache.readHeader(entry.Name())
if err != nil {
return out, err
}
Expand All @@ -88,15 +87,15 @@ func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
Clear empties out the cache - this will delete any and all detected cache files in the cache directory. Use with caution!
*/
func (cache ExperimentCache) Clear() error {
infos, err := ioutil.ReadDir(cache.Path)
entries, err := os.ReadDir(cache.Path)
if err != nil {
return err
}
for _, info := range infos {
if filepath.Ext(info.Name()) != CACHE_EXT {
for _, entry := range entries {
if filepath.Ext(entry.Name()) != CACHE_EXT {
continue
}
err := os.Remove(filepath.Join(cache.Path, info.Name()))
err := os.Remove(filepath.Join(cache.Path, entry.Name()))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions matchers/be_a_directory_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package matchers_test

import (
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
Expand All @@ -14,12 +13,12 @@ var _ = Describe("BeADirectoryMatcher", func() {
It("should do the right thing", func() {
Expect("/dne/test").ShouldNot(BeADirectory())

tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Expect(tmpFile.Name()).ShouldNot(BeADirectory())

tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Expect(tmpDir).Should(BeADirectory())
Expand Down
5 changes: 2 additions & 3 deletions matchers/be_a_regular_file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package matchers_test

import (
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
Expand All @@ -14,12 +13,12 @@ var _ = Describe("BeARegularFileMatcher", func() {
It("should do the right thing", func() {
Expect("/dne/test").ShouldNot(BeARegularFile())

tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Expect(tmpFile.Name()).Should(BeARegularFile())

tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Expect(tmpDir).ShouldNot(BeARegularFile())
Expand Down
5 changes: 2 additions & 3 deletions matchers/be_an_existing_file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package matchers_test

import (
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
Expand All @@ -14,12 +13,12 @@ var _ = Describe("BeAnExistingFileMatcher", func() {
It("should do the right thing", func() {
Expect("/dne/test").ShouldNot(BeAnExistingFile())

tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Expect(tmpFile.Name()).Should(BeAnExistingFile())

tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
Expect(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Expect(tmpDir).Should(BeAnExistingFile())
Expand Down
4 changes: 2 additions & 2 deletions matchers/have_http_body_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package matchers

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -81,7 +81,7 @@ func (matcher *HaveHTTPBodyMatcher) body(actual interface{}) ([]byte, error) {
if a.Body != nil {
defer a.Body.Close()
var err error
matcher.cachedBody, err = ioutil.ReadAll(a.Body)
matcher.cachedBody, err = io.ReadAll(a.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
Expand Down

0 comments on commit 07f405d

Please sign in to comment.