Skip to content

Commit

Permalink
Merge pull request #60 from randilt/extra-line-fix-grpc-source
Browse files Browse the repository at this point in the history
Fix extra line in generated ballerina source in grpc tool
  • Loading branch information
daneshk authored Jan 8, 2025
2 parents 66acf72 + 5202704 commit 19c92ce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private static void writeOutputFile(SyntaxTree syntaxTree, String outPath)
throw new CodeBuilderException("Formatter Error while formatting output source code. " + e.getMessage(), e);
}
try (PrintWriter writer = new PrintWriter(outPath, StandardCharsets.UTF_8.name())) {
writer.println(content);
writer.print(content);
} catch (IOException e) {
throw new CodeBuilderException("IO Error while writing output to Ballerina file. " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,39 @@ public void testImportsWithEnums() {
assertGeneratedSources("data-types/enum_imports", "child.proto",
"child_pb.bal", "helloworld_service.bal", "helloworld_client.bal", "tool_test_data_type_26");
}

@Test
public void testGeneratedFileNewlines() {
try {
Files.createDirectories(Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_newline_test"));
} catch (IOException e) {
Assert.fail("Could not create target directories", e);
}

Path protoFilePath = Paths.get(RESOURCE_DIRECTORY.toString(), PROTO_FILE_DIRECTORY, "data-types",
"message.proto");
Path outputDirPath = Paths.get(GENERATED_SOURCES_DIRECTORY, "tool_test_newline_test");
Path generatedFile = outputDirPath.resolve("message_pb.bal");

generateSourceCode(protoFilePath, outputDirPath, null, null);
Assert.assertTrue(Files.exists(generatedFile), "Generated file does not exist");

try {
String content = Files.readString(generatedFile);
int newlineCount = 0;
int index = content.length() - 1;

// check for newlines at the end of the file
while (index >= 0 && content.charAt(index) == '\n') {
newlineCount++;
index--;
}

// if the newline count is not 1, fail the test
Assert.assertEquals(newlineCount, 1,
"Generated file should have exactly one newline at the end, found " + newlineCount);
} catch (IOException e) {
Assert.fail("Failed to read generated file", e);
}
}
}

0 comments on commit 19c92ce

Please sign in to comment.