Skip to content

Commit 15e6f10

Browse files
committed
[agents] Synchronize top-level packaged agent files (#185)
1 parent 19c0967 commit 15e6f10

2 files changed

Lines changed: 51 additions & 19 deletions

File tree

src/Sync/PackagedDirectorySynchronizer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ public function synchronize(string $targetDir, string $packagePath, string $dire
7878

7979
$finder = $this->finderFactory
8080
->create()
81-
->directories()
8281
->in($packagePath)
8382
->depth('== 0');
8483

85-
foreach ($finder as $packagedDirectory) {
86-
$entryName = $packagedDirectory->getFilename();
84+
foreach ($finder as $packagedEntry) {
85+
$entryName = $packagedEntry->getFilename();
8786
$targetLink = Path::makeAbsolute($entryName, $targetDir);
88-
$sourcePath = $packagedDirectory->getRealPath();
87+
$sourcePath = $packagedEntry->getRealPath();
8988

9089
$this->processLink($entryName, $targetLink, $sourcePath, $result);
9190
}

tests/Sync/PackagedDirectorySynchronizerTest.php

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function synchronizeWithMissingTargetDirWillCreateItAndCreateLinks(): voi
123123
$entryPath = '/package/.agents/agents/issue-editor';
124124
$relativeEntryPath = '../../../package/.agents/agents/issue-editor';
125125

126-
$this->mockFinder($this->createDirectory('issue-editor', $entryPath));
126+
$this->mockFinder($this->createEntry('issue-editor', $entryPath));
127127

128128
$this->filesystem->exists('/package/.agents/agents')
129129
->willReturn(true);
@@ -161,7 +161,7 @@ public function synchronizeWillPreserveExistingValidSymlink(): void
161161
$entryPath = '/package/.agents/agents/issue-editor';
162162
$targetLink = '/consumer/.agents/agents/issue-editor';
163163

164-
$this->mockFinder($this->createDirectory('issue-editor', $entryPath));
164+
$this->mockFinder($this->createEntry('issue-editor', $entryPath));
165165

166166
$this->filesystem->exists('/package/.agents/agents')
167167
->willReturn(true);
@@ -197,7 +197,7 @@ public function synchronizeWillRepairBrokenSymlink(): void
197197
$brokenPath = '/obsolete/.agents/agents/issue-editor';
198198
$relativeEntryPath = '../../../package/.agents/agents/issue-editor';
199199

200-
$this->mockFinder($this->createDirectory('issue-editor', $entryPath));
200+
$this->mockFinder($this->createEntry('issue-editor', $entryPath));
201201

202202
$this->filesystem->exists('/package/.agents/agents')
203203
->willReturn(true);
@@ -243,7 +243,7 @@ public function synchronizeWillPreserveExistingNonSymlinkDirectory(): void
243243
$entryPath = '/package/.agents/agents/issue-editor';
244244
$targetLink = '/consumer/.agents/agents/issue-editor';
245245

246-
$this->mockFinder($this->createDirectory('issue-editor', $entryPath));
246+
$this->mockFinder($this->createEntry('issue-editor', $entryPath));
247247

248248
$this->filesystem->exists('/package/.agents/agents')
249249
->willReturn(true);
@@ -266,28 +266,61 @@ public function synchronizeWillPreserveExistingNonSymlinkDirectory(): void
266266
}
267267

268268
/**
269-
* @param SplFileInfo $directories
269+
* @return void
270+
*/
271+
#[Test]
272+
public function synchronizeWillCreateLinksForTopLevelFiles(): void
273+
{
274+
$entryPath = '/package/.agents/agents/issue-editor.md';
275+
$targetLink = '/consumer/.agents/agents/issue-editor.md';
276+
$relativeEntryPath = '../../../package/.agents/agents/issue-editor.md';
277+
278+
$this->mockFinder($this->createEntry('issue-editor.md', $entryPath));
279+
280+
$this->filesystem->exists('/package/.agents/agents')
281+
->willReturn(true);
282+
$this->filesystem->exists('/consumer/.agents/agents')
283+
->willReturn(true);
284+
$this->filesystem->exists($targetLink)
285+
->willReturn(false);
286+
$this->filesystem->dirname($targetLink)
287+
->willReturn('/consumer/.agents/agents')
288+
->shouldBeCalledOnce();
289+
$this->filesystem->makePathRelative($entryPath, '/consumer/.agents/agents')
290+
->willReturn($relativeEntryPath)
291+
->shouldBeCalledOnce();
292+
$this->filesystem->symlink($relativeEntryPath, $targetLink)
293+
->shouldBeCalledOnce();
294+
$this->logger->info('Created link: issue-editor.md -> ' . $relativeEntryPath)
295+
->shouldBeCalledOnce();
296+
297+
$result = $this->createSynchronizer()
298+
->synchronize('/consumer/.agents/agents', '/package/.agents/agents', '.agents/agents');
299+
300+
self::assertFalse($result->failed());
301+
self::assertSame(['issue-editor.md'], $result->getCreatedLinks());
302+
}
303+
304+
/**
305+
* @param SplFileInfo $entries
270306
*
271307
* @return void
272308
*/
273-
private function mockFinder(SplFileInfo ...$directories): void
309+
private function mockFinder(SplFileInfo ...$entries): void
274310
{
275311
$finder = $this->finder->reveal();
276312

277313
$this->finderFactory->create()
278314
->willReturn($finder)
279315
->shouldBeCalledOnce();
280-
$this->finder->directories()
281-
->willReturn($finder)
282-
->shouldBeCalledOnce();
283316
$this->finder->in('/package/.agents/agents')
284317
->willReturn($finder)
285318
->shouldBeCalledOnce();
286319
$this->finder->depth('== 0')
287320
->willReturn($finder)
288321
->shouldBeCalledOnce();
289322
$this->finder->getIterator()
290-
->willReturn(new ArrayIterator($directories));
323+
->willReturn(new ArrayIterator($entries));
291324
}
292325

293326
/**
@@ -296,15 +329,15 @@ private function mockFinder(SplFileInfo ...$directories): void
296329
*
297330
* @return SplFileInfo
298331
*/
299-
private function createDirectory(string $entryName, string $sourcePath): SplFileInfo
332+
private function createEntry(string $entryName, string $sourcePath): SplFileInfo
300333
{
301-
$directory = $this->prophesize(SplFileInfo::class);
302-
$directory->getFilename()
334+
$entry = $this->prophesize(SplFileInfo::class);
335+
$entry->getFilename()
303336
->willReturn($entryName);
304-
$directory->getRealPath()
337+
$entry->getRealPath()
305338
->willReturn($sourcePath);
306339

307-
return $directory->reveal();
340+
return $entry->reveal();
308341
}
309342

310343
/**

0 commit comments

Comments
 (0)