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

Log PHP Extensions during build #391

Closed
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ file that looks like the following:
version = "8.0.4"
```

To determine which extensions are packaged with this version of PHP, set `BP_LOG_LEVEL=DEBUG` and perform a build.

## Usage

To package this buildpack for consumption:
Expand Down
13 changes: 12 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,22 @@ func Build(dependencies DependencyManager,
return packit.BuildResult{}, err
}

logger.Debug.Subprocess("Finding PHP extensions directory")
extensionsDir, err := files.FindExtensions(phpLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}

logger.Debug.Subprocess("Found PHP extensions directory: %s", extensionsDir)

extensions, err := os.ReadDir(extensionsDir)
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Action("Listing PHP extensions:")
for _, ext := range extensions {
logger.Debug.Detail("- %s", ext.Name())
}

logger.Debug.Break()

libDir := "lib"
Expand Down
54 changes: 45 additions & 9 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"strings"
"testing"

"github.com/paketo-buildpacks/occam/matchers"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/sbom"

//nolint Ignore SA1019, informed usage of deprecated package
"github.com/paketo-buildpacks/packit/v2/paketosbom"
"github.com/paketo-buildpacks/packit/v2/postal"
Expand All @@ -30,6 +30,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
workingDir string
cnbDir string
extensionDir string
dependencyManager *fakes.DependencyManager
sbomGenerator *fakes.SBOMGenerator
files *fakes.FileManager
Expand All @@ -45,9 +46,15 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())

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

cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())

extensionDir, err = os.MkdirTemp("", "extension")
Expect(err).NotTo(HaveOccurred())

dependencyManager = &fakes.DependencyManager{}
dependencyManager.ResolveCall.Returns.Dependency = postal.Dependency{Name: "PHP"}
dependencyManager.GenerateBillOfMaterialsCall.Returns.BOMEntrySlice = []packit.BOMEntry{
Expand All @@ -68,17 +75,14 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
sbomGenerator.GenerateFromDependencyCall.Returns.SBOM = sbom.SBOM{}

files = &fakes.FileManager{}
files.FindExtensionsCall.Returns.String = "no-debug-non-zts-12345"
files.FindExtensionsCall.Returns.String = extensionDir
files.WriteConfigCall.Returns.DefaultConfig = "some/ini/path/php.ini"
files.WriteConfigCall.Returns.BuildpackConfig = "some/other/path/buildpack.ini"

clock = chronos.DefaultClock

environment = &fakes.EnvironmentConfiguration{}

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

buffer = bytes.NewBuffer(nil)
logEmitter := scribe.NewEmitter(buffer)

Expand All @@ -87,8 +91,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(extensionDir)).To(Succeed())
})

it("returns a result that builds correctly", func() {
Expand Down Expand Up @@ -153,7 +158,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
filepath.Join(layersDir, "php", "lib", "php"),
filepath.Join(workingDir, "lib"),
}, ":"),
ExtensionDir: "no-debug-non-zts-12345",
ExtensionDir: extensionDir,
}))

Expect(dependencyManager.GenerateBillOfMaterialsCall.Receives.Dependencies).To(Equal([]postal.Dependency{{Name: "PHP"}}))
Expand All @@ -162,7 +167,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

Expect(environment.ConfigureCall.CallCount).To(Equal(1))
Expect(environment.ConfigureCall.Receives.Layer.Path).To(Equal(filepath.Join(layersDir, "php")))
Expect(environment.ConfigureCall.Receives.ExtensionsDir).To(Equal("no-debug-non-zts-12345"))
Expect(environment.ConfigureCall.Receives.ExtensionsDir).To(Equal(extensionDir))
Expect(environment.ConfigureCall.Receives.DefaultIni).To(Equal("some/ini/path/php.ini"))
Expect(environment.ConfigureCall.Receives.ScanDirs).To(Equal([]string{
"some/ini/path",
Expand Down Expand Up @@ -210,7 +215,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
filepath.Join(layersDir, "php", "lib", "php"),
filepath.Join(workingDir, "user-lib-dir"),
}, ":"),
ExtensionDir: "no-debug-non-zts-12345",
ExtensionDir: extensionDir,
}))
})
})
Expand Down Expand Up @@ -408,6 +413,37 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
})

context("when there are extensions to log", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(extensionDir, "ext1.so"), []byte(""), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(extensionDir, "ext2.so"), []byte(""), os.ModePerm)).To(Succeed())

logEmitter := scribe.NewEmitter(buffer).WithLevel("DEBUG")

build = phpdist.Build(dependencyManager, files, environment, sbomGenerator, logEmitter, clock)
})

it.After(func() {
})

it("logs the extensions", func() {
_, err := build(packit.BuildContext{
CNBPath: cnbDir,
BuildpackInfo: packit.BuildpackInfo{},
Plan: packit.BuildpackPlan{},
Layers: packit.Layers{Path: layersDir},
})

Expect(err).NotTo(HaveOccurred())
Expect(buffer).To(matchers.ContainLines(
MatchRegexp(`\s*Found PHP extensions directory: %s`, extensionDir),
MatchRegexp(`\s*Listing PHP extensions:`),
MatchRegexp(`\s*- ext1.so`),
MatchRegexp(`\s*- ext2.so`),
))
})
})

context("failure cases", func() {
context("when a dependency cannot be resolved", func() {
it.Before(func() {
Expand Down
2 changes: 1 addition & 1 deletion integration/simple_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func testSimpleApp(t *testing.T, context spec.G, it spec.S) {
" application/vnd.syft+json",
))
Expect(logs).To(ContainLines(
" Finding PHP extensions directory",
MatchRegexp(`\s*Found PHP extensions directory:.*`),
))
Expect(logs).To(ContainLines(
" Generating default PHP configuration",
Expand Down
2 changes: 1 addition & 1 deletion php_file_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/paketo-buildpacks/packit/v2/fs"
)

// PHPIniConfig represents the data that will be inserted in a templated
// PhpIniConfig represents the data that will be inserted in a templated
// php.ini file.
type PhpIniConfig struct {
IncludePath string
Expand Down