Skip to content

Commit

Permalink
SDK-5690: Cleanup integration.lock and add it into .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
vol4onok committed Dec 13, 2023
1 parent 322cd1a commit 9ea3594
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 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);

Check warning on line 44 in src/Console/IntegratorLockUpdaterConsole.php

View check run for this annotation

Codecov / codecov/patch

src/Console/IntegratorLockUpdaterConsole.php#L43-L44

Added lines #L43 - L44 were not covered by tests

$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
21 changes: 14 additions & 7 deletions src/IntegratorLock/IntegratorLockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

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

/**
* @var \SprykerSdk\Integrator\IntegratorConfig
*/
Expand Down Expand Up @@ -41,25 +46,27 @@ 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)]);
}

/**
* @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 !$process->getOutput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 9ea3594

Please sign in to comment.