mirror of
https://we.phorge.it/source/arcanist.git
synced 2025-03-13 04:44:53 +01:00
Summary: Fixes T9846. This restores the last missing feature, ANSI highlighting of diff sections. Test Plan: Added a mode so we can actually test this stuff, activated that mode, wrote unit tests. Did a bunch of actual lint locally too and looked at it, all seemed sane. Reviewers: chad Reviewed By: chad Maniphest Tasks: T9846 Differential Revision: https://secure.phabricator.com/D18512
145 lines
3.5 KiB
PHP
145 lines
3.5 KiB
PHP
<?php
|
|
|
|
final class ArcanistConsoleLintRendererTestCase
|
|
extends PhutilTestCase {
|
|
|
|
public function testRendering() {
|
|
$map = array(
|
|
'simple' => array(
|
|
'line' => 1,
|
|
'char' => 1,
|
|
'original' => 'a',
|
|
'replacement' => 'z',
|
|
),
|
|
'inline' => array(
|
|
'line' => 1,
|
|
'char' => 7,
|
|
'original' => 'cat',
|
|
'replacement' => 'dog',
|
|
),
|
|
|
|
// In this test, the original and replacement texts have a large
|
|
// amount of overlap.
|
|
'overlap' => array(
|
|
'line' => 1,
|
|
'char' => 1,
|
|
'original' => 'tantawount',
|
|
'replacement' => 'tantamount',
|
|
),
|
|
|
|
'newline' => array(
|
|
'line' => 6,
|
|
'char' => 1,
|
|
'original' => "\n",
|
|
'replacement' => '',
|
|
),
|
|
|
|
'addline' => array(
|
|
'line' => 3,
|
|
'char' => 1,
|
|
'original' => '',
|
|
'replacement' => "cherry\n",
|
|
),
|
|
|
|
'addlinesuffix' => array(
|
|
'line' => 2,
|
|
'char' => 7,
|
|
'original' => '',
|
|
'replacement' => "\ncherry",
|
|
),
|
|
|
|
'xml' => array(
|
|
'line' => 3,
|
|
'char' => 6,
|
|
'original' => '',
|
|
'replacement' => "\n",
|
|
),
|
|
|
|
'caret' => array(
|
|
'line' => 2,
|
|
'char' => 13,
|
|
'name' => 'Fruit Misinformation',
|
|
'description' => 'Arguably untrue.',
|
|
),
|
|
|
|
'original' => array(
|
|
'line' => 1,
|
|
'char' => 4,
|
|
'original' => 'should of',
|
|
),
|
|
);
|
|
|
|
$defaults = array(
|
|
'severity' => ArcanistLintSeverity::SEVERITY_WARNING,
|
|
'name' => 'Lint Warning',
|
|
'path' => 'path/to/example.c',
|
|
'description' => 'Consider this.',
|
|
'code' => 'WARN123',
|
|
);
|
|
|
|
foreach ($map as $key => $test_case) {
|
|
$data = $this->readTestData("{$key}.txt");
|
|
$expect = $this->readTestData("{$key}.expect");
|
|
|
|
$test_case = $test_case + $defaults;
|
|
|
|
$path = $test_case['path'];
|
|
$severity = $test_case['severity'];
|
|
$name = $test_case['name'];
|
|
$description = $test_case['description'];
|
|
$code = $test_case['code'];
|
|
|
|
$line = $test_case['line'];
|
|
$char = $test_case['char'];
|
|
|
|
$original = idx($test_case, 'original');
|
|
$replacement = idx($test_case, 'replacement');
|
|
|
|
$message = id(new ArcanistLintMessage())
|
|
->setPath($path)
|
|
->setSeverity($severity)
|
|
->setName($name)
|
|
->setDescription($description)
|
|
->setCode($code)
|
|
->setLine($line)
|
|
->setChar($char)
|
|
->setOriginalText($original)
|
|
->setReplacementText($replacement);
|
|
|
|
$result = id(new ArcanistLintResult())
|
|
->setPath($path)
|
|
->setData($data)
|
|
->addMessage($message);
|
|
|
|
$renderer = id(new ArcanistConsoleLintRenderer())
|
|
->setTestableMode(true);
|
|
|
|
try {
|
|
PhutilConsoleFormatter::disableANSI(true);
|
|
$actual = $renderer->renderLintResult($result);
|
|
PhutilConsoleFormatter::disableANSI(false);
|
|
} catch (Exception $ex) {
|
|
PhutilConsoleFormatter::disableANSI(false);
|
|
throw $ex;
|
|
}
|
|
|
|
// Trim "~" off the ends of lines. This allows the "expect" file to test
|
|
// for trailing whitespace without actually containing trailing
|
|
// whitespace.
|
|
$expect = preg_replace('/~+$/m', '', $expect);
|
|
|
|
$this->assertEqual(
|
|
$expect,
|
|
$actual,
|
|
pht(
|
|
'Lint rendering for "%s".',
|
|
$key));
|
|
}
|
|
}
|
|
|
|
private function readTestData($filename) {
|
|
$path = dirname(__FILE__).'/data/'.$filename;
|
|
return Filesystem::readFile($path);
|
|
}
|
|
|
|
}
|