From 9ea35949dad9d0ddcd274861345b61967c12dc4e Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 21:19:34 +0200 Subject: [PATCH 01/12] SDK-5690: Cleanup integration.lock and add it into .gitignore --- src/Console/IntegratorLockUpdaterConsole.php | 6 +++--- src/IntegratorConfig.php | 7 ++++++- src/IntegratorLock/IntegratorLockCleaner.php | 21 ++++++++++++------- .../IntegratorLock/IntegratorLockTest.php | 18 ++++++++++++---- 4 files changed, 37 insertions(+), 15 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/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index e373b2a3..d1624091 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -14,6 +14,11 @@ class IntegratorLockCleaner implements IntegratorLockCleanerInterface { + /** + * @var string + */ + protected const GITIGNORE_FILE = '.gitignore'; + /** * @var \SprykerSdk\Integrator\IntegratorConfig */ @@ -41,14 +46,16 @@ public function deleteLock(): void { $lockFilePath = $this->config->getIntegratorLockFilePath(); - unlink($lockFilePath); - - if (!$this->isLockFileChangeTrackedByGit($lockFilePath)) { + if ($this->isLockFileIgnoredByGit($lockFilePath)) { return; } + if (file_exists($lockFilePath)) { + unlink($lockFilePath); + } + $this->processExecutor->execute([sprintf('echo %s >> %s', $this->config::INTEGRATOR_LOCK, static::GITIGNORE_FILE)]); $this->processExecutor->execute(['git', 'add', $lockFilePath]); - $this->processExecutor->execute(['git', 'commit', '-m', 'Removed `integrator.lock` file.']); + $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK)]); } /** @@ -56,10 +63,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 !$process->getOutput(); } } diff --git a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php index 336875ec..2b99c066 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php @@ -15,6 +15,7 @@ use SprykerSdk\Integrator\IntegratorLock\IntegratorLockReader; use SprykerSdk\Integrator\IntegratorLock\IntegratorLockWriter; use SprykerSdkTest\Integrator\BaseTestCase; +use Symfony\Component\Process\Process; class IntegratorLockTest extends BaseTestCase { @@ -34,7 +35,8 @@ public function testWriteFileLock(): void ], ], ]; - $tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.'); + $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; + touch($tmpIntegratorLockFilePath); $integratorLockWriter = $this->createIntegratorLockWriter($tmpIntegratorLockFilePath); $integratorLockWriter->storeLock($lockData); @@ -52,7 +54,8 @@ public function testWriteFileLock(): void */ public function testReadFileLock(): void { - $tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.'); + $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; + touch($tmpIntegratorLockFilePath); $compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json'; file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath)); @@ -71,7 +74,8 @@ public function testReadFileLock(): void */ public function testDeleteFileLock(): void { - $tmpIntegratorLockFilePath = tempnam(sys_get_temp_dir(), 'integrator.lock.'); + $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; + touch($tmpIntegratorLockFilePath); $compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json'; file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath)); @@ -109,7 +113,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(IntegratorConfig::INTEGRATOR_LOCK); + + $processExecutorMock = $this->createMock(ProcessExecutor::class); + $processExecutorMock->method('execute')->willReturn($processMock); + + return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock); } /** From 860be52654fb9797ec9fd9ecd4b4638a43fd92f1 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 21:40:35 +0200 Subject: [PATCH 02/12] SDK-5690: Cleanup integration.lock and add it into .gitignore --- src/IntegratorLock/IntegratorLockCleaner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index d1624091..68f4cfc0 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -67,6 +67,6 @@ protected function isLockFileIgnoredByGit(string $filepath): bool { $process = $this->processExecutor->execute(['git', 'check-ignore', $filepath]); - return !$process->getOutput(); + return (bool)$process->getOutput(); } } From c8e7293757d0b17ed9051799e37e5faf23b1f7c9 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 21:51:17 +0200 Subject: [PATCH 03/12] SDK-5690: Cleanup integration.lock and add it into .gitignore --- bin/integrator | 2 +- src/IntegratorLock/IntegratorLockCleaner.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/integrator b/bin/integrator index a0020d35..aa14795e 100755 --- a/bin/integrator +++ b/bin/integrator @@ -3,7 +3,7 @@ defined('APPLICATION_ROOT_DIR') || define('APPLICATION_ROOT_DIR', getcwd() ); -defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__, 4) ); +defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__) ); defined('APPLICATION_SOURCE_DIR') || define('APPLICATION_SOURCE_DIR', APPLICATION_ROOT_DIR . DIRECTORY_SEPARATOR . 'src'); diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index 68f4cfc0..dd6b3f71 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -55,7 +55,7 @@ public function deleteLock(): void $this->processExecutor->execute([sprintf('echo %s >> %s', $this->config::INTEGRATOR_LOCK, static::GITIGNORE_FILE)]); $this->processExecutor->execute(['git', 'add', $lockFilePath]); - $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK)]); + $process = $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK), '-n']); } /** From baa511fdf9b62c6df4de2f54d9baddd88a6c3d19 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:20:51 +0200 Subject: [PATCH 04/12] SDK-5690: Added fixes --- .gitignore | 1 + src/IntegratorLock/IntegratorLockCleaner.php | 10 +++++++--- .../Integrator/IntegratorLock/IntegratorLockTest.php | 5 ++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ddb5614c..c8459617 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ tests/tmp # resources data/ +integrator.lock diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index dd6b3f71..6e5b0cde 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -52,10 +52,14 @@ public function deleteLock(): void if (file_exists($lockFilePath)) { unlink($lockFilePath); } + $gitignorePath = $this->config->getProjectRootDirectory() . static::GITIGNORE_FILE; - $this->processExecutor->execute([sprintf('echo %s >> %s', $this->config::INTEGRATOR_LOCK, static::GITIGNORE_FILE)]); - $this->processExecutor->execute(['git', 'add', $lockFilePath]); - $process = $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK), '-n']); + if (strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) { + file_put_contents($gitignorePath, $this->config::INTEGRATOR_LOCK . PHP_EOL, FILE_APPEND); + } + + $this->processExecutor->execute(['git', 'add', $lockFilePath, $gitignorePath]); + $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK), '-n']); } /** diff --git a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php index 2b99c066..58779313 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php @@ -76,9 +76,8 @@ public function testDeleteFileLock(): void { $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; touch($tmpIntegratorLockFilePath); - $compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json'; - file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath)); + file_put_contents($tmpIntegratorLockFilePath, 'test'); $integratorLockCleaner = $this->createIntegratorLockCleaner($tmpIntegratorLockFilePath); $integratorLockCleaner->deleteLock(); @@ -114,7 +113,7 @@ private function createIntegratorLockReader(string $tmpIntegratorLockFilePath): private function createIntegratorLockCleaner(string $tmpIntegratorLockFilePath): IntegratorLockCleaner { $processMock = $this->createMock(Process::class); - $processMock->method('getOutput')->willReturn(IntegratorConfig::INTEGRATOR_LOCK); + $processMock->method('getOutput')->willReturn(''); $processExecutorMock = $this->createMock(ProcessExecutor::class); $processExecutorMock->method('execute')->willReturn($processMock); From 643a691f0a8dcfa138ebee573930b38d7b00e711 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:22:00 +0200 Subject: [PATCH 05/12] SDK-5690: Reverted debug --- bin/integrator | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/integrator b/bin/integrator index aa14795e..a0020d35 100755 --- a/bin/integrator +++ b/bin/integrator @@ -3,7 +3,7 @@ defined('APPLICATION_ROOT_DIR') || define('APPLICATION_ROOT_DIR', getcwd() ); -defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__) ); +defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__, 4) ); defined('APPLICATION_SOURCE_DIR') || define('APPLICATION_SOURCE_DIR', APPLICATION_ROOT_DIR . DIRECTORY_SEPARATOR . 'src'); From 4bfed471ab631eb9d7a03f492e54834703c78aa3 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:41:59 +0200 Subject: [PATCH 06/12] Removed `integrator.lock` file. --- .../Integrator/IntegratorFacadeTest.php | 18 ------------------ 1 file changed, 18 deletions(-) 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); - } } From 23d534aa94748d00433a732fa1e9c0b0c7eaa18e Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:42:27 +0200 Subject: [PATCH 07/12] SDK-5690: Fixed tests --- src/IntegratorLock/IntegratorLockCleaner.php | 4 ++-- .../Integrator/IntegratorFacadeTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index 6e5b0cde..6e00de8e 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -46,7 +46,7 @@ public function deleteLock(): void { $lockFilePath = $this->config->getIntegratorLockFilePath(); - if ($this->isLockFileIgnoredByGit($lockFilePath)) { + if ($this->isLockFileIgnoredByGit($lockFilePath) && !file_exists($lockFilePath)) { return; } if (file_exists($lockFilePath)) { @@ -54,7 +54,7 @@ public function deleteLock(): void } $gitignorePath = $this->config->getProjectRootDirectory() . static::GITIGNORE_FILE; - if (strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) { + if (!file_exists($gitignorePath) || strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) { file_put_contents($gitignorePath, $this->config::INTEGRATOR_LOCK . PHP_EOL, FILE_APPEND); } diff --git a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php index 494918dd..74a89e5a 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php @@ -561,4 +561,23 @@ public function testRunUpdateLock(): void $this->assertNotEmpty(trim(file_get_contents($integratorLock))); } + + /** + * @group test1 + * @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); + } } From 8e2075ddf996d1eecf25948e15bf0fcedb7abbb9 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:49:20 +0200 Subject: [PATCH 08/12] SDK-5690: Fixed tests --- bin/integrator | 2 +- tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/integrator b/bin/integrator index a0020d35..aa14795e 100755 --- a/bin/integrator +++ b/bin/integrator @@ -3,7 +3,7 @@ defined('APPLICATION_ROOT_DIR') || define('APPLICATION_ROOT_DIR', getcwd() ); -defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__, 4) ); +defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__) ); defined('APPLICATION_SOURCE_DIR') || define('APPLICATION_SOURCE_DIR', APPLICATION_ROOT_DIR . DIRECTORY_SEPARATOR . 'src'); diff --git a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php index 74a89e5a..98e49e75 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php @@ -564,6 +564,7 @@ public function testRunUpdateLock(): void /** * @group test1 + * * @return void */ public function testRunCleanLock(): void From 35bb22b884fabf9b13fc78f7e0f2609d71f6b6cd Mon Sep 17 00:00:00 2001 From: vol4onok Date: Wed, 13 Dec 2023 22:50:52 +0200 Subject: [PATCH 09/12] SDK-5690: Fixed tests --- bin/integrator | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/integrator b/bin/integrator index aa14795e..a0020d35 100755 --- a/bin/integrator +++ b/bin/integrator @@ -3,7 +3,7 @@ defined('APPLICATION_ROOT_DIR') || define('APPLICATION_ROOT_DIR', getcwd() ); -defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__) ); +defined('INTEGRATOR_ROOT_DIR') || define('INTEGRATOR_ROOT_DIR', dirname(__DIR__, 4) ); defined('APPLICATION_SOURCE_DIR') || define('APPLICATION_SOURCE_DIR', APPLICATION_ROOT_DIR . DIRECTORY_SEPARATOR . 'src'); From 1a4f2ea6bb6ed4c1d0026eae413b7b8e3742d694 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 14 Dec 2023 12:11:40 +0200 Subject: [PATCH 10/12] SDK-5690: Refactoring --- src/IntegratorFactory.php | 2 +- src/IntegratorLock/IntegratorLockCleaner.php | 20 +++++++++++----- .../Integrator/AbstractIntegratorTestCase.php | 2 +- .../Integrator/IntegratorFacadeTest.php | 2 -- .../IntegratorLock/IntegratorLockTest.php | 23 +++++++++++++------ 5 files changed, 32 insertions(+), 17 deletions(-) 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 6e00de8e..d8dc35dd 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -11,6 +11,7 @@ use SprykerSdk\Integrator\Executor\ProcessExecutor; use SprykerSdk\Integrator\IntegratorConfig; +use Symfony\Component\Filesystem\Filesystem; class IntegratorLockCleaner implements IntegratorLockCleanerInterface { @@ -29,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; } /** @@ -46,16 +54,16 @@ public function deleteLock(): void { $lockFilePath = $this->config->getIntegratorLockFilePath(); - if ($this->isLockFileIgnoredByGit($lockFilePath) && !file_exists($lockFilePath)) { + if ($this->isLockFileIgnoredByGit($lockFilePath) && !$this->filesystem->exists($lockFilePath)) { return; } - if (file_exists($lockFilePath)) { - unlink($lockFilePath); + if ($this->filesystem->exists($lockFilePath)) { + $this->filesystem->remove($lockFilePath); } $gitignorePath = $this->config->getProjectRootDirectory() . static::GITIGNORE_FILE; - if (!file_exists($gitignorePath) || strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) { - file_put_contents($gitignorePath, $this->config::INTEGRATOR_LOCK . PHP_EOL, FILE_APPEND); + 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); } $this->processExecutor->execute(['git', 'add', $lockFilePath, $gitignorePath]); 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 98e49e75..01506368 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php @@ -563,8 +563,6 @@ public function testRunUpdateLock(): void } /** - * @group test1 - * * @return void */ public function testRunCleanLock(): void diff --git a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php index 58779313..e4c8036a 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php @@ -15,6 +15,7 @@ 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 @@ -35,8 +36,7 @@ public function testWriteFileLock(): void ], ], ]; - $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; - touch($tmpIntegratorLockFilePath); + $tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath(); $integratorLockWriter = $this->createIntegratorLockWriter($tmpIntegratorLockFilePath); $integratorLockWriter->storeLock($lockData); @@ -54,8 +54,7 @@ public function testWriteFileLock(): void */ public function testReadFileLock(): void { - $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; - touch($tmpIntegratorLockFilePath); + $tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath(); $compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json'; file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath)); @@ -74,8 +73,7 @@ public function testReadFileLock(): void */ public function testDeleteFileLock(): void { - $tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK; - touch($tmpIntegratorLockFilePath); + $tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath(); file_put_contents($tmpIntegratorLockFilePath, 'test'); @@ -118,7 +116,7 @@ private function createIntegratorLockCleaner(string $tmpIntegratorLockFilePath): $processExecutorMock = $this->createMock(ProcessExecutor::class); $processExecutorMock->method('execute')->willReturn($processMock); - return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock); + return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock, new Filesystem()); } /** @@ -145,4 +143,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; + } } From 0bd3e8bbade91b4386275474a072c79dc34caa75 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 14 Dec 2023 12:55:08 +0200 Subject: [PATCH 11/12] SDK-5690: Refactoring --- src/IntegratorLock/IntegratorLockCleaner.php | 12 ++++++++---- .../Integrator/IntegratorFacadeTest.php | 18 ------------------ 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/IntegratorLock/IntegratorLockCleaner.php b/src/IntegratorLock/IntegratorLockCleaner.php index d8dc35dd..d840583f 100644 --- a/src/IntegratorLock/IntegratorLockCleaner.php +++ b/src/IntegratorLock/IntegratorLockCleaner.php @@ -54,20 +54,24 @@ public function deleteLock(): void { $lockFilePath = $this->config->getIntegratorLockFilePath(); - if ($this->isLockFileIgnoredByGit($lockFilePath) && !$this->filesystem->exists($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; 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']); } - - $this->processExecutor->execute(['git', 'add', $lockFilePath, $gitignorePath]); - $this->processExecutor->execute(['git', 'commit', '-m', sprintf('Removed `%s` file.', $this->config::INTEGRATOR_LOCK), '-n']); } /** 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); - } } From 6ead79654fd94391d144af7b378880f8a1bb9ed9 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 14 Dec 2023 13:21:37 +0200 Subject: [PATCH 12/12] SDK-5690: Refactoring --- .gitignore | 1 - .../Integrator/IntegratorLock/IntegratorLockTest.php | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c8459617..ddb5614c 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,3 @@ tests/tmp # resources data/ -integrator.lock diff --git a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php index e4c8036a..03207d0d 100644 --- a/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php +++ b/tests/SprykerSdkTest/Integrator/IntegratorLock/IntegratorLockTest.php @@ -20,6 +20,15 @@ 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 */ @@ -130,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; }