Skip to content

Commit

Permalink
fix: use full import paths from vendor for new composer (#163)
Browse files Browse the repository at this point in the history
Fixes #130: use full import paths from vendor for new composer

Replace vendor/bin/router.php paths with the full router.php path in vendor/. Starting with composer 2.2, which is in package repos, composer changed the way it was handling the bin directive by creating wrapper files with shebangs in vendor/bin rather than symlinks. This caused our (incorrect) use of these bin/ files to break when they're used as a script from php because php expects the first entry in the file to be <?php when passed a script.

The composer in the FF buildpacks is old enough that it doesn't introduce the shebang which is why it still works.

I'm introducing tests to ensure we don't break that until we're safely upgraded the FF buildpack to use the correct router.
  • Loading branch information
josephlewis42 authored Sep 26, 2023
1 parent 5b645ee commit fb92e9e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ run the following commands:

```sh
export FUNCTION_TARGET=helloHttp
php -S localhost:8080 vendor/bin/router.php
php -S localhost:8080 vendor/google/cloud-functions-framework/router.php
```

Open `http://localhost:8080/` in your browser and see *Hello World from a PHP HTTP function!*.
Expand Down Expand Up @@ -231,7 +231,7 @@ variable to `cloudevent`.
```sh
export FUNCTION_TARGET=helloCloudEvent
export FUNCTION_SIGNATURE_TYPE=cloudevent
php -S localhost:8080 vendor/bin/router.php
php -S localhost:8080 vendor/google/cloud-functions-framework/router.php
```

In a separate tab, make a cURL request in Cloud Event format to your function:
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /srv/
#
# We configure it to use the `router.php` file included in this package.
RUN mkdir .googleconfig && \
echo '{"entrypointContents": "serve vendor/bin/router.php"}' > .googleconfig/app_start.json
echo '{"entrypointContents": "serve vendor/google/cloud-functions-framework/router.php"}' > .googleconfig/app_start.json

# Copy over composer files and run "composer install"
COPY composer.* ./
Expand Down
56 changes: 51 additions & 5 deletions tests/vendorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testDefaultFunctionSource(): void
'FUNCTION_SOURCE=' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
' php %s/vendor/google/cloud-functions-framework/router.php',
self::$tmpDir
);
exec($cmd, $output);
Expand All @@ -72,7 +72,7 @@ public function testRelativeFunctionSource(): void
'FUNCTION_SOURCE=relative.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
' php %s/vendor/google/cloud-functions-framework/router.php',
self::$tmpDir
);
exec($cmd, $output);
Expand All @@ -87,7 +87,7 @@ public function testAbsoluteFunctionSource(): void
'FUNCTION_SOURCE=%s/absolute.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
' php %s/vendor/google/cloud-functions-framework/router.php',
self::$tmpDir,
self::$tmpDir
);
Expand All @@ -103,7 +103,7 @@ public function testGcsIsNotRegistered(): void
'FUNCTION_SOURCE=%s/gcs.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
' php %s/vendor/google/cloud-functions-framework/router.php',
self::$tmpDir,
self::$tmpDir
);
Expand All @@ -124,12 +124,58 @@ public function testGcsIsRegistered(): void
'FUNCTION_SOURCE=%s/gcs.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
' php %s/vendor/google/cloud-functions-framework/router.php',
self::$tmpDir,
self::$tmpDir
);
exec($cmd, $output);

$this->assertEquals(['GCS Stream Wrapper is registered'], $output);
}

public function testLegacyVendorBinDefaultFunctionSource(): void
{
copy(__DIR__ . '/fixtures/index.php', self::$tmpDir . '/index.php');
$cmd = sprintf(
'FUNCTION_SOURCE=' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
self::$tmpDir
);
exec($cmd, $output);

$this->assertSame(['Hello Default!'], $output);
}

public function testLegacyVendorBinRelativeFunctionSource(): void
{
copy(__DIR__ . '/fixtures/relative.php', self::$tmpDir . '/relative.php');
$cmd = sprintf(
'FUNCTION_SOURCE=relative.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
self::$tmpDir
);
exec($cmd, $output);

$this->assertSame(['Hello Relative!'], $output);
}

public function testLegacyVendorBinAbsoluteFunctionSource(): void
{
copy(__DIR__ . '/fixtures/absolute.php', self::$tmpDir . '/absolute.php');
$cmd = sprintf(
'FUNCTION_SOURCE=%s/absolute.php' .
' FUNCTION_SIGNATURE_TYPE=http' .
' FUNCTION_TARGET=helloDefault' .
' php %s/vendor/bin/router.php',
self::$tmpDir,
self::$tmpDir
);
exec($cmd, $output);

$this->assertSame(['Hello Absolute!'], $output);
}
}

0 comments on commit fb92e9e

Please sign in to comment.