Skip to content

Commit

Permalink
[Feature] Add options expansion to Add Lines Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mnocon authored and fabpot committed Feb 5, 2024
1 parent 7b40eec commit 644e426
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Configurator/AddLinesConfigurator.php
Expand Up @@ -97,7 +97,7 @@ public function executeConfigure(Recipe $recipe, $config): void
}
$content = $patch['content'];

$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
$file = $this->path->concatenate([$this->options->get('root-dir'), $this->options->expandTargetDir($patch['file'])]);
$warnIfMissing = isset($patch['warn_if_missing']) && $patch['warn_if_missing'];
if (!is_file($file)) {
$this->write([
Expand Down Expand Up @@ -147,7 +147,7 @@ public function executeUnconfigure(Recipe $recipe, $config): void
// Ignore "requires": the target packages may have just become uninstalled.
// Checking for a "content" match is enough.

$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
$file = $this->path->concatenate([$this->options->get('root-dir'), $this->options->expandTargetDir($patch['file'])]);
if (!is_file($file)) {
continue;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Configurator/AddLinesConfiguratorTest.php
Expand Up @@ -67,6 +67,26 @@ public function testLinesAddedToTopOfFile()
$actualContents);
}

public function testExpandTargetDirWhenConfiguring()
{
$this->saveFile('config/file.txt', 'FirstLine');

$this->runConfigure([
[
'file' => '%CONFIG_DIR%/file.txt',
'position' => 'top',
'content' => 'NewFirstLine',
],
]);
$actualContents = $this->readFile('config/file.txt');
$this->assertSame(<<<EOF
NewFirstLine
FirstLine
EOF
,
$actualContents);
}

public function testLinesAddedToBottomOfFile()
{
$this->saveFile('assets/app.js', <<<EOF
Expand Down Expand Up @@ -318,6 +338,28 @@ public function testUnconfigure(string $originalContents, string $value, string
$this->assertSame($expectedContents, $actualContents);
}

public function testExpandTargetDirWhenUnconfiguring()
{
$this->saveFile('config/file.txt',
<<<EOF
Line1
Line2
EOF
);

$this->runUnconfigure([
[
'file' => '%CONFIG_DIR%/file.txt',
'content' => 'Line1',
],
]);
$actualContents = $this->readFile('config/file.txt');
$this->assertSame(<<<EOF
Line2
EOF
, $actualContents);
}

public function getUnconfigureTests()
{
yield 'found_middle' => [
Expand Down

0 comments on commit 644e426

Please sign in to comment.