Skip to content

Commit 66d5e16

Browse files
committed
isMergeInProgress unit tests
1 parent 2acc701 commit 66d5e16

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/Traits/ConsoleHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Symfony\Component\Console\Input\StringInput;
99
use Symfony\Component\Console\Output\ConsoleOutput;
1010

11+
/**
12+
* @codeCoverageIgnore
13+
*/
1114
trait ConsoleHelper
1215
{
1316
use ProcessHelper,

src/Traits/GitHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public function updateCommitMessageContentInFile(string $path, string $message):
6262
*/
6363
public function isMergeInProgress()
6464
{
65-
$command = $this->runCommands('git merge HEAD &> /dev/null');
65+
$command = $this->runCommands('git merge HEAD', [
66+
'silent' => true,
67+
]);
6668

6769
// If a merge is in progress, the process returns code 128
6870
return $command->getExitCode() === 128;

tests/Unit/Traits/GitHelperTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,50 @@
5151
$this->updateCommitMessageContentInFile($gitFilePath, $newMessage);
5252
expect(file_get_contents($gitFilePath))->toBe($newMessage);
5353
});
54+
55+
test('isMergeInProgress returns true when a merge is in progress', function () {
56+
$testFileName = 'test.txt';
57+
58+
$this->makeTempFile('../'.$testFileName, 'Test merge');
59+
60+
$gitAddCommand = sprintf('git add %s', $testFileName);
61+
62+
// Generating a Fake Merge process
63+
$commandsToGenerateFakeMerge = [
64+
'git checkout -b main',
65+
$gitAddCommand,
66+
'git commit -m "Add test file"',
67+
'git push main',
68+
'git checkout -b test-branch',
69+
'git checkout main',
70+
"echo 'Test merge (edit on main)' > $testFileName",
71+
$gitAddCommand,
72+
'git commit -m "edited on main"',
73+
'git push main',
74+
'git checkout test-branch',
75+
"echo 'Test merge (edit on test-branch)' > $testFileName",
76+
$gitAddCommand,
77+
'git commit -m "edited on test-branch"',
78+
'git push test-branch',
79+
'git checkout main',
80+
'git merge test-branch',
81+
];
82+
83+
chdir(base_path());
84+
85+
$noOutputSuffix = ' > '.(PHP_OS_FAMILY == 'Windows' ? 'NUL' : '/dev/null 2>&1');
86+
foreach ($commandsToGenerateFakeMerge as $command) {
87+
if (strpos($command, 'git') === 0) {
88+
$command .= $noOutputSuffix;
89+
}
90+
shell_exec($command);
91+
}
92+
93+
expect($this->isMergeInProgress())->toBeTrue();
94+
});
95+
96+
test('isMergeInProgress returns false when a merge is not in progress', function () {
97+
chdir(base_path());
98+
shell_exec('git merge --abort');
99+
expect($this->isMergeInProgress())->toBeFalse();
100+
});

0 commit comments

Comments
 (0)