Skip to content

Commit

Permalink
Merge pull request #408 from jolicode/cache-directory-test
Browse files Browse the repository at this point in the history
Use a different cache directory for tests
  • Loading branch information
lyrixx committed Apr 11, 2024
2 parents b2d5dc9 + d8c6456 commit 34e0714
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require __DIR__ . '/../vendor/autoload.php';

use Castor\Console\ApplicationFactory;
use Castor\Helper\PlatformHelper;
use Castor\Tests\Helper\OutputCleaner;
use Castor\Tests\Helper\WebServerHelper;
use Symfony\Component\Console\Input\ArrayInput;
Expand All @@ -16,12 +15,13 @@
use function Symfony\Component\String\u;

$_SERVER['ENDPOINT'] ??= 'http://127.0.0.1:9955';
$_SERVER['CASTOR_CACHE_DIR'] ??= '/tmp/castor-tests/cache';
WebServerHelper::start();

displayTitle('Cleaning');

$fs = new Filesystem();
$fs->remove(PlatformHelper::getCacheDirectory());
$fs->remove($_SERVER['CASTOR_CACHE_DIR']);
$fs->remove(__DIR__ . '/../tests/Generated');
$fs->mkdir(__DIR__ . '/../tests/Generated');
$fs->remove((new Finder())
Expand Down
2 changes: 1 addition & 1 deletion src/Console/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function create(): SymfonyApplication
$container = self::buildContainer($repacked);
$container->getParameterBag()->add([
'root_dir' => $rootDir,
'cache_dir' => PlatformHelper::getCacheDirectory(),
'cache_dir' => $_SERVER['CASTOR_CACHE_DIR'] ?? PlatformHelper::getDefaultCacheDirectory(),
'event_dispatcher.event_aliases' => ConsoleEvents::ALIASES,
'repacked' => $repacked,
]);
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Castor\Console\Command;

use Castor\Helper\PathHelper;
use Castor\Helper\PlatformHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -24,7 +23,8 @@ class CompileCommand extends Command

public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly Filesystem $fs
private readonly Filesystem $fs,
private readonly string $cacheDir,
) {
parent::__construct();
}
Expand Down Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$phpBuildCacheKey = $this->generatePHPBuildCacheKey($input);

$spcBinaryPath = PlatformHelper::getCacheDirectory() . '/castor-php-static-compiler/' . $phpBuildCacheKey . '/spc';
$spcBinaryPath = $this->cacheDir . '/castor-php-static-compiler/' . $phpBuildCacheKey . '/spc';
$spcBinaryDir = \dirname($spcBinaryPath);

$os = $input->getOption('os');
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/PlatformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function getEnv(string $name): string|false
return getenv($name);
}

public static function getCacheDirectory(): string
public static function getDefaultCacheDirectory(): string
{
try {
$home = self::getUserDirectory();
Expand Down
9 changes: 5 additions & 4 deletions tests/Examples/Fingerprint/FingerprintedTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Castor\Tests\Examples\Fingerprint;

use Castor\Helper\PlatformHelper;
use Castor\Tests\TaskTestCase;
use Symfony\Component\Finder\Finder;

Expand All @@ -20,10 +19,12 @@ protected function setUp(): void

private static function clearFingerprintsCache(): void
{
if (is_dir(PlatformHelper::getCacheDirectory())) {
$cacheDir = self::$castorCacheDir;

if (is_dir($cacheDir)) {
foreach (
(new Finder())
->in(PlatformHelper::getCacheDirectory())
->in($cacheDir)
->contains('.fingerprint')
->files() as $file
) {
Expand All @@ -32,7 +33,7 @@ private static function clearFingerprintsCache(): void

foreach (
(new Finder())
->in(PlatformHelper::getCacheDirectory())
->in($cacheDir)
->notContains('.fingerprint')
->directories() as $directory
) {
Expand Down
3 changes: 3 additions & 0 deletions tests/TaskTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
abstract class TaskTestCase extends TestCase
{
public static string $castorBin;
public static string $castorCacheDir;
public static bool $binary = false;

public static function setUpBeforeClass(): void
{
WebServerHelper::start();

self::$castorBin = $_SERVER['CASTOR_BIN'] ?? __DIR__ . '/../bin/castor';
self::$castorCacheDir = $_SERVER['CASTOR_CACHE_DIR'] ?? '/tmp/castor-tests/cache';
self::$binary = 'application/x-executable' === mime_content_type(self::$castorBin);
}

Expand All @@ -27,6 +29,7 @@ public function runTask(array $args, ?string $cwd = null, bool $needRemote = fal

$extraEnv = [
'ENDPOINT' => $_SERVER['ENDPOINT'],
'CASTOR_CACHE_DIR' => self::$castorCacheDir,
];

if (!$needRemote) {
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/compile-get-cache-key
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $command = (new class() extends CompileCommand {
}}
)
->setCode(Closure::bind(
fn($input) => (print PlatformHelper::getCacheDirectory() . '/castor-php-static-compiler/' . $this->generatePHPBuildCacheKey($input)) && 0,
fn($input) => (print PlatformHelper::getDefaultCacheDirectory() . '/castor-php-static-compiler/' . $this->generatePHPBuildCacheKey($input)) && 0,
null,
CompileCommand::class
))
Expand Down

0 comments on commit 34e0714

Please sign in to comment.