1#!/usr/bin/perl 2# 3# Copyright (C) 2010 Apple Inc. All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions 7# are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 25# Unit tests of VCSUtils::mergeChangeLogs(). 26 27use strict; 28 29use Test::Simple tests => 16; 30use File::Temp qw(tempfile); 31use VCSUtils; 32 33# Read contents of a file and return it. 34sub readFile($) 35{ 36 my ($fileName) = @_; 37 38 local $/; 39 open(FH, "<", $fileName); 40 my $content = <FH>; 41 close(FH); 42 43 return $content; 44} 45 46# Write a temporary file and return the filename. 47sub writeTempFile($$$) 48{ 49 my ($name, $extension, $content) = @_; 50 51 my ($FH, $fileName) = tempfile( 52 $name . "-XXXXXXXX", 53 DIR => ($ENV{'TMPDIR'} || "/tmp"), 54 UNLINK => 0, 55 ); 56 print $FH $content; 57 close $FH; 58 59 if ($extension) { 60 my $newFileName = $fileName . $extension; 61 rename($fileName, $newFileName); 62 $fileName = $newFileName; 63 } 64 65 return $fileName; 66} 67 68# -------------------------------------------------------------------------------- 69 70{ 71 # New test 72 my $title = "mergeChangeLogs: traditional rejected patch success"; 73 74 my $fileNewerContent = <<'EOF'; 752010-01-29 Mark Rowe <mrowe@apple.com> 76 77 Fix the Mac build. 78 79 Disable ENABLE_INDEXED_DATABASE since it is "completely non-functional". 80 812010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 82 83 Rubber-stamped by Maciej Stachowiak. 84 85 Fix the ARM build. 86EOF 87 my $fileNewer = writeTempFile("file", "", $fileNewerContent); 88 89 my $fileMineContent = <<'EOF'; 90*************** 91*** 1,3 **** 92 2010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 93 94 Rubber-stamped by Maciej Stachowiak. 95--- 1,9 ---- 96+ 2010-01-29 Oliver Hunt <oliver@apple.com> 97+ 98+ Reviewed by Darin Adler. 99+ 100+ JSC is failing to propagate anonymous slot count on some transitions 101+ 102 2010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 103 104 Rubber-stamped by Maciej Stachowiak. 105EOF 106 my $fileMine = writeTempFile("file", ".rej", $fileMineContent); 107 rename($fileMine, $fileNewer . ".rej"); 108 $fileMine = $fileNewer . ".rej"; 109 110 my $fileOlderContent = $fileNewerContent; 111 my $fileOlder = writeTempFile("file", ".orig", $fileOlderContent); 112 rename($fileOlder, $fileNewer . ".orig"); 113 $fileOlder = $fileNewer . ".orig"; 114 115 my $exitStatus = mergeChangeLogs($fileMine, $fileOlder, $fileNewer); 116 117 # mergeChangeLogs() should return 1 since the patch succeeded. 118 ok($exitStatus == 1, "$title: should return 1 for success"); 119 120 ok(readFile($fileMine) eq $fileMineContent, "$title: \$fileMine should be unchanged"); 121 ok(readFile($fileOlder) eq $fileOlderContent, "$title: \$fileOlder should be unchanged"); 122 123 my $expectedContent = <<'EOF'; 1242010-01-29 Oliver Hunt <oliver@apple.com> 125 126 Reviewed by Darin Adler. 127 128 JSC is failing to propagate anonymous slot count on some transitions 129 130EOF 131 $expectedContent .= $fileNewerContent; 132 ok(readFile($fileNewer) eq $expectedContent, "$title: \$fileNewer should be updated to include patch"); 133 134 unlink($fileMine, $fileOlder, $fileNewer); 135} 136 137# -------------------------------------------------------------------------------- 138 139{ 140 # New test 141 my $title = "mergeChangeLogs: traditional rejected patch failure"; 142 143 my $fileNewerContent = <<'EOF'; 1442010-01-29 Mark Rowe <mrowe@apple.com> 145 146 Fix the Mac build. 147 148 Disable ENABLE_INDEXED_DATABASE since it is "completely non-functional". 149 1502010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 151 152 Rubber-stamped by Maciej Stachowiak. 153 154 Fix the ARM build. 155EOF 156 my $fileNewer = writeTempFile("file", "", $fileNewerContent); 157 158 my $fileMineContent = <<'EOF'; 159*************** 160*** 1,9 **** 161- 2010-01-29 Oliver Hunt <oliver@apple.com> 162- 163- Reviewed by Darin Adler. 164- 165- JSC is failing to propagate anonymous slot count on some transitions 166- 167 2010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 168 169 Rubber-stamped by Maciej Stachowiak. 170--- 1,3 ---- 171 2010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 172 173 Rubber-stamped by Maciej Stachowiak. 174EOF 175 my $fileMine = writeTempFile("file", ".rej", $fileMineContent); 176 rename($fileMine, $fileNewer . ".rej"); 177 $fileMine = $fileNewer . ".rej"; 178 179 my $fileOlderContent = $fileNewerContent; 180 my $fileOlder = writeTempFile("file", ".orig", $fileOlderContent); 181 rename($fileOlder, $fileNewer . ".orig"); 182 $fileOlder = $fileNewer . ".orig"; 183 184 my $exitStatus = mergeChangeLogs($fileMine, $fileOlder, $fileNewer); 185 186 # mergeChangeLogs() should return 0 since the patch failed. 187 ok($exitStatus == 0, "$title: should return 0 for failure"); 188 189 ok(readFile($fileMine) eq $fileMineContent, "$title: \$fileMine should be unchanged"); 190 ok(readFile($fileOlder) eq $fileOlderContent, "$title: \$fileOlder should be unchanged"); 191 ok(readFile($fileNewer) eq $fileNewerContent, "$title: \$fileNewer should be unchanged"); 192 193 unlink($fileMine, $fileOlder, $fileNewer); 194} 195 196# -------------------------------------------------------------------------------- 197 198{ 199 # New test 200 my $title = "mergeChangeLogs: patch succeeds"; 201 202 my $fileMineContent = <<'EOF'; 2032010-01-29 Oliver Hunt <oliver@apple.com> 204 205 Reviewed by Darin Adler. 206 207 JSC is failing to propagate anonymous slot count on some transitions 208 2092010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 210 211 Rubber-stamped by Maciej Stachowiak. 212 213 Fix the ARM build. 214EOF 215 my $fileMine = writeTempFile("fileMine", "", $fileMineContent); 216 217 my $fileOlderContent = <<'EOF'; 2182010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 219 220 Rubber-stamped by Maciej Stachowiak. 221 222 Fix the ARM build. 223EOF 224 my $fileOlder = writeTempFile("fileOlder", "", $fileOlderContent); 225 226 my $fileNewerContent = <<'EOF'; 2272010-01-29 Mark Rowe <mrowe@apple.com> 228 229 Fix the Mac build. 230 231 Disable ENABLE_INDEXED_DATABASE since it is "completely non-functional". 232 2332010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 234 235 Rubber-stamped by Maciej Stachowiak. 236 237 Fix the ARM build. 238EOF 239 my $fileNewer = writeTempFile("fileNewer", "", $fileNewerContent); 240 241 my $exitStatus = mergeChangeLogs($fileMine, $fileOlder, $fileNewer); 242 243 # mergeChangeLogs() should return 1 since the patch succeeded. 244 ok($exitStatus == 1, "$title: should return 1 for success"); 245 246 ok(readFile($fileMine) eq $fileMineContent, "$title: \$fileMine should be unchanged"); 247 ok(readFile($fileOlder) eq $fileOlderContent, "$title: \$fileOlder should be unchanged"); 248 249 my $expectedContent = <<'EOF'; 2502010-01-29 Oliver Hunt <oliver@apple.com> 251 252 Reviewed by Darin Adler. 253 254 JSC is failing to propagate anonymous slot count on some transitions 255 256EOF 257 $expectedContent .= $fileNewerContent; 258 259 ok(readFile($fileNewer) eq $expectedContent, "$title: \$fileNewer should be patched"); 260 261 unlink($fileMine, $fileOlder, $fileNewer); 262} 263 264# -------------------------------------------------------------------------------- 265 266{ 267 # New test 268 my $title = "mergeChangeLogs: patch fails"; 269 270 my $fileMineContent = <<'EOF'; 2712010-01-29 Mark Rowe <mrowe@apple.com> 272 273 Fix the Mac build. 274 275 Disable ENABLE_INDEXED_DATABASE since it is "completely non-functional". 276 2772010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 278 279 Rubber-stamped by Maciej Stachowiak. 280 281 Fix the ARM build. 282EOF 283 my $fileMine = writeTempFile("fileMine", "", $fileMineContent); 284 285 my $fileOlderContent = <<'EOF'; 2862010-01-29 Mark Rowe <mrowe@apple.com> 287 288 Fix the Mac build. 289 290 Disable ENABLE_INDEXED_DATABASE since it is "completely non-functional". 291 2922010-01-29 Oliver Hunt <oliver@apple.com> 293 294 Reviewed by Darin Adler. 295 296 JSC is failing to propagate anonymous slot count on some transitions 297 2982010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 299 300 Rubber-stamped by Maciej Stachowiak. 301 302 Fix the ARM build. 303EOF 304 my $fileOlder = writeTempFile("fileOlder", "", $fileOlderContent); 305 306 my $fileNewerContent = <<'EOF'; 3072010-01-29 Oliver Hunt <oliver@apple.com> 308 309 Reviewed by Darin Adler. 310 311 JSC is failing to propagate anonymous slot count on some transitions 312 3132010-01-29 Simon Hausmann <simon.hausmann@nokia.com> 314 315 Rubber-stamped by Maciej Stachowiak. 316 317 Fix the ARM build. 318EOF 319 my $fileNewer = writeTempFile("fileNewer", "", $fileNewerContent); 320 321 my $exitStatus = mergeChangeLogs($fileMine, $fileOlder, $fileNewer); 322 323 # mergeChangeLogs() should return a non-zero exit status since the patch failed. 324 ok($exitStatus == 0, "$title: return non-zero exit status for failure"); 325 326 ok(readFile($fileMine) eq $fileMineContent, "$title: \$fileMine should be unchanged"); 327 ok(readFile($fileOlder) eq $fileOlderContent, "$title: \$fileOlder should be unchanged"); 328 329 # $fileNewer should still exist unchanged because the patch failed 330 ok(readFile($fileNewer) eq $fileNewerContent, "$title: \$fileNewer should be unchanged"); 331 332 unlink($fileMine, $fileOlder, $fileNewer); 333} 334 335# -------------------------------------------------------------------------------- 336 337