Skip to content

Commit

Permalink
Use io or os instead of ioutil
Browse files Browse the repository at this point in the history
ioutil has been deprecated and the Go 1.19 linter complains if you use it. So I have updated our usage of ioutil to use the corresponding functions in os or io packages.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Oct 11, 2022
1 parent 04df9b7 commit 8ad1834
Show file tree
Hide file tree
Showing 45 changed files with 124 additions and 134 deletions.
11 changes: 5 additions & 6 deletions mage/docs/docs_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docs

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -13,7 +12,7 @@ import (

func TestEnsureOperatorRepository(t *testing.T) {
t.Run("has local repo", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "porter-docs-test")
tmp, err := os.MkdirTemp("", "porter-docs-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)

Expand All @@ -23,7 +22,7 @@ func TestEnsureOperatorRepository(t *testing.T) {
})

t.Run("missing local repo", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "porter-docs-test")
tmp, err := os.MkdirTemp("", "porter-docs-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)

Expand All @@ -33,7 +32,7 @@ func TestEnsureOperatorRepository(t *testing.T) {
})

t.Run("local repo unset", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "porter-docs-test")
tmp, err := os.MkdirTemp("", "porter-docs-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)

Expand All @@ -43,7 +42,7 @@ func TestEnsureOperatorRepository(t *testing.T) {
})

t.Run("empty default path clones repo", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "porter-docs-test")
tmp, err := os.MkdirTemp("", "porter-docs-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)

Expand All @@ -56,7 +55,7 @@ func TestEnsureOperatorRepository(t *testing.T) {
})

t.Run("changes in default path are reset", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "porter-docs-test")
tmp, err := os.MkdirTemp("", "porter-docs-test")
require.NoError(t, err)
defer os.RemoveAll(tmp)

Expand Down
3 changes: 1 addition & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package main
import (
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -495,7 +494,7 @@ func Install() {

// Copy mixin binaries
mixinsDir := filepath.Join("bin", "mixins")
mixinsDirItems, err := ioutil.ReadDir(mixinsDir)
mixinsDirItems, err := os.ReadDir(mixinsDir)
if err != nil {
mgx.Must(fmt.Errorf("could not list mixins in bin: %w", err))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package agent

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -55,7 +54,7 @@ func TestExecute(t *testing.T) {
}

func makeTestPorterHome(t *testing.T) string {
home, err := ioutil.TempDir("", "porter-home")
home, err := os.MkdirTemp("", "porter-home")
require.NoError(t, err)
require.NoError(t, shx.Copy("../../bin/porter", home))
return home
Expand Down
5 changes: 2 additions & 3 deletions pkg/cnab/config-adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -873,15 +872,15 @@ func TestManifestConverter_generateCustomMetadata(t *testing.T) {
require.NoError(t, err, "ToBundle failed")
assert.Len(t, bun.Custom, 4)

f, err := ioutil.TempFile("", "")
f, err := os.CreateTemp("", "")
require.NoError(t, err, "Failed to create bundle file")
defer os.Remove(f.Name())

_, err = bun.WriteTo(f)
require.NoError(t, err, "Failed to write bundle file")

expectedCustomMetaData := "{\"foo\":{\"test1\":true,\"test2\":1,\"test3\":\"value\",\"test4\":[\"one\",\"two\",\"three\"],\"test5\":{\"1\":\"one\",\"two\":\"two\"}}"
bundleData, err := ioutil.ReadFile(f.Name())
bundleData, err := os.ReadFile(f.Name())
require.NoError(t, err, "Failed to read bundle file")

assert.Contains(t, string(bundleData), expectedCustomMetaData, "Created bundle should be equal to expected bundle ")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cnab/dependencies_v1_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cnab

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

"github.com/cnabio/cnab-go/bundle"
Expand All @@ -12,7 +12,7 @@ import (
func TestReadDependencyV1Properties(t *testing.T) {
t.Parallel()

data, err := ioutil.ReadFile("testdata/bundle.json")
data, err := os.ReadFile("testdata/bundle.json")
require.NoError(t, err, "cannot read bundle file")

b, err := bundle.Unmarshal(data)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cnab/helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cnab

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

"github.com/cnabio/cnab-go/bundle"
"github.com/stretchr/testify/require"
)

func ReadTestBundle(t *testing.T, path string) ExtendedBundle {
bunD, err := ioutil.ReadFile(path)
bunD, err := os.ReadFile(path)
require.NoError(t, err, "ReadFile failed for %s", path)

bun, err := bundle.Unmarshal(bunD)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cnab/parameter_sources_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cnab

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

"github.com/cnabio/cnab-go/bundle"
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestProcessedExtensions_GetParameterSourcesExtension(t *testing.T) {
func TestReadParameterSourcesProperties(t *testing.T) {
t.Parallel()

data, err := ioutil.ReadFile("testdata/bundle.json")
data, err := os.ReadFile("testdata/bundle.json")
require.NoError(t, err, "cannot read bundle file")

b, err := bundle.Unmarshal(data)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cnab/provider/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cnabprovider

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

"github.com/cnabio/cnab-go/bundle"
Expand All @@ -14,7 +14,7 @@ import (
func TestAddRelocation(t *testing.T) {
t.Parallel()

data, err := ioutil.ReadFile("testdata/relocation-mapping.json")
data, err := os.ReadFile("testdata/relocation-mapping.json")
require.NoError(t, err)

d := NewTestRuntime(t)
Expand Down
34 changes: 18 additions & 16 deletions pkg/exec/builder/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"io"

"get.porter.sh/porter/pkg/runtime"
"get.porter.sh/porter/pkg/tracing"
Expand All @@ -13,16 +13,17 @@ import (

// UnmarshalAction handles unmarshaling any action, given a pointer to a slice of Steps.
// Iterate over the results and cast back to the Steps to use the results.
// var steps []Step
// results, err := UnmarshalAction(unmarshal, &steps)
// if err != nil {
// return err
// }
//
// for _, result := range results {
// step := result.(*[]Step)
// a.Steps = append(a.Steps, *step...)
// }
// var steps []Step
// results, err := UnmarshalAction(unmarshal, &steps)
// if err != nil {
// return err
// }
//
// for _, result := range results {
// step := result.(*[]Step)
// a.Steps = append(a.Steps, *step...)
// }
func UnmarshalAction(unmarshal func(interface{}) error, builder BuildableAction) (map[string][]interface{}, error) {
actionMap := map[string][]interface{}{}
err := unmarshal(&actionMap)
Expand Down Expand Up @@ -70,11 +71,12 @@ func unmarshalActionMap(actionMap map[string][]interface{}, builder BuildableAct
// Action instance.
//
// Example:
// var action Action
// err := builder.LoadAction(m.Context, opts.File, func(contents []byte) (interface{}, error) {
// err := yaml.Unmarshal(contents, &action)
// return &action, err
// })
//
// var action Action
// err := builder.LoadAction(m.Context, opts.File, func(contents []byte) (interface{}, error) {
// err := yaml.Unmarshal(contents, &action)
// return &action, err
// })
func LoadAction(ctx context.Context, cfg runtime.RuntimeConfig, commandFile string, unmarshal func([]byte) (interface{}, error)) error {
//lint:ignore SA4006 ignore unused ctx for now
ctx, span := tracing.StartSpan(ctx)
Expand All @@ -98,7 +100,7 @@ func readInputFromStdinOrFile(cfg runtime.RuntimeConfig, commandFile string) ([]
var err error
if commandFile == "" {
reader := bufio.NewReader(cfg.In)
b, err = ioutil.ReadAll(reader)
b, err = io.ReadAll(reader)
} else {
b, err = cfg.FileSystem.ReadFile(commandFile)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/exec/builder/flags_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builder

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

Expand All @@ -13,7 +13,7 @@ import (
var testStep = TestStep{}

func TestFlags_UnmarshalYAML(t *testing.T) {
b, err := ioutil.ReadFile("testdata/flags-input.yaml")
b, err := os.ReadFile("testdata/flags-input.yaml")
require.NoError(t, err, "could not read the input file")

var flags Flags
Expand Down
4 changes: 2 additions & 2 deletions pkg/exec/builder/output_jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package builder

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestJsonPathOutputs(t *testing.T) {
{"string", "$[0].cpuPlatform", `Intel Haswell`},
}

stdout, err := ioutil.ReadFile("testdata/install-output.json")
stdout, err := os.ReadFile("testdata/install-output.json")
require.NoError(t, err, "could not read testdata")

for _, tc := range testcases {
Expand Down
6 changes: 3 additions & 3 deletions pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"sort"
"testing"

Expand All @@ -16,7 +16,7 @@ import (
)

func TestAction_UnmarshalYAML(t *testing.T) {
b, err := ioutil.ReadFile("testdata/install-input.yaml")
b, err := os.ReadFile("testdata/install-input.yaml")
require.NoError(t, err)

action := Action{}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestMixin_Uninstall(t *testing.T) {

func TestMixin_SuffixArgs(t *testing.T) {
ctx := context.Background()
b, err := ioutil.ReadFile("testdata/suffix-args-input.yaml")
b, err := os.ReadFile("testdata/suffix-args-input.yaml")
require.NoError(t, err, "ReadFile failed")

var action Action
Expand Down
4 changes: 2 additions & 2 deletions pkg/exec/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package exec
import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"

"get.porter.sh/porter/pkg"
Expand All @@ -19,7 +19,7 @@ func TestMixin_Execute(t *testing.T) {
err := m.Config.FileSystem.WriteFile("config.txt", []byte("abc123"), pkg.FileModeWritable)
require.NoError(t, err)

stdin, err := ioutil.ReadFile("testdata/outputs.yaml")
stdin, err := os.ReadFile("testdata/outputs.yaml")
require.NoError(t, err)
m.Config.In = bytes.NewBuffer(stdin)

Expand Down
4 changes: 2 additions & 2 deletions pkg/exec/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package exec
import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"

"get.porter.sh/porter/pkg/linter"
Expand All @@ -15,7 +15,7 @@ func TestMixin_Lint(t *testing.T) {
ctx := context.Background()
m := NewTestMixin(t)

input, err := ioutil.ReadFile("testdata/lint-input.yaml")
input, err := os.ReadFile("testdata/lint-input.yaml")
require.NoError(t, err, "could not read lint testdata")
m.Config.In = bytes.NewReader(input)

Expand Down
6 changes: 3 additions & 3 deletions pkg/exec/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exec

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

"github.com/ghodss/yaml" // We are not using go-yaml because of serialization problems with jsonschema, don't use this library elsewhere
Expand All @@ -17,7 +17,7 @@ func TestMixin_PrintSchema(t *testing.T) {
m.PrintSchema()
gotSchema := m.TestConfig.TestContext.GetOutput()

wantSchema, err := ioutil.ReadFile("schema/exec.json")
wantSchema, err := os.ReadFile("schema/exec.json")
require.NoError(t, err)

assert.Equal(t, string(wantSchema), gotSchema)
Expand All @@ -44,7 +44,7 @@ func TestMixin_ValidateSchema(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
// Read the mixin input as a go dump
mixinInputB, err := ioutil.ReadFile(tc.file)
mixinInputB, err := os.ReadFile(tc.file)
require.NoError(t, err)
mixinInputMap := make(map[string]interface{})
err = yaml.Unmarshal(mixinInputB, &mixinInputMap)
Expand Down
Loading

0 comments on commit 8ad1834

Please sign in to comment.