From 2c85e0dd5b690f819832d447a616781d2e7bdb8c Mon Sep 17 00:00:00 2001 From: Daniel Kinzler Date: Sat, 10 Nov 2012 17:18:12 +0100 Subject: [PATCH] avoid STDERR diff3 messages when using wfMerge() 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 | 10 ++- .../includes/GlobalFunctions/GlobalTest.php | 80 +++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3de25e716d..7dc9a7085e 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index e8aabfd9f1..7304bd9551 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -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() */ -- 2.20.1