Skip to content

Commit

Permalink
Fixed plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DreierF committed Jul 2, 2019
1 parent de48c03 commit 03689b8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ class TestDataWriter {
this.reportDirectory = reportDirectory;
}

/** Writes the given test executions to a report file. */
void dumpTestExecutions(List<TestExecution> testExecutions) {
writeReport(new File(reportDirectory, "test-execution.json"), testExecutions);
File file = new File(reportDirectory, "test-execution.json");
try {
ReportUtils.writeTestExecutionReport(file, testExecutions);
} catch (IOException e) {
LOGGER.error(e, () -> "Error while writing report to file: " + file);
}
}

/** Writes the given test details to a report file. */
void dumpTestDetails(List<TestDetails> testDetails) {
writeReport(new File(reportDirectory, "test-list.json"), testDetails);
}

private static <T> void writeReport(File file, T report) {
File file = new File(reportDirectory, "test-list.json");
try {
ReportUtils.writeReportToFile(file, report);
ReportUtils.writeTestListReport(file, testDetails);
} catch (IOException e) {
LOGGER.error(e, () -> "Error while writing report to file: " + file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import com.teamscale.client.FileSystemUtils;
import com.teamscale.client.TestDetails;
import com.teamscale.report.testwise.ETestArtifactFormat;
import com.teamscale.report.testwise.model.TestExecution;
import com.teamscale.report.testwise.model.TestwiseCoverageReport;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
Expand All @@ -21,23 +25,46 @@ public class ReportUtils {

private static Moshi moshi = new Moshi.Builder().build();

private static JsonAdapter<List<TestDetails>> testDetailsAdapter = moshi.<List<TestDetails>>adapter(
Types.newParameterizedType(List.class, TestDetails.class)).indent("\t");

private static JsonAdapter<List<TestExecution>> testExecutionAdapter = moshi.<List<TestExecution>>adapter(
Types.newParameterizedType(List.class, TestExecution.class)).indent("\t");

private static JsonAdapter<TestwiseCoverageReport> testwiseCoverageReportAdapter = moshi
.adapter(TestwiseCoverageReport.class).indent("\t");

/** Converts to given test list to a json report and writes it to the given file. */
public static void writeTestListReport(File reportFile, List<TestDetails> report) throws IOException {
writeReportToFile(reportFile, report, testDetailsAdapter);
}

/** Converts to given test execution report to a json report and writes it to the given file. */
public static void writeTestExecutionReport(File reportFile, List<TestExecution> report) throws IOException {
writeReportToFile(reportFile, report, testExecutionAdapter);
}

/** Converts to given testwise coverage report to a json report and writes it to the given file. */
public static <T> void writeReportToFile(File reportFile, T report) throws IOException {
public static void writeTestwiseCoverageReport(File reportFile, TestwiseCoverageReport report) throws IOException {
writeReportToFile(reportFile, report, testwiseCoverageReportAdapter);
}

/** Converts to given report to a json string. For testing only. */
public static String getTestwiseCoverageReportAsString(TestwiseCoverageReport report) {
return testwiseCoverageReportAdapter.toJson(report);
}

/** Writes the report object to the given file as json. */
private static <T> void writeReportToFile(File reportFile, T report, JsonAdapter<T> adapter) throws IOException {
File directory = reportFile.getParentFile();
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new IOException("Failed to create directory " + directory.getAbsolutePath());
}
try (BufferedSink sink = Okio.buffer(Okio.sink(reportFile))) {
JsonAdapter<T> test = moshi.adapter((Class<T>) report.getClass()).indent("\t");
test.toJson(sink, report);
adapter.toJson(sink, report);
}
}

/** Converts to given report to a json string. */
public static <T> String getReportAsString(T report) {
return moshi.adapter((Class<T>) report.getClass()).indent("\t").toJson(report);
}

/** Recursively lists all files in the given directory that match the specified extension. */
public static <T> List<T> readObjects(ETestArtifactFormat format, Class<T[]> clazz,
List<File> directoriesOrFiles) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Arrays;
import java.util.Collections;

import static com.teamscale.report.ReportUtils.getReportAsString;
import static com.teamscale.report.ReportUtils.getTestwiseCoverageReportAsString;

/** Tests for {@link ClosureTestwiseCoverageGenerator}. */
public class ClosureTestwiseCoverageGeneratorTest extends TestDataBase {
Expand All @@ -36,6 +36,7 @@ private String runGenerator(String closureCoverageFolder) {
TestwiseCoverage testwiseCoverage = new ClosureTestwiseCoverageGenerator(
Collections.singletonList(coverageFolder), includeFilter, new CommandLineLogger())
.readTestCoverage();
return getReportAsString(JaCoCoTestwiseReportGeneratorTest.generateDummyReportFrom(testwiseCoverage));
return getTestwiseCoverageReportAsString(
JaCoCoTestwiseReportGeneratorTest.generateDummyReportFrom(testwiseCoverage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private String runGenerator(String testDataFolder, String execFileName) throws E
Collections.singletonList(classFileFolder),
includeFilter, EDuplicateClassFileBehavior.IGNORE,
mock(ILogger.class)).convert(useTestFile(execFileName));
return ReportUtils.getReportAsString(generateDummyReportFrom(testwiseCoverage));
return ReportUtils.getTestwiseCoverageReportAsString(generateDummyReportFrom(testwiseCoverage));
}

/** Generates a dummy coverage report object that wraps the given {@link TestwiseCoverage}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ open class TeamscaleReportTask : DefaultTask() {

val report = TestwiseCoverageReportBuilder.createFrom(testDetails, testwiseCoverage.tests, testExecutions)
logger.info("Writing report to ${reportConfig.reportFile}")
ReportUtils.writeReportToFile(reportConfig.reportFile, report)
ReportUtils.writeTestwiseCoverageReport(reportConfig.reportFile, report)

if (reportConfig.upload) {
uploadTask.reports.add(reportConfig)
Expand Down

0 comments on commit 03689b8

Please sign in to comment.