Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter in function maybeGeneratePropertiesFile in order to choose to escape unicode or not. #11

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/pl/project13/core/PropertiesFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public PropertiesFileGenerator(LogInterface log, BuildFileChangeListener buildFi
this.projectName = projectName;
}

public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset) throws GitCommitIdExecutionException {
public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset, boolean escapeUnicode) throws GitCommitIdExecutionException {
try {
final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
final boolean isJsonFormat = "json".equalsIgnoreCase(format);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
} else {
log.info(String.format("Writing properties file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
PropertyManager.dumpProperties(outputStream, sortedLocalProperties);
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
}
} catch (final IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/pl/project13/core/util/PropertyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull C
}
}

public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties) throws IOException {
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.ISO_8859_1)) {
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
TheSnoozer marked this conversation as resolved.
Show resolved Hide resolved
// use the OrderedProperties.store(Writer, ...)-method to avoid illegal reflective access warning
// see: https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/523
outputWriter.write("#Generated by Git-Commit-Id-Plugin");
outputWriter.write(System.getProperty("line.separator"));
for (Map.Entry<String, String> e : sortedLocalProperties.entrySet()) {
String key = saveConvert(e.getKey(), true, true);
String val = saveConvert(e.getValue(), false, true);
String key = saveConvert(e.getKey(), true, escapeUnicode);
String val = saveConvert(e.getValue(), false, escapeUnicode);
outputWriter.write(key + "=" + val);
outputWriter.write(System.getProperty("line.separator"));
}
Expand Down
46 changes: 42 additions & 4 deletions src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ public void setUp() {

propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, "properties", "", "test");
}

@Test
public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExecutionException, IOException {
Properties properties = new Properties();
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, false);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
+ "branch=develop\n"
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
+ "commit.message.short=測試中文\n";
assertEquals(expectedContent, actualContent);
}

@Test
public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionException, IOException {
Properties properties = new Properties();
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
+ "branch=develop\n"
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
+ "commit.message.short=\\u6E2C\\u8A66\\u4E2D\\u6587\n";
assertEquals(expectedContent, actualContent);
}

@Test
public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitIdExecutionException, IOException {
Expand All @@ -56,7 +94,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
properties.put(GitCommitPropertyConstant.BRANCH, "develop");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
Expand All @@ -73,10 +111,10 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
properties.put(GitCommitPropertyConstant.BRANCH, "develop");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

// Re-read the generated properties file.
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
Expand All @@ -85,4 +123,4 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
assertEquals(expectedContent, actualContent);
}
}
}