From 583f966daad60ebf1f5dab3718c260e7918f8e31 Mon Sep 17 00:00:00 2001 From: Alexander Veselov Date: Thu, 14 Dec 2023 18:07:47 +0200 Subject: [PATCH] SDK-5690: Cleanup integration.lock and add it into .gitignore (#171) --- src/Console/IntegratorLockUpdaterConsole.php | 6 +-- src/IntegratorConfig.php | 7 +++- src/IntegratorFactory.php | 2 +- src/IntegratorLock/IntegratorLockCleaner.php | 41 +++++++++++++++---- .../Integrator/AbstractIntegratorTestCase.php | 2 +- .../Integrator/IntegratorFacadeTest.php | 18 -------- .../IntegratorLock/IntegratorLockTest.php | 41 ++++++++++++++++--- 7 files changed, 78 insertions(+), 39 deletions(-) diff --git a/src/Console/IntegratorLockUpdaterConsole.php b/src/Console/IntegratorLockUpdaterConsole.php index 1030008e..47eea50d 100644 --- a/src/Console/IntegratorLockUpdaterConsole.php +++ b/src/Console/IntegratorLockUpdaterConsole.php @@ -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; } } diff --git a/src/IntegratorConfig.php b/src/IntegratorConfig.php index 7541894a..6ff5db1b 100644 --- a/src/IntegratorConfig.php +++ b/src/IntegratorConfig.php @@ -15,6 +15,11 @@ class IntegratorConfig { + /** + * @var string + */ + public const INTEGRATOR_LOCK = 'integrator.lock'; + /** * @var string */ @@ -366,7 +371,7 @@ protected function getDefaultConfigPath(): string */ public function getIntegratorLockFilePath(): string { - return $this->getProjectRootDirectory() . 'integrator.lock'; + return $this->getProjectRootDirectory() . static::INTEGRATOR_LOCK; } /** diff --git a/src/IntegratorFactory.php b/src/IntegratorFactory.php index 63b27cd3..04da197f 100644 --- a/src/IntegratorFactory.php +++ b/src/IntegratorFactory.php @@ -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()); } /** diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index e373b2a3..d840583f 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -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 */ @@ -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; } /** @@ -41,14 +54,24 @@ 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']); + } } /** @@ -56,10 +79,10 @@ public function deleteLock(): void * * @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(); } } diff --git a/tests/SprykerSdkTest/Integrator/AbstractIntegratorTestCase.php b/tests/SprykerSdkTest/Integrator/AbstractIntegratorTestCase.php index 7deb3dbe..4b8df706 100644 --- a/tests/SprykerSdkTest/Integrator/AbstractIntegratorTestCase.php +++ b/tests/SprykerSdkTest/Integrator/AbstractIntegratorTestCase.php @@ -63,7 +63,7 @@ public static function tearDownAfterClass(): void } /** - * @return \SprykerSdk\Integrator\Business\IntegratorFacade + * @return \SprykerSdk\Integrator\IntegratorFacade */ protected function createIntegratorFacade(): IntegratorFacade { diff --git a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php index 01506368..494918dd 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php @@ -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); - } } diff --git a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php index 336875ec..03207d0d 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php @@ -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 */ @@ -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); @@ -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)); @@ -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(); @@ -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()); } /** @@ -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; } @@ -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; + } }