Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tile name field for bake output filename #499

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions internal/acceptance/workflows/scenario/step_funcs_tile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"

"github.com/pivotal-cf/kiln/pkg/cargo"
Expand All @@ -23,12 +23,18 @@ import (

// aTileIsCreated asserts the output tile exists
func aTileIsCreated(ctx context.Context) error {
tilePath, err := defaultFilePathForTile(ctx)
p, err := tileRepoPath(ctx)
if err != nil {
return err
}
matches, err := filepath.Glob(filepath.Join(p, "*.pivotal"))
if err != nil {
return err
}
_, err = os.Stat(tilePath)
return err
if len(matches) == 0 {
return errors.New("no tile found")
}
return nil
}

// theTileContains checks that the filePaths exist in the tile
Expand Down
22 changes: 9 additions & 13 deletions internal/commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,23 @@ func (b *Bake) loadFlags(args []string) error {
}

if shouldReadVersionFile(b, args) {
fileInfo, err := b.fs.Stat("version")
// TODO: test this
if fileInfo != nil && err == nil {
var file File
file, err = b.fs.Open(fileInfo.Name())
if err != nil && file == nil {
return err
}
file, err := b.fs.Open("version")
if err == nil {
defer closeAndIgnoreError(file)

versionBuf := make([]byte, fileInfo.Size())
_, _ = file.Read(versionBuf)
versionBuf, _ := io.ReadAll(file)
b.Options.Version = strings.TrimSpace(string(versionBuf))
}
}

if shouldGenerateTileFileName(b, args) {
b.Options.OutputFile = "tile.pivotal"
prefix := "tile"
if b.Options.TileName != "" {
prefix = b.Options.TileName
}
if b.Options.Version != "" {
b.Options.OutputFile = "tile-" + b.Options.Version + ".pivotal"
prefix += "-" + b.Options.Version
}
b.Options.OutputFile = prefix + ".pivotal"
}

if shouldNotUseDefaultKilnfileFlag(args) {
Expand Down
88 changes: 75 additions & 13 deletions internal/commands/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"testing"
"time"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"

"github.com/pivotal-cf/kiln/pkg/cargo"

"github.com/pivotal-cf/kiln/pkg/bake"
Expand Down Expand Up @@ -51,7 +54,8 @@ var _ = Describe("Bake", func() {
fakeTileWriter *fakes.TileWriter
fakeChecksummer *fakes.Checksummer

fakeFilesystem *fakes.FileSystem
fakeFilesystem billy.Filesystem

fakeHomeDirFunc func() (string, error)

otherReleasesDirectory string
Expand Down Expand Up @@ -97,15 +101,20 @@ var _ = Describe("Bake", func() {
fakeJobsService = &fakes.MetadataTemplatesParser{}
fakePropertiesService = &fakes.MetadataTemplatesParser{}
fakeRuntimeConfigsService = &fakes.MetadataTemplatesParser{}
fakeFilesystem = &fakes.FileSystem{}
fakeVersionInfo := &fakes.FileInfo{}
fileVersion := "some-version"
fakeVersionInfo.SizeReturns(int64(len(fileVersion)))
fakeVersionInfo.NameReturns("version")
fakeFilesystem.StatReturns(fakeVersionInfo, nil)
result1 := &fakes.File{}
result1.ReadReturns(0, nil)
fakeFilesystem.OpenReturns(result1, nil)

{
fakeFilesystem = memfs.New()

for name, contents := range map[string]string{
"version": "1.2.3",
"/home/.kiln/credentials.yml": "",
} {
file, _ := fakeFilesystem.Create(name)
_, _ = file.Write([]byte(contents))
_ = file.Close()
}
}

fakeHomeDirFunc = func() (string, error) {
return "/home/", nil
}
Expand Down Expand Up @@ -226,9 +235,7 @@ var _ = Describe("Bake", func() {

Expect(fakeTemplateVariablesService.FromPathsAndPairsCallCount()).To(Equal(1))
varFiles, variables := fakeTemplateVariablesService.FromPathsAndPairsArgsForCall(0)
Expect(len(varFiles)).To(Equal(2))
Expect(varFiles[0]).To(Equal("some-variables-file"))
Expect(varFiles[1]).To(Equal("/home/.kiln/credentials.yml"))
Expect(varFiles).To(ConsistOf("some-variables-file", "/home/.kiln/credentials.yml"))
Expect(variables).To(Equal([]string{"some-variable=some-variable-value"}))

Expect(fakeBOSHVariablesService.ParseMetadataTemplatesCallCount()).To(Equal(1))
Expand Down Expand Up @@ -398,8 +405,49 @@ var _ = Describe("Bake", func() {
Expect(string(fakeBakeRecordFunc.productTemplate)).To(Equal("some-interpolated-metadata"), "it gives the bake recorder the product template")
})

Context("when the output flag is not set", func() {
When("no version is provided", func() {
It("uses the tile as the filename prefix", func() {
_ = fakeFilesystem.Remove("version")
err := bake.Execute([]string{
"--metadata", "some-metadata",
})
Expect(err).To(Not(HaveOccurred()))
Expect(fakeTileWriter.WriteCallCount()).To(Equal(1))
_, input := fakeTileWriter.WriteArgsForCall(0)
Expect(input.OutputFile).To(Equal(filepath.Join("tile.pivotal")))
})
})
When("the version flag is provided", func() {
It("uses the tile as the filename prefix", func() {
err := bake.Execute([]string{
"--metadata", "some-metadata",
"--version", "some-version",
})
Expect(err).To(Not(HaveOccurred()))
Expect(fakeTileWriter.WriteCallCount()).To(Equal(1))
_, input := fakeTileWriter.WriteArgsForCall(0)
Expect(input.OutputFile).To(Equal(filepath.Join("tile-some-version.pivotal")))
})
})
When("the tile name is provided", func() {
It("uses the tile as the filename prefix", func() {
err := bake.Execute([]string{
"--metadata", "some-metadata",
"--tile-name", "p-rune",
})
Expect(err).To(Not(HaveOccurred()))
Expect(fakeTileWriter.WriteCallCount()).To(Equal(1))
_, input := fakeTileWriter.WriteArgsForCall(0)
Expect(input.OutputFile).To(Equal(filepath.Join("p-rune-1.2.3.pivotal")))
})
})
})

Context("when bake configuration is in the Kilnfile", func() {
BeforeEach(func() {
kf, _ := fakeFilesystem.Create("Kilnfile")
_ = kf.Close()
bake = bake.WithKilnfileFunc(func(s string) (cargo.Kilnfile, error) {
return cargo.Kilnfile{
BakeConfigurations: []cargo.BakeConfiguration{
Expand All @@ -426,6 +474,8 @@ var _ = Describe("Bake", func() {
})
Context("when bake configuration has multiple options", func() {
BeforeEach(func() {
kf, _ := fakeFilesystem.Create("Kilnfile")
_ = kf.Close()
bake = bake.WithKilnfileFunc(func(s string) (cargo.Kilnfile, error) {
return cargo.Kilnfile{
BakeConfigurations: []cargo.BakeConfiguration{
Expand Down Expand Up @@ -586,6 +636,10 @@ var _ = Describe("Bake", func() {
})

Context("when Kilnfile is specified", func() {
BeforeEach(func() {
kf, _ := fakeFilesystem.Create("Kilnfile")
_ = kf.Close()
})
It("renders the stemcell criteria in tile metadata from that specified the Kilnfile.lock", func() {
outputFile := "some-output-dir/some-product-file-1.2.3-build.4"
err := bake.Execute([]string{
Expand Down Expand Up @@ -872,6 +926,10 @@ var _ = Describe("Bake", func() {
})

Context("when both the --kilnfile and --stemcells-directory are provided", func() {
BeforeEach(func() {
kf, _ := fakeFilesystem.Create("Kilnfile")
_ = kf.Close()
})
It("returns an error", func() {
err := bake.Execute([]string{
"--metadata", "some-metadata",
Expand All @@ -885,6 +943,10 @@ var _ = Describe("Bake", func() {

// todo: When --stemcell-tarball is removed, delete this test
Context("when both the --stemcell-tarball and --kilnfile are provided", func() {
BeforeEach(func() {
kf, _ := fakeFilesystem.Create("Kilnfile")
_ = kf.Close()
})
It("returns an error", func() {
err := bake.Execute([]string{
"--metadata", "some-metadata",
Expand Down
Loading