Skip to content

Commit

Permalink
replace ioutil package calls with os
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore authored and ryanmoran committed Jan 31, 2022
1 parent aaf56fa commit f5d668e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 40 deletions.
13 changes: 6 additions & 7 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bundler_test
import (
"bytes"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -42,13 +41,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
var err error
layersDir, err = ioutil.TempDir("", "layers")
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())

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

err = ioutil.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte(`api = "0.2"
err = os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), []byte(`api = "0.2"
[buildpack]
id = "org.some-org.some-buildpack"
name = "Some Buildpack"
Expand Down Expand Up @@ -268,7 +267,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
var err error
workingDir, err = ioutil.TempDir("", "working-dir")
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Expand Down Expand Up @@ -353,7 +352,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
var err error
workingDir, err = ioutil.TempDir("", "working-dir")
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

entryResolver.ResolveCall.Returns.BuildpackPlanEntry = packit.BuildpackPlanEntry{
Expand Down Expand Up @@ -437,7 +436,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
entryResolver.MergeLayerTypesCall.Returns.Launch = false
entryResolver.MergeLayerTypesCall.Returns.Build = true

err := ioutil.WriteFile(filepath.Join(layersDir, "bundler.toml"), []byte("[metadata]\ndependency-sha = \"some-sha\"\n"), 0600)
err := os.WriteFile(filepath.Join(layersDir, "bundler.toml"), []byte("[metadata]\ndependency-sha = \"some-sha\"\n"), 0600)
Expect(err).NotTo(HaveOccurred())

dependencyManager.ResolveCall.Returns.Dependency = postal.Dependency{
Expand Down
5 changes: 2 additions & 3 deletions buildpack_yml_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bundler_test

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

Expand All @@ -20,7 +19,7 @@ func testBuildpackYMLParser(t *testing.T, context spec.G, it spec.S) {
)

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

Expand Down Expand Up @@ -76,7 +75,7 @@ bundler:

context("when the contents of the buildpack.yml file are malformed", func() {
it.Before(func() {
err := ioutil.WriteFile(path, []byte("%%%"), 0644)
err := os.WriteFile(path, []byte("%%%"), 0644)
Expect(err).NotTo(HaveOccurred())
})

Expand Down
5 changes: 2 additions & 3 deletions gemfile_lock_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bundler_test

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

Expand All @@ -20,7 +19,7 @@ func testGemfileLockParser(t *testing.T, context spec.G, it spec.S) {
)

it.Before(func() {
file, err := ioutil.TempFile("", "Gemfile.lock")
file, err := os.CreateTemp("", "Gemfile.lock")
Expect(err).NotTo(HaveOccurred())
defer file.Close()

Expand Down Expand Up @@ -83,7 +82,7 @@ BUNDLED WITH

context("when the bundler version is not valid semver", func() {
it.Before(func() {
err := ioutil.WriteFile(path, []byte(`GEM
err := os.WriteFile(path, []byte(`GEM
remote: https://rubygems.org/
specs:
Expand Down
19 changes: 4 additions & 15 deletions integration/reuse_layer_rebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package integration_test

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -153,16 +151,8 @@ func testReusingLayerRebuild(t *testing.T, context spec.G, it spec.S) {
containerIDs[secondContainer.ID] = struct{}{}

Eventually(secondContainer).Should(BeAvailable())

response, err := http.Get(fmt.Sprintf("http://localhost:%s", secondContainer.HostPort("8080")))
Expect(err).NotTo(HaveOccurred())
Expect(response.StatusCode).To(Equal(http.StatusOK))

content, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())

Expect(string(content)).To(ContainSubstring(fmt.Sprintf("/layers/%s/bundler/bin/bundler", strings.ReplaceAll(settings.Buildpack.ID, "/", "_"))))
Expect(string(content)).To(MatchRegexp(`Bundler version 2\.\d+\.\d+`))
Eventually(secondContainer).Should(Serve(ContainSubstring(fmt.Sprintf("/layers/%s/bundler/bin/bundler", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")))).OnPort(8080))
Eventually(secondContainer).Should(Serve(MatchRegexp(`Bundler version 2\.\d+\.\d+`)))

Expect(secondImage.Buildpacks[1].Layers["bundler"].Metadata["built_at"]).To(Equal(firstImage.Buildpacks[1].Layers["bundler"].Metadata["built_at"]))
})
Expand Down Expand Up @@ -232,11 +222,11 @@ func testReusingLayerRebuild(t *testing.T, context spec.G, it spec.S) {

Eventually(firstContainer).Should(BeAvailable())

contents, err := ioutil.ReadFile(filepath.Join(source, "Gemfile.lock"))
contents, err := os.ReadFile(filepath.Join(source, "Gemfile.lock"))
Expect(err).NotTo(HaveOccurred())

re := regexp.MustCompile(`BUNDLED WITH\s+\d+\.\d+\.\d+`)
err = ioutil.WriteFile(filepath.Join(source, "Gemfile.lock"), re.ReplaceAll(contents, []byte("BUNDLED WITH\n 2.1.4")), 0644)
err = os.WriteFile(filepath.Join(source, "Gemfile.lock"), re.ReplaceAll(contents, []byte("BUNDLED WITH\n 2.1.4")), 0644)
Expect(err).NotTo(HaveOccurred())

// Second pack build
Expand Down Expand Up @@ -279,7 +269,6 @@ func testReusingLayerRebuild(t *testing.T, context spec.G, it spec.S) {

containerIDs[secondContainer.ID] = struct{}{}

Eventually(secondContainer).Should(BeAvailable())
Eventually(secondContainer).Should(Serve(ContainSubstring(fmt.Sprintf("/layers/%s/bundler/bin/bundler", strings.ReplaceAll(settings.Buildpack.ID, "/", "_")))).OnPort(8080))
Eventually(secondContainer).Should(Serve(MatchRegexp(`Bundler version 2\.\d+\.\d+`)).OnPort(8080))

Expand Down
3 changes: 1 addition & 2 deletions version_shimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundler

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

Expand Down Expand Up @@ -50,7 +49,7 @@ func (s VersionShimmer) Shim(dir, version string) error {

content := fmt.Sprintf(VersionShimTemplate, original, version)

err = ioutil.WriteFile(file, []byte(content), 0755)
err = os.WriteFile(file, []byte(content), 0755)
if err != nil {
return fmt.Errorf("failed to rewrite bundler executables: %w", err)
}
Expand Down
19 changes: 9 additions & 10 deletions version_shimmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundler_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -23,16 +22,16 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
var err error
dir, err = ioutil.TempDir("", "bin")
dir, err = os.MkdirTemp("", "bin")
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(filepath.Join(dir, "first"), []byte("first"), 0755)
err = os.WriteFile(filepath.Join(dir, "first"), []byte("first"), 0755)
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(filepath.Join(dir, "second"), []byte("second"), 0755)
err = os.WriteFile(filepath.Join(dir, "second"), []byte("second"), 0755)
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(filepath.Join(dir, "third"), []byte("third"), 0644)
err = os.WriteFile(filepath.Join(dir, "third"), []byte("third"), 0644)
Expect(err).NotTo(HaveOccurred())

err = os.Mkdir(filepath.Join(dir, "fourth"), os.ModePerm)
Expand Down Expand Up @@ -66,7 +65,7 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
defer first.Close()

content, err := ioutil.ReadAll(first)
content, err := os.ReadFile(first.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("first"))

Expand All @@ -78,7 +77,7 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
defer firstShim.Close()

content, err = ioutil.ReadAll(firstShim)
content, err = os.ReadFile(firstShim.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(fmt.Sprintf("#!/usr/bin/env sh\nexec %s _some-version_ ${@:-}", filepath.Join(dir, "_first"))))

Expand All @@ -90,7 +89,7 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
defer second.Close()

content, err = ioutil.ReadAll(second)
content, err = os.ReadFile(second.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("second"))

Expand All @@ -102,7 +101,7 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
defer secondShim.Close()

content, err = ioutil.ReadAll(secondShim)
content, err = os.ReadFile(secondShim.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(fmt.Sprintf("#!/usr/bin/env sh\nexec %s _some-version_ ${@:-}", filepath.Join(dir, "_second"))))

Expand All @@ -114,7 +113,7 @@ func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
defer third.Close()

content, err = ioutil.ReadAll(third)
content, err = os.ReadFile(third.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("third"))

Expand Down

0 comments on commit f5d668e

Please sign in to comment.