diff --git a/internal/commands/bake.go b/internal/commands/bake.go index 0d1bca10..3b582a24 100644 --- a/internal/commands/bake.go +++ b/internal/commands/bake.go @@ -437,7 +437,7 @@ func (b Bake) Execute(args []string) error { // TODO remove when stemcell tarball is deprecated stemcellManifest, err = b.stemcell.FromTarball(b.Options.StemcellTarball) } else if b.Options.Kilnfile != "" { - if err := bakeArgumentsFromKilnfileConfiguration(&b.Options, templateVariables); err != nil { + if err := BakeArgumentsFromKilnfileConfiguration(&b.Options, templateVariables); err != nil { return fmt.Errorf("failed to parse releases: %s", err) } templateVariables, err = b.templateVariables.FromPathsAndPairs(b.Options.VariableFiles, b.Options.Variables) @@ -590,7 +590,7 @@ func (b Bake) Usage() jhanda.Usage { } } -func bakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[string]any) error { +func BakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[string]any) error { if options.Kilnfile == "" { return nil } @@ -610,7 +610,7 @@ func bakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[ } if tileName, ok := variables[builder.TileNameVariable]; ok { name, ok := tileName.(string) - if ok { + if !ok { return fmt.Errorf("%s value must be a string got value %#[2]v with type %[2]T", builder.TileNameVariable, tileName) } if index := slices.IndexFunc(kf.BakeConfigurations, func(configuration cargo.BakeConfiguration) bool { diff --git a/internal/commands/bake_test.go b/internal/commands/bake_test.go index 1a36baea..a9c022a8 100644 --- a/internal/commands/bake_test.go +++ b/internal/commands/bake_test.go @@ -20,6 +20,7 @@ import ( "github.com/pivotal-cf/kiln/internal/builder" "github.com/pivotal-cf/kiln/internal/commands" "github.com/pivotal-cf/kiln/internal/commands/fakes" + "github.com/pivotal-cf/kiln/internal/commands/flags" "github.com/pivotal-cf/kiln/pkg/proofing" "github.com/pivotal-cf/kiln/pkg/source" ) @@ -940,6 +941,77 @@ var _ = Describe("Bake", func() { }) }) +var _ = Describe("BakeArgumentsFromKilnfileConfiguration", func() { + It("handles empty options and variables", func() { + opts := &commands.BakeOptions{} + variables := map[string]any{} + + err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables) + Expect(err).NotTo(HaveOccurred()) + }) + + It("handles a Kilnfile that does not exist", func() { + opts := &commands.BakeOptions{ + Standard: flags.Standard{ + Kilnfile: "/does/not/exist", + }, + } + variables := map[string]any{} + + err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables) + Expect(err).NotTo(HaveOccurred()) + }) + + const validKilnfile = `--- +release_sources: + - type: s3 + compiled: true + bucket: bucket + region: region + access_key_id: access_key_id + secret_access_key: secret_access_key + path_template: path_template +` + When("passing a valid Kilnfile", func() { + var opts *commands.BakeOptions + + BeforeEach(func() { + tempDir, err := os.MkdirTemp("", "bake_") + Expect(err).NotTo(HaveOccurred()) + + path := filepath.Join(tempDir, "Kilnfile") + Expect(os.WriteFile(path, []byte(validKilnfile), 0o644)).ToNot(HaveOccurred()) + + opts = &commands.BakeOptions{ + Standard: flags.Standard{ + Kilnfile: path, + }, + } + }) + + It("handles empty variables", func() { + variables := map[string]any{} + + err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables) + Expect(err).NotTo(HaveOccurred()) + }) + + It("handles a valid tile_name variable", func() { + variables := map[string]any{"tile_name": "SRT"} + + err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables) + Expect(err).NotTo(HaveOccurred()) + }) + + It("returns an error for unexpected tile_name type", func() { + variables := map[string]any{"tile_name": 8675309} + + err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables) + Expect(err).To(MatchError("tile_name value must be a string got value 8675309 with type int")) + }) + }) +}) + type fakeWriteBakeRecordFunc struct { tilePath, recordPath string productTemplate []byte