Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Configurator/AddLinesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ public function executeConfigure(Recipe $recipe, $config): void
continue;
}
$target = isset($patch['target']) ? $patch['target'] : null;
$createIfMissing = isset($patch['create_if_missing']) ? $patch['create_if_missing'] : null;

$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing);
$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing, $createIfMissing);
$this->fileContents[$file] = $newContents;
}
}
Expand Down Expand Up @@ -164,7 +165,7 @@ public function executeUnconfigure(Recipe $recipe, $config): void
}
}

private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing): string
private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing, ?string $createIfMissing = null): string
{
$fileContents = $this->readFile($file);

Expand Down Expand Up @@ -192,6 +193,15 @@ private function getPatchedContents(string $file, string $value, string $positio
break;
}
}

if (!$targetFound && null !== $createIfMissing) {
// Insert the create_if_missing content at the end of the file, then add the value after the target
$createIfMissingLines = explode("\n", $createIfMissing);
$lines = array_merge($lines, $createIfMissingLines);
$lines[] = $value;
$targetFound = true;
}

$fileContents = implode("\n", $lines);

if (!$targetFound) {
Expand Down
70 changes: 70 additions & 0 deletions tests/Configurator/AddLinesConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,66 @@ public function testSkippedIfTargetCannotBeFound()
$this->assertSame($originalContent, $this->readFile('webpack.config.js'));
}

public function testCreateIfMissingCreatesTargetAndAddsContent()
{
$this->copyFixture('ai_without_store.yaml', 'config/packages/ai.yaml');

$this->runConfigure([
[
'file' => 'config/packages/ai.yaml',
'position' => 'after_target',
'target' => ' store:',
'create_if_missing' => ' store:',
'content' => " azuresearch:\n default:\n endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'",
],
]);

$this->assertSame(
$this->loadFixture('ai_without_store_expected.yaml'),
$this->readFile('config/packages/ai.yaml')
);
}

public function testCreateIfMissingWithExistingTarget()
{
$this->copyFixture('ai_with_store.yaml', 'config/packages/ai.yaml');

$this->runConfigure([
[
'file' => 'config/packages/ai.yaml',
'position' => 'after_target',
'target' => ' store:',
'create_if_missing' => ' store:',
'content' => " azuresearch:\n default:\n endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'",
],
]);

$this->assertSame(
$this->loadFixture('ai_with_store_expected.yaml'),
$this->readFile('config/packages/ai.yaml')
);
}

public function testCreateIfMissingNotUsedWhenOptionNotSet()
{
$this->copyFixture('ai_without_store.yaml', 'config/packages/ai.yaml');

$this->runConfigure([
[
'file' => 'config/packages/ai.yaml',
'position' => 'after_target',
'target' => ' store:',
'content' => " azuresearch:\n default:\n endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'",
],
]);

// Content should remain unchanged since target was not found and no create_if_missing was provided
$this->assertSame(
$this->loadFixture('ai_without_store.yaml'),
$this->readFile('config/packages/ai.yaml')
);
}

public function testPatchIgnoredIfValueAlreadyExists()
{
$originalContents = <<<JS
Expand Down Expand Up @@ -677,6 +737,16 @@ private function readFile(string $filename): string
return file_get_contents(FLEX_TEST_DIR.'/'.$filename);
}

private function loadFixture(string $filename): string
{
return rtrim(file_get_contents(__DIR__.'/Fixtures/AddLines/'.$filename), "\n");
}

private function copyFixture(string $fixtureName, string $targetPath): void
{
$this->saveFile($targetPath, $this->loadFixture($fixtureName));
}

private function createComposerMockWithPackagesInstalled(array $packages)
{
$packages = array_map(fn ($package) => explode(':', $package), $packages);
Expand Down
5 changes: 5 additions & 0 deletions tests/Configurator/Fixtures/AddLines/ai_with_store.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ai:
store:
pinecone:
default:
api_key: 'xxx'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ai:
store:
azuresearch:
default:
endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'
pinecone:
default:
api_key: 'xxx'
4 changes: 4 additions & 0 deletions tests/Configurator/Fixtures/AddLines/ai_without_store.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ai:
platform:
default:
api_key: '%env(OPENAI_API_KEY)%'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ai:
platform:
default:
api_key: '%env(OPENAI_API_KEY)%'
store:
azuresearch:
default:
endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'