avoid STDERR diff3 messages when using wfMerge()
authorDaniel Kinzler <daniel.kinzler@wikimedia.de>
Sat, 10 Nov 2012 16:18:12 +0000 (17:18 +0100)
committerGerrit Code Review <gerrit@wikimedia.org>
Sat, 10 Nov 2012 17:26:08 +0000 (17:26 +0000)
diff3 issues a warning to stderr if any of the files does not end with a
newline character.

TextContent::preSaveTransform() does normalize revision text by simply
calling trim().  Thus to avoid a diff3 error we simply apply the same
normalization and add a newline to please the command.

Change-Id: I7baa3df95dd70cbc865b2491ccc791e60f8b9e6e

includes/GlobalFunctions.php
tests/phpunit/includes/GlobalFunctions/GlobalTest.php

index 3de25e7..7dc9a70 100644 (file)
@@ -2895,11 +2895,15 @@ function wfMerge( $old, $mine, $yours, &$result ) {
        $mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' );
        $yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' );
 
-       fwrite( $oldtextFile, $old );
+       # NOTE: diff3 issues a warning to stderr if any of the files does not end with
+       #       a newline character. To avoid this, we normalize the trailing whitespace before
+       #       creating the diff.
+
+       fwrite( $oldtextFile, rtrim( $old ) . "\n" );
        fclose( $oldtextFile );
-       fwrite( $mytextFile, $mine );
+       fwrite( $mytextFile, rtrim( $mine ) . "\n" );
        fclose( $mytextFile );
-       fwrite( $yourtextFile, $yours );
+       fwrite( $yourtextFile, rtrim( $yours ) . "\n" );
        fclose( $yourtextFile );
 
        # Check for a conflict
index e8aabfd..7304bd9 100644 (file)
@@ -488,6 +488,86 @@ class GlobalTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * @param String $old: Text as it was in the database
+        * @param String $mine: Text submitted while user was editing
+        * @param String $yours: Text submitted by the user
+        * @param Boolean $expectedMergeResult Whether the merge should be a success
+        * @param String $expectedText: Text after merge has been completed
+        *
+        * @dataProvider provideMerge()
+        */
+       public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
+               $mergedText = null;
+               $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
+
+               $msg = 'Merge should be a ';
+               $msg .= $expectedMergeResult ? 'success' : 'failure';
+               $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
+
+               if( $isMerged ) {
+                       // Verify the merged text
+                       $this->assertEquals( $expectedText, $mergedText,
+                               'is merged text as expected?' );
+               }
+       }
+
+       public static function provideMerge() {
+               $EXPECT_MERGE_SUCCESS = true;
+               $EXPECT_MERGE_FAILURE = false;
+
+               return array(
+
+                       // #0: clean merge
+                       array(
+                               // old:
+                               "one one one\n" . // trimmed
+                               "\n" .
+                               "two two two",
+
+                               // mine:
+                               "one one one ONE ONE\n" .
+                               "\n" .
+                               "two two two\n", // with tailing whitespace
+
+                               // yours:
+                               "one one one\n" .
+                               "\n" .
+                               "two two TWO TWO", // trimmed
+
+                               // ok:
+                               $EXPECT_MERGE_SUCCESS,
+
+                               // result:
+                               "one one one ONE ONE\n" .
+                               "\n" .
+                               "two two TWO TWO\n", // note: will always end in a newline
+                       ),
+
+                       // #1: conflict, fail
+                       array(
+                               // old:
+                               "one one one", // trimmed
+
+                               // mine:
+                               "one one one ONE ONE\n" .
+                               "\n" .
+                               "bla bla\n" .
+                               "\n", // with tailing whitespace
+
+                               // yours:
+                               "one one one\n" .
+                               "\n" .
+                               "two two", // trimmed
+
+                               $EXPECT_MERGE_FAILURE,
+
+                               // result:
+                               null,
+                       ),
+               );
+       }
+
        /**
         * @dataProvider provideMakeUrlIndexes()
         */