Skip to content

Commit 7b65a25

Browse files
committed
Fix PropertiesFileGeneratorTest failing on Windows
The properties files are written with OS-specific line breaks, but the expected output previously only considered '\n' instead of Windows '\r\n'. Also simplified the code reading the properties file content in PropertiesFileGeneratorTest.
1 parent 55a0db2 commit 7b65a25

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/main/java/pl/project13/core/util/PropertyManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ protected static void dumpProperties(OutputStream outputStream, OrderedPropertie
6060
// use the OrderedProperties.store(Writer, ...)-method to avoid illegal reflective access warning
6161
// see: https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/523
6262
outputWriter.write("#Generated by Git-Commit-Id-Plugin");
63-
outputWriter.write(System.getProperty("line.separator"));
63+
outputWriter.write(System.lineSeparator());
6464
for (Map.Entry<String, String> e : sortedLocalProperties.entrySet()) {
6565
String key = saveConvert(e.getKey(), true, escapeUnicode);
6666
String val = saveConvert(e.getValue(), false, escapeUnicode);
6767
outputWriter.write(key + "=" + val);
68-
outputWriter.write(System.getProperty("line.separator"));
68+
outputWriter.write(System.lineSeparator());
6969
}
7070
}
7171
}

src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public void setUp() {
5151
propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, CommitIdPropertiesOutputFormat.PROPERTIES, "", "test");
5252
}
5353

54+
/**
55+
* Replaces {@code \n} with the OS-specific line break characters.
56+
*/
57+
private static String convertLineBreaks(String s) {
58+
return s.replace("\n", System.lineSeparator());
59+
}
60+
5461
@Test
5562
public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExecutionException, IOException {
5663
Properties properties = new Properties();
@@ -62,12 +69,11 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec
6269
propertiesFileGenerator.maybeGeneratePropertiesFile(
6370
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, false);
6471

65-
byte[] bytes = Files.readAllBytes(propertiesPath);
66-
String actualContent = new String(bytes, UTF_8);
67-
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
72+
String actualContent = Files.readString(propertiesPath, UTF_8);
73+
String expectedContent = convertLineBreaks("#Generated by Git-Commit-Id-Plugin\n"
6874
+ "branch=develop\n"
6975
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
70-
+ "commit.message.short=測試中文\n";
76+
+ "commit.message.short=測試中文\n");
7177
assertEquals(expectedContent, actualContent);
7278
}
7379

@@ -82,12 +88,11 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx
8288
propertiesFileGenerator.maybeGeneratePropertiesFile(
8389
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
8490

85-
byte[] bytes = Files.readAllBytes(propertiesPath);
86-
String actualContent = new String(bytes, UTF_8);
87-
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
91+
String actualContent = Files.readString(propertiesPath, UTF_8);
92+
String expectedContent = convertLineBreaks("#Generated by Git-Commit-Id-Plugin\n"
8893
+ "branch=develop\n"
8994
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
90-
+ "commit.message.short=\\u6E2C\\u8A66\\u4E2D\\u6587\n";
95+
+ "commit.message.short=\\u6E2C\\u8A66\\u4E2D\\u6587\n");
9196
assertEquals(expectedContent, actualContent);
9297
}
9398

@@ -101,11 +106,10 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
101106
propertiesFileGenerator.maybeGeneratePropertiesFile(
102107
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
103108

104-
byte[] bytes = Files.readAllBytes(propertiesPath);
105-
String actualContent = new String(bytes, UTF_8);
106-
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
109+
String actualContent = Files.readString(propertiesPath, UTF_8);
110+
String expectedContent = convertLineBreaks("#Generated by Git-Commit-Id-Plugin\n"
107111
+ "branch=develop\n"
108-
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
112+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n");
109113
assertEquals(expectedContent, actualContent);
110114
}
111115

@@ -123,11 +127,10 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
123127
propertiesFileGenerator.maybeGeneratePropertiesFile(
124128
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
125129

126-
byte[] bytes = Files.readAllBytes(propertiesPath);
127-
String actualContent = new String(bytes, UTF_8);
128-
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
130+
String actualContent = Files.readString(propertiesPath, UTF_8);
131+
String expectedContent = convertLineBreaks("#Generated by Git-Commit-Id-Plugin\n"
129132
+ "branch=develop\n"
130-
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
133+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n");
131134
assertEquals(expectedContent, actualContent);
132135
}
133136

@@ -143,10 +146,9 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
143146

144147
Path absolutePath = temporaryFolder.getRoot().toPath().resolve("src/blah/blub/git.properties");
145148
assertTrue(absolutePath.toFile().exists());
146-
byte[] bytes = Files.readAllBytes(absolutePath);
147-
String actualContent = new String(bytes, UTF_8);
148-
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
149-
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
149+
String actualContent = Files.readString(absolutePath, UTF_8);
150+
String expectedContent = convertLineBreaks("#Generated by Git-Commit-Id-Plugin\n"
151+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n");
150152
assertEquals(expectedContent, actualContent);
151153
}
152154
}

0 commit comments

Comments
 (0)