Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI-1344: [push:artifact] support pushing to master #1763

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Command/Push/PushArtifactCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
82 changes: 49 additions & 33 deletions tests/phpunit/src/Commands/Push/PushArtifactCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -43,7 +57,7 @@ public function testPushArtifact(): void {
// Choose an Acquia environment:
0,
];
$this->executeCommand([], $inputs);
$this->executeCommand([], $inputs, $verbosity);

$output = $this->getDisplay();

Expand All @@ -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 ([email protected]: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 ([email protected]:site.git)', $output);
}
}

public function testPushTagArtifact(): void {
Expand Down Expand Up @@ -212,7 +228,7 @@ public function testPushArtifactNoClone(): void {
$this->assertStringNotContainsString('Pushing changes to Acquia Git ([email protected]: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');
Expand All @@ -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();
Expand All @@ -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();
}

Expand All @@ -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();
}
}
Expand Down