From adf5a98768df4f8548e374ce7ec8c4a3ea1ac81a Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 09:31:07 +0100 Subject: [PATCH] Task: #53687 Handle missing mode fallback and add no-mode cfg tests --- bin/cfg | 2 +- src/Settings.php | 15 +++++++++++++-- tests/CfgTest.php | 19 +++++++++++++++++++ tests/SettingsTest.php | 9 +++++++++ tests/cfg/noMode/config/default.conf.php | 3 +++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/cfg/noMode/config/default.conf.php diff --git a/bin/cfg b/bin/cfg index 6f138cf..2ce253f 100755 --- a/bin/cfg +++ b/bin/cfg @@ -334,7 +334,7 @@ try { elseif (is_readable($cfgFile = $cfg->buildFileName())) { $cfg->load(require($cfgFile)); } -} catch (Exception $e) { +} catch (\Throwable $e) { fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL); exit(1); } diff --git a/src/Settings.php b/src/Settings.php index 56e1b0f..ebf7aeb 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -159,7 +159,18 @@ class Settings implements \Iterator, \Countable // if a mode was set in the constructor do not overwrite it if (! isset($this->mode)) { // if a localConf Mode is set use it, or take the default conf mode - $this->mode = (isset($localConf['mode'])) ? $localConf['mode'] : $this->settings['mode']; + // if local/default config has no mode, fall back to the first known mode (normally "prod") + if (isset($localConf['mode']) && is_string($localConf['mode']) && $localConf['mode'] !== '') { + $this->mode = $localConf['mode']; + } + elseif (isset($this->settings['mode']) && is_string($this->settings['mode']) && $this->settings['mode'] !== '') { + $this->mode = $this->settings['mode']; + } + else { + // Backward-compatible fallback for configs without explicit mode. + $this->mode = (string)array_key_first($this->modes); + $this->settings['mode'] = $this->mode; + } } else { $this->settings['mode'] = $this->mode; } @@ -358,4 +369,4 @@ class Settings implements \Iterator, \Countable /* jEdit buffer local properties {{{ * :folding=explicit:collapseFolds=1: -}}}*/ \ No newline at end of file +}}}*/ diff --git a/tests/CfgTest.php b/tests/CfgTest.php index 508a212..79167ea 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -44,6 +44,25 @@ class CfgTest extends TestCase $this->assertSame('218523', $cfg['auth']['projectId']); } + public function testWriteWithoutModeFallsBackToDefaultMode(): void + { + file_put_contents( + $this->tmpDir.'/config/myCEESV.default.conf.php', + " [\n\t\t'projectId' => '',\n\t],\n];\n" + ); + + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/myCEESV.conf.php'); + } + public function testWriteWithDirectoryNameCreatesAndWritesSiteConfig(): void { $result = $this->runCfg([ diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index c895bba..ae70ef7 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -192,6 +192,15 @@ class SettingsTest extends TestCase $cfg = $this->appPath($cfg, 'localOverride')->load(); $this->assertEquals(42, $cfg->answer); } + + public function testLoadWithoutModeFallsBackToFirstKnownMode(): void + { + $cfg = new Settings(); + $cfg = $this->appPath($cfg, 'noMode')->load(); + + $this->assertEquals('prod', $cfg->mode); + $this->assertEquals('default', $cfg->testFiles); + } /** * @dataProvider fileNameData diff --git a/tests/cfg/noMode/config/default.conf.php b/tests/cfg/noMode/config/default.conf.php new file mode 100644 index 0000000..c0a0d9b --- /dev/null +++ b/tests/cfg/noMode/config/default.conf.php @@ -0,0 +1,3 @@ + 'default', +];