/external/webkit/Tools/Scripts/webkitperl/VCSUtils_unittest/parseDiffWithMockFiles.pl

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Perl · 305 lines · 245 code · 23 blank · 37 comment · 0 complexity · af595eb9a4281965b3a7360c8a011e15 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2011 Research In Motion Limited. All rights reserved.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. # Unit tests of parseDiff() with mock files; test override of patch EOL with EOL of target file.
  19. use strict;
  20. use warnings;
  21. use File::Temp;
  22. use POSIX qw/getcwd/;
  23. use Test::More;
  24. use VCSUtils;
  25. my $gitDiffHeaderForNewFile = <<EOF;
  26. diff --git a/Makefile b/Makefile
  27. new file mode 100644
  28. index 0000000..756e864
  29. --- /dev/null
  30. +++ b/Makefile
  31. @@ -0,0 +1,17 @@
  32. EOF
  33. my $gitDiffHeader = <<EOF;
  34. diff --git a/Makefile b/Makefile
  35. index 756e864..04d2ae1 100644
  36. --- a/Makefile
  37. +++ b/Makefile
  38. @@ -1,3 +1,4 @@
  39. EOF
  40. my $svnConvertedGitDiffHeader = <<EOF;
  41. Index: Makefile
  42. index 756e864..04d2ae1 100644
  43. --- Makefile
  44. +++ Makefile
  45. @@ -1,3 +1,4 @@
  46. EOF
  47. my $svnConvertedGitDiffHeaderForNewFile = <<EOF;
  48. Index: Makefile
  49. new file mode 100644
  50. index 0000000..756e864
  51. --- Makefile
  52. +++ Makefile
  53. @@ -0,0 +1,17 @@
  54. EOF
  55. my $svnDiffHeaderForNewFile = <<EOF;
  56. Index: Makefile
  57. ===================================================================
  58. --- Makefile (revision 0)
  59. +++ Makefile (revision 0)
  60. @@ -0,0 +1,17 @@
  61. EOF
  62. my $svnDiffHeader = <<EOF;
  63. Index: Makefile
  64. ===================================================================
  65. --- Makefile (revision 53052)
  66. +++ Makefile (working copy)
  67. @@ -1,3 +1,4 @@
  68. EOF
  69. my $diffBody = <<EOF;
  70. +
  71. MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools
  72. all:
  73. EOF
  74. my $MakefileContents = <<EOF;
  75. MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools
  76. all:
  77. EOF
  78. my $mockDir = File::Temp->tempdir("parseDiffXXXX", CLEANUP => 1);
  79. writeToFile(File::Spec->catfile($mockDir, "MakefileWithUnixEOL"), $MakefileContents);
  80. writeToFile(File::Spec->catfile($mockDir, "MakefileWithWindowsEOL"), toWindowsLineEndings($MakefileContents));
  81. # The array of test cases.
  82. my @testCaseHashRefs = (
  83. ###
  84. # SVN test cases
  85. ##
  86. {
  87. # New test
  88. diffName => "SVN: Patch with Unix line endings and IndexPath has Unix line endings",
  89. inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody,
  90. expectedReturn => [
  91. [{
  92. svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, # Same as input text
  93. indexPath => "MakefileWithUnixEOL",
  94. isSvn => 1,
  95. sourceRevision => "53052",
  96. }],
  97. undef],
  98. expectedNextLine => undef,
  99. },
  100. {
  101. # New test
  102. diffName => "SVN: Patch with Windows line endings and IndexPath has Unix line endings",
  103. inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . toWindowsLineEndings($diffBody),
  104. expectedReturn => [
  105. [{
  106. svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody,
  107. indexPath => "MakefileWithUnixEOL",
  108. isSvn => 1,
  109. sourceRevision => "53052",
  110. }],
  111. undef],
  112. expectedNextLine => undef,
  113. },
  114. {
  115. # New test
  116. diffName => "SVN: Patch with Windows line endings and IndexPath has Windows line endings",
  117. inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody),
  118. expectedReturn => [
  119. [{
  120. svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), # Same as input text
  121. indexPath => "MakefileWithWindowsEOL",
  122. isSvn => 1,
  123. sourceRevision => "53052",
  124. }],
  125. undef],
  126. expectedNextLine => undef,
  127. },
  128. {
  129. # New test
  130. diffName => "SVN: Patch with Unix line endings and IndexPath has Windows line endings",
  131. inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . $diffBody,
  132. expectedReturn => [
  133. [{
  134. svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody),
  135. indexPath => "MakefileWithWindowsEOL",
  136. isSvn => 1,
  137. sourceRevision => "53052",
  138. }],
  139. undef],
  140. expectedNextLine => undef,
  141. },
  142. {
  143. # New test
  144. diffName => "SVN: Patch with Unix line endings and nonexistent IndexPath",
  145. inputText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody,
  146. expectedReturn => [
  147. [{
  148. svnConvertedText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, # Same as input text
  149. indexPath => "NonexistentFile",
  150. isSvn => 1,
  151. isNew => 1,
  152. }],
  153. undef],
  154. expectedNextLine => undef,
  155. },
  156. {
  157. # New test
  158. diffName => "SVN: Patch with Windows line endings and nonexistent IndexPath",
  159. inputText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody),
  160. expectedReturn => [
  161. [{
  162. svnConvertedText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), # Same as input text
  163. indexPath => "NonexistentFile",
  164. isSvn => 1,
  165. isNew => 1,
  166. }],
  167. undef],
  168. expectedNextLine => undef,
  169. },
  170. ###
  171. # Git test cases
  172. ##
  173. {
  174. # New test
  175. diffName => "Git: Patch with Unix line endings and IndexPath has Unix line endings",
  176. inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody,
  177. expectedReturn => [
  178. [{
  179. svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, # Same as input text
  180. indexPath => "MakefileWithUnixEOL",
  181. isGit => 1,
  182. }],
  183. undef],
  184. expectedNextLine => undef,
  185. },
  186. {
  187. # New test
  188. diffName => "Git: Patch with Windows line endings and IndexPath has Unix line endings",
  189. inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithUnixEOL") . toWindowsLineEndings($diffBody),
  190. expectedReturn => [
  191. [{
  192. svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody,
  193. indexPath => "MakefileWithUnixEOL",
  194. isGit => 1,
  195. }],
  196. undef],
  197. expectedNextLine => undef,
  198. },
  199. {
  200. # New test
  201. diffName => "Git: Patch with Windows line endings and IndexPath has Windows line endings",
  202. inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody),
  203. expectedReturn => [
  204. [{
  205. svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), # Same as input text
  206. indexPath => "MakefileWithWindowsEOL",
  207. isGit => 1,
  208. }],
  209. undef],
  210. expectedNextLine => undef,
  211. },
  212. {
  213. # New test
  214. diffName => "Git: Patch with Unix line endings and IndexPath has Windows line endings",
  215. inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . $diffBody,
  216. expectedReturn => [
  217. [{
  218. svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody),
  219. indexPath => "MakefileWithWindowsEOL",
  220. isGit => 1,
  221. }],
  222. undef],
  223. expectedNextLine => undef,
  224. },
  225. {
  226. # New test
  227. diffName => "Git: Patch with Unix line endings and nonexistent IndexPath",
  228. inputText => substituteString($gitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody,
  229. expectedReturn => [
  230. [{
  231. svnConvertedText => substituteString($svnConvertedGitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, # Same as input text
  232. indexPath => "NonexistentFile",
  233. isGit => 1,
  234. isNew => 1,
  235. }],
  236. undef],
  237. expectedNextLine => undef,
  238. },
  239. {
  240. # New test
  241. diffName => "Git: Patch with Windows line endings and nonexistent IndexPath",
  242. inputText => substituteString($gitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody),
  243. expectedReturn => [
  244. [{
  245. svnConvertedText => substituteString($svnConvertedGitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), # Same as input text
  246. indexPath => "NonexistentFile",
  247. isGit => 1,
  248. isNew => 1,
  249. }],
  250. undef],
  251. expectedNextLine => undef,
  252. },
  253. );
  254. my $testCasesCount = @testCaseHashRefs;
  255. plan(tests => 2 * $testCasesCount); # Total number of assertions.
  256. my $savedCWD = getcwd();
  257. chdir($mockDir) or die;
  258. foreach my $testCase (@testCaseHashRefs) {
  259. my $testNameStart = "parseDiff(): $testCase->{diffName}: comparing";
  260. my $fileHandle;
  261. open($fileHandle, "<", \$testCase->{inputText});
  262. my $line = <$fileHandle>;
  263. my @got = VCSUtils::parseDiff($fileHandle, $line);
  264. my $expectedReturn = $testCase->{expectedReturn};
  265. is_deeply(\@got, $expectedReturn, "$testNameStart return value.");
  266. my $gotNextLine = <$fileHandle>;
  267. is($gotNextLine, $testCase->{expectedNextLine}, "$testNameStart next read line.");
  268. }
  269. chdir($savedCWD);
  270. sub substituteString
  271. {
  272. my ($string, $searchString, $replacementString) = @_;
  273. $string =~ s/$searchString/$replacementString/g;
  274. return $string;
  275. }
  276. sub writeToFile
  277. {
  278. my ($file, $text) = @_;
  279. open(FILE, ">$file") or die;
  280. print FILE $text;
  281. close(FILE);
  282. }