Skip to content

Commit

Permalink
Replacing the deprecated ioutil package (quii#569)
Browse files Browse the repository at this point in the history
* replaced ioutil in .md files

* replaced ioutil in .go files
  • Loading branch information
enkeyz authored Jun 11, 2022
1 parent 95e65cd commit 0f0f8dd
Show file tree
Hide file tree
Showing 35 changed files with 61 additions and 75 deletions.
3 changes: 1 addition & 2 deletions command-line/v1/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package poker

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions command-line/v1/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package poker

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
3 changes: 1 addition & 2 deletions command-line/v2/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package poker

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions command-line/v2/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package poker

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
3 changes: 1 addition & 2 deletions command-line/v3/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package poker

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions command-line/v3/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package poker

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
2 changes: 1 addition & 1 deletion context-aware-reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ They combined two simple abstractions (`context.Context` and `io.Reader`) to sol

Let's try and TDD some functionality so that we can wrap an `io.Reader` so it can be cancelled.

Testing this poses an interesting challenge. Normally when using an `io.Reader` you're usually supplying it to some other function and you don't really concern yourself with the details; such as `json.NewDecoder` or `ioutil.ReadAll`.
Testing this poses an interesting challenge. Normally when using an `io.Reader` you're usually supplying it to some other function and you don't really concern yourself with the details; such as `json.NewDecoder` or `io.ReadAll`.

What we want to demonstrate is something like

Expand Down
2 changes: 1 addition & 1 deletion error-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DumbGetter(url string) (string, error) {
}

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
body, _ := io.ReadAll(res.Body) // ignoring err for brevity

return string(body), nil
}
Expand Down
6 changes: 3 additions & 3 deletions io.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ Let's create some helper functions which will create a temporary file with some
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand All @@ -483,7 +483,7 @@ func assertScoreEquals(t testing.TB, got, want int) {
}
```

[TempFile](https://golang.org/pkg/io/ioutil/#TempFile) creates a temporary file for us to use. The `"db"` value we've passed in is a prefix put on a random file name it will create. This is to ensure it won't clash with other files by accident.
[CreateTemp](https://pkg.go.dev/os#CreateTemp) creates a temporary file for us to use. The `"db"` value we've passed in is a prefix put on a random file name it will create. This is to ensure it won't clash with other files by accident.

You'll notice we're not only returning our `ReadWriteSeeker` (the file) but also a function. We need to make sure that the file is removed once the test is finished. We don't want to leak details of the files into the test as it's prone to error and uninteresting for the reader. By returning a `removeFile` function, we can take care of the details in our helper and all the caller has to do is run `defer cleanDatabase()`.

Expand Down Expand Up @@ -881,7 +881,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
3 changes: 1 addition & 2 deletions io/v3/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package main

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

func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
3 changes: 1 addition & 2 deletions io/v4/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package main

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

func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
3 changes: 1 addition & 2 deletions io/v5/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package main

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

func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
3 changes: 1 addition & 2 deletions io/v6/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package main

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

func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
3 changes: 1 addition & 2 deletions io/v7/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions io/v7/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
3 changes: 1 addition & 2 deletions io/v8/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions io/v8/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
3 changes: 1 addition & 2 deletions io/v9/file_system_store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

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

func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
tmpfile, err := os.CreateTemp("", "db")

if err != nil {
t.Fatalf("could not create temp file %v", err)
Expand Down
4 changes: 2 additions & 2 deletions io/v9/tape_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

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

Expand All @@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
tape.Write([]byte("abc"))

file.Seek(0, 0)
newFileContents, _ := ioutil.ReadAll(file)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
want := "abc"
Expand Down
2 changes: 1 addition & 1 deletion os-exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func getXMLFromCommand() io.Reader {
out, _ := cmd.StdoutPipe()

cmd.Start()
data, _ := ioutil.ReadAll(out)
data, _ := io.ReadAll(out)
cmd.Wait()

return bytes.NewReader(data)
Expand Down
4 changes: 2 additions & 2 deletions q-and-a/error-types/error-types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package errortypes

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -30,7 +30,7 @@ func DumbGetter(url string) (string, error) {
}

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
body, _ := io.ReadAll(res.Body) // ignoring err for brevity

return string(body), nil
}
Expand Down
4 changes: 2 additions & 2 deletions q-and-a/error-types/v2/error-types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package errortypes
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -31,7 +31,7 @@ func DumbGetter(url string) (string, error) {
}

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
body, _ := io.ReadAll(res.Body) // ignoring err for brevity

return string(body), nil
}
Expand Down
3 changes: 1 addition & 2 deletions q-and-a/os-exec/os-exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"os/exec"
"strings"
"testing"
Expand All @@ -25,7 +24,7 @@ func getXMLFromCommand() io.Reader {
out, _ := cmd.StdoutPipe()

cmd.Start()
data, _ := ioutil.ReadAll(out)
data, _ := io.ReadAll(out)
cmd.Wait()

return bytes.NewReader(data)
Expand Down
Loading

0 comments on commit 0f0f8dd

Please sign in to comment.