Skip to content

Commit

Permalink
Normalise all the things for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Jan 23, 2025
1 parent bb8a463 commit 1b30f41
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
10 changes: 8 additions & 2 deletions src/ComposerIntegration/PieJsonEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function str_replace;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class PieJsonEditor
Expand Down Expand Up @@ -77,7 +78,7 @@ public function addRepository(
$this->pieJsonFilename,
),
))
->addRepository($url, [
->addRepository($this->normaliseRepositoryName($url), [
'type' => $type,
'url' => $url,
]);
Expand All @@ -101,8 +102,13 @@ public function removeRepository(
$this->pieJsonFilename,
),
))
->removeRepository($name);
->removeRepository($this->normaliseRepositoryName($name));

return $originalPieJsonContent;
}

private function normaliseRepositoryName(string $url): string
{
return str_replace('\\', '/', $url);
}
}
7 changes: 4 additions & 3 deletions test/unit/Command/CommandHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

use function array_combine;
use function array_map;
use function str_replace;
use function trim;

#[CoversClass(CommandHelper::class)]
Expand Down Expand Up @@ -217,14 +218,14 @@ public function testListRepositories(): void
CommandHelper::listRepositories($composer, $output);

self::assertSame(
<<<'OUTPUT'
str_replace("\r\n", "\n", <<<'OUTPUT'
The following repositories are in use for this Target PHP:
- Packagist
- Composer (https://repo.packagist.com/example)
- VCS Repository (https://github.com/php/pie)
- Path Repository (/path/to/repo)
OUTPUT,
trim($output->fetch()),
OUTPUT),
str_replace("\r\n", "\n", trim($output->fetch())),
);
}
}
49 changes: 35 additions & 14 deletions test/unit/ComposerIntegration/PieJsonEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use PHPUnit\Framework\TestCase;

use function file_get_contents;
use function json_decode;
use function json_encode;
use function sys_get_temp_dir;
use function trim;
use function uniqid;

use const DIRECTORY_SEPARATOR;
Expand All @@ -27,7 +28,10 @@ public function testCreatingPieJson(): void
(new PieJsonEditor($testPieJson))->ensureExists();

self::assertFileExists($testPieJson);
self::assertSame("{\n}\n", file_get_contents($testPieJson));
self::assertSame(
$this->normaliseJson("{\n}\n"),
$this->normaliseJson(file_get_contents($testPieJson)),
);
}

public function testCanAddRequire(): void
Expand All @@ -39,14 +43,14 @@ public function testCanAddRequire(): void

$editor->addRequire('foo/bar', '^1.2');
self::assertSame(
<<<'EOF'
$this->normaliseJson(<<<'EOF'
{
"require": {
"foo/bar": "^1.2"
}
}
EOF,
trim(file_get_contents($testPieJson)),
EOF),
$this->normaliseJson(file_get_contents($testPieJson)),
);
}

Expand All @@ -58,7 +62,10 @@ public function testCanRevert(): void
$editor->ensureExists();
$originalContent = $editor->addRequire('foo/bar', '^1.2');
$editor->revert($originalContent);
self::assertSame($originalContent, file_get_contents($testPieJson));
self::assertSame(
$this->normaliseJson($originalContent),
$this->normaliseJson(file_get_contents($testPieJson)),
);
}

public function testCanAddAndRemoveRepositories(): void
Expand All @@ -73,9 +80,12 @@ public function testCanAddAndRemoveRepositories(): void
'https://github.com/php/pie',
);

self::assertSame("{\n}\n", $originalContent);
self::assertSame(
$this->normaliseJson("{\n}\n"),
$this->normaliseJson($originalContent),
);

$expectedRepoContent = <<<'EOF'
$expectedRepoContent = $this->normaliseJson(<<<'EOF'
{
"repositories": {
"https://github.com/php/pie": {
Expand All @@ -84,21 +94,32 @@ public function testCanAddAndRemoveRepositories(): void
}
}
}
EOF;
EOF);

self::assertSame($expectedRepoContent, trim(file_get_contents($testPieJson)));
self::assertSame(
$expectedRepoContent,
$this->normaliseJson(file_get_contents($testPieJson)),
);

$originalContent2 = $editor->removeRepository('https://github.com/php/pie');
self::assertSame($expectedRepoContent, trim($originalContent2));
self::assertSame(
$expectedRepoContent,
$this->normaliseJson($originalContent2),
);

self::assertSame(
<<<'EOF'
$this->normaliseJson(<<<'EOF'
{
"repositories": {
}
}
EOF,
trim(file_get_contents($testPieJson)),
EOF),
$this->normaliseJson(file_get_contents($testPieJson)),
);
}

private function normaliseJson(string $fileContent): string
{
return json_encode(json_decode($fileContent));
}
}

0 comments on commit 1b30f41

Please sign in to comment.