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

Check and enable platform reqs before running composer install #332

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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: 7 additions & 7 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func Build(
workspaceVendorDir = filepath.Join(context.WorkingDir, value)
}

err = runCheckAndEnablePlatformReqs(logger, checkPlatformReqsExec, context.WorkingDir, composerPhpIniPath, path)
if err != nil {
return packit.BuildResult{}, err
}

var composerPackagesLayer packit.Layer
logger.Process("Executing build process")
duration, err := clock.Measure(func() error {
Expand Down Expand Up @@ -121,11 +126,6 @@ func Build(
return packit.BuildResult{}, err
}

err = runCheckPlatformReqs(logger, checkPlatformReqsExec, context.WorkingDir, composerPhpIniPath, path)
if err != nil {
return packit.BuildResult{}, err
}

return packit.BuildResult{
Layers: []packit.Layer{
composerPackagesLayer,
Expand Down Expand Up @@ -407,7 +407,7 @@ extension = openssl.so`, os.Getenv(PhpExtensionDir))
return composerPhpIniPath, os.WriteFile(composerPhpIniPath, []byte(phpIni), os.ModePerm)
}

// runCheckPlatformReqs will run Composer command `check-platform-reqs`
// runCheckAndEnablePlatformReqs will run Composer command `check-platform-reqs`
// to see which platform requirements are "missing".
// https://getcomposer.org/doc/03-cli.md#check-platform-reqs
//
Expand All @@ -421,7 +421,7 @@ extension = openssl.so`, os.Getenv(PhpExtensionDir))
// https://github.com/paketo-buildpacks/php-composer/blob/5e2604b74cbeb30090bf7eadb1cfc158b374efc0/composer/composer.go#L76-L100
//
// In case you are curious about exit code 2: https://getcomposer.org/doc/03-cli.md#process-exit-codes
func runCheckPlatformReqs(logger scribe.Emitter, checkPlatformReqsExec Executable, workingDir, composerPhpIniPath, path string) error {
func runCheckAndEnablePlatformReqs(logger scribe.Emitter, checkPlatformReqsExec Executable, workingDir, composerPhpIniPath, path string) error {

args := []string{"check-platform-reqs"}
logger.Process("Running 'composer %s'", strings.Join(args, " "))
Expand Down
28 changes: 14 additions & 14 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
composerConfigExecutable *fakes.Executable
composerInstallExecutable *fakes.Executable
composerGlobalExecutable *fakes.Executable
composerCheckPlatformReqsExecExecutable *fakes.Executable
composerCheckAndEnablePlatformReqsExecExecutable *fakes.Executable
composerConfigExecution pexec.Execution
composerInstallExecution pexec.Execution
composerGlobalExecution pexec.Execution
composerCheckPlatformReqsExecExecution pexec.Execution
composerCheckAndEnablePlatformReqsExecExecution pexec.Execution
sbomGenerator *fakes.SBOMGenerator
calculator *fakes.Calculator

Expand All @@ -59,7 +59,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
composerConfigExecutable = &fakes.Executable{}
composerInstallExecutable = &fakes.Executable{}
composerGlobalExecutable = &fakes.Executable{}
composerCheckPlatformReqsExecExecutable = &fakes.Executable{}
composerCheckAndEnablePlatformReqsExecExecutable = &fakes.Executable{}

composerConfigExecutable.ExecuteCall.Stub = func(temp pexec.Execution) error {
Expect(fmt.Fprint(temp.Stdout, "stdout from composer config\n")).To(Equal(28))
Expand All @@ -84,8 +84,8 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
return nil
}

composerCheckPlatformReqsExecExecutable.ExecuteCall.Stub = func(temp pexec.Execution) error {
composerCheckPlatformReqsExecExecution = temp
composerCheckAndEnablePlatformReqsExecExecutable.ExecuteCall.Stub = func(temp pexec.Execution) error {
composerCheckAndEnablePlatformReqsExecExecution = temp

_, err := temp.Stdout.Write([]byte(`ext-hello 8.1.4 missing
ext-foo 8.1.4 success
Expand Down Expand Up @@ -117,7 +117,7 @@ php 8.1.4 success
composerConfigExecutable,
composerInstallExecutable,
composerGlobalExecutable,
composerCheckPlatformReqsExecExecutable,
composerCheckAndEnablePlatformReqsExecExecutable,
sbomGenerator,
"fake-path-from-tests",
calculator,
Expand Down Expand Up @@ -539,11 +539,11 @@ composer-lock-sha = "sha-from-composer-lock"
})
Expect(err).NotTo(HaveOccurred())

Expect(composerCheckPlatformReqsExecExecution.Args[0]).To(Equal("check-platform-reqs"))
Expect(composerCheckPlatformReqsExecExecution.Dir).To(Equal(workingDir))
Expect(len(composerCheckPlatformReqsExecExecution.Env)).To(Equal(len(os.Environ()) + 3))
Expect(composerCheckAndEnablePlatformReqsExecExecution.Args[0]).To(Equal("check-platform-reqs"))
Expect(composerCheckAndEnablePlatformReqsExecExecution.Dir).To(Equal(workingDir))
Expect(len(composerCheckAndEnablePlatformReqsExecExecution.Env)).To(Equal(len(os.Environ()) + 3))

Expect(composerCheckPlatformReqsExecExecution.Env).To(ContainElements(
Expect(composerCheckAndEnablePlatformReqsExecExecution.Env).To(ContainElements(
"COMPOSER_NO_INTERACTION=1",
fmt.Sprintf("PHPRC=%s", filepath.Join(layersDir, "composer-php-ini", "composer-php.ini")),
"PATH=fake-path-from-tests"))
Expand Down Expand Up @@ -625,11 +625,11 @@ extension = bar.so
})
})

context("when composerCheckPlatformReqsExecution fails", func() {
context("when composerCheckAndEnablePlatformReqsExecution fails", func() {
it.Before(func() {
composerCheckPlatformReqsExecExecutable.ExecuteCall.Stub = func(temp pexec.Execution) error {
composerCheckPlatformReqsExecExecution = temp
_, _ = fmt.Fprint(composerCheckPlatformReqsExecExecution.Stderr, "error message from check-platform-reqs")
composerCheckAndEnablePlatformReqsExecExecutable.ExecuteCall.Stub = func(temp pexec.Execution) error {
composerCheckAndEnablePlatformReqsExecExecution = temp
_, _ = fmt.Fprint(composerCheckAndEnablePlatformReqsExecExecution.Stderr, "error message from check-platform-reqs")
return errors.New("some error from check-platform-reqs")
}
})
Expand Down
98 changes: 49 additions & 49 deletions integration/testdata/with_extensions/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading