Skip to content

Commit 14c8138

Browse files
committed
Change svn workflow to avoid diff and hash for unchanged updated file
1 parent 3ee84ae commit 14c8138

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

PhpcsChanged/Cli.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,15 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s
237237
throw new ShellException("Cannot read file '{$svnFile}'");
238238
}
239239
$svnFileInfo = getSvnFileInfo($svnFile, $svn, [$shell, 'executeCommand'], $debug);
240-
$unifiedDiff = getSvnUnifiedDiff($svnFile, $svn, [$shell, 'executeCommand'], $debug);
241240
$revisionId = getSvnRevisionId($svnFileInfo);
242241
$isNewFile = isNewSvnFile($svnFileInfo);
243242

244-
$newFileHash = $shell->getFileHash($svnFile);
245-
$newFilePhpcsOutput = isCachingEnabled($options) ? $cache->getCacheForFile($svnFile, 'new', $newFileHash, $phpcsStandard ?? '') : null;
243+
$newFileHash = '';
244+
$newFilePhpcsOutput = null;
245+
if (isCachingEnabled($options)) {
246+
$newFileHash = $shell->getFileHash($svnFile);
247+
$newFilePhpcsOutput = $cache->getCacheForFile($svnFile, 'new', $newFileHash, $phpcsStandard ?? '');
248+
}
246249
if ($newFilePhpcsOutput) {
247250
$debug("Using cache for new file '{$svnFile}' at revision '{$revisionId}', hash '{$newFileHash}', and standard '{$phpcsStandard}'");
248251
}
@@ -253,12 +256,19 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s
253256
$cache->setCacheForFile($svnFile, 'new', $newFileHash, $phpcsStandard ?? '', $newFilePhpcsOutput);
254257
}
255258
}
256-
$fileName = DiffLineMap::getFileNameFromDiff($unifiedDiff);
259+
260+
$fileName = $shell->getFileNameFromPath($svnFile);
257261
$newFilePhpcsMessages = PhpcsMessages::fromPhpcsJson($newFilePhpcsOutput, $fileName);
258262
$hasNewPhpcsMessages = !empty($newFilePhpcsMessages->getMessages());
259263

264+
if (! $hasNewPhpcsMessages) {
265+
throw new NoChangesException("New file '{$svnFile}' has no PHPCS messages; skipping");
266+
}
267+
268+
$unifiedDiff = getSvnUnifiedDiff($svnFile, $svn, [$shell, 'executeCommand'], $debug);
269+
260270
$oldFilePhpcsOutput = '';
261-
if ( ! $isNewFile && $hasNewPhpcsMessages) {
271+
if (! $isNewFile) {
262272
$oldFilePhpcsOutput = isCachingEnabled($options) ? $cache->getCacheForFile($svnFile, 'old', $revisionId, $phpcsStandard ?? '') : null;
263273
if ($oldFilePhpcsOutput) {
264274
$debug("Using cache for old file '{$svnFile}' at revision '{$revisionId}' and standard '{$phpcsStandard}'");
@@ -270,12 +280,6 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s
270280
$cache->setCacheForFile($svnFile, 'old', $revisionId, $phpcsStandard ?? '', $oldFilePhpcsOutput);
271281
}
272282
}
273-
} else {
274-
if ($isNewFile) {
275-
$debug('Skipping the linting of the orig file version as it is a new file.');
276-
} else {
277-
$debug('Skipping the linting of the orig file version as the new version of the file contains no lint errors.');
278-
}
279283
}
280284
} catch( NoChangesException $err ) {
281285
$debug($err->getMessage());

tests/SvnWorkflowTest.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ public function testFullSvnWorkflowForUnchangedFileWithCache() {
368368
];
369369
$expected = PhpcsMessages::fromArrays([], 'bin/foobar.php');
370370
$cache = new TestCache();
371+
$cache->setEntry('foobar.php', 'new', 'foobar.php', '', $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson());
371372
$cache->setEntry('foobar.php', 'old', '188280', '', $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson());
372373
$messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug');
373374
$this->assertEquals($expected->getMessages(), $messages->getMessages());
@@ -415,14 +416,27 @@ public function testFullSvnWorkflowForUnchangedFileWithoutPhpCsMessages() {
415416
$shell = new TestShell([$svnFile]);
416417
$shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getEmptyFileDiff());
417418
$shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php'));
418-
$shell->registerCommand("svn cat 'foobar.php'|phpcs", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
419-
$shell->registerCommand("cat 'foobar.php'|phpcs", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
419+
$shell->registerCommand("svn cat 'foobar.php'", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
420+
$shell->registerCommand("cat 'foobar.php'", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
420421
$options = [];
421422
$expected = PhpcsMessages::fromArrays([], 'STDIN');
422423
$messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug');
423424
$this->assertEquals($expected->getMessages(), $messages->getMessages());
424425
}
425426

427+
public function testFullSvnWorkflowForChangedFileWithoutPhpCsMessagesLintsOnlyNewFile() {
428+
$svnFile = 'foobar.php';
429+
$shell = new TestShell([$svnFile]);
430+
$shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getEmptyFileDiff());
431+
$shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php'));
432+
$shell->registerCommand("svn cat 'foobar.php'", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
433+
$shell->registerCommand("cat 'foobar.php'", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}');
434+
$options = [];
435+
runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug');
436+
$this->assertFalse($shell->wasCommandCalled("svn diff 'foobar.php'"));
437+
$this->assertFalse($shell->wasCommandCalled("svn cat 'foobar.php'"));
438+
}
439+
426440
public function testFullSvnWorkflowForNonSvnFile() {
427441
$this->expectException(ShellException::class);
428442
$svnFile = 'foobar.php';
@@ -442,7 +456,7 @@ public function testFullSvnWorkflowForNewFile() {
442456
$shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfoNewFile('foobar.php'));
443457
$shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson());
444458
$options = [];
445-
$expected = $this->phpcs->getResults('STDIN', [20, 21]);
459+
$expected = $this->phpcs->getResults('foobar.php', [20, 21]);
446460
$messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug');
447461
$this->assertEquals($expected->getMessages(), $messages->getMessages());
448462
}

0 commit comments

Comments
 (0)