Skip to content

Commit

Permalink
Merge pull request #235 from paketo-buildpacks/ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikusa authored Apr 25, 2023
2 parents 4323db8 + fd8d6c5 commit 72c096e
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 251 deletions.
13 changes: 6 additions & 7 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package libpak_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -54,17 +53,17 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

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

builder = &mocks.Builder{}

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

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()
Expand All @@ -77,10 +76,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)

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

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

tomlWriter = &mocks.TOMLWriter{}
Expand All @@ -105,7 +104,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})

it("handles error from Builder", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
api = "0.6"
[buildpack]
Expand Down
5 changes: 2 additions & 3 deletions carton/build_image_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package carton

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

Expand Down Expand Up @@ -48,7 +47,7 @@ func (i BuildImageDependency) Update(options ...Option) {
logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity("Build Image", i.Version))

c, err := ioutil.ReadFile(i.BuilderPath)
c, err := os.ReadFile(i.BuilderPath)
if err != nil {
config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", i.BuilderPath, err))
return
Expand All @@ -64,7 +63,7 @@ func (i BuildImageDependency) Update(options ...Option) {
s := fmt.Sprintf(ImageDependencySubstitution, i.Version)
c = r.ReplaceAll(c, []byte(s))

if err := ioutil.WriteFile(i.BuilderPath, c, 0644); err != nil {
if err := os.WriteFile(i.BuilderPath, c, 0644); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", i.BuilderPath, err))
return
}
Expand Down
5 changes: 2 additions & 3 deletions carton/build_image_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package carton_test

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

Expand All @@ -43,7 +42,7 @@ func testBuildImageDependency(t *testing.T, context spec.G, it spec.S) {
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)

f, err := ioutil.TempFile("", "carton-image-dependency")
f, err := os.CreateTemp("", "carton-image-dependency")
Expect(err).NotTo(HaveOccurred())

_, err = f.WriteString(`test-prologue
Expand All @@ -67,7 +66,7 @@ test-epilogue

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(Equal([]byte(`test-prologue
Expect(os.ReadFile(path)).To(Equal([]byte(`test-prologue
build-image = "image-name:test-version-2"
test-epilogue
`)))
Expand Down
5 changes: 2 additions & 3 deletions carton/buildpack_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package carton
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"

Expand Down Expand Up @@ -81,7 +80,7 @@ func (b BuildpackDependency) Update(options ...Option) {
return
}

c, err := ioutil.ReadFile(b.BuildpackPath)
c, err := os.ReadFile(b.BuildpackPath)
if err != nil {
config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", b.BuildpackPath, err))
return
Expand Down Expand Up @@ -187,7 +186,7 @@ func (b BuildpackDependency) Update(options ...Option) {

c = append(comments, c...)

if err := ioutil.WriteFile(b.BuildpackPath, c, 0644); err != nil {
if err := os.WriteFile(b.BuildpackPath, c, 0644); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", b.BuildpackPath, err))
return
}
Expand Down
31 changes: 15 additions & 16 deletions carton/buildpack_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package carton_test

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

Expand All @@ -44,7 +43,7 @@ func testBuildpackDependency(t *testing.T, context spec.G, it spec.S) {
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)

f, err := ioutil.TempFile("", "carton-buildpack-dependency")
f, err := os.CreateTemp("", "carton-buildpack-dependency")
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).To(Succeed())
path = f.Name()
Expand All @@ -55,7 +54,7 @@ func testBuildpackDependency(t *testing.T, context spec.G, it spec.S) {
})

it("updates dependency", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.6"
Expect(os.WriteFile(path, []byte(`api = "0.6"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -81,7 +80,7 @@ stacks = [ "test-stack" ]

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.6"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.6"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -97,7 +96,7 @@ stacks = [ "test-stack" ]
})

it("updates dependency with purl & cpes", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.7"
Expect(os.WriteFile(path, []byte(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -129,7 +128,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -147,7 +146,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*
})

it("updates multiple dependencies with different versions", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.7"
Expect(os.WriteFile(path, []byte(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -189,7 +188,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -218,7 +217,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*
})

it("updates dependency with missing purl, still updates cpe", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.7"
Expect(os.WriteFile(path, []byte(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -249,7 +248,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -266,7 +265,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*
})

it("updates dependency with invalid purl, still updates cpe", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.7"
Expect(os.WriteFile(path, []byte(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -298,7 +297,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -316,7 +315,7 @@ cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*
})

it("updates dependency with invalid cpe, still updates purl", func() {
Expect(ioutil.WriteFile(path, []byte(`api = "0.7"
Expect(os.WriteFile(path, []byte(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -348,7 +347,7 @@ cpes = 1234

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7"
[buildpack]
id = "some-buildpack"
name = "Some Buildpack"
Expand All @@ -366,7 +365,7 @@ cpes = 1234
})

it("updates indented dependency", func() {
Expect(ioutil.WriteFile(path, []byte(`# it should preserve
Expect(os.WriteFile(path, []byte(`# it should preserve
# these comments
# exactly
Expand Down Expand Up @@ -396,7 +395,7 @@ version = "1.2.3"

d.Update(carton.WithExitHandler(exitHandler))

body, err := ioutil.ReadFile(path)
body, err := os.ReadFile(path)
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(HavePrefix(`# it should preserve
# these comments
Expand Down
5 changes: 2 additions & 3 deletions carton/lifecycle_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package carton

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

Expand Down Expand Up @@ -48,7 +47,7 @@ func (l LifecycleDependency) Update(options ...Option) {
logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity("Lifecycle", l.Version))

c, err := ioutil.ReadFile(l.BuilderPath)
c, err := os.ReadFile(l.BuilderPath)
if err != nil {
config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", l.BuilderPath, err))
return
Expand All @@ -64,7 +63,7 @@ func (l LifecycleDependency) Update(options ...Option) {
s := fmt.Sprintf(LifecycleDependencySubstitution, l.Version)
c = r.ReplaceAll(c, []byte(s))

if err := ioutil.WriteFile(l.BuilderPath, c, 0644); err != nil {
if err := os.WriteFile(l.BuilderPath, c, 0644); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", l.BuilderPath, err))
return
}
Expand Down
5 changes: 2 additions & 3 deletions carton/lifecycle_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package carton_test

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

Expand All @@ -43,7 +42,7 @@ func testLifecycleDependency(t *testing.T, context spec.G, it spec.S) {
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)

f, err := ioutil.TempFile("", "carton-builder-dependency")
f, err := os.CreateTemp("", "carton-builder-dependency")
Expect(err).NotTo(HaveOccurred())

_, err = f.WriteString(`test-prologue
Expand All @@ -70,7 +69,7 @@ test-epilogue

d.Update(carton.WithExitHandler(exitHandler))

Expect(ioutil.ReadFile(path)).To(Equal([]byte(`test-prologue
Expect(os.ReadFile(path)).To(Equal([]byte(`test-prologue
[lifecycle]
uri = "https://github.com/buildpacks/lifecycle/releases/download/vtest-version-3/lifecycle-vtest-version-3+linux.x86-64.tgz"
Expand Down
3 changes: 1 addition & 2 deletions carton/netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package carton

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/user"
Expand Down Expand Up @@ -48,7 +47,7 @@ func (n Netrc) BasicAuth(request *http.Request) (*http.Request, error) {
}

func ParseNetrc(path string) (Netrc, error) {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions carton/netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package carton_test

import (
"io/ioutil"
"net/http"
"os"
"os/user"
Expand All @@ -40,7 +39,7 @@ func testNetrc(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

f, err := ioutil.TempFile("", "netrc")
f, err := os.CreateTemp("", "netrc")
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).To(Succeed())
path = f.Name()
Expand Down Expand Up @@ -75,7 +74,7 @@ func testNetrc(t *testing.T, context spec.G, it spec.S) {

context("parse", func() {
it("parses one-liner", func() {
Expect(ioutil.WriteFile(path, []byte(`machine test-machine login test-login password test-password`), 0644)).To(Succeed())
Expect(os.WriteFile(path, []byte(`machine test-machine login test-login password test-password`), 0644)).To(Succeed())

Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{
{
Expand All @@ -87,7 +86,7 @@ func testNetrc(t *testing.T, context spec.G, it spec.S) {
})

it("parses multi-liner", func() {
Expect(ioutil.WriteFile(path, []byte(`
Expect(os.WriteFile(path, []byte(`
machine test-machine
login test-login
password test-password
Expand All @@ -103,7 +102,7 @@ password test-password
})

it("ignores macdef", func() {
Expect(ioutil.WriteFile(path, []byte(`
Expect(os.WriteFile(path, []byte(`
macdef uploadtest
cd /pub/tests
bin
Expand All @@ -123,7 +122,7 @@ machine test-machine login test-login password test-password
})

it("ignores all after default", func() {
Expect(ioutil.WriteFile(path, []byte(`
Expect(os.WriteFile(path, []byte(`
machine test-machine-1 login test-login-1 password test-password-1
default
Expand Down
Loading

0 comments on commit 72c096e

Please sign in to comment.