Skip to content
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
29 changes: 19 additions & 10 deletions src/Hook/Composer/Action/CheckLockFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ class CheckLockFile implements Action
*/
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$path = $action->getOptions()->get('path', getcwd());
$name = $action->getOptions()->get('name', 'composer');
$pathname = $path . DIRECTORY_SEPARATOR . $name;
$lockFileHash = $this->getLockFileHash($pathname . '.lock');
$configFileHash = $this->getConfigFileHash($pathname . '.json');
$path = $action->getOptions()->get('path', getcwd());
$name = $action->getOptions()->get('name', 'composer');
$pathname = $path . DIRECTORY_SEPARATOR . $name;
$lockFileHash = $this->getLockFileHash($pathname . '.lock');
$configFileHashes = $this->getConfigFileHashes($pathname . '.json');

if ($lockFileHash !== $configFileHash) {
if (!in_array($lockFileHash, $configFileHashes, true)) {
throw new Exception('Your composer.lock file is out of date');
}
}
Expand All @@ -88,16 +88,18 @@ private function getLockFileHash(string $composerLock): string
}

/**
* Read the composer.json file and create a md5 hash on its relevant content
* Create current and pre-2.11 Composer content hashes from composer.json.
*
* Legacy hashes omit config.policy and cannot detect policy-only changes.
*
* This more or less is composer internal code to generate the content-hash so this might not be the best idea
* and will be removed in the future.
*
* @param string $composerJson
* @return string
* @return array<string>
* @throws \Exception
*/
private function getConfigFileHash(string $composerJson): string
private function getConfigFileHashes(string $composerJson): array
{
$content = json_decode($this->loadFile($composerJson), true);
$relevantContent = [];
Expand All @@ -109,8 +111,15 @@ private function getConfigFileHash(string $composerJson): string
$relevantContent['config']['platform'] = $content['config']['platform'];
}
ksort($relevantContent);
$hashes = [md5((string)json_encode($relevantContent))];

if (isset($content['config']['policy'])) {
$relevantContent['config']['policy'] = $content['config']['policy'];
ksort($relevantContent);
$hashes[] = md5((string)json_encode($relevantContent));
}

return md5((string)json_encode($relevantContent));
return $hashes;
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/Hook/Composer/Action/CheckLockFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use CaptainHook\App\Console\IO\NullIO;
use CaptainHook\App\Mockery;
use Exception;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class CheckLockFileTest extends TestCase
Expand All @@ -36,6 +38,60 @@ public function testExecute(): void
$this->assertTrue(true);
}

#[DataProvider('policyConfigurations')]
public function testExecuteWithPolicy(
bool $withPlatform,
bool $changedPolicy,
string $lockHash,
bool $outdated
): void {
$composerConfig = ['policy' => ['advisories' => ['block' => $changedPolicy]]];
if ($withPlatform) {
$composerConfig['platform'] = ['php' => '8.0.0'];
}
$directory = vfsStream::setup('composer', null, [
'composer.json' => json_encode(['config' => $composerConfig]),
'composer.lock' => json_encode(['content-hash' => $lockHash]),
]);
$action = new Config\Action(CheckLockFile::class, ['path' => $directory->url()]);

if ($outdated) {
$this->expectExceptionMessage('Your composer.lock file is out of date');
$this->expectException(Exception::class);
}

$standard = new CheckLockFile();
$standard->execute(
new Config(CH_PATH_FILES . '/captainhook.json'),
new NullIO(),
$this->createRepositoryMock(),
$action
);

$this->assertTrue(true);
}

/**
* @return array<string, array{bool, bool, string, bool}>
*/
public static function policyConfigurations(): array
{
return [
'policy only' => [false, false, '9c8aef63892fcbcf3f78ce06dca9c8e4', false],
'policy and platform' => [true, false, 'e5d31e87b36c3dd2f424a4585c07a01e', false],
'changed policy only' => [false, true, '9c8aef63892fcbcf3f78ce06dca9c8e4', true],
'changed policy with platform' => [true, true, 'e5d31e87b36c3dd2f424a4585c07a01e', true],
'legacy policy only' => [false, false, 'd751713988987e9331980363e24189ce', false],
'legacy policy and platform' => [true, false, 'e1263a071d7944e34ef234afdc44ba57', false],
'legacy changed policy only' => [false, true, 'd751713988987e9331980363e24189ce', false],
'legacy changed policy with platform' => [true, true, 'e1263a071d7944e34ef234afdc44ba57', false],
'added platform' => [true, false, '9c8aef63892fcbcf3f78ce06dca9c8e4', true],
'removed platform' => [false, false, 'e5d31e87b36c3dd2f424a4585c07a01e', true],
'legacy added platform' => [true, false, 'd751713988987e9331980363e24189ce', true],
'legacy removed platform' => [false, false, 'e1263a071d7944e34ef234afdc44ba57', true],
];
}

public function testExecuteFail(): void
{
$this->expectException(Exception::class);
Expand Down
Loading