Skip to content

Commit

Permalink
Fix hg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 11, 2024
1 parent 1d44725 commit 9180bec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Monolog/Processor/MercurialProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ private static function getMercurialInfo(): array
}

$result = explode(' ', trim((string) shell_exec('hg id -nb')));

if (\count($result) >= 3) {
return self::$cache = [
'branch' => $result[1],
'revision' => $result[2],
];
}
if (\count($result) === 2) {
return self::$cache = [
'branch' => $result[1],
'revision' => $result[0],
];
}

return self::$cache = [];
}
Expand Down
46 changes: 43 additions & 3 deletions tests/Monolog/Processor/MercurialProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@

class MercurialProcessorTest extends TestCase
{
private string $oldCwd;
private string $testDir;

protected function setUp(): void
{
parent::setUp();

$this->oldCwd = getcwd();
$this->testDir = sys_get_temp_dir().'/monolog-processor-mercurial-test';

mkdir($this->testDir, recursive: true);
chdir($this->testDir);
}

protected function tearDown(): void
{
parent::tearDown();

chdir($this->oldCwd);

if (!file_exists($this->testDir)) {
return;
}
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->testDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($items as $item) {
$item->isDir() ? rmdir((string) $item) : unlink((string) $item);
}

rmdir($this->testDir);
}

/**
* @covers Monolog\Processor\MercurialProcessor::__invoke
*/
Expand All @@ -31,12 +66,17 @@ public function testProcessor()
return;
}

`hg init`;
exec('hg init');
exec('hg branch default');
touch('test.txt');
exec('hg add test.txt');
exec('hg commit -u foo -m "initial commit"');

$processor = new MercurialProcessor();
$record = $processor($this->getRecord());

$this->assertArrayHasKey('hg', $record->extra);
$this->assertTrue(!\is_array($record->extra['hg']['branch']));
$this->assertTrue(!\is_array($record->extra['hg']['revision']));
$this->assertSame('default', $record->extra['hg']['branch']);
$this->assertSame('0', $record->extra['hg']['revision']);
}
}

0 comments on commit 9180bec

Please sign in to comment.