From 8d213fdf1588ac13708a044aaab65cd41267ba5e Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 19 Jul 2024 10:24:44 +0000 Subject: [PATCH] Make runtime directory optional 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 --- src/Configuration.php | 6 ++++-- src/Shell.php | 2 +- test/ConfigurationTest.php | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 3bb70640..6b9b37cd 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -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 { $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))); } diff --git a/src/Shell.php b/src/Shell.php index 4fd97b72..7830cc62 100644 --- a/src/Shell.php +++ b/src/Shell.php @@ -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, diff --git a/test/ConfigurationTest.php b/test/ConfigurationTest.php index 64cd2308..fca410bf 100644 --- a/test/ConfigurationTest.php +++ b/test/ConfigurationTest.php @@ -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 */