Skip to content

Commit cd97cb3

Browse files
committed
Remove ioutil
The package has been deprecated.
1 parent 31c2b37 commit cd97cb3

12 files changed

Lines changed: 40 additions & 86 deletions

cmd/compare_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
//go:build !windows
12
// +build !windows
23

34
package cmd
45

56
import (
6-
"io/ioutil"
7-
"os"
87
"path/filepath"
98
"testing"
109
"time"
@@ -17,9 +16,7 @@ import (
1716
func Test_compare(t *testing.T) {
1817
t.Parallel()
1918

20-
tmp, err := ioutil.TempDir("", "go-jwlm")
21-
assert.NoError(t, err)
22-
defer os.RemoveAll(tmp)
19+
tmp := t.TempDir()
2320

2421
emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
2522
leftFilename := filepath.Join(tmp, "left.jwlibrary")

cmd/merge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package cmd
66
import (
77
"bytes"
88
"database/sql"
9-
"io/ioutil"
10-
"os"
119
"path/filepath"
1210
"testing"
1311

@@ -22,9 +20,7 @@ import (
2220
func Test_merge(t *testing.T) {
2321
t.Parallel()
2422

25-
tmp, err := ioutil.TempDir("", "go-jwlm")
26-
assert.NoError(t, err)
27-
defer os.RemoveAll(tmp)
23+
tmp := t.TempDir()
2824

2925
emptyFilename := filepath.Join(tmp, "empty.jwlibrary")
3026
leftFilename := filepath.Join(tmp, "left.jwlibrary")

cmd/purge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package cmd
55

66
import (
7-
"io/ioutil"
8-
"os"
97
"path/filepath"
108
"testing"
119

@@ -18,9 +16,7 @@ import (
1816
func Test_purge(t *testing.T) {
1917
t.Parallel()
2018

21-
tmp, err := ioutil.TempDir("", "go-jwlm")
22-
assert.NoError(t, err)
23-
defer os.RemoveAll(tmp)
19+
tmp := t.TempDir()
2420

2521
inputFilename := filepath.Join(tmp, "left.jwlibrary")
2622
assert.NoError(t, leftDB.ExportJWLBackup(inputFilename))

gomobile/CatalogDB_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
package gomobile
@@ -6,7 +7,6 @@ import (
67
"crypto/sha256"
78
"fmt"
89
"io"
9-
"io/ioutil"
1010
"net/http"
1111
"net/http/httptest"
1212
"os"
@@ -20,15 +20,13 @@ import (
2020
)
2121

2222
func TestDownloadCatalog(t *testing.T) {
23-
tmp, err := ioutil.TempDir("", "go-jwlm")
24-
assert.NoError(t, err)
25-
defer os.RemoveAll(tmp)
23+
tmp := t.TempDir()
2624

2725
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
2826
if strings.Contains(req.URL.String(), "manifest.json") {
2927
rw.Write([]byte(`{"version": 1, "current": "164a1c4b-4dbd-4909-8f88-8e7a18c562f2"}`))
3028
} else {
31-
data, err := ioutil.ReadFile(filepath.Join("../publication/testdata", "catalog.db.gz"))
29+
data, err := os.ReadFile(filepath.Join("../publication/testdata", "catalog.db.gz"))
3230
assert.NoError(t, err)
3331
rw.Write(data)
3432
}
@@ -62,9 +60,7 @@ func TestDownloadCatalog(t *testing.T) {
6260
}
6361

6462
func TestDownloadManager_CancelDownload(t *testing.T) {
65-
tmp, err := ioutil.TempDir("", "go-jwlm")
66-
assert.NoError(t, err)
67-
defer os.RemoveAll(tmp)
63+
tmp := t.TempDir()
6864

6965
dm := DownloadCatalog(filepath.Join(tmp, "catalog.db"))
7066
dm.CancelDownload()
@@ -77,14 +73,13 @@ func TestDownloadManager_CancelDownload(t *testing.T) {
7773
}
7874

7975
func TestCatalogNeedsUpdate(t *testing.T) {
80-
tmp, err := ioutil.TempDir("", "go-jwlm")
81-
assert.NoError(t, err)
82-
defer os.RemoveAll(tmp)
76+
tmp := t.TempDir()
8377

8478
assert.True(t, CatalogNeedsUpdate("not-valid-path"))
8579

8680
filePath := filepath.Join(tmp, "catalog.db")
87-
_, err = os.Create(filePath)
81+
_, err := os.Create(filePath)
82+
assert.NoError(t, err)
8883

8984
assert.False(t, CatalogNeedsUpdate(filePath))
9085

@@ -93,12 +88,11 @@ func TestCatalogNeedsUpdate(t *testing.T) {
9388
}
9489

9590
func TestCatalogExists(t *testing.T) {
96-
tmp, err := ioutil.TempDir("", "go-jwlm")
97-
assert.NoError(t, err)
98-
defer os.RemoveAll(tmp)
91+
tmp := t.TempDir()
9992

10093
filePath := filepath.Join(tmp, "catalog.db")
101-
_, err = os.Create(filePath)
94+
_, err := os.Create(filePath)
95+
assert.NoError(t, err)
10296

10397
assert.False(t, CatalogExists("not-valid-path"))
10498
assert.True(t, CatalogExists(filePath))

gomobile/Database_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package gomobile
55

66
import (
7-
"io/ioutil"
8-
"os"
97
"path/filepath"
108
"testing"
119

@@ -181,9 +179,7 @@ func TestDatabaseWrapper_DBIsLoaded(t *testing.T) {
181179
}
182180

183181
func TestDatabaseWrapper_ExportMerged(t *testing.T) {
184-
tmp, err := ioutil.TempDir("", "go-jwlm")
185-
assert.NoError(t, err)
186-
defer os.RemoveAll(tmp)
182+
tmp := t.TempDir()
187183

188184
dbw := &DatabaseWrapper{}
189185
dbw.merged = &model.Database{}

gomobile/Merge_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package gomobile
55

66
import (
77
"database/sql"
8-
"io/ioutil"
9-
"os"
108
"path/filepath"
119
"testing"
1210

@@ -164,9 +162,7 @@ func Test_MergeNwt(t *testing.T) {
164162
func Test_MergeNwtDifferentDocID(t *testing.T) {
165163
// As the cleanup happens in the PostMerge hook, which is called on export,
166164
// we also need to export the merged DB
167-
tmp, err := ioutil.TempDir("", "go-jwlm")
168-
assert.NoError(t, err)
169-
defer os.RemoveAll(tmp)
165+
tmp := t.TempDir()
170166

171167
dbw := DatabaseWrapper{
172168
left: model.MakeDatabaseCopy(leftDBNwtWithDifferentDocID),

model/Database.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
"reflect"
@@ -767,7 +766,7 @@ func insertEntries(sqlite *sql.DB, m []Model) error {
767766

768767
// createEmptySQLiteDB creates a new SQLite database at filename with the base userData.db from JWLibrary
769768
func createEmptySQLiteDB(filename string) error {
770-
if err := ioutil.WriteFile(filename, userDataDatabaseFile, 0644); err != nil {
769+
if err := os.WriteFile(filename, userDataDatabaseFile, 0644); err != nil {
771770
return errors.Wrap(err, fmt.Sprintf("Error while saving new SQLite database at %s", filename))
772771
}
773772

model/Database_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
_ "embed"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"log"
1211
"os"
1312
"path/filepath"
@@ -864,13 +863,10 @@ func TestDatabase_ExportJWLBackup(t *testing.T) {
864863

865864
func Test_createEmptySQLiteDB(t *testing.T) {
866865
// Create tmp folder and place all files there
867-
tmp, err := ioutil.TempDir("", "go-jwlm")
868-
assert.NoError(t, err)
869-
defer os.RemoveAll(tmp)
866+
tmp := t.TempDir()
870867

871868
path := filepath.Join(tmp, userDataFilename)
872-
err = createEmptySQLiteDB(path)
873-
assert.NoError(t, err)
869+
assert.NoError(t, createEmptySQLiteDB(path))
874870

875871
// Test if file has correct hash
876872
f, err := os.Open(path)
@@ -889,9 +885,7 @@ func Test_createEmptySQLiteDB(t *testing.T) {
889885

890886
func TestDatabase_saveToNewSQLite(t *testing.T) {
891887
// Create tmp folder and place all files there
892-
tmp, err := ioutil.TempDir("", "go-jwlm")
893-
assert.NoError(t, err)
894-
defer os.RemoveAll(tmp)
888+
tmp := t.TempDir()
895889

896890
db := Database{
897891
BlockRange: []*BlockRange{{3, 2, 13, sql.NullInt32{Int32: 0, Valid: true}, sql.NullInt32{Int32: 14, Valid: true}, 3}},

model/manifest.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"log"
109
"os"
1110
"path/filepath"
@@ -41,7 +40,7 @@ func (mfst *manifest) importManifest(path string) error {
4140
}
4241
defer file.Close()
4342

44-
blob, _ := ioutil.ReadAll(file)
43+
blob, _ := io.ReadAll(file)
4544

4645
err = json.Unmarshal([]byte(blob), &mfst)
4746
if err != nil {
@@ -113,7 +112,7 @@ func (mfst *manifest) exportManifest(path string) error {
113112
return errors.Wrap(err, "Error while marshalling manifest")
114113
}
115114

116-
if err := ioutil.WriteFile(path, bytes, 0644); err != nil {
115+
if err := os.WriteFile(path, bytes, 0644); err != nil {
117116
return errors.Wrap(err, fmt.Sprintf("Error while saving manifest file at %v", path))
118117
}
119118

model/manifest_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package model
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"os"
75
"path/filepath"
86
"testing"
97
"time"
@@ -158,13 +156,11 @@ func Test_generateManifest(t *testing.T) {
158156
}
159157

160158
func Test_exportManifest(t *testing.T) {
161-
tmp, err := ioutil.TempDir("", "go-jwlm")
162-
assert.NoError(t, err)
163-
defer os.RemoveAll(tmp)
159+
tmp := t.TempDir()
164160

165161
path := filepath.Join(tmp, "test_manifest.json")
166162
fmt.Println(path)
167-
err = exampleManifest.exportManifest(path)
163+
err := exampleManifest.exportManifest(path)
168164
assert.NoError(t, err)
169165
assert.FileExists(t, path)
170166

0 commit comments

Comments
 (0)