Skip to content

Commit

Permalink
Remove usage of ioutil/*
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Mikusa <dan@mikusa.com>
  • Loading branch information
dmikusa committed Apr 12, 2023
1 parent fb77fe5 commit 0f3d436
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 162 deletions.
63 changes: 29 additions & 34 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package libcnb_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -58,15 +57,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

applicationPath, err = ioutil.TempDir("", "build-application-path")
Expect(err).NotTo(HaveOccurred())
applicationPath = t.TempDir()
applicationPath, err = filepath.EvalSymlinks(applicationPath)
Expect(err).NotTo(HaveOccurred())

builder = &mocks.Builder{}

buildpackPath, err = ioutil.TempDir("", "build-buildpack-path")
Expect(err).NotTo(HaveOccurred())
buildpackPath = t.TempDir()
Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())

bpTOMLContents = `
Expand Down Expand Up @@ -108,14 +105,14 @@ test-key = "test-value"
err = buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.6"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())

f, err := ioutil.TempFile("", "build-buildpackplan-path")
f, err := os.CreateTemp("", "build-buildpackplan-path")
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).NotTo(HaveOccurred())
buildpackPlanPath = f.Name()

Expect(ioutil.WriteFile(buildpackPlanPath,
Expect(os.WriteFile(buildpackPlanPath,
[]byte(`
[[entries]]
name = "test-name"
Expand All @@ -137,26 +134,24 @@ test-key = "test-value"

layerContributor = &mocks.LayerContributor{}

layersPath, err = ioutil.TempDir("", "build-layers-path")
Expect(err).NotTo(HaveOccurred())
layersPath = t.TempDir()

Expect(ioutil.WriteFile(filepath.Join(layersPath, "store.toml"),
Expect(os.WriteFile(filepath.Join(layersPath, "store.toml"),
[]byte(`
[metadata]
test-key = "test-value"
`),
0600),
).To(Succeed())

platformPath, err = ioutil.TempDir("", "build-platform-path")
Expect(err).NotTo(HaveOccurred())
platformPath = t.TempDir()

Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
Expect(os.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
[]byte("test-secret-value"), 0600)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
Expect(os.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
To(Succeed())

tomlWriter = &mocks.TOMLWriter{}
Expand Down Expand Up @@ -189,7 +184,7 @@ test-key = "test-value"

context("buildpack API is not within the supported range", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.4"
Expand Down Expand Up @@ -221,7 +216,7 @@ version = "1.1.1"
envVar := e
context(fmt.Sprintf("when %s is unset", envVar), func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.8"
Expand Down Expand Up @@ -276,7 +271,7 @@ version = "1.1.1"

context("when BP API >= 0.8", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.8"
Expand Down Expand Up @@ -526,7 +521,7 @@ version = "1.1.1"
err := buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.5"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())

layer := libcnb.Layer{
Name: "test-name",
Expand Down Expand Up @@ -695,7 +690,7 @@ version = "1.1.1"
err := buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.8"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{
Processes: []libcnb.Process{
{
Expand Down Expand Up @@ -754,9 +749,9 @@ version = "1.1.1"
})

it("removes stale layers", func() {
Expect(ioutil.WriteFile(filepath.Join(layersPath, "alpha.toml"), []byte(""), 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "bravo.toml"), []byte(""), 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "store.toml"), []byte(""), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "alpha.toml"), []byte(""), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "bravo.toml"), []byte(""), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "store.toml"), []byte(""), 0600)).To(Succeed())

layer := libcnb.Layer{Name: "alpha"}
layerContributor.On("Contribute", mock.Anything).Return(layer, nil)
Expand Down Expand Up @@ -832,7 +827,7 @@ version = "1.1.1"
err = buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.7"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
})

it("writes launch.toml without BOM entries", func() {
Expand Down Expand Up @@ -917,7 +912,7 @@ version = "1.1.1"

context("Validates SBOM entries", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.7"
Expand All @@ -944,7 +939,7 @@ sbom-formats = ["application/vnd.cyclonedx+json"]
})

it("has no accepted formats", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.7"
Expand All @@ -957,7 +952,7 @@ sbom-formats = []
0600),
).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
Expand All @@ -969,7 +964,7 @@ sbom-formats = []
})

it("skips if API is not 0.7", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.6"
Expand All @@ -982,7 +977,7 @@ sbom-formats = []
0600),
).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
Expand All @@ -994,7 +989,7 @@ sbom-formats = []
})

it("has no matching formats", func() {
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
Expand All @@ -1006,8 +1001,8 @@ sbom-formats = []
})

it("has a matching format", func() {
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "launch.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
Expand All @@ -1018,8 +1013,8 @@ sbom-formats = []
})

it("has a junk format", func() {
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.random.json"), []byte{}, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "launch.sbom.random.json"), []byte{}, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
Expand Down
23 changes: 10 additions & 13 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package libcnb_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -50,16 +49,15 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

applicationPath, err = ioutil.TempDir("", "detect-application-path")
Expect(err).NotTo(HaveOccurred())
applicationPath = t.TempDir()
applicationPath, err = filepath.EvalSymlinks(applicationPath)
Expect(err).NotTo(HaveOccurred())

buildpackPath, err = ioutil.TempDir("", "detect-buildpack-path")
buildpackPath = t.TempDir()
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.6"
Expand Down Expand Up @@ -89,7 +87,7 @@ test-key = "test-value"
0600),
).To(Succeed())

f, err := ioutil.TempFile("", "detect-buildplan-path")
f, err := os.CreateTemp("", "detect-buildplan-path")
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).NotTo(HaveOccurred())
buildPlanPath = f.Name()
Expand All @@ -103,15 +101,14 @@ test-key = "test-value"
exitHandler.On("Fail")
exitHandler.On("Pass")

platformPath, err = ioutil.TempDir("", "detect-platform-path")
Expect(err).NotTo(HaveOccurred())
platformPath = t.TempDir()

Expect(os.MkdirAll(filepath.Join(platformPath, "bindings", "alpha"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
Expect(os.WriteFile(filepath.Join(platformPath, "bindings", "alpha", "test-secret-key"),
[]byte("test-secret-value"), 0600)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(platformPath, "env"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
Expect(os.WriteFile(filepath.Join(platformPath, "env", "TEST_ENV"), []byte("test-value"), 0600)).
To(Succeed())

tomlWriter = &mocks.TOMLWriter{}
Expand Down Expand Up @@ -141,7 +138,7 @@ test-key = "test-value"

context("buildpack API is not within the supported range", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.4"
Expand Down Expand Up @@ -195,7 +192,7 @@ version = "1.1.1"
envVar := e
context(fmt.Sprintf("when %s is unset", envVar), func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.8"
Expand Down Expand Up @@ -224,7 +221,7 @@ version = "1.1.1"

context("when BP API >= 0.8", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.8"
Expand Down
17 changes: 3 additions & 14 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package libcnb_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -39,13 +38,7 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
var err error
app, err = ioutil.TempDir("", "application-path-formatter")
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(os.RemoveAll(app)).To(Succeed())
app = t.TempDir()
})

it("lists empty directory contents", func() {
Expand All @@ -67,9 +60,7 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
var err error
bp, err = ioutil.TempDir("", "buildpack-path-formatter")
Expect(err).NotTo(HaveOccurred())
bp = t.TempDir()
})

it.After(func() {
Expand All @@ -95,9 +86,7 @@ func testFormatter(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
var err error
plat.Path, err = ioutil.TempDir("", "platform-formatter")
Expect(err).NotTo(HaveOccurred())
plat.Path = t.TempDir()
})

it.After(func() {
Expand Down
3 changes: 1 addition & 2 deletions internal/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package internal

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -45,7 +44,7 @@ func NewConfigMapFromPath(path string) (ConfigMap, error) {
} else if stat.IsDir() {
continue
}
contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to read file %s\n%w", file, err)
}
Expand Down

0 comments on commit 0f3d436

Please sign in to comment.