Skip to content

Commit

Permalink
Make runtime directory optional
Browse files Browse the repository at this point in the history
The runtime directory used to be used internally for a bunch of things, but at this point it's mainly used as a directory to write `edit` command buffers to.

Since those aren't crucial to the operation of PsySH, and since non-writable runtime directory configuration apparently happens sometimes, let's try making a writable runtime directory entirely optional.

See #443
  • Loading branch information
bobthecow committed Jul 19, 2024
1 parent 4fd21e6 commit 8d213fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,14 @@ public function setRuntimeDir(string $dir)
* overridden.
*
* @throws RuntimeException if no temporary directory is set and it is not possible to create one
*
* @param bool $create False to suppress directory creation if it does not exist
*/
public function getRuntimeDir(): string
public function getRuntimeDir($create = true): string

This comment has been minimized.

Copy link
@BafS

BafS Jul 20, 2024

Contributor

Why not bool $create = true?

{
$runtimeDir = $this->configPaths->runtimeDir();

if (!\is_dir($runtimeDir)) {
if ($create && !\is_dir($runtimeDir)) {
if (!@\mkdir($runtimeDir, 0700, true)) {
throw new RuntimeException(\sprintf('Unable to create PsySH runtime directory. Make sure PHP is able to write to %s in order to continue.', \dirname($runtimeDir)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected function getDefaultCommands(): array
new Command\TraceCommand(),
new Command\BufferCommand(),
new Command\ClearCommand(),
new Command\EditCommand($this->config->getRuntimeDir()),
new Command\EditCommand($this->config->getRuntimeDir(false)),
// new Command\PsyVersionCommand(),
$sudo,
$hist,
Expand Down
15 changes: 15 additions & 0 deletions test/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ public function testGettersAndSetters()
$this->assertSame('wheee', $config->getConfigDir());
}

public function testGetRuntimeDir()
{
$dirName = \tempnam(\sys_get_temp_dir(), 'psysh-config-test-');
\unlink($dirName);

$config = $this->getConfig();
$config->setRuntimeDir($dirName);

$this->assertSame($config->getRuntimeDir(false), $dirName);
$this->assertDirectoryDoesNotExist($dirName);

$this->assertSame($config->getRuntimeDir(true), $dirName);
$this->assertDirectoryExists($dirName);
}

/**
* @group isolation-fail
*/
Expand Down

0 comments on commit 8d213fd

Please sign in to comment.