Skip to content

Commit

Permalink
fix: stop using the deprecated io/ioutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
web3-bot committed Aug 22, 2023
1 parent 9086077 commit c20ed1a
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 54 deletions.
3 changes: 2 additions & 1 deletion cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"path"
"testing"

"github.com/ipfs/ipfs-ds-convert/testutil"
"os"

"github.com/ipfs/ipfs-ds-convert/testutil"
)

func TestBasicConvert(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package config

import (
"encoding/json"
"io/ioutil"
"os"
)

func Load(path string, out *map[string]interface{}) error {
cfgbytes, err := ioutil.ReadFile(path)
cfgbytes, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions convert/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package convert_test

import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
Expand All @@ -22,7 +21,7 @@ func TestInvalidRepoVersion(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, "version"), []byte("147258369"), 0664)
err := os.WriteFile(path.Join(dir, "version"), []byte("147258369"), 0664)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func TestInvalidVersion(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, "version"), []byte("a"), 0600)
err := os.WriteFile(path.Join(dir, "version"), []byte("a"), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -128,7 +127,7 @@ func TestInvalidSpecJson(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, repo.SpecsFile), []byte("}"), 0600)
err := os.WriteFile(path.Join(dir, repo.SpecsFile), []byte("}"), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -149,7 +148,7 @@ func TestInvalidSpecFile(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, repo.SpecsFile), []byte("{}"), 0600)
err := os.WriteFile(path.Join(dir, repo.SpecsFile), []byte("{}"), 0600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -191,7 +190,7 @@ func TestInvalidConfigJson(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, repo.ConfigFile), []byte("}"), 0600)
err := os.WriteFile(path.Join(dir, repo.ConfigFile), []byte("}"), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -212,7 +211,7 @@ func TestInvalidConfigFile(t *testing.T) {
dir, _close, _, _ := testutil.PrepareTest(t, 10, 10)
defer _close(t)

err := ioutil.WriteFile(path.Join(dir, repo.ConfigFile), []byte("{}"), 0600)
err := os.WriteFile(path.Join(dir, repo.ConfigFile), []byte("{}"), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -231,7 +230,7 @@ func TestInvalidConfigFile(t *testing.T) {
t.Fatal(err)
}

err = ioutil.WriteFile(path.Join(dir, repo.ConfigFile), []byte(`{"Datastore":{}}`), 0600)
err = os.WriteFile(path.Join(dir, repo.ConfigFile), []byte(`{"Datastore":{}}`), 0600)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package convert

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

Expand Down Expand Up @@ -135,7 +134,7 @@ func (c *Conversion) saveNewSpec(backup bool) (err error) {
return err
}

err = ioutil.WriteFile(filepath.Join(c.path, repo.SpecsFile), []byte(toDiskId), 0660)
err = os.WriteFile(filepath.Join(c.path, repo.SpecsFile), []byte(toDiskId), 0660)
if err != nil {
return err
}
Expand All @@ -151,12 +150,12 @@ func (c *Conversion) saveNewSpec(backup bool) (err error) {
}

func (c *Conversion) backupSpec() error {
backupFile, err := ioutil.TempFile(c.path, "datastore_spec_backup")
backupFile, err := os.CreateTemp(c.path, "datastore_spec_backup")
if err != nil {
return err
}

specData, err := ioutil.ReadFile(filepath.Join(c.path, repo.SpecsFile))
specData, err := os.ReadFile(filepath.Join(c.path, repo.SpecsFile))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestLossyConvert(t *testing.T) {
t.Errorf("expected error 'adding missing to src spec: couldn't find best match for specA /'")
}

//should cover noop case in convert.go
// should cover noop case in convert.go
func TestNoopConvert(t *testing.T) {
//Prepare repo
dir, _close, s1, s2 := testutil.PrepareTest(t, 3000, 3000)
Expand Down
5 changes: 2 additions & 3 deletions convert/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package convert
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -131,7 +130,7 @@ func (c *Copy) openDatastores() (err error) {
}
c.logStep("open datastore at %s", c.path)

c.newDsDir, err = ioutil.TempDir(c.path, "ds-convert")
c.newDsDir, err = os.MkdirTemp(c.path, "ds-convert")
if err != nil {
return errors.Wrapf(err, "error creating temp datastore at %s", c.path)
}
Expand Down Expand Up @@ -231,7 +230,7 @@ func CopyKeys(fromDs repo.Datastore, toDs repo.Datastore) error {
}

func (c *Copy) swapDatastores() (err error) {
c.oldDsDir, err = ioutil.TempDir(c.path, "ds-convert-old")
c.oldDsDir, err = os.MkdirTemp(c.path, "ds-convert-old")
if err != nil {
return errors.Wrapf(err, "error creating temp datastore at %s", c.path)
}
Expand Down
7 changes: 3 additions & 4 deletions convert/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package convert

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -57,7 +56,7 @@ var (
)

func TestInvalidSpecLeft(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "ds-convert-test-")
d, err := os.MkdirTemp(os.TempDir(), "ds-convert-test-")
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -76,7 +75,7 @@ func TestInvalidSpecLeft(t *testing.T) {
}

func TestInvalidSpecRight(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "ds-convert-test-")
d, err := os.MkdirTemp(os.TempDir(), "ds-convert-test-")
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -94,7 +93,7 @@ func TestInvalidSpecRight(t *testing.T) {
}

func TestOpenNonexist(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "ds-convert-test-")
d, err := os.MkdirTemp(os.TempDir(), "ds-convert-test-")
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions convert/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package convert

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand All @@ -15,7 +14,7 @@ import (
)

func (c *Conversion) checkRepoVersion() error {
vstr, err := ioutil.ReadFile(filepath.Join(c.path, "version"))
vstr, err := os.ReadFile(filepath.Join(c.path, "version"))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions revert/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type ActionLogger struct {
file *os.File
}

//NewActionLogger creates revert action logger which logs actions needed to
//revert conversion steps
// NewActionLogger creates revert action logger which logs actions needed to
// revert conversion steps
func NewActionLogger(repoPath string) (*ActionLogger, error) {
if _, err := os.Stat(path.Join(repoPath, ConvertLog)); !os.IsNotExist(err) {
return nil, fmt.Errorf("Log file %s already exists, you may want to run revert", path.Join(repoPath, ConvertLog))
Expand Down
9 changes: 4 additions & 5 deletions revert/log_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package revert_test

import (
"io/ioutil"
"os"
"path"
"strings"
Expand All @@ -11,12 +10,12 @@ import (
)

func TestNewActionLogger(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "ds-convert-test-")
d, err := os.MkdirTemp(os.TempDir(), "ds-convert-test-")
if err != nil {
t.Fatal(err)
}

err = ioutil.WriteFile(path.Join(d, revert.ConvertLog), []byte{}, 0664)
err = os.WriteFile(path.Join(d, revert.ConvertLog), []byte{}, 0664)
if err != nil {
t.Fatal(err)
}
Expand All @@ -38,7 +37,7 @@ func TestNewActionLogger(t *testing.T) {
}

func TestLog(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "ds-convert-test-")
d, err := os.MkdirTemp(os.TempDir(), "ds-convert-test-")
if err != nil {
t.Fatal(err)
}
Expand All @@ -54,7 +53,7 @@ func TestLog(t *testing.T) {
t.Fatal(err)
}

b, err := ioutil.ReadFile(path.Join(d, revert.ConvertLog))
b, err := os.ReadFile(path.Join(d, revert.ConvertLog))
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions revert/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/ipfs/ipfs-ds-convert/repo"

"encoding/json"

lock "github.com/ipfs/go-fs-lock"
"github.com/ipfs/ipfs-ds-convert/config"
"github.com/pkg/errors"
"io/ioutil"
)

var Log = logging.New(os.Stderr, "revert ", logging.LstdFlags)
Expand Down Expand Up @@ -222,7 +222,7 @@ func fixConfig(repoPath string) error {
return err
}

err = ioutil.WriteFile(filepath.Join(repoPath, repo.ConfigFile), []byte(confBytes), 0660)
err = os.WriteFile(filepath.Join(repoPath, repo.ConfigFile), []byte(confBytes), 0660)
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions revert/revert_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package revert_test

import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestBasicConvertRevertUnknownStep(t *testing.T) {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"unknownactiontype","arg":[]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"unknownactiontype","arg":[]}`), 0600)

err = revert.Revert(dir, true, false, false)
if !strings.Contains(err.Error(), "unknown revert step 'unknownactiontype'") {
Expand Down Expand Up @@ -176,35 +176,35 @@ func TestBasicConvertRevertInvalidArgs(t *testing.T) {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionRemove+`","arg":[]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionRemove+`","arg":[]}`), 0600)

err = revert.Revert(dir, true, false, false)
if !strings.Contains(err.Error(), "revert remove: arg count 0 != 1") {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionMove+`","arg":[]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionMove+`","arg":[]}`), 0600)

err = revert.Revert(dir, true, false, false)
if !strings.Contains(err.Error(), "revert move: arg count 0 != 2") {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionMkdir+`","arg":[]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionMkdir+`","arg":[]}`), 0600)

err = revert.Revert(dir, true, false, false)
if !strings.Contains(err.Error(), "revert mkdir: arg count 0 != 1") {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionCleanup+`","arg":["a"]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionCleanup+`","arg":["a"]}`), 0600)

err = revert.Revert(dir, true, false, true)
if !strings.Contains(err.Error(), "cannot cleanup after failed conversion") {
t.Fatal(err)
}

ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionCleanup+`","arg":[]}`+"\n"+`{"action":"`+revert.ActionDone+`","arg":[]}`), 0600)
os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(`{"action":"`+revert.ActionCleanup+`","arg":[]}`+"\n"+`{"action":"`+revert.ActionDone+`","arg":[]}`), 0600)

err = revert.Revert(dir, true, false, true)
if !strings.Contains(err.Error(), "cleanup arg count 0 != 1") {
Expand All @@ -222,7 +222,7 @@ func TestRevertMkdirChecks(t *testing.T) {
t.Fatal(err)
}

err = ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
err = os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -243,7 +243,7 @@ func TestRevertMoveChecks(t *testing.T) {
t.Fatal(err)
}

err = ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
err = os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -258,7 +258,7 @@ func TestRevertMoveChecks(t *testing.T) {
t.Fatal(err)
}

err = ioutil.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
err = os.WriteFile(path.Join(dir, revert.ConvertLog), []byte(l), 0600)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit c20ed1a

Please sign in to comment.