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
2 changes: 1 addition & 1 deletion src/StaticPHP/Package/PackageInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function run(bool $disable_delay_msg = false): void
$this->tracker?->startTracking($package, 'build');

if ($is_to_build && ($this->options['pack-mode'] ?? false) === true) {
$dirdiff = new DirDiff(BUILD_ROOT_PATH, false);
$dirdiff = $this->tracker?->copySnapshot() ?? new DirDiff(BUILD_ROOT_PATH, false);
ApplicationContext::set(DirDiff::class, $dirdiff);
}
/** @var LibraryPackage $package */
Expand Down
10 changes: 10 additions & 0 deletions src/StaticPHP/Util/BuildRootTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public function startTracking(Package $package, string $type = 'build'): void
$this->current_diff = new DirDiff(BUILD_ROOT_PATH, false);
}

/**
* Copy the current snapshot so packaging can reuse the baseline and reset its copy independently.
*
* @internal
*/
public function copySnapshot(): ?DirDiff
{
return $this->current_diff === null ? null : clone $this->current_diff;
}

/**
* Stop tracking and record the changes.
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/StaticPHP/Util/BuildRootTrackerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Tests\StaticPHP\Util;

use PHPUnit\Framework\TestCase;
use StaticPHP\Util\BuildRootTracker;
use StaticPHP\Util\DirDiff;
use StaticPHP\Util\FileSystem;

/**
* @internal
*/
class BuildRootTrackerTest extends TestCase
{
public function testPackagingCanResetItsCopyOfTheSnapshotIndependently(): void
{
$dir = tempnam(sys_get_temp_dir(), 'spc-buildroot-');
$this->assertNotFalse($dir);
unlink($dir);
mkdir($dir);

try {
$baseline = new DirDiff($dir);
$tracker = new class($baseline) extends BuildRootTracker {
public function __construct(DirDiff $baseline)
{
$this->current_diff = $baseline;
}
};

touch($dir . '/built.a');
$snapshot = $tracker->copySnapshot();
$this->assertNotNull($snapshot);
$this->assertSame(['built.a'], $snapshot->getIncrementFiles(true));

$snapshot->reset();
touch($dir . '/packed.a');
$this->assertSame(['packed.a'], array_values($snapshot->getIncrementFiles(true)));
$this->assertSame(['built.a', 'packed.a'], $baseline->getIncrementFiles(true));
} finally {
FileSystem::removeDir($dir);
}
}
}
Loading