mirror of
https://github.com/pestphp/pest.git
synced 2026-09-05 06:13:35 +02:00
fix: fail missing snapshots in CI (#1842)
Prevent snapshot assertions from silently creating missing snapshots during CI runs unless --update-snapshots is enabled.
This commit is contained in:
@@ -734,6 +734,12 @@ final class Expectation
|
||||
};
|
||||
|
||||
if (! $snapshots->has()) {
|
||||
if (! Snapshot::shouldCreateMissingSnapshots()) {
|
||||
$filename = $snapshots->filename();
|
||||
|
||||
Assert::fail($message === '' ? "Snapshot is missing at [$filename]. Run Pest with --update-snapshots to create it." : $message);
|
||||
}
|
||||
|
||||
$filename = $snapshots->save($string);
|
||||
|
||||
TestSuite::getInstance()->registerSnapshotChange("Snapshot created at [$filename]");
|
||||
|
||||
@@ -16,6 +16,36 @@ final class Snapshot implements HandlesArguments
|
||||
|
||||
public static bool $updateSnapshots = false;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private const array CI_ENVIRONMENT_VARIABLES = [
|
||||
'CI',
|
||||
'GITHUB_ACTIONS',
|
||||
'GITLAB_CI',
|
||||
'CIRCLECI',
|
||||
'TRAVIS',
|
||||
'APPVEYOR',
|
||||
'BITBUCKET_BUILD_NUMBER',
|
||||
'BUILDKITE',
|
||||
'TEAMCITY_VERSION',
|
||||
'JENKINS_URL',
|
||||
'SYSTEM_COLLECTIONURI',
|
||||
'CI_NAME',
|
||||
'TASKCLUSTER_ROOT_URL',
|
||||
'DRONE',
|
||||
'WERCKER',
|
||||
'NEVERCODE',
|
||||
'SEMAPHORE',
|
||||
'NETLIFY',
|
||||
'NOW_BUILDER',
|
||||
];
|
||||
|
||||
public static function shouldCreateMissingSnapshots(): bool
|
||||
{
|
||||
return self::$updateSnapshots || ! self::runningOnCI();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@@ -119,4 +149,19 @@ final class Snapshot implements HandlesArguments
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function runningOnCI(): bool
|
||||
{
|
||||
if (Environment::name() === Environment::CI) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (self::CI_ENVIRONMENT_VARIABLES as $environmentVariable) {
|
||||
if (getenv($environmentVariable) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,12 @@ final class SnapshotRepository
|
||||
|
||||
file_put_contents($snapshotFilename, $snapshot);
|
||||
|
||||
return str_replace(dirname($this->testsPath).'/', '', $snapshotFilename);
|
||||
return $this->filename();
|
||||
}
|
||||
|
||||
public function filename(): string
|
||||
{
|
||||
return str_replace(dirname($this->testsPath).'/', '', $this->getSnapshotFilename());
|
||||
}
|
||||
|
||||
public function flush(): void
|
||||
|
||||
Reference in New Issue
Block a user