Skip to content

Commit

Permalink
Refactor to remove ioutil which is depercated
Browse files Browse the repository at this point in the history
- Also formats the imports
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Mar 11, 2021
1 parent 173f021 commit 5a60c46
Show file tree
Hide file tree
Showing 52 changed files with 265 additions and 304 deletions.
75 changes: 37 additions & 38 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package packit_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -35,18 +34,18 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
workingDir, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())

tmpDir, err = ioutil.TempDir("", "working-dir")
tmpDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

tmpDir, err = filepath.EvalSymlinks(tmpDir)
Expect(err).NotTo(HaveOccurred())

Expect(os.Chdir(tmpDir)).To(Succeed())

layersDir, err = ioutil.TempDir("", "layers")
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())

file, err := ioutil.TempFile("", "plan.toml")
file, err := os.CreateTemp("", "plan.toml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()

Expand All @@ -62,10 +61,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

planPath = file.Name()

cnbDir, err = ioutil.TempDir("", "cnb")
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())

envCnbDir, err = ioutil.TempDir("", "envCnb")
envCnbDir, err = os.MkdirTemp("", "envCnb")
Expect(err).NotTo(HaveOccurred())

bpTOML := []byte(`
Expand All @@ -76,8 +75,8 @@ api = "0.5"
version = "some-version"
clear-env = false
`)
Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())

binaryPath = filepath.Join(cnbDir, "bin", "build")

Expand Down Expand Up @@ -140,8 +139,8 @@ api = "0.4"
version = "some-version"
clear-env = false
`)
Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())

})

Expand All @@ -154,7 +153,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, "", "", planPath}))

contents, err := ioutil.ReadFile(planPath)
contents, err := os.ReadFile(planPath)
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand Down Expand Up @@ -202,7 +201,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "some-layer.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "some-layer.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -222,10 +221,10 @@ cache = true
it.Before(func() {
obsoleteLayerPath = filepath.Join(layersDir, "obsolete-layer")
Expect(os.MkdirAll(obsoleteLayerPath, os.ModePerm)).To(Succeed())
Expect(ioutil.WriteFile(obsoleteLayerPath+".toml", []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(obsoleteLayerPath+".toml", []byte{}, 0600)).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(layersDir, "launch.toml"), []byte{}, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersDir, "store.toml"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersDir, "launch.toml"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersDir, "store.toml"), []byte{}, 0600)).To(Succeed())
})

it("removes them", func() {
Expand Down Expand Up @@ -295,7 +294,7 @@ cache = true
it.Before(func() {
unremovableTOMLPath = filepath.Join(layersDir, "unremovable.toml")
Expect(os.MkdirAll(filepath.Join(layersDir, "unremovable"), os.ModePerm)).To(Succeed())
Expect(ioutil.WriteFile(unremovableTOMLPath, []byte{}, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(unremovableTOMLPath, []byte{}, os.ModePerm)).To(Succeed())
Expect(os.Chmod(layersDir, 0666)).To(Succeed())
})

Expand Down Expand Up @@ -335,7 +334,7 @@ cache = true
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "build.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "build.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -358,8 +357,8 @@ api = "0.4"
version = "some-version"
clear-env = false
`)
Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())

})
it("throws an error", func() {
Expand Down Expand Up @@ -403,7 +402,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "build.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "build.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -423,8 +422,8 @@ api = "0.4"
version = "some-version"
clear-env = false
`)
Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())

})

Expand Down Expand Up @@ -470,7 +469,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand Down Expand Up @@ -501,7 +500,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand Down Expand Up @@ -530,7 +529,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand Down Expand Up @@ -558,7 +557,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -581,7 +580,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -605,7 +604,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand All @@ -632,7 +631,7 @@ api = "0.4"
}, nil
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

contents, err := ioutil.ReadFile(filepath.Join(layersDir, "launch.toml"))
contents, err := os.ReadFile(filepath.Join(layersDir, "launch.toml"))
Expect(err).NotTo(HaveOccurred())

Expect(string(contents)).To(MatchTOML(`
Expand Down Expand Up @@ -679,7 +678,7 @@ api = "0.4"
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

for _, modifier := range []string{"append", "default", "delim", "prepend", "override"} {
contents, err := ioutil.ReadFile(filepath.Join(layersDir, "some-layer", "env", fmt.Sprintf("SOME_VAR.%s", modifier)))
contents, err := os.ReadFile(filepath.Join(layersDir, "some-layer", "env", fmt.Sprintf("SOME_VAR.%s", modifier)))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("%s-value", modifier)))
}
Expand All @@ -706,7 +705,7 @@ api = "0.4"
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

for _, modifier := range []string{"append", "default", "delim", "prepend", "override"} {
contents, err := ioutil.ReadFile(filepath.Join(layersDir, "some-layer", "env.launch", fmt.Sprintf("SOME_VAR.%s", modifier)))
contents, err := os.ReadFile(filepath.Join(layersDir, "some-layer", "env.launch", fmt.Sprintf("SOME_VAR.%s", modifier)))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("%s-value", modifier)))
}
Expand Down Expand Up @@ -740,7 +739,7 @@ api = "0.4"

for _, process := range []string{"process-name", "another-process-name"} {
for _, modifier := range []string{"append", "default", "delim", "prepend", "override"} {
contents, err := ioutil.ReadFile(filepath.Join(layersDir, "some-layer", "env.launch", process, fmt.Sprintf("SOME_VAR.%s", modifier)))
contents, err := os.ReadFile(filepath.Join(layersDir, "some-layer", "env.launch", process, fmt.Sprintf("SOME_VAR.%s", modifier)))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("%s-value", modifier)))
}
Expand Down Expand Up @@ -768,7 +767,7 @@ api = "0.4"
}, packit.WithArgs([]string{binaryPath, layersDir, "", planPath}))

for _, modifier := range []string{"append", "default", "delim", "prepend", "override"} {
contents, err := ioutil.ReadFile(filepath.Join(layersDir, "some-layer", "env.build", fmt.Sprintf("SOME_VAR.%s", modifier)))
contents, err := os.ReadFile(filepath.Join(layersDir, "some-layer", "env.build", fmt.Sprintf("SOME_VAR.%s", modifier)))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(fmt.Sprintf("%s-value", modifier)))
}
Expand All @@ -779,7 +778,7 @@ api = "0.4"
context("failure cases", func() {
context("when the buildpack plan.toml is malformed", func() {
it.Before(func() {
err := ioutil.WriteFile(planPath, []byte("%%%"), 0600)
err := os.WriteFile(planPath, []byte("%%%"), 0600)
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -804,7 +803,7 @@ api = "0.4"

context("when the buildpack.toml is malformed", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte("%%%"), 0600)
err := os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte("%%%"), 0600)
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -827,8 +826,8 @@ api = "0.4"
version = "some-version"
clear-env = false
`)
Expect(ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
Expect(os.Chmod(planPath, 0444)).To(Succeed())
})

Expand Down Expand Up @@ -893,7 +892,7 @@ api = "0.4"
var envDir string
it.Before(func() {
var err error
envDir, err = ioutil.TempDir("", "environment")
envDir, err = os.MkdirTemp("", "environment")
Expect(err).NotTo(HaveOccurred())

Expect(os.Chmod(envDir, 0000)).To(Succeed())
Expand Down
5 changes: 2 additions & 3 deletions cargo/buildpack_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cargo_test

import (
"io/ioutil"
"os"
"testing"
"time"
Expand All @@ -21,7 +20,7 @@ func testBuildpackParser(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
file, err := ioutil.TempFile("", "buildpack.toml")
file, err := os.CreateTemp("", "buildpack.toml")
Expect(err).NotTo(HaveOccurred())

_, err = file.WriteString(`api = "0.2"
Expand Down Expand Up @@ -103,7 +102,7 @@ pre-package = "some-pre-package-script.sh"

context("when the buildpack.toml is malformed", func() {
it.Before(func() {
Expect(ioutil.WriteFile(path, []byte("%%%"), 0644)).To(Succeed())
Expect(os.WriteFile(path, []byte("%%%"), 0644)).To(Succeed())
})

it("returns an error", func() {
Expand Down
14 changes: 7 additions & 7 deletions cargo/directory_duplicator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cargo_test

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -23,16 +23,16 @@ func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

sourceDir, err = ioutil.TempDir("", "source")
sourceDir, err = os.MkdirTemp("", "source")
Expect(err).NotTo(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(sourceDir, "some-file"), []byte("some content"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(sourceDir, "some-file"), []byte("some content"), 0644)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(sourceDir, "some-dir"), os.ModePerm)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(sourceDir, "some-dir", "other-file"), []byte("other content"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(sourceDir, "some-dir", "other-file"), []byte("other content"), 0755)).To(Succeed())
Expect(os.Symlink("other-file", filepath.Join(sourceDir, "some-dir", "link"))).To(Succeed())

destDir, err = ioutil.TempDir("", "dest")
destDir, err = os.MkdirTemp("", "dest")
Expect(err).NotTo(HaveOccurred())

directoryDup = cargo.NewDirectoryDuplicator()
Expand All @@ -50,7 +50,7 @@ func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) {
file, err := os.Open(filepath.Join(destDir, "some-file"))
Expect(err).NotTo(HaveOccurred())

content, err := ioutil.ReadAll(file)
content, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some content"))

Expand All @@ -67,7 +67,7 @@ func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) {
file, err = os.Open(filepath.Join(destDir, "some-dir", "other-file"))
Expect(err).NotTo(HaveOccurred())

content, err = ioutil.ReadAll(file)
content, err = io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("other content"))

Expand Down
10 changes: 5 additions & 5 deletions cargo/jam/commands/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package commands

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/paketo-buildpacks/packit/cargo"
"github.com/paketo-buildpacks/packit/cargo/jam/internal"
"github.com/paketo-buildpacks/packit/pexec"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

type packFlags struct {
Expand Down Expand Up @@ -56,7 +56,7 @@ func init() {
}

func packRun(flags packFlags) error {
buildpackDir, err := ioutil.TempDir("", "dup-dest")
buildpackDir, err := os.MkdirTemp("", "dup-dest")
if err != nil {
return fmt.Errorf("unable to create temporary directory: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cargo/jam/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/spf13/cobra"

var (
rootCmd = &cobra.Command{
Use: "jam",
Use: "jam",
}
)

Expand Down
Loading

0 comments on commit 5a60c46

Please sign in to comment.