rob-revisited/ide/tests/SvnTest.php

230 lines
12 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Async\Deferred;
use JumPHP\Ide\Async\Environment;
use JumPHP\Ide\Async\Processes;
use JumPHP\Ide\Svn\Svn;
use JumPHP\Ide\Svn\SvnAgentTools;
use PHPUnit\Framework\TestCase;
/** Subversion in the IDE (ide/src/Svn): svn's XML read, the commands built, and a real working copy when svn is installed. */
final class SvnTest extends TestCase
{
private const FIXTURES = __DIR__ . '/fixtures/svn';
/** @var list<string> */
private array $temp = [];
protected function tearDown(): void
{
foreach ($this->temp as $dir) {
exec('rm -rf ' . escapeshellarg($dir));
}
}
public function testTheStatusOfAWorkingCopyIsReadLikeGitsFiles(): void
{
$files = Svn::parseStatus((string)file_get_contents(self::FIXTURES . '/status.xml'));
$byPath = array_column($files, null, 'path');
self::assertSame(['a.txt', '[email protected]', 'new.txt', 'src/b.php'], array_keys($byPath));
self::assertSame(['M', 'modified'], [$byPath['a.txt']['code'], $byPath['a.txt']['status']]);
self::assertSame(['?', 'untracked'], [$byPath['new.txt']['code'], $byPath['new.txt']['status']]);
self::assertSame(['!', 'deleted'], [$byPath['src/b.php']['code'], $byPath['src/b.php']['status']], 'a missing file shows as deleted');
self::assertFalse($byPath['a.txt']['conflict']);
self::assertTrue($byPath['a.txt']['unstaged']);
}
public function testAConflictAfterAnUpdateIsMarked(): void
{
$files = array_column(Svn::parseStatus((string)file_get_contents(self::FIXTURES . '/status-conflict.xml')), null, 'path');
self::assertSame('conflict', $files['a.txt']['status']);
self::assertTrue($files['a.txt']['conflict']);
self::assertSame('C', $files['a.txt']['code']);
self::assertSame('untracked', $files['a.txt.mine']['status']);
}
public function testTheInfoGivesTheUrlRevisionAndBranch(): void
{
$info = Svn::parseInfo((string)file_get_contents(self::FIXTURES . '/info.xml'));
self::assertNotNull($info);
self::assertSame('file:///home/alice/work/repo/trunk', $info['url']);
self::assertSame('^/trunk', $info['relativeUrl']);
self::assertSame('file:///home/alice/work/repo', $info['repositoryRoot']);
self::assertSame(3, $info['revision']);
self::assertSame('/home/alice/work/wc', $info['wcRoot']);
self::assertSame('alice', $info['author']);
self::assertSame(gmmktime(13, 50, 7, 11, 15, 2026), $info['time']);
self::assertSame('trunk', Svn::branchOf($info['relativeUrl']));
self::assertSame('feature-x', Svn::branchOf('^/game/branches/feature-x/src'));
self::assertSame('tags/v1.0', Svn::branchOf('^/tags/v1.0'));
self::assertNull(Svn::parseInfo('svn: E155007: not a working copy'));
}
public function testTheLogHasRevisionsMessagesAndChangedPaths(): void
{
$log = Svn::parseLog((string)file_get_contents(self::FIXTURES . '/log.xml'));
self::assertSame([3, 2, 1], array_column($log, 'revision'));
self::assertSame('Second', $log[0]['message']);
self::assertSame("First files\n\nwith a body", $log[1]['message']);
self::assertSame('alice', $log[1]['author']);
self::assertSame([['action' => 'M', 'path' => '/trunk/a.txt', 'kind' => 'file', 'from' => null, 'fromRevision' => null]], $log[0]['paths']);
self::assertSame(['/trunk/a.txt', '/trunk/src', '/trunk/src/b.php'], array_column($log[1]['paths'], 'path'));
self::assertSame('dir', $log[1]['paths'][1]['kind']);
}
public function testBlameGivesTheRevisionOfEachLine(): void
{
$lines = Svn::parseBlame((string)file_get_contents(self::FIXTURES . '/blame.xml'));
self::assertSame([1, 2, 3, 4, 5], array_column($lines, 'line'));
self::assertSame([2, 3, 2, 3, 0], array_column($lines, 'revision'), 'line 5 is not committed yet (svn blames the working file)');
self::assertSame('alice', $lines[0]['author']);
self::assertGreaterThan(0, $lines[0]['time']);
}
public function testUpdateAndCommitOutputsAreRead(): void
{
$update = Svn::parseUpdate("Updating '.':\nU a.txt\nC src/b.php\n C lib\nA new/c.txt\nUpdated to revision 12.\nSummary of conflicts:\n Text conflicts: 1\n Tree conflicts: 1\n");
self::assertSame(12, $update['revision']);
self::assertSame(['src/b.php', 'lib'], $update['conflicts']);
self::assertSame(7, Svn::parseUpdate("Updating '.':\nAt revision 7.\n")['revision']);
self::assertSame(4, Svn::parseUpdate("Mise à jour de '.' :\nActualisé à la révision 4.\n")['revision'], 'a translated svn');
self::assertSame(['revision' => 5, 'sha' => 'r5'], Svn::committed("Adding new.txt\nTransmitting file data .done\nCommitting transaction...\nCommitted revision 5.\n"));
self::assertSame(['revision' => null, 'sha' => ''], Svn::committed(''));
}
public function testCommandsAreBuiltForTheLocalSvnOrForDocker(): void
{
$dir = $this->tempDir();
mkdir("$dir/wc/.svn", 0777, true);
mkdir("$dir/wc/games/pong", 0777, true);
self::assertSame("$dir/wc", Svn::workingCopyOf("$dir/wc/games/pong"), 'a parent holds the .svn');
self::assertNull(Svn::workingCopyOf($dir));
$local = new Svn(new Processes(), "$dir/wc/games/pong", '/usr/bin/svn');
self::assertSame('local', $local->mode());
self::assertTrue($local->isWorkingCopy());
self::assertSame(['/usr/bin/svn', '--non-interactive', 'status', '--xml', '.'], $local->command(['status', '--xml', '.']));
self::assertSame('[email protected]@', Svn::target('[email protected]'), 'an @ in a path is not a peg revision');
self::assertSame('.', Svn::target(''));
$docker = new Svn(new Processes(), "$dir/wc/games/pong", '/usr/bin/svn', null, 'Linux');
$docker->setMode('docker');
$command = $docker->command(['info', '--xml']);
self::assertSame(['docker', 'run', '--rm', '-i', '-v', "$dir/wc:/wc", '-w', '/wc/games/pong'], array_slice($command, 0, 8), 'the working copy mounted, the project inside it');
self::assertSame([Svn::imageTag(), 'svn', '--non-interactive', 'info', '--xml'], array_slice($command, -5));
self::assertMatchesRegularExpression('/^jumphp\/svn:[0-9a-f]{12}$/', Svn::imageTag());
self::assertStringContainsString('apk add --no-cache subversion', Svn::DOCKERFILE);
$wsl = new Svn(new Processes(), "$dir/wc", '/usr/bin/svn', fn (): string => 'wsl', 'Windows');
$wsl->setMode('docker');
$command = $wsl->command(['status', '--xml', '.'], 'C:\\Users\\ada\\wc', 'C:\\Users\\ada\\wc');
self::assertSame(['-e', 'bash', '-lc'], array_slice($command, 1, 3), 'docker run inside WSL');
self::assertStringContainsString('/mnt/c/Users/ada/wc:/wc', $command[4]);
}
public function testClaudeHasTheSvnTools(): void
{
$names = array_column(SvnAgentTools::all(), 'name');
self::assertSame(['svn_status', 'svn_diff', 'svn_commit', 'svn_update', 'svn_log'], $names);
$all = array_column(\JumPHP\Ide\Agent\AgentTools::all(), 'name');
foreach ($names as $name) {
self::assertContains($name, $all);
}
}
/** A real repository (svnadmin create) and working copy: add, commit, change, status, diff, revert, update, log, blame. */
public function testAWorkingCopyEndToEnd(): void
{
if (Environment::which('svn') === null || Environment::which('svnadmin') === null) {
self::markTestSkipped('svn and svnadmin are not installed here');
}
$dir = $this->tempDir();
$processes = new Processes();
exec('svnadmin create ' . escapeshellarg("$dir/repo") . ' 2>&1', $out, $code);
self::assertSame(0, $code, implode("\n", $out));
$url = 'file://' . $dir . '/repo';
exec('svn mkdir -q --non-interactive -m layout ' . escapeshellarg("$url/trunk") . ' ' . escapeshellarg("$url/branches") . ' 2>&1', $out, $code);
self::assertSame(0, $code, implode("\n", $out));
$svn = new Svn($processes, $dir);
self::assertSame("$dir/wc", $this->wait($processes, $svn->checkout("$url/trunk", "$dir/wc")));
$wc = new Svn($processes, "$dir/wc");
self::assertTrue($wc->isWorkingCopy());
file_put_contents("$dir/wc/a.txt", "one\ntwo\nthree\n");
mkdir("$dir/wc/src");
file_put_contents("$dir/wc/src/b.php", "<?php echo 1;\n");
$status = $this->wait($processes, $wc->status());
self::assertSame('svn', $status['vcs']);
self::assertSame('trunk', $status['branch']);
self::assertSame(['a.txt' => 'untracked', 'src' => 'untracked'], array_column($status['files'], 'status', 'path'));
self::assertTrue(array_column($status['files'], 'dir', 'path')['src']);
// unversioned files are added by the commit itself
$commit = $this->wait($processes, $wc->commit('First files', ['a.txt', 'src']));
self::assertSame(2, $commit['revision']);
self::assertSame('r2', $commit['sha']);
self::assertSame([], $this->wait($processes, $wc->status())['files']);
file_put_contents("$dir/wc/a.txt", "one\nTWO\nthree\n");
$status = $this->wait($processes, $wc->status());
self::assertSame(['a.txt' => 'modified'], array_column($status['files'], 'status', 'path'));
self::assertSame("one\ntwo\nthree\n", $this->wait($processes, $wc->original('a.txt')), 'BASE: the gutter\'s and the diff\'s original');
self::assertStringContainsString("+TWO", $this->wait($processes, $wc->diff('a.txt')));
$this->wait($processes, $wc->revert(['a.txt']));
self::assertSame("one\ntwo\nthree\n", file_get_contents("$dir/wc/a.txt"));
// a new file reverted is gone, like Git's rollback
file_put_contents("$dir/wc/tmp.txt", 'x');
$this->wait($processes, $wc->revert(['tmp.txt']));
self::assertFileDoesNotExist("$dir/wc/tmp.txt");
// a second working copy commits; the first updates
$other = $this->wait($processes, $svn->checkout("$url/trunk", "$dir/wc2"));
file_put_contents("$other/a.txt", "one\ntwo\nthree\nfour\n");
$wc2 = new Svn($processes, $other);
self::assertSame(3, $this->wait($processes, $wc2->commit('Four', null))['revision']);
$update = $this->wait($processes, $wc->update());
self::assertSame(3, $update['revision']);
self::assertSame([], $update['conflicts']);
self::assertSame("one\ntwo\nthree\nfour\n", file_get_contents("$dir/wc/a.txt"));
// a conflict, then resolved with the repository's version
file_put_contents("$other/a.txt", "one\nTHEIRS\nthree\nfour\n");
$this->wait($processes, $wc2->commit('Theirs'));
file_put_contents("$dir/wc/a.txt", "one\nMINE\nthree\nfour\n");
$update = $this->wait($processes, $wc->update());
self::assertSame(['a.txt'], $update['conflicts']);
self::assertSame('conflict', array_column($this->wait($processes, $wc->status())['files'], 'status', 'path')['a.txt']);
$this->wait($processes, $wc->resolve('a.txt', 'theirs-full'));
self::assertSame("one\nTHEIRS\nthree\nfour\n", file_get_contents("$dir/wc/a.txt"));
$log = $this->wait($processes, $wc->log(null, 10));
self::assertSame([4, 3, 2, 1], array_column($log, 'revision'));
self::assertSame('Theirs', $log[0]['message']);
self::assertSame("one\ntwo\nthree\n", $this->wait($processes, $wc->original('/trunk/a.txt', '2')), 'a file of the log at its revision');
self::assertStringContainsString('+four', $this->wait($processes, $wc->diff(null, 3)));
$blame = $this->wait($processes, $wc->blame('a.txt'));
self::assertSame([2, 4, 2, 3], array_column($blame, 'revision'));
self::assertSame('Theirs', $blame[1]['summary']);
$info = $this->wait($processes, $wc->info());
self::assertSame(4, $info['revision']);
self::assertSame([['name' => 'trunk', 'current' => true]], $this->wait($processes, $wc->branches()));
}
private function tempDir(): string
{
$dir = sys_get_temp_dir() . '/jumphp-ide-svn-' . bin2hex(random_bytes(4));
mkdir($dir);
$this->temp[] = $dir;
return (string)realpath($dir);
}
private function wait(Processes $processes, Deferred $d): mixed
{
return $processes->wait($d, 60);
}
}