Skip to content

Commit

Permalink
SDK-5690: Cleanup integration.lock and add it into .gitignore (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
vol4onok authored Dec 14, 2023
1 parent 2e5c64e commit 583f966
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/Console/IntegratorLockUpdaterConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$commandArgumentsTransfer = $this->buildCommandArgumentsTransfer($input);
$io = $this->createInputOutputAdapter($input, $output, $commandArgumentsTransfer->getFormat());
$this->getFacade()
->runCleanLock($io);

$this->getFacade()->runUpdateLock(
$io,
$commandArgumentsTransfer,
);

$this->getFacade()
->runCleanLock($io);

return 0;
}
}
7 changes: 6 additions & 1 deletion src/IntegratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

class IntegratorConfig
{
/**
* @var string
*/
public const INTEGRATOR_LOCK = 'integrator.lock';

/**
* @var string
*/
Expand Down Expand Up @@ -366,7 +371,7 @@ protected function getDefaultConfigPath(): string
*/
public function getIntegratorLockFilePath(): string
{
return $this->getProjectRootDirectory() . 'integrator.lock';
return $this->getProjectRootDirectory() . static::INTEGRATOR_LOCK;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/IntegratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function createIntegratorLockWriter(): IntegratorLockWriterInterface
*/
public function createIntegratorLockCleaner(): IntegratorLockCleanerInterface
{
return new IntegratorLockCleaner($this->getConfig(), new ProcessExecutor());
return new IntegratorLockCleaner($this->getConfig(), new ProcessExecutor(), $this->createFilesystem());
}

/**
Expand Down
41 changes: 32 additions & 9 deletions src/IntegratorLock/IntegratorLockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

use SprykerSdk\Integrator\Executor\ProcessExecutor;
use SprykerSdk\Integrator\IntegratorConfig;
use Symfony\Component\Filesystem\Filesystem;

class IntegratorLockCleaner implements IntegratorLockCleanerInterface
{
/**
* @var string
*/
protected const GITIGNORE_FILE = '.gitignore';

/**
* @var \SprykerSdk\Integrator\IntegratorConfig
*/
Expand All @@ -24,14 +30,21 @@ class IntegratorLockCleaner implements IntegratorLockCleanerInterface
*/
protected ProcessExecutor $processExecutor;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected Filesystem $filesystem;

/**
* @param \SprykerSdk\Integrator\IntegratorConfig $config
* @param \SprykerSdk\Integrator\Executor\ProcessExecutor $processExecutor
* @param \Symfony\Component\Filesystem\Filesystem $filesystem
*/
public function __construct(IntegratorConfig $config, ProcessExecutor $processExecutor)
public function __construct(IntegratorConfig $config, ProcessExecutor $processExecutor, Filesystem $filesystem)
{
$this->config = $config;
$this->processExecutor = $processExecutor;
$this->filesystem = $filesystem;
}

/**
Expand All @@ -41,25 +54,35 @@ public function deleteLock(): void
{
$lockFilePath = $this->config->getIntegratorLockFilePath();

unlink($lockFilePath);

if (!$this->isLockFileChangeTrackedByGit($lockFilePath)) {
if ($this->isLockFileIgnoredByGit($lockFilePath)) {
return;
}
$gitAddCommand = ['git', 'add'];
if ($this->filesystem->exists($lockFilePath)) {
$this->filesystem->remove($lockFilePath);
$gitAddCommand[] = $lockFilePath;
}
$gitignorePath = $this->config->getProjectRootDirectory() . static::GITIGNORE_FILE;

$this->processExecutor->execute(['git', 'add', $lockFilePath]);
$this->processExecutor->execute(['git', 'commit', '-m', 'Removed `integrator.lock` file.']);
if (!$this->filesystem->exists($gitignorePath) || strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) {
$this->filesystem->appendToFile($gitignorePath, PHP_EOL . $this->config::INTEGRATOR_LOCK . PHP_EOL);
$gitAddCommand[] = $gitignorePath;
}
if (count($gitAddCommand) > 2) {
$this->processExecutor->execute($gitAddCommand);
$this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK), '-n']);
}
}

/**
* @param string $filepath
*
* @return bool
*/
protected function isLockFileChangeTrackedByGit(string $filepath): bool
protected function isLockFileIgnoredByGit(string $filepath): bool
{
$process = $this->processExecutor->execute(['git', 'status', '--porcelain', $filepath]);
$process = $this->processExecutor->execute(['git', 'check-ignore', $filepath]);

return $process->getOutput() !== '';
return (bool)$process->getOutput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function tearDownAfterClass(): void
}

/**
* @return \SprykerSdk\Integrator\Business\IntegratorFacade
* @return \SprykerSdk\Integrator\IntegratorFacade
*/
protected function createIntegratorFacade(): IntegratorFacade
{
Expand Down
18 changes: 0 additions & 18 deletions tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,22 +561,4 @@ public function testRunUpdateLock(): void

$this->assertNotEmpty(trim(file_get_contents($integratorLock)));
}

/**
* @return void
*/
public function testRunCleanLock(): void
{
// Arrange
$ioAdapter = $this->buildSymfonyConsoleInputOutputAdapter();

file_put_contents($this->getTestTmpDirPath() . '/integrator.lock', 'test');

// Act
$this->createIntegratorFacade()->runCleanLock($ioAdapter);

// Assert
$integratorLock = $this->getTestTmpDirPath() . '/integrator.lock';
$this->assertFileDoesNotExist($integratorLock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
use SprykerSdk\Integrator\IntegratorLock\IntegratorLockReader;
use SprykerSdk\Integrator\IntegratorLock\IntegratorLockWriter;
use SprykerSdkTest\Integrator\BaseTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

class IntegratorLockTest extends BaseTestCase
{
/**
* @return void
*/
protected function setUp(): void
{
@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK);
@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . '.gitignore');
}

/**
* @return void
*/
Expand All @@ -34,7 +45,7 @@ public function testWriteFileLock(): void
],
],
];
$tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.');
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();

$integratorLockWriter = $this->createIntegratorLockWriter($tmpIntegratorLockFilePath);
$integratorLockWriter->storeLock($lockData);
Expand All @@ -52,7 +63,7 @@ public function testWriteFileLock(): void
*/
public function testReadFileLock(): void
{
$tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.');
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();
$compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json';

file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath));
Expand All @@ -71,10 +82,9 @@ public function testReadFileLock(): void
*/
public function testDeleteFileLock(): void
{
$tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.');
$compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json';
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();

file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath));
file_put_contents($tmpIntegratorLockFilePath, 'test');

$integratorLockCleaner = $this->createIntegratorLockCleaner($tmpIntegratorLockFilePath);
$integratorLockCleaner->deleteLock();
Expand Down Expand Up @@ -109,7 +119,13 @@ private function createIntegratorLockReader(string $tmpIntegratorLockFilePath):
*/
private function createIntegratorLockCleaner(string $tmpIntegratorLockFilePath): IntegratorLockCleaner
{
return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), new ProcessExecutor());
$processMock = $this->createMock(Process::class);
$processMock->method('getOutput')->willReturn('');

$processExecutorMock = $this->createMock(ProcessExecutor::class);
$processExecutorMock->method('execute')->willReturn($processMock);

return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock, new Filesystem());
}

/**
Expand All @@ -123,6 +139,8 @@ private function mockIntegratorConfig(string $tmpIntegratorLockFilePath): Integr

$integratorConfigMock->method('getIntegratorLockFilePath')
->willReturn($tmpIntegratorLockFilePath);
$integratorConfigMock->method('getProjectRootDirectory')
->willReturn(sys_get_temp_dir() . DIRECTORY_SEPARATOR);

return $integratorConfigMock;
}
Expand All @@ -136,4 +154,15 @@ private function removeFile(string $path): void
{
$this->createFilesystem()->remove($path);
}

/**
* @return string
*/
protected function createTmpIntegratorLockFilePath(): string
{
$tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK;
touch($tmpIntegratorLockFilePath);

return $tmpIntegratorLockFilePath;
}
}

0 comments on commit 583f966

Please sign in to comment.