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

Fix extra line in generated ballerina source in grpc tool #60

Merged
merged 2 commits into from
Jan 8, 2025
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
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);
}
}
}
Loading