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 19 # Unit tests of parseDiff() with mock files; test override of patch EOL with EOL of target file. 20 21 use strict; 22 use warnings; 23 24 use File::Temp; 25 use POSIX qw/getcwd/; 26 use Test::More; 27 use VCSUtils; 28 29 my $gitDiffHeaderForNewFile = <<EOF; 30 diff --git a/Makefile b/Makefile 31 new file mode 100644 32 index 0000000..756e864 33 --- /dev/null 34 +++ b/Makefile 35 @@ -0,0 +1,17 @@ 36 EOF 37 38 my $gitDiffHeader = <<EOF; 39 diff --git a/Makefile b/Makefile 40 index 756e864..04d2ae1 100644 41 --- a/Makefile 42 +++ b/Makefile 43 @@ -1,3 +1,4 @@ 44 EOF 45 46 my $svnConvertedGitDiffHeader = <<EOF; 47 Index: Makefile 48 index 756e864..04d2ae1 100644 49 --- Makefile 50 +++ Makefile 51 @@ -1,3 +1,4 @@ 52 EOF 53 54 my $svnConvertedGitDiffHeaderForNewFile = <<EOF; 55 Index: Makefile 56 new file mode 100644 57 index 0000000..756e864 58 --- Makefile 59 +++ Makefile 60 @@ -0,0 +1,17 @@ 61 EOF 62 63 my $svnDiffHeaderForNewFile = <<EOF; 64 Index: Makefile 65 =================================================================== 66 --- Makefile (revision 0) 67 +++ Makefile (revision 0) 68 @@ -0,0 +1,17 @@ 69 EOF 70 71 my $svnDiffHeader = <<EOF; 72 Index: Makefile 73 =================================================================== 74 --- Makefile (revision 53052) 75 +++ Makefile (working copy) 76 @@ -1,3 +1,4 @@ 77 EOF 78 79 my $diffBody = <<EOF; 80 + 81 MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools 82 83 all: 84 EOF 85 86 my $MakefileContents = <<EOF; 87 MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools 88 89 all: 90 EOF 91 92 my $mockDir = File::Temp->tempdir("parseDiffXXXX", CLEANUP => 1); 93 writeToFile(File::Spec->catfile($mockDir, "MakefileWithUnixEOL"), $MakefileContents); 94 writeToFile(File::Spec->catfile($mockDir, "MakefileWithWindowsEOL"), toWindowsLineEndings($MakefileContents)); 95 96 # The array of test cases. 97 my @testCaseHashRefs = ( 98 ### 99 # SVN test cases 100 ## 101 { 102 # New test 103 diffName => "SVN: Patch with Unix line endings and IndexPath has Unix line endings", 104 inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, 105 expectedReturn => [ 106 [{ 107 svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, # Same as input text 108 indexPath => "MakefileWithUnixEOL", 109 isSvn => 1, 110 sourceRevision => "53052", 111 }], 112 undef], 113 expectedNextLine => undef, 114 }, 115 { 116 # New test 117 diffName => "SVN: Patch with Windows line endings and IndexPath has Unix line endings", 118 inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . toWindowsLineEndings($diffBody), 119 expectedReturn => [ 120 [{ 121 svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, 122 indexPath => "MakefileWithUnixEOL", 123 isSvn => 1, 124 sourceRevision => "53052", 125 }], 126 undef], 127 expectedNextLine => undef, 128 }, 129 { 130 # New test 131 diffName => "SVN: Patch with Windows line endings and IndexPath has Windows line endings", 132 inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), 133 expectedReturn => [ 134 [{ 135 svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), # Same as input text 136 indexPath => "MakefileWithWindowsEOL", 137 isSvn => 1, 138 sourceRevision => "53052", 139 }], 140 undef], 141 expectedNextLine => undef, 142 }, 143 { 144 # New test 145 diffName => "SVN: Patch with Unix line endings and IndexPath has Windows line endings", 146 inputText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . $diffBody, 147 expectedReturn => [ 148 [{ 149 svnConvertedText => substituteString($svnDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), 150 indexPath => "MakefileWithWindowsEOL", 151 isSvn => 1, 152 sourceRevision => "53052", 153 }], 154 undef], 155 expectedNextLine => undef, 156 }, 157 { 158 # New test 159 diffName => "SVN: Patch with Unix line endings and nonexistent IndexPath", 160 inputText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, 161 expectedReturn => [ 162 [{ 163 svnConvertedText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, # Same as input text 164 indexPath => "NonexistentFile", 165 isSvn => 1, 166 isNew => 1, 167 }], 168 undef], 169 expectedNextLine => undef, 170 }, 171 { 172 # New test 173 diffName => "SVN: Patch with Windows line endings and nonexistent IndexPath", 174 inputText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), 175 expectedReturn => [ 176 [{ 177 svnConvertedText => substituteString($svnDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), # Same as input text 178 indexPath => "NonexistentFile", 179 isSvn => 1, 180 isNew => 1, 181 }], 182 undef], 183 expectedNextLine => undef, 184 }, 185 ### 186 # Git test cases 187 ## 188 { 189 # New test 190 diffName => "Git: Patch with Unix line endings and IndexPath has Unix line endings", 191 inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, 192 expectedReturn => [ 193 [{ 194 svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, # Same as input text 195 indexPath => "MakefileWithUnixEOL", 196 isGit => 1, 197 }], 198 undef], 199 expectedNextLine => undef, 200 }, 201 { 202 # New test 203 diffName => "Git: Patch with Windows line endings and IndexPath has Unix line endings", 204 inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithUnixEOL") . toWindowsLineEndings($diffBody), 205 expectedReturn => [ 206 [{ 207 svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithUnixEOL") . $diffBody, 208 indexPath => "MakefileWithUnixEOL", 209 isGit => 1, 210 }], 211 undef], 212 expectedNextLine => undef, 213 }, 214 { 215 # New test 216 diffName => "Git: Patch with Windows line endings and IndexPath has Windows line endings", 217 inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), 218 expectedReturn => [ 219 [{ 220 svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), # Same as input text 221 indexPath => "MakefileWithWindowsEOL", 222 isGit => 1, 223 }], 224 undef], 225 expectedNextLine => undef, 226 }, 227 { 228 # New test 229 diffName => "Git: Patch with Unix line endings and IndexPath has Windows line endings", 230 inputText => substituteString($gitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . $diffBody, 231 expectedReturn => [ 232 [{ 233 svnConvertedText => substituteString($svnConvertedGitDiffHeader, "Makefile", "MakefileWithWindowsEOL") . toWindowsLineEndings($diffBody), 234 indexPath => "MakefileWithWindowsEOL", 235 isGit => 1, 236 }], 237 undef], 238 expectedNextLine => undef, 239 }, 240 { 241 # New test 242 diffName => "Git: Patch with Unix line endings and nonexistent IndexPath", 243 inputText => substituteString($gitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, 244 expectedReturn => [ 245 [{ 246 svnConvertedText => substituteString($svnConvertedGitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . $diffBody, # Same as input text 247 indexPath => "NonexistentFile", 248 isGit => 1, 249 isNew => 1, 250 }], 251 undef], 252 expectedNextLine => undef, 253 }, 254 { 255 # New test 256 diffName => "Git: Patch with Windows line endings and nonexistent IndexPath", 257 inputText => substituteString($gitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), 258 expectedReturn => [ 259 [{ 260 svnConvertedText => substituteString($svnConvertedGitDiffHeaderForNewFile, "Makefile", "NonexistentFile") . toWindowsLineEndings($diffBody), # Same as input text 261 indexPath => "NonexistentFile", 262 isGit => 1, 263 isNew => 1, 264 }], 265 undef], 266 expectedNextLine => undef, 267 }, 268 ); 269 270 my $testCasesCount = @testCaseHashRefs; 271 plan(tests => 2 * $testCasesCount); # Total number of assertions. 272 273 my $savedCWD = getcwd(); 274 chdir($mockDir) or die; 275 foreach my $testCase (@testCaseHashRefs) { 276 my $testNameStart = "parseDiff(): $testCase->{diffName}: comparing"; 277 278 my $fileHandle; 279 open($fileHandle, "<", \$testCase->{inputText}); 280 my $line = <$fileHandle>; 281 282 my @got = VCSUtils::parseDiff($fileHandle, $line); 283 my $expectedReturn = $testCase->{expectedReturn}; 284 285 is_deeply(\@got, $expectedReturn, "$testNameStart return value."); 286 287 my $gotNextLine = <$fileHandle>; 288 is($gotNextLine, $testCase->{expectedNextLine}, "$testNameStart next read line."); 289 } 290 chdir($savedCWD); 291 292 sub substituteString 293 { 294 my ($string, $searchString, $replacementString) = @_; 295 $string =~ s/$searchString/$replacementString/g; 296 return $string; 297 } 298 299 sub writeToFile 300 { 301 my ($file, $text) = @_; 302 open(FILE, ">$file") or die; 303 print FILE $text; 304 close(FILE); 305 } 306