Skip to content

Commit

Permalink
tweak(Module/WPCLI) do not create cachw dir w/ custom bin
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Nov 26, 2024
1 parent 90e4ea1 commit 5f0dc9a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/Module/WPCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,16 @@ private function getDefaultEnv(): array
* cache-dir?: non-empty-string,
* config-path?: non-empty-string,
* custom-shell?: non-empty-string,
* packages-dir?: non-empty-string
* packages-dir?: non-empty-string,
* bin?: string
* } $config Validated config.
*/
$config = $this->config;
$cacheDir = $config['cache-dir'] ?? (Filesystem::cacheDir() . '/wp-cli');
$env['WP_CLI_CACHE_DIR'] = Filesystem::mkdirp($cacheDir);

if (empty($config['bin'])) {
$cacheDir = $config['cache-dir'] ?? (Filesystem::cacheDir() . '/wp-cli');
$env['WP_CLI_CACHE_DIR'] = Filesystem::mkdirp($cacheDir);
}

if (isset($config['config-path'])) {
$env['WP_CLI_CONFIG_PATH'] = $config['config-path'];
Expand Down
31 changes: 27 additions & 4 deletions tests/unit/lucatume/WPBrowser/Module/WPCLICustomBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@
class WPCLICustomBinaryTest extends Unit
{
private ?string $homeBackup = null;
private string|null|false $envCacheDirValue = null;
private string $wpCliCacheDir;
private string $wpCliCacheDirBackup;

public function setUp(): void
{
parent::setUp();
if (isset($_SERVER['HOME'])) {
$this->homeBackup = $_SERVER['HOME'];
}
$this->wpCliCacheDir = Filesystem::cacheDir() . '/wp-cli';
$this->wpCliCacheDirBackup = dirname($this->wpCliCacheDir) . '/wp-cli-cache-dir-backup';
if (is_dir($this->wpCliCacheDirBackup)) {
Filesystem::rrmdir($this->wpCliCacheDirBackup);
}
if (is_dir($this->wpCliCacheDir)) {
rename($this->wpCliCacheDir, $this->wpCliCacheDirBackup);
}
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir);
}

public function tearDown(): void
Expand All @@ -27,6 +39,12 @@ public function tearDown(): void
if ($this->homeBackup !== null) {
$_SERVER['HOME'] = $this->homeBackup;
}
if (is_dir($this->wpCliCacheDir)) {
Filesystem::rrmdir($this->wpCliCacheDir);
}
if (is_dir($this->wpCliCacheDirBackup)) {
rename($this->wpCliCacheDirBackup, $this->wpCliCacheDir);
}
}

public function test_configuration_allows_custom_binary(): void
Expand All @@ -44,15 +62,17 @@ public function test_configuration_allows_custom_binary(): void
'Hello from wp-cli custom binary',
$module->grabLastShellOutput()
);
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir);
}

public function test_configuration_supports_tilde_for_home_in_custom_binary():void{
public function test_configuration_supports_tilde_for_home_in_custom_binary(): void
{
$_SERVER['HOME'] = codecept_data_dir();
$binary = '~/bins/wp-cli-custom-bin';
$binaryPath = codecept_data_dir('bins/wp-cli-custom-bin');
$moduleContainer = new ModuleContainer(new Di(), []);
// Sanity check.
$this->assertEquals(rtrim(codecept_data_dir(),'\\/'),Filesystem::homeDir());
$this->assertEquals(rtrim(codecept_data_dir(), '\\/'), Filesystem::homeDir());

$module = new WPCLI($moduleContainer, [
'path' => 'var/wordpress',
Expand All @@ -64,9 +84,11 @@ public function test_configuration_supports_tilde_for_home_in_custom_binary():vo
'Hello from wp-cli custom binary',
$module->grabLastShellOutput()
);
$this->assertDirectoryDoesNotExist($this->wpCliCacheDir);
}

public function test_throws_if_custom_binary_does_not_exist(): void{
public function test_throws_if_custom_binary_does_not_exist(): void
{
$binary = codecept_data_dir('bins/not-a-bin');
$moduleContainer = new ModuleContainer(new Di(), []);

Expand All @@ -79,7 +101,8 @@ public function test_throws_if_custom_binary_does_not_exist(): void{
$module->cli(['core', 'version']);
}

public function test_throws_if_custom_binary_is_not_executable(): void{
public function test_throws_if_custom_binary_is_not_executable(): void
{
$binary = codecept_data_dir('bins/not-executable');
$moduleContainer = new ModuleContainer(new Di(), []);

Expand Down

0 comments on commit 5f0dc9a

Please sign in to comment.