Skip to content

Commit

Permalink
fix (CreteConfigurationFileCommand): Checks if the configuration file…
Browse files Browse the repository at this point in the history
… already exists to prevent override
  • Loading branch information
Wtyd committed Jun 21, 2024
1 parent d8a99eb commit 1c55146
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
29 changes: 20 additions & 9 deletions app/Commands/CreateConfigurationFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Wtyd\GitHooks\App\Commands;

// use Illuminate\Support\Facades\Storage;
use LaravelZero\Framework\Commands\Command;
use Wtyd\GitHooks\Utils\Printer;
use Wtyd\GitHooks\Utils\Storage;

class CreateConfigurationFileCommand extends Command
{
Expand All @@ -23,20 +25,29 @@ public function __construct(Printer $printer)

public function handle()
{
$root = getcwd();
$origin = "$root/vendor/wtyd/githooks/qa/githooks.dist.yml";

$destiny = "$root/githooks.yml";
// $this->printer->error("Failed to copy $origin to $destiny");
// $this->printer->success('Configuration file githooks.yml has been created in root path');
// exit();
return $this->copyFile($origin, $destiny);
$origin = "vendor/wtyd/githooks/qa/githooks.dist.yml";
$destiny = "githooks.yml";

if ($this->checkIfConfigurationFileExists()) {
return $this->copyFile($origin, $destiny);
} else {
return 1;
}
}

protected function checkIfConfigurationFileExists(): bool
{
if (Storage::exists('githooks.yml') || Storage::exists('qa/githooks.yml')) {
$this->printer->error('Configuration file githooks.yml already exists in root path');
return false;
}
return true;
}

protected function copyFile(string $origin, string $destiny): int
{
try {
if (copy($origin, $destiny) === false) {
if (!Storage::copy($origin, $destiny)) {
$this->printer->error("Failed to copy $origin to $destiny");
return 1;
} else {
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Wtyd\GitHooks\App\Providers;

use Illuminate\Support\ServiceProvider;
use Wtyd\GitHooks\Tools\Tool\SecurityCheckerFake;
use Tests\Utils\FileReaderFake;
use Wtyd\GitHooks\ConfigurationFile\FileReader;
use Wtyd\GitHooks\Container\RegisterBindings;
use Wtyd\GitHooks\Tools\Process\ProcessExecutionFactory\ProcessExecutionFactoryAbstract;
use Wtyd\GitHooks\Tools\Process\ProcessExecutionFactory\ProcessExecutionFactoryFake;
use Wtyd\GitHooks\Tools\Tool\SecurityChecker;
use Wtyd\GitHooks\Tools\Tool\SecurityCheckerFake;
use Wtyd\GitHooks\Utils\Storage;

class AppServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -57,6 +58,7 @@ protected function testsRegister(): void
$this->app->singleton(FileReader::class, FileReaderFake::class);
$this->app->singleton(SecurityChecker::class, SecurityCheckerFake::class);
$this->app->singleton(ProcessExecutionFactoryAbstract::class, ProcessExecutionFactoryFake::class);
Storage::$disk = 'testing';
}
}
}
4 changes: 4 additions & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
'driver' => 'local',
'root' => getcwd(),
],
'testing' => [
'driver' => 'local',
'root' => getcwd() . '/testsDir',
],
],
];
35 changes: 35 additions & 0 deletions src/Utils/Storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Wtyd\GitHooks\Utils;

use Illuminate\Support\Facades\Storage as FacadesStorage;

class Storage
{
/** @var string */
public static $disk = 'local';
/**
* Determine if a file or directory exists.
*
* @param string $path
* @return bool
*/
public static function exists($path)
{
return FacadesStorage::disk(self::$disk)->exists($path);
}

/**
* Copy a file to a new location.
*
* @param string $path
* @param string $target
* @return bool
*/
public static function copy($path, $target)
{
return FacadesStorage::disk(self::$disk)->copy($path, $target);
}
}
20 changes: 9 additions & 11 deletions tests/System/Commands/CreateConfigurationFileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Tests\System\Commands;

use phpmock\MockBuilder;
// use Illuminate\Support\Facades\Storage;
use phpmock\Mock as PhpmockMock;
use phpmock\MockBuilder;
use Tests\Utils\TestCase\SystemTestCase;
use Wtyd\GitHooks\Utils\Storage;

class CreateConfigurationFileCommandTest extends SystemTestCase
{
Expand All @@ -14,7 +16,8 @@ class CreateConfigurationFileCommandTest extends SystemTestCase
public function getMockRootDirectory(): PhpmockMock
{
$builder = new MockBuilder();
$builder->setNamespace('Wtyd\GitHooks\App\Commands')
// $builder->setNamespace('Wtyd\GitHooks\App\Commands')
$builder->setNamespace('Illuminate\Filesystem')
->setName('getcwd')
->setFunction(
function () {
Expand All @@ -32,30 +35,25 @@ function it_creates_the_configuration_file_in_the_root_of_the_project_using_the_
mkdir($templatePath, 0777, true);
file_put_contents($templatePath . 'githooks.dist.yml', '');

$mock = $this->getMockRootDirectory();
$mock->enable();
// $mock = $this->getMockRootDirectory();
// $mock->enable();

$this->artisan('conf:init')
->containsStringInOutput('Configuration file githooks.yml has been created in root path')
->assertExitCode(0);

$this->assertFileEquals($templatePath . 'githooks.dist.yml', $this->path . '/githooks.yml');

$mock->disable();
// $mock->disable();
}

/** @test */
function it_prints_an_error_message_when_something_wrong_happens()
{
$mock = $this->getMockRootDirectory();
$mock->enable();

$this->artisan('conf:init')
->containsStringInOutput('Failed to copy ' . $this->path . '/vendor/wtyd/githooks/qa/githooks.dist.yml' . ' to ' . $this->path . '/githooks.yml')
->containsStringInOutput('Failed to copy vendor/wtyd/githooks/qa/githooks.dist.yml to githooks.yml')
->assertExitCode(1);

$this->assertFileDoesNotExist($this->path . '/githooks.yml');

$mock->disable();
}
}

0 comments on commit 1c55146

Please sign in to comment.