Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback on churn.yml.dist #338

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Loader
*/
public static function fromPath(string $confPath, bool $isDefaultValue): Config
{
$confPath = self::normalizePath($originalConfPath = $confPath);
$originalConfPath = $confPath;
$confPath = self::normalizePath($isDefaultValue ? '.' : $confPath);

if (false !== $confPath && \is_readable($confPath)) {
$config = new EditableConfig($confPath);
Expand All @@ -54,10 +55,19 @@ public static function fromPath(string $confPath, bool $isDefaultValue): Config
*/
private static function normalizePath(string $confPath)
{
if (\is_dir($confPath)) {
$confPath = \rtrim($confPath, '/\\') . '/churn.yml';
if (!\is_dir($confPath)) {
return \realpath($confPath);
}

$confPath = \rtrim($confPath, '/\\') . '/churn.yml';
$realConfPath = \realpath($confPath);

if (false !== $realConfPath) {
return $realConfPath;
}

$confPath .= '.dist';

return \realpath($confPath);
}

Expand Down
41 changes: 38 additions & 3 deletions tests/Unit/Configuration/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Churn\Tests\Unit\Configuration;

use Churn\Configuration\Config;
use Churn\Configuration\EditableConfig;
use Churn\Configuration\Loader;
use Churn\Tests\BaseTestCase;
use InvalidArgumentException;
Expand All @@ -14,10 +15,17 @@ class LoaderTest extends BaseTestCase
/** @test */
public function it_returns_the_default_values_if_there_is_no_default_file()
{
$config = Loader::fromPath('non-existing-config-file.yml', true);
$cwd = \getcwd();
try {
chdir(__DIR__);
$config = Loader::fromPath('churn.yml', true);

$this->assertEquals(new Config(), $config);
$this->assertEquals(\getcwd(), $config->getDirPath());
$this->assertEquals(new Config(), $config);
$this->assertEquals(\getcwd(), $config->getDirPath());
} finally {
// restore cwd
chdir($cwd);
}
}

/** @test */
Expand All @@ -33,4 +41,31 @@ public function it_throws_if_the_content_is_invalid()
$this->expectException(InvalidArgumentException::class);
Loader::fromPath(__FILE__, false);
}

/** @test */
public function it_fallbacks_on_the_distributed_file()
{
$dirPath = \realpath(__DIR__ . '/config/dist');
$config = Loader::fromPath($dirPath, false);

$this->assertEquals(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config);
$this->assertEquals($dirPath, $config->getDirPath());
}

/** @test */
public function it_fallbacks_on_the_default_distributed_file()
{
$cwd = \getcwd();
$dirPath = \realpath(__DIR__ . '/config/dist');
try {
chdir($dirPath);
$config = Loader::fromPath('churn.yml', true);

$this->assertEquals(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config);
$this->assertEquals($dirPath, $config->getDirPath());
} finally {
// restore cwd
chdir($cwd);
}
}
}
Empty file.