From a845434bba92ec28950389e56278630753389a82 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 2 Jul 2024 14:12:52 -0700 Subject: [PATCH] CLI-1344: [push:artifact] support pushing to master (#1763) * CLI-1344: [push:artifact] support pushing to master * kill mutant --- src/Command/Push/PushArtifactCommand.php | 2 +- .../Commands/Push/PushArtifactCommandTest.php | 82 +++++++++++-------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/Command/Push/PushArtifactCommand.php b/src/Command/Push/PushArtifactCommand.php index 556f59bfa..b94678466 100644 --- a/src/Command/Push/PushArtifactCommand.php +++ b/src/Command/Push/PushArtifactCommand.php @@ -180,7 +180,7 @@ private function cloneSourceBranch(Closure $outputCallback, string $artifactDir, if (!$process->isSuccessful()) { throw new AcquiaCliException('Failed to clone repository from the Cloud Platform: {message}', ['message' => $process->getErrorOutput()]); } - $process = $this->localMachineHelper->execute(['git', 'fetch', '--depth=1', $vcsUrl, $vcsPath . ':' . $vcsPath], $outputCallback, $artifactDir, ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL)); + $process = $this->localMachineHelper->execute(['git', 'fetch', '--depth=1', '--update-head-ok', $vcsUrl, $vcsPath . ':' . $vcsPath], $outputCallback, $artifactDir, ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL)); if (!$process->isSuccessful()) { // Remote branch does not exist. Just create it locally. This will create // the new branch off of the current commit. diff --git a/tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php b/tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php index fbef27e9a..32a35d6a6 100644 --- a/tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php +++ b/tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php @@ -9,6 +9,7 @@ use Acquia\Cli\Tests\Commands\Pull\PullCommandTestBase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; use Symfony\Component\Process\Process; @@ -27,12 +28,25 @@ public function testNoAuthenticationRequired(): void { $this->assertStringNotContainsString('This command requires authentication', $help); } - public function testPushArtifact(): void { + /** + * @return mixed[] + */ + public function providerTestPushArtifact(): array { + return [ + [OutputInterface::VERBOSITY_NORMAL, FALSE], + [OutputInterface::VERBOSITY_VERY_VERBOSE, TRUE], + ]; + } + + /** + * @dataProvider providerTestPushArtifact + */ + public function testPushArtifact(int $verbosity, bool $printOutput): void { $applications = $this->mockRequest('getApplications'); $this->mockRequest('getApplicationByUuid', $applications[0]->uuid); $environments = $this->mockRequest('getApplicationEnvironments', $applications[0]->uuid); $localMachineHelper = $this->mockLocalMachineHelper(); - $this->setUpPushArtifact($localMachineHelper, $environments[0]->vcs->path, [$environments[0]->vcs->url]); + $this->setUpPushArtifact($localMachineHelper, $environments[0]->vcs->path, [$environments[0]->vcs->url], 'master:master', TRUE, TRUE, TRUE, $printOutput); $inputs = [ // Would you like Acquia CLI to search for a Cloud application that matches your local git config? 'n', @@ -43,7 +57,7 @@ public function testPushArtifact(): void { // Choose an Acquia environment: 0, ]; - $this->executeCommand([], $inputs); + $this->executeCommand([], $inputs, $verbosity); $output = $this->getDisplay(); @@ -57,16 +71,18 @@ public function testPushArtifact(): void { $this->assertStringContainsString('- Compile the contents of vfs://root/project into an artifact', $output); $this->assertStringContainsString('- Copy the artifact files into the checked out copy of master', $output); $this->assertStringContainsString('- Commit changes and push the master branch', $output); - $this->assertStringContainsString('Removing', $output); - $this->assertStringContainsString('Initializing Git', $output); - $this->assertStringContainsString('Global .gitignore file', $output); - $this->assertStringContainsString('Removing vendor', $output); - $this->assertStringContainsString('Mirroring source', $output); - $this->assertStringContainsString('Installing Composer', $output); - $this->assertStringContainsString('Finding Drupal', $output); - $this->assertStringContainsString('Removing sensitive', $output); - $this->assertStringContainsString('Adding and committing', $output); - $this->assertStringContainsString('Pushing changes to Acquia Git (site@svn-3.hosted.acquia-sites.com:site.git)', $output); + if ($printOutput) { + $this->assertStringContainsString('Removing', $output); + $this->assertStringContainsString('Initializing Git', $output); + $this->assertStringContainsString('Global .gitignore file', $output); + $this->assertStringContainsString('Removing vendor', $output); + $this->assertStringContainsString('Mirroring source', $output); + $this->assertStringContainsString('Installing Composer', $output); + $this->assertStringContainsString('Finding Drupal', $output); + $this->assertStringContainsString('Removing sensitive', $output); + $this->assertStringContainsString('Adding and committing', $output); + $this->assertStringContainsString('Pushing changes to Acquia Git (site@svn-3.hosted.acquia-sites.com:site.git)', $output); + } } public function testPushTagArtifact(): void { @@ -212,7 +228,7 @@ public function testPushArtifactNoClone(): void { $this->assertStringNotContainsString('Pushing changes to Acquia Git (site@svn-3.hosted.acquia-sites.com:site.git)', $output); } - protected function setUpPushArtifact(ObjectProphecy $localMachineHelper, string $vcsPath, array $vcsUrls, string $destGitRef = 'master:master', bool $clone = TRUE, bool $commit = TRUE, bool $push = TRUE): void { + protected function setUpPushArtifact(ObjectProphecy $localMachineHelper, string $vcsPath, array $vcsUrls, string $destGitRef = 'master:master', bool $clone = TRUE, bool $commit = TRUE, bool $push = TRUE, bool $printOutput = TRUE): void { touch(Path::join($this->projectDir, 'composer.json')); mkdir(Path::join($this->projectDir, 'docroot')); $artifactDir = Path::join(sys_get_temp_dir(), 'acli-push-artifact'); @@ -225,52 +241,52 @@ protected function setUpPushArtifact(ObjectProphecy $localMachineHelper, string $this->mockExecuteGitStatus(FALSE, $localMachineHelper, $this->projectDir); $commitHash = 'abc123'; $this->mockGetLocalCommitHash($localMachineHelper, $this->projectDir, $commitHash); - $this->mockComposerInstall($localMachineHelper, $artifactDir); + $this->mockComposerInstall($localMachineHelper, $artifactDir, $printOutput); $this->mockReadComposerJson($localMachineHelper, $artifactDir); $localMachineHelper->checkRequiredBinariesExist(['git'])->shouldBeCalled(); if ($clone) { - $this->mockLocalGitConfig($localMachineHelper, $artifactDir); - $this->mockCloneShallow($localMachineHelper, $vcsPath, $vcsUrls[0], $artifactDir); + $this->mockLocalGitConfig($localMachineHelper, $artifactDir, $printOutput); + $this->mockCloneShallow($localMachineHelper, $vcsPath, $vcsUrls[0], $artifactDir, $printOutput); } if ($commit) { - $this->mockGitAddCommit($localMachineHelper, $artifactDir, $commitHash); + $this->mockGitAddCommit($localMachineHelper, $artifactDir, $commitHash, $printOutput); } if ($push) { - $this->mockGitPush($vcsUrls, $localMachineHelper, $artifactDir, $destGitRef); + $this->mockGitPush($vcsUrls, $localMachineHelper, $artifactDir, $destGitRef, $printOutput); } } - protected function mockCloneShallow(ObjectProphecy $localMachineHelper, string $vcsPath, string $vcsUrl, mixed $artifactDir): void { + protected function mockCloneShallow(ObjectProphecy $localMachineHelper, string $vcsPath, string $vcsUrl, string $artifactDir, bool $printOutput = TRUE): void { $process = $this->prophet->prophesize(Process::class); $process->isSuccessful()->willReturn(TRUE)->shouldBeCalled(); - $localMachineHelper->execute(['git', 'clone', '--depth=1', $vcsUrl, $artifactDir], Argument::type('callable'), NULL, TRUE) + $localMachineHelper->execute(['git', 'clone', '--depth=1', $vcsUrl, $artifactDir], Argument::type('callable'), NULL, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); - $localMachineHelper->execute(['git', 'fetch', '--depth=1', $vcsUrl, $vcsPath . ':' . $vcsPath], Argument::type('callable'), Argument::type('string'), TRUE) + $localMachineHelper->execute(['git', 'fetch', '--depth=1', '--update-head-ok', $vcsUrl, $vcsPath . ':' . $vcsPath], Argument::type('callable'), Argument::type('string'), $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); - $localMachineHelper->execute(['git', 'checkout', $vcsPath], Argument::type('callable'), Argument::type('string'), TRUE) + $localMachineHelper->execute(['git', 'checkout', $vcsPath], Argument::type('callable'), Argument::type('string'), $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); } - protected function mockLocalGitConfig(ObjectProphecy $localMachineHelper, string $artifactDir): void { + protected function mockLocalGitConfig(ObjectProphecy $localMachineHelper, string $artifactDir, bool $printOutput = TRUE): void { $process = $this->prophet->prophesize(Process::class); - $localMachineHelper->execute(['git', 'config', '--local', 'core.excludesFile', 'false'], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['git', 'config', '--local', 'core.excludesFile', 'false'], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); - $localMachineHelper->execute(['git', 'config', '--local', 'core.fileMode', 'true'], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['git', 'config', '--local', 'core.fileMode', 'true'], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); } - protected function mockComposerInstall(ObjectProphecy $localMachineHelper, mixed $artifactDir): void { + protected function mockComposerInstall(ObjectProphecy $localMachineHelper, mixed $artifactDir, bool $printOutput = TRUE): void { $localMachineHelper->checkRequiredBinariesExist(['composer'])->shouldBeCalled(); $process = $this->prophet->prophesize(Process::class); $process->isSuccessful()->willReturn(TRUE); - $localMachineHelper->execute(['composer', 'install', '--no-dev', '--no-interaction', '--optimize-autoloader'], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['composer', 'install', '--no-dev', '--no-interaction', '--optimize-autoloader'], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); } - protected function mockGitAddCommit(ObjectProphecy $localMachineHelper, mixed $artifactDir, mixed $commitHash): void { + protected function mockGitAddCommit(ObjectProphecy $localMachineHelper, string $artifactDir, string $commitHash, bool $printOutput): void { $process = $this->mockProcess(); - $localMachineHelper->execute(['git', 'add', '-A'], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['git', 'add', '-A'], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); $localMachineHelper->execute(['git', 'add', '-f', 'docroot/index.php'], NULL, $artifactDir, FALSE) ->willReturn($process->reveal())->shouldBeCalled(); @@ -280,7 +296,7 @@ protected function mockGitAddCommit(ObjectProphecy $localMachineHelper, mixed $a ->willReturn($process->reveal())->shouldBeCalled(); $localMachineHelper->execute(['git', 'add', '-f', 'vendor'], NULL, $artifactDir, FALSE) ->willReturn($process->reveal())->shouldBeCalled(); - $localMachineHelper->execute(['git', 'commit', '-m', "Automated commit by Acquia CLI (source commit: $commitHash)"], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['git', 'commit', '-m', "Automated commit by Acquia CLI (source commit: $commitHash)"], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); } @@ -303,10 +319,10 @@ protected function mockReadComposerJson(ObjectProphecy $localMachineHelper, stri ->willReturn($composerJson); } - protected function mockGitPush(array $gitUrls, ObjectProphecy $localMachineHelper, string $artifactDir, string $destGitRef): void { + protected function mockGitPush(array $gitUrls, ObjectProphecy $localMachineHelper, string $artifactDir, string $destGitRef, bool $printOutput): void { $process = $this->mockProcess(); foreach ($gitUrls as $gitUrl) { - $localMachineHelper->execute(['git', 'push', $gitUrl, $destGitRef], Argument::type('callable'), $artifactDir, TRUE) + $localMachineHelper->execute(['git', 'push', $gitUrl, $destGitRef], Argument::type('callable'), $artifactDir, $printOutput) ->willReturn($process->reveal())->shouldBeCalled(); } }