Skip to content

Commit

Permalink
Merge pull request #279 from acoulton/4.x-deprecate-version
Browse files Browse the repository at this point in the history
change: Deprecate Gherkin::VERSION constant and change file cache namespacing
  • Loading branch information
carlos-granados authored Dec 24, 2024
2 parents 51ddede + 3ea6101 commit 70cf6bf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
],

"require": {
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*"
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
"composer-runtime-api": "^2.2"
},

"require-dev": {
"symfony/filesystem": "^5.4 || ^6.4 || ^7.0",
"symfony/yaml": "^5.4 || ^6.4 || ^7.0",
"phpunit/phpunit": "^10.5",
"cucumber/cucumber": "dev-gherkin-24.1.0",
Expand Down
15 changes: 13 additions & 2 deletions src/Behat/Gherkin/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Behat\Gherkin\Cache;

use Behat\Gherkin\Exception\CacheException;
use Behat\Gherkin\Gherkin;
use Behat\Gherkin\Node\FeatureNode;
use Composer\InstalledVersions;

/**
* File cache.
Expand All @@ -24,6 +24,17 @@ class FileCache implements CacheInterface
{
private $path;

/**
* Used as part of the cache directory path to invalidate cache if the installed package version changes.
*/
private static function getGherkinVersionHash(): string
{
$version = InstalledVersions::getVersion('behat/gherkin');

// Composer version strings can contain arbitrary content so hash for filesystem safety
return md5($version);
}

/**
* Initializes file cache.
*
Expand All @@ -33,7 +44,7 @@ class FileCache implements CacheInterface
*/
public function __construct($path)
{
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'v' . Gherkin::VERSION;
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::getGherkinVersionHash();

if (!is_dir($this->path)) {
@mkdir($this->path, 0777, true);
Expand Down
5 changes: 5 additions & 0 deletions src/Behat/Gherkin/Gherkin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
class Gherkin
{
/**
* @deprecated this constant will not be updated for releases after 4.8.0 and will be removed in the next major.
* You can use composer's runtime API to get the behat version if you need it. Note that composer's versions will
* not always be simple numeric values.
*/
public const VERSION = '4.8.0';

/**
Expand Down
20 changes: 14 additions & 6 deletions tests/Behat/Gherkin/Cache/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

use Behat\Gherkin\Cache\FileCache;
use Behat\Gherkin\Exception\CacheException;
use Behat\Gherkin\Gherkin;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\ScenarioNode;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

class FileCacheTest extends TestCase
{
Expand Down Expand Up @@ -58,9 +58,19 @@ public function testCacheAndRead(): void

public function testBrokenCacheRead(): void
{
// First, write a valid cache and find the file that was written
$this->cache->write(
'broken_feature',
new FeatureNode(null, null, [], null, [], null, null, null, null),
);
$files = glob($this->path . '/**/*.feature.cache');
$this->assertCount(1, $files, 'Cache should have written a single file');

// Now simulate the file being corrupted and attempt to read it
file_put_contents($files[0], '');

$this->expectException(CacheException::class);

touch($this->path . '/v' . Gherkin::VERSION . '/' . md5('broken_feature') . '.feature.cache');
$this->cache->read('broken_feature');
}

Expand All @@ -77,13 +87,11 @@ public function testUnwriteableCacheDir(): void

protected function setUp(): void
{
$this->cache = new FileCache($this->path = sys_get_temp_dir() . '/gherkin-test');
$this->cache = new FileCache($this->path = sys_get_temp_dir() . uniqid('/gherkin-test'));
}

protected function tearDown(): void
{
foreach (glob($this->path . '/*.feature.cache') as $file) {
unlink((string) $file);
}
(new Filesystem())->remove($this->path);
}
}

0 comments on commit 70cf6bf

Please sign in to comment.