Changeset View
Changeset View
Standalone View
Standalone View
src/parser/__tests__/ArcanistDiffParserTestCase.php
Show First 20 Lines • Show All 529 Lines • ▼ Show 20 Lines | EOTEXT | ||||
break; | break; | ||||
case 'hg-patch-git.hgdiff': | case 'hg-patch-git.hgdiff': | ||||
$this->assertEqual(1, count($changes)); | $this->assertEqual(1, count($changes)); | ||||
break; | break; | ||||
case 'custom-prefixes.gitdiff': | case 'custom-prefixes.gitdiff': | ||||
$this->assertEqual(1, count($changes)); | $this->assertEqual(1, count($changes)); | ||||
$change = head($changes); | $change = head($changes); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
'dst/file', | 'file', | ||||
$change->getCurrentPath()); | |||||
break; | |||||
case 'custom-prefixes-edit.gitdiff': | |||||
$this->assertEqual(1, count($changes)); | |||||
$change = head($changes); | |||||
$this->assertEqual( | |||||
'file', | |||||
$change->getCurrentPath()); | $change->getCurrentPath()); | ||||
break; | break; | ||||
case 'more-newlines.svndiff': | case 'more-newlines.svndiff': | ||||
$this->assertEqual(1, count($changes)); | $this->assertEqual(1, count($changes)); | ||||
break; | break; | ||||
case 'suppress-blank-empty.gitdiff': | case 'suppress-blank-empty.gitdiff': | ||||
$this->assertEqual(1, count($changes)); | $this->assertEqual(1, count($changes)); | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | EOTEXT | ||||
$this->assertEqual('file with spaces.txt', $change->getOldPath()); | $this->assertEqual('file with spaces.txt', $change->getOldPath()); | ||||
break; | break; | ||||
default: | default: | ||||
throw new Exception(pht('No test block for diff file %s.', $diff_file)); | throw new Exception(pht('No test block for diff file %s.', $diff_file)); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
public function testGitPrefixStripping() { | public function testGitCommonFilenameExtraction() { | ||||
static $tests = array( | |||||
'a/file.c' => 'file.c', | |||||
'b/file.c' => 'file.c', | |||||
'i/file.c' => 'file.c', | |||||
'c/file.c' => 'file.c', | |||||
'w/file.c' => 'file.c', | |||||
'o/file.c' => 'file.c', | |||||
'1/file.c' => 'file.c', | |||||
'2/file.c' => 'file.c', | |||||
'src/file.c' => 'src/file.c', | |||||
'file.c' => 'file.c', | |||||
); | |||||
foreach ($tests as $input => $expect) { | |||||
$this->assertEqual( | |||||
$expect, | |||||
ArcanistDiffParser::stripGitPathPrefix($input), | |||||
pht("Strip git prefix from '%s'.", $input)); | |||||
} | |||||
} | |||||
public function testGitPathSplitting() { | |||||
static $tests = array( | static $tests = array( | ||||
'a/old.c b/new.c' => array('old.c', 'new.c'), | 'a/filename.c b/filename.c' => 'filename.c', | ||||
"a/old.c b/new.c\n" => array('old.c', 'new.c'), | "a/filename.c b/filename.c\n" => 'filename.c', | ||||
"a/old.c b/new.c\r\n" => array('old.c', 'new.c'), | "a/filename.c b/filename.c\r\n" => 'filename.c', | ||||
'old.c new.c' => array('old.c', 'new.c'), | 'filename.c filename.c' => 'filename.c', | ||||
'1/old.c 2/new.c' => array('old.c', 'new.c'), | '1/filename.c 2/filename.c' => 'filename.c', | ||||
'"a/\\"quotes1\\"" "b/\\"quotes2\\""' => array( | '"a/\\"quotes\\"" "b/\\"quotes\\""' => '"quotes"', | ||||
'"quotes1"', | '"a/\\"quotes and spaces\\"" "b/\\"quotes and spaces\\""' => | ||||
'"quotes2"', | '"quotes and spaces"', | ||||
), | '"a/\\342\\230\\203" "b/\\342\\230\\203"' => | ||||
'"a/\\"quotes and spaces1\\"" "b/\\"quotes and spaces2\\""' => array( | "\xE2\x98\x83", | ||||
'"quotes and spaces1"', | 'a/Core Data/filename.c b/Core Data/filename.c' => | ||||
'"quotes and spaces2"', | 'Core Data/filename.c', | ||||
), | 'some file with spaces.c some file with spaces.c' => | ||||
'"a/\\342\\230\\2031" "b/\\342\\230\\2032"' => array( | |||||
"\xE2\x98\x831", | |||||
"\xE2\x98\x832", | |||||
), | |||||
'a/Core Data/old.c b/Core Data/new.c' => array( | |||||
'Core Data/old.c', | |||||
'Core Data/new.c', | |||||
), | |||||
'some file with spaces.c some file with spaces.c' => array( | |||||
'some file with spaces.c', | |||||
'some file with spaces.c', | 'some file with spaces.c', | ||||
), | '"foo bar.c" foo bar.c' => 'foo bar.c', | ||||
'"a/foo bar.c" b/foo bar.c' => 'foo bar.c', | |||||
'src/file dst/file' => 'file', | |||||
// Renames are handled by the "rename from ..." lines later in | |||||
// the diff, for simplicity of parsing; this is also how git | |||||
epriestley: "thisi s" -> "this is" | |||||
// itself handles it. | |||||
'a/foo.c b/bar.c' => null, | |||||
'a/foo bar.c b/baz troz.c' => null, | |||||
'"a/foo bar.c" b/baz troz.c' => null, | |||||
'a/foo bar.c "b/baz troz.c"' => null, | |||||
'"a/foo bar.c" "b/baz troz.c"' => null, | |||||
'filename file with spaces.c filename file with spaces.c' => | |||||
'filename file with spaces.c', | |||||
); | ); | ||||
foreach ($tests as $input => $expect) { | foreach ($tests as $input => $expect) { | ||||
$result = ArcanistDiffParser::splitGitDiffPaths($input); | $result = ArcanistDiffParser::extractGitCommonFilename($input); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
$expect, | $expect, | ||||
$result, | $result, | ||||
pht('Split: %s', $input)); | pht('Split: %s', $input)); | ||||
} | } | ||||
} | |||||
static $ambiguous = array( | public function runSingleRename($diffline, $from, $to, $old, $new) { | ||||
'old file with spaces.c new file with spaces.c', | $str = "diff --git $diffline\nsimilarity index 95%\n" | ||||
); | ."rename from $from\nrename to $to\n"; | ||||
$parser = new ArcanistDiffParser(); | |||||
foreach ($ambiguous as $input) { | $changes = $parser->parseDiff($str); | ||||
$caught = null; | |||||
try { | |||||
ArcanistDiffParser::splitGitDiffPaths($input); | |||||
} catch (Exception $ex) { | |||||
$caught = $ex; | |||||
} | |||||
$this->assertTrue( | $this->assertTrue( | ||||
Not Done Inline ActionsPrefer pht() on any human-readable text, even exceptions / unit tests / stuff you're 100% sure is completely developer-facing forever. See here for rationale: https://secure.phabricator.com/book/phabcontrib/article/internationalization/#exceptions-and-errors epriestley: Prefer `pht()` on any human-readable text, even exceptions / unit tests / stuff you're 100%… | |||||
($caught instanceof Exception), | $changes !== null, | ||||
pht('Ambiguous: %s', $input)); | pht("Parsed:\n%s", $str)); | ||||
} | $this->assertEqual( | ||||
$old == $new ? 1 : 2, count($changes), | |||||
pht("Parsed one change:\n%s", $str)); | |||||
$change = reset($changes); | |||||
$this->assertEqual( | |||||
array($old, $new), | |||||
array($change->getOldPath(), $change->getCurrentPath()), | |||||
pht('Split: %s', $diffline)); | |||||
} | |||||
public function testGitRenames() { | |||||
$this->runSingleRename('a/old.c b/new.c', | |||||
'old.c', 'new.c', | |||||
'old.c', 'new.c'); | |||||
$this->runSingleRename('old.c new.c', | |||||
'old.c', 'new.c', | |||||
'old.c', 'new.c'); | |||||
$this->runSingleRename('1/old.c 2/new.c', | |||||
'old.c', 'new.c', | |||||
'old.c', 'new.c'); | |||||
$this->runSingleRename('from/file.c to/file.c', | |||||
'from/file.c', 'to/file.c', | |||||
'from/file.c', 'to/file.c'); | |||||
$this->runSingleRename('"a/\\"quotes1\\"" "b/\\"quotes2\\""', | |||||
'"\\"quotes1\\""', '"\\"quotes2\\""', | |||||
'"quotes1"', '"quotes2"'); | |||||
$this->runSingleRename('"a/\\"quotes spaces1\\"" "b/\\"quotes spaces2\\""', | |||||
'"\\"quotes spaces1\\""', '"\\"quotes spaces2\\""', | |||||
'"quotes spaces1"', '"quotes spaces2"'); | |||||
$this->runSingleRename('"a/\\342\\230\\2031" "b/\\342\\230\\2032"', | |||||
'"\\342\\230\\2031"', '"\\342\\230\\2032"', | |||||
"\xE2\x98\x831", "\xE2\x98\x832"); | |||||
$this->runSingleRename('a/Core Data/old.c b/Core Data/new.c', | |||||
'Core Data/old.c', 'Core Data/new.c', | |||||
'Core Data/old.c', 'Core Data/new.c'); | |||||
$this->runSingleRename('file with spaces.c file with spaces.c', | |||||
'file with spaces.c', 'file with spaces.c', | |||||
'file with spaces.c', 'file with spaces.c'); | |||||
$this->runSingleRename('a/non-quoted filename.c "b/quoted filename.c"', | |||||
'non-quoted filename.c', '"quoted filename.c"', | |||||
'non-quoted filename.c', 'quoted filename.c'); | |||||
$this->runSingleRename('non-quoted filename.c "quoted filename.c"', | |||||
'non-quoted filename.c', '"quoted filename.c"', | |||||
'non-quoted filename.c', 'quoted filename.c'); | |||||
$this->runSingleRename('"a/quoted filename.c" b/non quoted filename.c', | |||||
'"quoted filename.c"', 'non quoted filename.c', | |||||
'quoted filename.c', 'non quoted filename.c'); | |||||
$this->runSingleRename('"quoted filename.c" non-quoted filename.c', | |||||
'"quoted filename.c"', 'non-quoted filename.c', | |||||
'quoted filename.c', 'non-quoted filename.c'); | |||||
$this->runSingleRename('old file with spaces.c new file with spaces.c', | |||||
'old file with spaces.c', 'new file with spaces.c', | |||||
'old file with spaces.c', 'new file with spaces.c'); | |||||
$this->runSingleRename('old file old file', | |||||
'old file old', 'file', | |||||
'old file old', 'file'); | |||||
} | } | ||||
} | } |
"thisi s" -> "this is"