Skip to content

Commit

Permalink
Refactoring to remove ioutil and custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Jun 25, 2021
1 parent 24ab9d9 commit 142763c
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 100 deletions.
6 changes: 3 additions & 3 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/scribe"
)

//go:generate faux --interface BuildProcess --output fakes/build_process.go
Expand All @@ -29,7 +30,7 @@ func Build(
buildProcess BuildProcess,
pathManager PathManager,
clock chronos.Clock,
logs LogEmitter,
logs scribe.Emitter,
sourceRemover SourceRemover,
) packit.BuildFunc {

Expand Down Expand Up @@ -104,8 +105,7 @@ func Build(
})
}

logs.Process("Assigning launch processes")
logs.ListProcesses(processes)
logs.LaunchProcesses(processes)

return packit.BuildResult{
Layers: []packit.Layer{targetsLayer, goCacheLayer},
Expand Down
11 changes: 5 additions & 6 deletions build_configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gobuild_test

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -28,7 +27,7 @@ func testBuildConfigurationParser(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())

targetManager = &fakes.TargetManager{}
Expand Down Expand Up @@ -208,7 +207,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {

context("when there is a buildpack.yml and environment variables are not set", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

targetManager.CleanAndValidateCall.Returns.StringSlice = []string{"./first", "./second"}
Expand All @@ -234,7 +233,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {

context("when there is a buildpack.yml and environment variables are set", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

os.Setenv("BP_GO_BUILD_IMPORT_PATH", "./some/import/path")
Expand Down Expand Up @@ -269,7 +268,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {

context("buildpack.yml specifies flags including -ldflags and BP_GO_BUILD_LDFLAGS is set", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

buildpackYMLParser.ParseCall.Returns.BuildConfiguration = gobuild.BuildConfiguration{
Expand Down Expand Up @@ -319,7 +318,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {

context("buildpack.yml parsing fails", func() {
it.Before(func() {
err := ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

buildpackYMLParser.ParseCall.Returns.Error = errors.New("failed to parse buildpack.yml")
Expand Down
14 changes: 7 additions & 7 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gobuild_test
import (
"bytes"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/paketo-buildpacks/go-build/fakes"
"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
Expand All @@ -38,13 +38,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())

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

buildProcess = &fakes.BuildProcess{}
Expand Down Expand Up @@ -75,7 +75,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
buildProcess,
pathManager,
clock,
gobuild.NewLogEmitter(logs),
scribe.NewEmitter(logs),
sourceRemover,
)
})
Expand Down Expand Up @@ -242,7 +242,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
context("failure cases", func() {
context("when the targets layer cannot be retrieved", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(layersDir, "targets.toml"), nil, 0000)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersDir, "targets.toml"), nil, 0000)).To(Succeed())
})

it("returns an error", func() {
Expand All @@ -263,7 +263,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("when the gocache layer cannot be retrieved", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(layersDir, "gocache.toml"), nil, 0000)).To(Succeed())
Expect(os.WriteFile(filepath.Join(layersDir, "gocache.toml"), nil, 0000)).To(Succeed())
})

it("returns an error", func() {
Expand Down
2 changes: 1 addition & 1 deletion buildpack.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
api = "0.4"
api = "0.5"

[buildpack]
homepage = "https://github.com/paketo-buildpacks/go-build"
Expand Down
5 changes: 3 additions & 2 deletions go_build_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/pexec"
"github.com/paketo-buildpacks/packit/scribe"
)

//go:generate faux --interface Executable --output fakes/executable.go
Expand All @@ -32,11 +33,11 @@ type GoBuildConfiguration struct {

type GoBuildProcess struct {
executable Executable
logs LogEmitter
logs scribe.Emitter
clock chronos.Clock
}

func NewGoBuildProcess(executable Executable, logs LogEmitter, clock chronos.Clock) GoBuildProcess {
func NewGoBuildProcess(executable Executable, logs scribe.Emitter, clock chronos.Clock) GoBuildProcess {
return GoBuildProcess{
executable: executable,
logs: logs,
Expand Down
16 changes: 8 additions & 8 deletions go_build_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/paketo-buildpacks/go-build/fakes"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/pexec"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
Expand All @@ -38,16 +38,16 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {

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

workspacePath, err = ioutil.TempDir("", "workspace")
workspacePath, err = os.MkdirTemp("", "workspace")
Expect(err).NotTo(HaveOccurred())

goPath, err = ioutil.TempDir("", "go-path")
goPath, err = os.MkdirTemp("", "go-path")
Expect(err).NotTo(HaveOccurred())

goCache, err = ioutil.TempDir("", "gocache")
goCache, err = os.MkdirTemp("", "gocache")
Expect(err).NotTo(HaveOccurred())

logs = bytes.NewBuffer(nil)
Expand Down Expand Up @@ -77,7 +77,7 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {
return t
})

buildProcess = gobuild.NewGoBuildProcess(executable, gobuild.NewLogEmitter(logs), clock)
buildProcess = gobuild.NewGoBuildProcess(executable, scribe.NewEmitter(logs), clock)
})

it.After(func() {
Expand Down Expand Up @@ -134,7 +134,7 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {

context("when there are build flags", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.Mkdir(filepath.Join(workspacePath, "vendor"), os.ModePerm)).To(Succeed())
})

Expand Down Expand Up @@ -181,7 +181,7 @@ func testGoBuildProcess(t *testing.T, context spec.G, it spec.S) {

context("when the GOPATH is empty", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.Mkdir(filepath.Join(workspacePath, "vendor"), os.ModePerm)).To(Succeed())
})

Expand Down
5 changes: 3 additions & 2 deletions go_buildpack_yml_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/Masterminds/semver"
"github.com/buildkite/interpolate"
"github.com/paketo-buildpacks/packit/scribe"
"gopkg.in/yaml.v2"
)

type GoBuildpackYMLParser struct {
logger LogEmitter
logger scribe.Emitter
}

func NewGoBuildpackYMLParser(logger LogEmitter) GoBuildpackYMLParser {
func NewGoBuildpackYMLParser(logger scribe.Emitter) GoBuildpackYMLParser {
return GoBuildpackYMLParser{
logger: logger,
}
Expand Down
16 changes: 8 additions & 8 deletions go_buildpack_yml_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package gobuild_test

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

gobuild "github.com/paketo-buildpacks/go-build"
"github.com/paketo-buildpacks/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
Expand All @@ -25,10 +25,10 @@ func testGoBuildpackYMLParser(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())

Expect(ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
go:
targets:
- first
Expand All @@ -42,7 +42,7 @@ go:
`), 0644)).To(Succeed())

logs = bytes.NewBuffer(nil)
goBuildpackYMLParser = gobuild.NewGoBuildpackYMLParser(gobuild.NewLogEmitter(logs))
goBuildpackYMLParser = gobuild.NewGoBuildpackYMLParser(scribe.NewEmitter(logs))
})

it.After(func() {
Expand Down Expand Up @@ -70,7 +70,7 @@ go:

context("when the flags have an env var in them", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
go:
build:
flags:
Expand Down Expand Up @@ -104,7 +104,7 @@ go:

context("when the buildpack.yml does not contain go configuration", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
not-go:
build:
flags:
Expand Down Expand Up @@ -136,7 +136,7 @@ not-go:

context("buildpack.yml fails to parse", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`%%%`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`%%%`), 0644)).To(Succeed())
})

it("returns an error", func() {
Expand All @@ -148,7 +148,7 @@ not-go:

context("when a the env var interpolation fails", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
Expect(os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), []byte(`---
go:
build:
flags:
Expand Down
3 changes: 1 addition & 2 deletions go_path_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gobuild

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

Expand All @@ -25,7 +24,7 @@ func (m GoPathManager) Setup(workspace, importPath string) (string, string, erro
return "", workspace, nil
}

path, err := ioutil.TempDir(m.tempDir, "gopath")
path, err := os.MkdirTemp(m.tempDir, "gopath")
if err != nil {
return "", "", fmt.Errorf("failed to setup GOPATH: %w", err)
}
Expand Down
11 changes: 5 additions & 6 deletions go_path_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gobuild_test

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

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

pathManager = gobuild.NewGoPathManager(tempDir)
Expand All @@ -37,10 +36,10 @@ func testGoPathManager(t *testing.T, context spec.G, it spec.S) {

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

Expect(ioutil.WriteFile(filepath.Join(workspacePath, "some-file"), nil, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workspacePath, "some-file"), nil, 0644)).To(Succeed())
})

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

context("when the workspace contains a go.mod file", func() {
it.Before(func() {
Expect(ioutil.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workspacePath, "go.mod"), nil, 0644)).To(Succeed())
})

it("does not setup a GOPATH", func() {
Expand Down Expand Up @@ -110,7 +109,7 @@ func testGoPathManager(t *testing.T, context spec.G, it spec.S) {

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

Expand Down
Loading

0 comments on commit 142763c

Please sign in to comment.