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
This commit is contained in:
Daniel Kinzler 2012-11-10 17:18:12 +01:00 committed by Gerrit Code Review
parent 6670bf66df
commit 2c85e0dd5b
2 changed files with 87 additions and 3 deletions

View 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

View 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()
*/