Skip to content

Commit

Permalink
fix: blend json encoding flag + escape dot (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Jul 11, 2024
1 parent c622800 commit a274437
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Command/BlendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,17 @@ private function blendJsonPath(InputInterface $input, OutputInterface $output):
$path = $this->composer->getConfig()->getConfigSource()->getName();
$file = file_get_contents($path) ?: throw new \RuntimeException(sprintf('File "%s" not found.', $path));
$data = json_decode($file, true) ?: throw new \RuntimeException(sprintf('File "%s" is not JSON.', $path));
$pointers = explode('.', $jsonPath);
$pattern = '/(?<!\\\\)\./'; // Regex pattern to match a dot not preceded by a backslash
$pointers = preg_split($pattern, $jsonPath);

if (!$pointers) {
$output->writeln('No pointers.');
return 1;
}

foreach ($pointers as &$part) {
$part = str_replace('\.', '.', $part);
}

$p = $pointers;
$value = $data;
Expand Down Expand Up @@ -179,7 +189,7 @@ private function blendJsonPath(InputInterface $input, OutputInterface $output):

unset($ref);
$ref = $value;
$fileContent = file_put_contents($path, json_encode($json, JSON_PRETTY_PRINT));
$fileContent = file_put_contents($path, json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

if (!$fileContent) {
$output->writeln(sprintf('Could not write JSON at path "%s".', $path));
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/BlendCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public function testBlendJsonPath(): void {
$this->assertEquals("", $output->fetch());
}

public function testBlendJsonPathEscapeDot(): void {
$this->files = [__DIR__ . '/../monorepo/packages/A/composer.json', __DIR__ . '/../monorepo/packages/B/composer.json', __DIR__ . '/../monorepo/packages/C/composer.json'];
$this->backups = array_map('file_get_contents', $this->files);
$output = new BufferedOutput;
$this->application->run(new StringInput('blend --json-path=extra.branch-alias.dev-3\\\.4 --force'), $output);

foreach ($this->files as $f) {
$json = file_get_contents($f) ?: throw new RuntimeException;
/** @var array{extra?: array{branch-alias?: array<string, string>}} */
$new = json_decode($json, true);
$this->assertEquals($new['extra']['branch-alias']['dev-3.4'] ?? null, '3.4.x-dev');
}
$this->assertEquals("", $output->fetch());
}

protected function tearDown(): void {
while ($file = array_shift($this->files)) {
if ($b = array_shift($this->backups)) {
Expand Down
3 changes: 2 additions & 1 deletion tests/monorepo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"extra": {
"branch-alias": {
"dev-main": "3.3.x-dev"
"dev-main": "3.3.x-dev",
"dev-3.4": "3.4.x-dev"
},
"projects": [
"test/a",
Expand Down

0 comments on commit a274437

Please sign in to comment.