Skip to content

Commit

Permalink
Merge branch 'fix/1081-1108-buildpack-duplicates' of github.com:build…
Browse files Browse the repository at this point in the history
…packs/pack into fix/1081-1108-buildpack-duplicates
  • Loading branch information
dwillist committed Apr 9, 2021
2 parents 5cbe23f + 9b11b44 commit 12b5d22
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 17 deletions.
51 changes: 41 additions & 10 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,47 @@ func testWithoutSpecificBuilderRequirement(
})

when("--format file", func() {
it("creates the package", func() {
packageTomlPath := generatePackageTomlWithOS(t, assert, pack, tmpDir, simplePackageConfigFixtureName, imageManager.HostOS())
destinationFile := filepath.Join(tmpDir, "package.cnb")
output := pack.RunSuccessfully(
"buildpack", "package", destinationFile,
"--format", "file",
"-c", packageTomlPath,
)
assertions.NewOutputAssertionManager(t, output).ReportsPackageCreation(destinationFile)
h.AssertTarball(t, destinationFile)
when("the file extension is .cnb", func() {
it("creates the package with the same extension", func() {
packageTomlPath := generatePackageTomlWithOS(t, assert, pack, tmpDir, simplePackageConfigFixtureName, imageManager.HostOS())
destinationFile := filepath.Join(tmpDir, "package.cnb")
output := pack.RunSuccessfully(
"buildpack", "package", destinationFile,
"--format", "file",
"-c", packageTomlPath,
)
assertions.NewOutputAssertionManager(t, output).ReportsPackageCreation(destinationFile)
h.AssertTarball(t, destinationFile)
})
})
when("the file extension is empty", func() {
it("creates the package with a .cnb extension", func() {
packageTomlPath := generatePackageTomlWithOS(t, assert, pack, tmpDir, simplePackageConfigFixtureName, imageManager.HostOS())
destinationFile := filepath.Join(tmpDir, "package")
expectedFile := filepath.Join(tmpDir, "package.cnb")
output := pack.RunSuccessfully(
"buildpack", "package", destinationFile,
"--format", "file",
"-c", packageTomlPath,
)
assertions.NewOutputAssertionManager(t, output).ReportsPackageCreation(expectedFile)
h.AssertTarball(t, expectedFile)
})
})
when("the file extension is not .cnb", func() {
it("creates the package with the given extension but shows a warning", func() {
packageTomlPath := generatePackageTomlWithOS(t, assert, pack, tmpDir, simplePackageConfigFixtureName, imageManager.HostOS())
destinationFile := filepath.Join(tmpDir, "package.tar.gz")
output := pack.RunSuccessfully(
"buildpack", "package", destinationFile,
"--format", "file",
"-c", packageTomlPath,
)
assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.ReportsPackageCreation(destinationFile)
assertOutput.ReportsInvalidExtension(".gz")
h.AssertTarball(t, destinationFile)
})
})
})

Expand Down
6 changes: 6 additions & 0 deletions acceptance/assertions/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func (o OutputAssertionManager) ReportsPackageCreation(name string) {
o.assert.ContainsF(o.output, "Successfully created package '%s'", name)
}

func (o OutputAssertionManager) ReportsInvalidExtension(extension string) {
o.testObject.Helper()

o.assert.ContainsF(o.output, "'%s' is not a valid extension for a packaged buildpack. Packaged buildpacks must have a '.cnb' extension", extension)
}

func (o OutputAssertionManager) ReportsPackagePublished(name string) {
o.testObject.Helper()

Expand Down
16 changes: 13 additions & 3 deletions internal/commands/buildpack_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, client Buildpack
Use: "package <name> --config <config-path>",
Short: "Package a buildpack in OCI format.",
Args: cobra.ExactValidArgs(1),
Example: "pack buildpack package my-buildpack --config ./package.toml",
Example: "pack buildpack package my-buildpack --config ./package.toml\npack buildpack package my-buildpack.cnb --config ./package.toml --f file",
Long: "buildpack package allows users to package (a) buildpack(s) into OCI format, which can then to be hosted in " +
"image repositories. You can also package a number of buildpacks together, to enable easier distribution of " +
"a set of buildpacks. Packaged buildpacks can be used as inputs to `pack build` (using the `--buildpack` flag), " +
"image repositories or persisted on disk as a '.cnb' file. You can also package a number of buildpacks " +
"together, to enable easier distribution of a set of buildpacks. " +
"Packaged buildpacks can be used as inputs to `pack build` (using the `--buildpack` flag), " +
"and they can be included in the configs used in `pack builder create` and `pack buildpack package`. For more " +
"on how to package a buildpack, see: https://buildpacks.io/docs/buildpack-author-guide/package-a-buildpack/.",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -82,6 +83,15 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, client Buildpack
}
}
name := args[0]
if flags.Format == pack.FormatFile {
switch ext := filepath.Ext(name); ext {
case pack.CNBExtension:
case "":
name += pack.CNBExtension
default:
logger.Warnf("%s is not a valid extension for a packaged buildpack. Packaged buildpacks must have a %s extension", style.Symbol(ext), style.Symbol(pack.CNBExtension))
}
}
if err := client.PackageBuildpack(cmd.Context(), pack.PackageBuildpackOptions{
RelativeBaseDir: relativeBaseDir,
Name: name,
Expand Down
34 changes: 34 additions & 0 deletions internal/commands/buildpack_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ func testPackageCommand(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, receivedOptions.Config, myConfig)
})

when("file format", func() {
when("extension is .cnb", func() {
it("does not modify the name", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager))
cmd.SetArgs([]string{"test.cnb", "-f", "file"})
h.AssertNil(t, cmd.Execute())

receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Name, "test.cnb")
})
})
when("extension is empty", func() {
it("appends .cnb to the name", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager))
cmd.SetArgs([]string{"test", "-f", "file"})
h.AssertNil(t, cmd.Execute())

receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Name, "test.cnb")
})
})
when("extension is something other than .cnb", func() {
it("does not modify the name but shows a warning", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager), withLogger(logger))
cmd.SetArgs([]string{"test.tar.gz", "-f", "file"})
h.AssertNil(t, cmd.Execute())

receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Name, "test.tar.gz")
h.AssertContains(t, outBuf.String(), "'.gz' is not a valid extension for a packaged buildpack. Packaged buildpacks must have a '.cnb' extension")
})
})
})

when("there is a path flag", func() {
it("returns an error saying that it cannot be used with the config flag", func() {
myConfig := pubbldpkg.Config{
Expand Down
6 changes: 2 additions & 4 deletions new_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
)

var (
bashBinBuild = `
#!/usr/bin/env bash
bashBinBuild = `#!/usr/bin/env bash
set -euo pipefail
Expand All @@ -26,8 +25,7 @@ plan_path="$3"
exit 0
`
bashBinDetect = `
#!/usr/bin/env bash
bashBinDetect = `#!/usr/bin/env bash
exit 0
`
Expand Down
3 changes: 3 additions & 0 deletions package_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const (

// Packaging indicator that format of output will be a file on the host filesystem.
FormatFile = "file"

// CNBExtension is the file extension for a cloud native buildpack tar archive
CNBExtension = ".cnb"
)

// PackageBuildpackOptions is a configuration object used to define
Expand Down

0 comments on commit 12b5d22

Please sign in to comment.