Skip to content

Commit

Permalink
Fix test report bug on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gayaldassanayake committed Jan 5, 2024
1 parent f228b14 commit 6a37fe4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,26 @@ public static void generateTesterinaReports(Project project, TestReport testRepo
}
}

Path reportZipPath = Paths.get(System.getProperty(BALLERINA_HOME)).resolve(BALLERINA_HOME_LIB).
resolve(TesterinaConstants.TOOLS_DIR_NAME).resolve(TesterinaConstants.COVERAGE_DIR).
resolve(REPORT_ZIP_NAME);
// Dump the Testerina html report only if '--test-report' flag is provided
if (project.buildOptions().testReport()) {
Path reportZipPath = getReportToolsPath();
if (Files.exists(reportZipPath)) {
String content;
try {
try (FileInputStream fileInputStream = new FileInputStream(reportZipPath.toFile())) {
CodeCoverageUtils.unzipReportResources(fileInputStream,
reportDir.toFile());
CodeCoverageUtils.unzipReportResources(fileInputStream, reportDir.toFile());

Check warning on line 185 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L185

Added line #L185 was not covered by tests
}
content = Files.readString(reportDir.resolve(RESULTS_HTML_FILE));
content = content.replace(REPORT_DATA_PLACEHOLDER, json);
} catch (IOException e) {
throw createLauncherException("error occurred while preparing test report: " + e.toString());
throw createLauncherException("error occurred while preparing test report: " + e);

Check warning on line 190 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L190

Added line #L190 was not covered by tests
}
File htmlFile = new File(reportDir.resolve(RESULTS_HTML_FILE).toString());
try (FileOutputStream fileOutputStream = new FileOutputStream(htmlFile)) {
try (Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
writer.write(new String(content.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
out.println("\tView the test report at: " +
FILE_PROTOCOL + Paths.get(htmlFile.getPath()).toAbsolutePath().normalize().toString());
FILE_PROTOCOL + Paths.get(htmlFile.getPath()).toAbsolutePath().normalize());

Check warning on line 197 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L197

Added line #L197 was not covered by tests
}
}
} else {
Expand All @@ -210,6 +207,17 @@ public static void generateTesterinaReports(Project project, TestReport testRepo
}
}

/**
* Get the path of the report tools template from Ballerina home.
*
* @return path of the report tools template
*/
public static Path getReportToolsPath() {
return Paths.get(System.getProperty(BALLERINA_HOME)).resolve(BALLERINA_HOME_LIB).
resolve(TesterinaConstants.TOOLS_DIR_NAME).resolve(TesterinaConstants.COVERAGE_DIR).
resolve(REPORT_ZIP_NAME);
}

/**
* Loads the ModuleStatus object by reading a given Json.
*
Expand All @@ -235,7 +243,7 @@ public static void writeToTestSuiteJson(Map<String, TestSuite> testSuiteMap, Pat
try {
Files.createDirectories(testsCachePath);
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't create test cache directories : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't create test cache directories : " + e);

Check warning on line 246 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L246

Added line #L246 was not covered by tests
}
}

Expand All @@ -247,10 +255,10 @@ public static void writeToTestSuiteJson(Map<String, TestSuite> testSuiteMap, Pat
String json = gson.toJson(testSuiteMap);
writer.write(new String(json.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e);

Check warning on line 258 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L258

Added line #L258 was not covered by tests
}
} catch (IOException e) {
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e.toString());
throw LauncherUtils.createLauncherException("couldn't write data to test suite file : " + e);

Check warning on line 261 in cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/TestUtils.java#L261

Added line #L261 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,8 @@ public static void unzipReportResources(InputStream source, File target) throws
final ZipInputStream zipStream = new ZipInputStream(source);
ZipEntry nextEntry;
while ((nextEntry = zipStream.getNextEntry()) != null) {

File zipFile = new File(REPORT_ZIP_DIRECTORY + nextEntry.getName());
String canonicalZipPath = zipFile.getCanonicalPath();

if (!canonicalZipPath.startsWith(REPORT_ZIP_DIRECTORY)) {
continue;
}
String name = canonicalZipPath.replace(REPORT_ZIP_DIRECTORY, "");

// only extract files
if (!nextEntry.getName().endsWith("/")) {
final File nextFile = new File(target, name);
if (!nextEntry.isDirectory()) {
final File nextFile = new File(target, nextEntry.getName());

// create directories
final File parent = nextFile.getParentFile();
Expand All @@ -234,7 +224,6 @@ public static void unzipReportResources(InputStream source, File target) throws
try (OutputStream targetStream = new FileOutputStream(nextFile)) {
final int bufferSize = 4 * 1024;
final byte[] buffer = new byte[bufferSize];

int nextCount;
while ((nextCount = zipStream.read(buffer)) >= 0) {
targetStream.write(buffer, 0, nextCount);
Expand Down

0 comments on commit 6a37fe4

Please sign in to comment.