Skip to content

Commit 07f405d

Browse files
authored
chore: stop using deprecated ioutil package (#467)
This package was deprecated in Go 1.16. This chore breaks compatability with Go 1.15 which is out of support.
1 parent afd6b76 commit 07f405d

15 files changed

+67
-76
lines changed

gexec/build.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"go/build"
9-
"io/ioutil"
109
"os"
1110
"os/exec"
1211
"path"
@@ -222,11 +221,11 @@ func temporaryDirectory() (string, error) {
222221
mu.Lock()
223222
defer mu.Unlock()
224223
if tmpDir == "" {
225-
tmpDir, err = ioutil.TempDir("", "gexec_artifacts")
224+
tmpDir, err = os.MkdirTemp("", "gexec_artifacts")
226225
if err != nil {
227226
return "", err
228227
}
229228
}
230229

231-
return ioutil.TempDir(tmpDir, "g")
230+
return os.MkdirTemp(tmpDir, "g")
232231
}

gexec/build_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gexec_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87

@@ -99,7 +98,7 @@ var _ = Describe(".BuildIn", func() {
9998
BeforeEach(func() {
10099
var err error
101100
original = os.Getenv("GOPATH")
102-
gopath, err = ioutil.TempDir("", "")
101+
gopath, err = os.MkdirTemp("", "")
103102
Expect(err).NotTo(HaveOccurred())
104103
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
105104
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
@@ -235,7 +234,7 @@ var _ = Describe(".CompiledTestIn", func() {
235234
BeforeEach(func() {
236235
var err error
237236
original = os.Getenv("GOPATH")
238-
gopath, err = ioutil.TempDir("", "")
237+
gopath, err = os.MkdirTemp("", "")
239238
Expect(err).NotTo(HaveOccurred())
240239
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
241240
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
@@ -278,7 +277,7 @@ var _ = Describe(".CompiledTestIn", func() {
278277

279278
func copyFile(source, directory, basename string) {
280279
Expect(os.MkdirAll(directory, 0755)).To(Succeed())
281-
content, err := ioutil.ReadFile(source)
280+
content, err := os.ReadFile(source)
282281
Expect(err).NotTo(HaveOccurred())
283-
Expect(ioutil.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed())
282+
Expect(os.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed())
284283
}

gexec/session_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package gexec_test
44

55
import (
66
"io"
7-
"io/ioutil"
87
"os/exec"
98
"syscall"
109
"time"
@@ -326,8 +325,8 @@ var _ = Describe("Session", func() {
326325

327326
When("discarding the output of the command", func() {
328327
BeforeEach(func() {
329-
outWriter = ioutil.Discard
330-
errWriter = ioutil.Discard
328+
outWriter = io.Discard
329+
errWriter = io.Discard
331330
})
332331

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

388387
When("discarding the output of the command", func() {
389388
BeforeEach(func() {
390-
outWriter = ioutil.Discard
391-
errWriter = ioutil.Discard
389+
outWriter = io.Discard
390+
errWriter = io.Discard
392391
})
393392

394393
It("executes succesfuly", func() {

ghttp/handlers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"net/url"
1212
"reflect"
@@ -117,7 +117,7 @@ func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.Handl
117117
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
118118
return CombineHandlers(
119119
func(w http.ResponseWriter, req *http.Request) {
120-
body, err := ioutil.ReadAll(req.Body)
120+
body, err := io.ReadAll(req.Body)
121121
req.Body.Close()
122122
g.gomega.Expect(err).ShouldNot(HaveOccurred())
123123
g.gomega.Expect(body).Should(Equal(expectedBody), "Body Mismatch")
@@ -133,7 +133,7 @@ func (g GHTTPWithGomega) VerifyJSON(expectedJSON string) http.HandlerFunc {
133133
return CombineHandlers(
134134
g.VerifyMimeType("application/json"),
135135
func(w http.ResponseWriter, req *http.Request) {
136-
body, err := ioutil.ReadAll(req.Body)
136+
body, err := io.ReadAll(req.Body)
137137
req.Body.Close()
138138
g.gomega.Expect(err).ShouldNot(HaveOccurred())
139139
g.gomega.Expect(body).Should(MatchJSON(expectedJSON), "JSON Mismatch")
@@ -182,7 +182,7 @@ func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.Ha
182182
return CombineHandlers(
183183
g.VerifyContentType("application/x-protobuf"),
184184
func(w http.ResponseWriter, req *http.Request) {
185-
body, err := ioutil.ReadAll(req.Body)
185+
body, err := io.ReadAll(req.Body)
186186
g.gomega.Expect(err).ShouldNot(HaveOccurred())
187187
req.Body.Close()
188188

ghttp/test_server.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ package ghttp
111111
import (
112112
"fmt"
113113
"io"
114-
"io/ioutil"
115114
"net/http"
116115
"net/http/httptest"
117116
"net/http/httputil"
@@ -269,7 +268,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
269268
} else {
270269
s.rwMutex.Unlock()
271270
if s.GetAllowUnhandledRequests() {
272-
ioutil.ReadAll(req.Body)
271+
io.ReadAll(req.Body)
273272
req.Body.Close()
274273
w.WriteHeader(s.GetUnhandledRequestStatusCode())
275274
} else {

ghttp/test_server_test.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ghttp_test
33
import (
44
"bytes"
55
"io"
6-
"io/ioutil"
76
"net/http"
87
"net/url"
98
"regexp"
@@ -60,7 +59,7 @@ var _ = Describe("TestServer", func() {
6059
Expect(err).ShouldNot(HaveOccurred())
6160
Expect(resp.StatusCode).Should(Equal(200))
6261

63-
body, err := ioutil.ReadAll(resp.Body)
62+
body, err := io.ReadAll(resp.Body)
6463
resp.Body.Close()
6564
Expect(err).ShouldNot(HaveOccurred())
6665

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

73-
body2, err := ioutil.ReadAll(resp.Body)
72+
body2, err := io.ReadAll(resp.Body)
7473
resp.Body.Close()
7574
Expect(err).ShouldNot(HaveOccurred())
7675

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

105-
data, err := ioutil.ReadAll(resp.Body)
104+
data, err := io.ReadAll(resp.Body)
106105
Expect(err).ShouldNot(HaveOccurred())
107106
Expect(data).Should(BeEmpty())
108107
})
@@ -792,7 +791,7 @@ var _ = Describe("TestServer", func() {
792791

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

795-
body, err := ioutil.ReadAll(resp.Body)
794+
body, err := io.ReadAll(resp.Body)
796795
Expect(err).ShouldNot(HaveOccurred())
797796
Expect(body).Should(Equal([]byte("sweet")))
798797

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

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

804-
body, err = ioutil.ReadAll(resp.Body)
803+
body, err = io.ReadAll(resp.Body)
805804
Expect(err).ShouldNot(HaveOccurred())
806805
Expect(body).Should(Equal([]byte("sour")))
807806
})
@@ -820,7 +819,7 @@ var _ = Describe("TestServer", func() {
820819
Expect(err).ShouldNot(HaveOccurred())
821820

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

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

857-
body, err := ioutil.ReadAll(resp.Body)
856+
body, err := io.ReadAll(resp.Body)
858857
Expect(err).ShouldNot(HaveOccurred())
859858
Expect(body).Should(Equal([]byte("tasty")))
860859

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

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

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

882881
Expect(err).ShouldNot(HaveOccurred())
883882
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
884-
body, err := ioutil.ReadAll(resp.Body)
883+
body, err := io.ReadAll(resp.Body)
885884
Expect(err).ShouldNot(HaveOccurred())
886885
Expect(body).Should(BeEmpty())
887886

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

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

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

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

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

10731072
var received protobuf.SimpleMessage
1074-
body, err := ioutil.ReadAll(resp.Body)
1073+
body, err := io.ReadAll(resp.Body)
10751074
Expect(err).ShouldNot(HaveOccurred())
10761075
err = proto.Unmarshal(body, &received)
10771076
Expect(err).ShouldNot(HaveOccurred())

gmeasure/cache.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/md5"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
)
@@ -66,16 +65,16 @@ func (cache ExperimentCache) readHeader(filename string) (CachedExperimentHeader
6665
List returns a list of all Cached Experiments found in the cache.
6766
*/
6867
func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
69-
out := []CachedExperimentHeader{}
70-
infos, err := ioutil.ReadDir(cache.Path)
68+
var out []CachedExperimentHeader
69+
entries, err := os.ReadDir(cache.Path)
7170
if err != nil {
7271
return out, err
7372
}
74-
for _, info := range infos {
75-
if filepath.Ext(info.Name()) != CACHE_EXT {
73+
for _, entry := range entries {
74+
if filepath.Ext(entry.Name()) != CACHE_EXT {
7675
continue
7776
}
78-
header, err := cache.readHeader(info.Name())
77+
header, err := cache.readHeader(entry.Name())
7978
if err != nil {
8079
return out, err
8180
}
@@ -88,15 +87,15 @@ func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
8887
Clear empties out the cache - this will delete any and all detected cache files in the cache directory. Use with caution!
8988
*/
9089
func (cache ExperimentCache) Clear() error {
91-
infos, err := ioutil.ReadDir(cache.Path)
90+
entries, err := os.ReadDir(cache.Path)
9291
if err != nil {
9392
return err
9493
}
95-
for _, info := range infos {
96-
if filepath.Ext(info.Name()) != CACHE_EXT {
94+
for _, entry := range entries {
95+
if filepath.Ext(entry.Name()) != CACHE_EXT {
9796
continue
9897
}
99-
err := os.Remove(filepath.Join(cache.Path, info.Name()))
98+
err := os.Remove(filepath.Join(cache.Path, entry.Name()))
10099
if err != nil {
101100
return err
102101
}

matchers/be_a_directory_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package matchers_test
22

33
import (
4-
"io/ioutil"
54
"os"
65

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

17-
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
16+
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
1817
Expect(err).ShouldNot(HaveOccurred())
1918
defer os.Remove(tmpFile.Name())
2019
Expect(tmpFile.Name()).ShouldNot(BeADirectory())
2120

22-
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
21+
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
2322
Expect(err).ShouldNot(HaveOccurred())
2423
defer os.Remove(tmpDir)
2524
Expect(tmpDir).Should(BeADirectory())

matchers/be_a_regular_file_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package matchers_test
22

33
import (
4-
"io/ioutil"
54
"os"
65

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

17-
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
16+
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
1817
Expect(err).ShouldNot(HaveOccurred())
1918
defer os.Remove(tmpFile.Name())
2019
Expect(tmpFile.Name()).Should(BeARegularFile())
2120

22-
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
21+
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
2322
Expect(err).ShouldNot(HaveOccurred())
2423
defer os.Remove(tmpDir)
2524
Expect(tmpDir).ShouldNot(BeARegularFile())

matchers/be_an_existing_file_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package matchers_test
22

33
import (
4-
"io/ioutil"
54
"os"
65

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

17-
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
16+
tmpFile, err := os.CreateTemp("", "gomega-test-tempfile")
1817
Expect(err).ShouldNot(HaveOccurred())
1918
defer os.Remove(tmpFile.Name())
2019
Expect(tmpFile.Name()).Should(BeAnExistingFile())
2120

22-
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
21+
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
2322
Expect(err).ShouldNot(HaveOccurred())
2423
defer os.Remove(tmpDir)
2524
Expect(tmpDir).Should(BeAnExistingFile())

matchers/have_http_body_matcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package matchers
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88

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

0 commit comments

Comments
 (0)