Skip to content

Commit

Permalink
Use System.lineSeparator() on all systems
Browse files Browse the repository at this point in the history
The current version of the `flatten-maven-plugin` switches between LF
and CRLF line endings based on the system name.

Reproducible builds require normalization of line endings on all
systems.  This PR allows to change the generated line endings by passing
an appropriate `-Dline.separator=...` option to Maven and therefore
allows to use LF line endings also on Windows machines.
  • Loading branch information
ppkarwasz authored and slawekjaranowski committed Mar 16, 2023
1 parent 4f80bad commit 1f0e61d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ protected void writePom( Model pom, File pomFile, String headerComment, KeepComm
protected void writeStringToFile( String data, File file, String encoding )
throws MojoExecutionException
{
if ( System.getProperty( "os.name" ).contains( "Windows" ) )
if ( !"\n".equals( System.lineSeparator() ) )
{
data = data.replace( "\n", "\r\n" );
data = data.replace( "\n", System.lineSeparator() );
}
byte[] binaryData;

Expand Down
48 changes: 28 additions & 20 deletions src/test/java/org/codehaus/mojo/flatten/KeepCommentsInPomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Pattern;

import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
Expand All @@ -32,7 +33,7 @@
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Test-Case for {@link FlattenMojo}.
Expand All @@ -48,6 +49,7 @@ public class KeepCommentsInPomTest
* Expected result since jdk11 with updated xml header and properties sequence.
*/
private static final String EXPECTED_FLATTENED_POM_JDK11 = PATH + "expected-flattened-pom-jdk11.xml";
private static final Pattern NEW_LINE_PATTERN = Pattern.compile( "\\n|\\r\\n?" );

@Rule
public MojoRule rule = new MojoRule();
Expand Down Expand Up @@ -77,18 +79,10 @@ public void keepsProfileActivationFile() throws Exception
// execute writes new FLATTENED_POM
flattenMojo.execute();

String tempExpectedContent;
if ( isJdk8() )
{
tempExpectedContent = getContent( EXPECTED_FLATTENED_POM );
}
else
{
tempExpectedContent = getContent( EXPECTED_FLATTENED_POM_JDK11 );
}
String tempActualContent = getContent( FLATTENED_POM );
assertEquals( "Expected POM does not match, see " + FLATTENED_POM, tempExpectedContent, tempActualContent );

Path expectedContentFile = Paths.get( isJdk8() ? EXPECTED_FLATTENED_POM : EXPECTED_FLATTENED_POM_JDK11 );
Path actualContentFile = Paths.get( FLATTENED_POM );
assertThat( actualContentFile ).hasSameTextualContentAs( expectedContentFile );
assertHasLineSeparator( actualContentFile , System.lineSeparator() );
}

/**
Expand All @@ -107,12 +101,26 @@ private boolean isJdk8()
return false;
}

/**
*
*/
private String getContent( String aPomFile ) throws IOException
private static void assertHasLineSeparator( final Path file, final String expectedSeparator ) throws IOException
{
return String.join( "\n", Files.readAllLines( Paths.get( aPomFile ), StandardCharsets.UTF_8 ) );
try ( Scanner scanner = new Scanner( file ) )
{
int lineNr = 0;
String actualSeparator;
while ( ( actualSeparator = scanner.findWithinHorizon( NEW_LINE_PATTERN, 0 ) ) != null )
{
lineNr++;
if ( !expectedSeparator.equals( actualSeparator ) )
{
final String actualDesc = actualSeparator.replace( "\r", "CR" ).replace( "\n", "LF" );
final String expectedDesc = expectedSeparator.replace( "\r", "CR" ).replace( "\n", "LF" );
throw new AssertionError(
String.format(
"\nLine %d of path %s has %s as line separator.\nExpected line separator: %s.",
lineNr, file, actualDesc, expectedDesc ) );
}
}
}
}

}

0 comments on commit 1f0e61d

Please sign in to comment.