Skip to content

Commit

Permalink
Use text/template instead of strings.Replace
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Mikusa <[email protected]>
  • Loading branch information
Daniel Mikusa committed Apr 20, 2021
1 parent 9785aec commit f26c239
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package libcnb_test

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"text/template"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
Expand All @@ -49,6 +50,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersPath string
platformPath string
tomlWriter *mocks.TOMLWriter
buildpackTOML *template.Template

workingDir string
)
Expand All @@ -68,7 +70,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())

bpTOMLContents = `
api = "##API_VERSION##"
api = "{{.APIVersion}}"
[buildpack]
id = "test-id"
Expand Down Expand Up @@ -99,10 +101,14 @@ mixins = ["test-name"]
[metadata]
test-key = "test-value"
`
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(strings.Replace(bpTOMLContents, "##API_VERSION##", "0.6", 1)),
0644),
).To(Succeed())
buildpackTOML, err = template.New("buildpack.toml").Parse(bpTOMLContents)
Expect(err).ToNot(HaveOccurred())

var b bytes.Buffer
err = buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.6"})
Expect(err).ToNot(HaveOccurred())

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

f, err := ioutil.TempFile("", "build-buildpackplan-path")
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -197,7 +203,7 @@ version = "1.1.1"
)

Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(
"this version of libcnb is only compatible with buildpack API 0.5 and 0.6",
"this version of libcnb is only compatible with buildpack APIs 0.5 and 0.6",
))
})
})
Expand Down Expand Up @@ -394,10 +400,11 @@ version = "1.1.1"
})

it("writes 0.5 layer metadata", func() {
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(strings.Replace(bpTOMLContents, "##API_VERSION##", "0.5", 1)),
0644),
).To(Succeed())
var b bytes.Buffer
err := buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.5"})
Expect(err).ToNot(HaveOccurred())

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

layer := libcnb.Layer{
Name: "test-name",
Expand Down

0 comments on commit f26c239

Please sign in to comment.