Skip to content

Commit

Permalink
Fix editing gitignore file for install command (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr-Moik authored Mar 31, 2023
1 parent c20f442 commit b228dfe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
20 changes: 14 additions & 6 deletions src/Console/Commands/GitlabDeployInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace HexideDigital\GitlabDeploy\Console\Commands;

use Closure;
use File;
use HexideDigital\GitlabDeploy\GitlabDeployServiceProvider;
use Illuminate\Console\Command;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Str;

class GitlabDeployInstallCommand extends Command
Expand Down Expand Up @@ -101,10 +103,14 @@ protected function updateGitignoreFile(): void

$this->info('Updating: .gitignore file');

$content = File::get($gitignoreFile);

$this->appendIgnore($content, $this->getSshDir());
$this->appendIgnore($content, $this->getWorkingDir());
$content = app(Pipeline::class)
->send(File::get($gitignoreFile))
->through([
fn (string $content, Closure $next) => $next($this->appendIgnore($content, $this->getSshDir())),
fn (string $content, Closure $next) => $next($this->appendIgnore($content, "/.ssh")),
fn (string $content, Closure $next) => $next($this->appendIgnore($content, $this->getWorkingDir())),
])
->thenReturn();

if ($content === File::get($gitignoreFile)) {
$this->info('No need to update.');
Expand All @@ -127,16 +133,18 @@ protected function appendIgnore(string $content, string $pattern): string
protected function getSshDir(): string
{
return Str::of(config('gitlab-deploy.ssh.folder'))
->replace('{{STAGE}}', '')
->replace(base_path(), '')
->start('/')
->dirname()
->replace(DIRECTORY_SEPARATOR, '/')
->value();
}

protected function getWorkingDir(): string
{
return Str::of(config('gitlab-deploy.config-file'))
->replace(base_path(), '')
->dirname()
->replace(DIRECTORY_SEPARATOR, '/')
->value();
}
}
78 changes: 73 additions & 5 deletions tests/Feature/Console/GitlabDeployInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ function safeUnlink(string $path): void
}
}

beforeEach(function () {
safeUnlink(config_path('gitlab-deploy.php'));
});

afterEach(function () {
safeUnlink(config_path('gitlab-deploy.php'));
});

test('the install command copies the configuration', function () {
// make sure we're starting from a clean state
safeUnlink(config_path('gitlab-deploy.php'));
assertFalse(File::isFile(config_path('gitlab-deploy.php')));

$this->artisan('gitlab-deploy:install')
Expand All @@ -38,8 +45,6 @@ function safeUnlink(string $path): void
->assertSuccessful();

assertEquals('initial content', File::get(config_path('gitlab-deploy.php')));

safeUnlink(config_path('gitlab-deploy.php'));
});

test('when a config file is present users can choose to overwrite it', function () {
Expand All @@ -64,8 +69,6 @@ function safeUnlink(string $path): void
File::get(__DIR__ . '/../../../config/gitlab-deploy.php'),
File::get(config_path('gitlab-deploy.php'))
);

safeUnlink(config_path('gitlab-deploy.php'));
});

test('the install command copies sample files', function (string $sampleFile) {
Expand All @@ -86,3 +89,68 @@ function safeUnlink(string $path): void
'.deploy/deploy-prepare.yml',
'deploy.php',
]);

test('adds ssh and work folders to root .gitignore file', function () {
File::put(base_path('.gitignore'), implode(PHP_EOL, ["/vendor", "/.idea"]));

config([
'gitlab-deploy.config-file' => base_path('.deploy/deploy-prepare.yml'),
'gitlab-deploy.ssh.folder' => base_path('.ssh/{{STAGE}}'),
]);

$command = $this->artisan('gitlab-deploy:install')
->assertSuccessful();

$command->execute();

$command
->expectsOutput('Updating: .gitignore file')
->expectsOutput('Updated: .gitignore file');

assertEquals(
implode(PHP_EOL, ["/vendor", "/.idea", "/.ssh", "/.deploy"]),
File::get(base_path('.gitignore'))
);

safeUnlink(base_path('.gitignore'));
});

test('do not edit when ssh and work folders already in root .gitignore file', function () {
File::put(base_path('.gitignore'), implode(PHP_EOL, ["/vendor", "/.idea", "/.ssh", "/.deploy"]));

config([
'gitlab-deploy.config-file' => base_path('.deploy/deploy-prepare.yml'),
'gitlab-deploy.ssh.folder' => base_path('.ssh/{{STAGE}}'),
]);

$command = $this->artisan('gitlab-deploy:install')
->assertSuccessful();

$command->execute();

$command
->expectsOutput('Updating: .gitignore file')
->expectsOutput('No need to update.')
->doesntExpectOutput('Updated: .gitignore file');

assertEquals(
implode(PHP_EOL, ["/vendor", "/.idea", "/.ssh", "/.deploy"]),
File::get(base_path('.gitignore'))
);

safeUnlink(base_path('.gitignore'));
});

test('do nothing when in project missing .gitignore file', function () {
safeUnlink(base_path('.gitignore'));
assertFalse(File::isFile(base_path('.gitignore')));

$command = $this->artisan('gitlab-deploy:install')
->assertSuccessful();

$command->execute();

$command->doesntExpectOutput('Updating: .gitignore file');

assertFalse(File::isFile(base_path('.gitignore')));
});

0 comments on commit b228dfe

Please sign in to comment.