Skip to content

Commit 70cf6bf

Browse files
Merge pull request #279 from acoulton/4.x-deprecate-version
change: Deprecate Gherkin::VERSION constant and change file cache namespacing
2 parents 51ddede + 3ea6101 commit 70cf6bf

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
],
1515

1616
"require": {
17-
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*"
17+
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
18+
"composer-runtime-api": "^2.2"
1819
},
1920

2021
"require-dev": {
22+
"symfony/filesystem": "^5.4 || ^6.4 || ^7.0",
2123
"symfony/yaml": "^5.4 || ^6.4 || ^7.0",
2224
"phpunit/phpunit": "^10.5",
2325
"cucumber/cucumber": "dev-gherkin-24.1.0",

src/Behat/Gherkin/Cache/FileCache.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace Behat\Gherkin\Cache;
1212

1313
use Behat\Gherkin\Exception\CacheException;
14-
use Behat\Gherkin\Gherkin;
1514
use Behat\Gherkin\Node\FeatureNode;
15+
use Composer\InstalledVersions;
1616

1717
/**
1818
* File cache.
@@ -24,6 +24,17 @@ class FileCache implements CacheInterface
2424
{
2525
private $path;
2626

27+
/**
28+
* Used as part of the cache directory path to invalidate cache if the installed package version changes.
29+
*/
30+
private static function getGherkinVersionHash(): string
31+
{
32+
$version = InstalledVersions::getVersion('behat/gherkin');
33+
34+
// Composer version strings can contain arbitrary content so hash for filesystem safety
35+
return md5($version);
36+
}
37+
2738
/**
2839
* Initializes file cache.
2940
*
@@ -33,7 +44,7 @@ class FileCache implements CacheInterface
3344
*/
3445
public function __construct($path)
3546
{
36-
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'v' . Gherkin::VERSION;
47+
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::getGherkinVersionHash();
3748

3849
if (!is_dir($this->path)) {
3950
@mkdir($this->path, 0777, true);

src/Behat/Gherkin/Gherkin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
class Gherkin
2525
{
26+
/**
27+
* @deprecated this constant will not be updated for releases after 4.8.0 and will be removed in the next major.
28+
* You can use composer's runtime API to get the behat version if you need it. Note that composer's versions will
29+
* not always be simple numeric values.
30+
*/
2631
public const VERSION = '4.8.0';
2732

2833
/**

tests/Behat/Gherkin/Cache/FileCacheTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
use Behat\Gherkin\Cache\FileCache;
1414
use Behat\Gherkin\Exception\CacheException;
15-
use Behat\Gherkin\Gherkin;
1615
use Behat\Gherkin\Node\FeatureNode;
1716
use Behat\Gherkin\Node\ScenarioNode;
1817
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Filesystem\Filesystem;
1919

2020
class FileCacheTest extends TestCase
2121
{
@@ -58,9 +58,19 @@ public function testCacheAndRead(): void
5858

5959
public function testBrokenCacheRead(): void
6060
{
61+
// First, write a valid cache and find the file that was written
62+
$this->cache->write(
63+
'broken_feature',
64+
new FeatureNode(null, null, [], null, [], null, null, null, null),
65+
);
66+
$files = glob($this->path . '/**/*.feature.cache');
67+
$this->assertCount(1, $files, 'Cache should have written a single file');
68+
69+
// Now simulate the file being corrupted and attempt to read it
70+
file_put_contents($files[0], '');
71+
6172
$this->expectException(CacheException::class);
6273

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

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

7888
protected function setUp(): void
7989
{
80-
$this->cache = new FileCache($this->path = sys_get_temp_dir() . '/gherkin-test');
90+
$this->cache = new FileCache($this->path = sys_get_temp_dir() . uniqid('/gherkin-test'));
8191
}
8292

8393
protected function tearDown(): void
8494
{
85-
foreach (glob($this->path . '/*.feature.cache') as $file) {
86-
unlink((string) $file);
87-
}
95+
(new Filesystem())->remove($this->path);
8896
}
8997
}

0 commit comments

Comments
 (0)