Skip to content

Commit

Permalink
TS-38628 Partial Kotlin migration on several modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Apr 27, 2024
1 parent fa2c68e commit f968675
Show file tree
Hide file tree
Showing 57 changed files with 743 additions and 719 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/build.gradle.kts
@@ -1,4 +1,5 @@
plugins {
kotlin("jvm") version "1.9.23"
com.teamscale.`java-convention`
alias(libs.plugins.markdownToPdf)
application
Expand Down
4 changes: 2 additions & 2 deletions agent/src/main/java/com/teamscale/jacoco/agent/Agent.java
Expand Up @@ -29,7 +29,7 @@
import com.teamscale.jacoco.agent.upload.IUploader;
import com.teamscale.jacoco.agent.upload.UploaderException;
import com.teamscale.jacoco.agent.util.AgentUtils;
import com.teamscale.jacoco.agent.util.Benchmark;
import com.teamscale.jacoco.agent.util.Benchmark2;
import com.teamscale.jacoco.agent.util.Timer;
import com.teamscale.report.jacoco.CoverageFile;
import com.teamscale.report.jacoco.EmptyReportException;
Expand Down Expand Up @@ -185,7 +185,7 @@ private void dumpReportUnsafe() {
return;
}

try (Benchmark ignored = new Benchmark("Generating the XML report")) {
try (Benchmark2 ignored = new Benchmark2("Generating the XML report")) {
File outputFile = options.createNewFileInOutputDirectory("jacoco", "xml");
CoverageFile coverageFile = generator.convert(dump, outputFile);
uploader.upload(coverageFile);
Expand Down
106 changes: 0 additions & 106 deletions agent/src/main/java/com/teamscale/jacoco/agent/convert/Converter.java

This file was deleted.

Expand Up @@ -5,7 +5,7 @@
+-------------------------------------------------------------------------*/
package com.teamscale.jacoco.agent.options;

import com.teamscale.client.EReportFormat;
import com.teamscale.client.ReportFormat;
import com.teamscale.client.FileSystemUtils;
import com.teamscale.client.StringUtils;
import com.teamscale.client.TeamscaleClient;
Expand Down Expand Up @@ -583,11 +583,11 @@ private IUploader createNwdiTeamscaleUploader(Instrumentation instrumentation) {
return uploader;
}

private EReportFormat getReportFormat() {
private ReportFormat getReportFormat() {
if (useTestwiseCoverageMode()) {
return EReportFormat.TESTWISE_COVERAGE;
return ReportFormat.TESTWISE_COVERAGE;
}
return EReportFormat.JACOCO;
return ReportFormat.JACOCO;
}

/**
Expand Down
@@ -1,6 +1,6 @@
package com.teamscale.jacoco.agent.testimpact;

import com.teamscale.client.EReportFormat;
import com.teamscale.client.ReportFormat;
import com.teamscale.jacoco.agent.JacocoRuntimeController;
import com.teamscale.jacoco.agent.options.AgentOptions;
import com.teamscale.jacoco.agent.upload.teamscale.TeamscaleConfig;
Expand Down Expand Up @@ -36,7 +36,7 @@ public CoverageToTeamscaleStrategy(JacocoRuntimeController controller, AgentOpti
protected void handleTestwiseCoverageJsonReady(String json) throws IOException {
try {
teamscaleClient
.uploadReport(EReportFormat.TESTWISE_COVERAGE, json,
.uploadReport(ReportFormat.TESTWISE_COVERAGE, json,
agentOptions.getTeamscaleServerOptions().commit,
agentOptions.getTeamscaleServerOptions().revision,
agentOptions.getTeamscaleServerOptions().partition,
Expand Down
Expand Up @@ -13,7 +13,7 @@
import org.slf4j.Logger;

import com.teamscale.client.HttpUtils;
import com.teamscale.jacoco.agent.util.Benchmark;
import com.teamscale.jacoco.agent.util.Benchmark2;
import com.teamscale.jacoco.agent.util.LoggingUtils;
import com.teamscale.report.jacoco.CoverageFile;

Expand Down Expand Up @@ -69,7 +69,7 @@ protected abstract Response<ResponseBody> uploadCoverageZip(File coverageFile)

@Override
public void upload(CoverageFile coverageFile) {
try (Benchmark ignored = new Benchmark("Uploading report via HTTP")) {
try (Benchmark2 ignored = new Benchmark2("Uploading report via HTTP")) {
if (tryUpload(coverageFile)) {
coverageFile.delete();
} else {
Expand Down
Expand Up @@ -16,7 +16,7 @@

import com.google.common.base.Strings;
import com.teamscale.client.CommitDescriptor;
import com.teamscale.client.EReportFormat;
import com.teamscale.client.ReportFormat;
import com.teamscale.client.HttpUtils;
import com.teamscale.client.StringUtils;
import com.teamscale.jacoco.agent.upload.HttpZipUploaderBase;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class ArtifactoryUploader extends HttpZipUploaderBase<IArtifactoryUploadA

/** Constructor. */
public ArtifactoryUploader(ArtifactoryConfig config, List<Path> additionalMetaDataFiles,
EReportFormat reportFormat) {
ReportFormat reportFormat) {
super(config.url, additionalMetaDataFiles, IArtifactoryUploadApi.class);
this.artifactoryConfig = config;
this.coverageFormat = reportFormat.name().toLowerCase();
Expand Down
Expand Up @@ -23,7 +23,7 @@

import org.conqat.lib.commons.filesystem.FileSystemUtils;

import com.teamscale.client.EReportFormat;
import com.teamscale.client.ReportFormat;
import com.teamscale.jacoco.agent.upload.HttpZipUploaderBase;
import com.teamscale.jacoco.agent.upload.IUploadRetry;
import com.teamscale.jacoco.agent.upload.UploaderException;
Expand Down Expand Up @@ -150,7 +150,7 @@ private void checkAndCreatePath(List<String> pathParts) throws IOException, Uplo

/** Creates a file name for the zip-archive containing the coverage. */
private String createFileName() {
return String.format("%s-%s.zip", EReportFormat.JACOCO.name().toLowerCase(), System.currentTimeMillis());
return String.format("%s-%s.zip", ReportFormat.JACOCO.name().toLowerCase(), System.currentTimeMillis());
}

/** Checks if the file with the given name exists */
Expand Down
Expand Up @@ -18,14 +18,14 @@

import com.google.common.base.Strings;
import com.teamscale.client.CommitDescriptor;
import com.teamscale.client.EReportFormat;
import com.teamscale.client.ReportFormat;
import com.teamscale.client.HttpUtils;
import com.teamscale.client.ITeamscaleService;
import com.teamscale.client.TeamscaleServer;
import com.teamscale.client.TeamscaleServiceGenerator;
import com.teamscale.jacoco.agent.upload.IUploadRetry;
import com.teamscale.jacoco.agent.upload.IUploader;
import com.teamscale.jacoco.agent.util.Benchmark;
import com.teamscale.jacoco.agent.util.Benchmark2;
import com.teamscale.jacoco.agent.util.LoggingUtils;
import com.teamscale.report.jacoco.CoverageFile;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void reupload(CoverageFile coverageFile, Properties reuploadProperties) {
}

private void doUpload(CoverageFile coverageFile, TeamscaleServer teamscaleServer) {
try (Benchmark benchmark = new Benchmark("Uploading report to Teamscale")) {
try (Benchmark2 benchmark = new Benchmark2("Uploading report to Teamscale")) {
if (tryUploading(coverageFile, teamscaleServer)) {
deleteCoverageFile(coverageFile);
} else {
Expand Down Expand Up @@ -130,7 +130,7 @@ private boolean tryUploading(CoverageFile coverageFile, TeamscaleServer teamscal
teamscaleServer.url, teamscaleServer.userName, teamscaleServer.userAccessToken,
HttpUtils.DEFAULT_READ_TIMEOUT, HttpUtils.DEFAULT_WRITE_TIMEOUT);
api.uploadReport(teamscaleServer.project, teamscaleServer.commit, teamscaleServer.revision,
teamscaleServer.partition, EReportFormat.JACOCO, teamscaleServer.getMessage(),
teamscaleServer.partition, ReportFormat.JACOCO, teamscaleServer.getMessage(),
coverageFile.createFormRequestBody());
return true;
} catch (IOException e) {
Expand Down
Expand Up @@ -8,7 +8,7 @@
* Use this in a try-with-resources. Time measurement starts when the resource
* is created and ends when it is closed.
*/
public class Benchmark implements AutoCloseable {
public class Benchmark2 implements AutoCloseable {

/** The logger. */
private final Logger logger = LoggingUtils.getLogger(this);
Expand All @@ -20,7 +20,7 @@ public class Benchmark implements AutoCloseable {
private String description;

/** Constructor. */
public Benchmark(String description) {
public Benchmark2(String description) {
this.description = description;
startTime = System.nanoTime();
}
Expand Down
@@ -0,0 +1,95 @@
package com.teamscale.jacoco.agent.convert

import com.teamscale.client.TestDetails
import com.teamscale.jacoco.agent.options.AgentOptionParseException
import com.teamscale.jacoco.agent.util.Benchmark.benchmark
import com.teamscale.jacoco.agent.util.Logging.logger
import com.teamscale.jacoco.agent.util.LoggingUtils.wrap
import com.teamscale.report.ReportUtils.filterByFormat
import com.teamscale.report.ReportUtils.readObjects
import com.teamscale.report.jacoco.EmptyReportException
import com.teamscale.report.jacoco.JaCoCoXmlReportGenerator
import com.teamscale.report.jacoco.dump.Dump
import com.teamscale.report.testwise.ETestArtifactFormat
import com.teamscale.report.testwise.TestwiseCoverageReportWriter
import com.teamscale.report.testwise.jacoco.JaCoCoTestwiseReportGenerator
import com.teamscale.report.testwise.model.TestExecution
import com.teamscale.report.testwise.model.factory.TestInfoFactory
import com.teamscale.report.util.ClasspathWildcardIncludeFilter
import com.teamscale.report.util.CommandLineLogger
import org.jacoco.core.tools.ExecFileLoader
import java.io.IOException
import java.nio.file.Paths

/** Converts one .exec binary coverage file to XML. */
class Converter(private val arguments: ConvertCommand) {

/** Converts one .exec binary coverage file to XML. */
@Throws(IOException::class, AgentOptionParseException::class)
fun runJaCoCoReportGeneration() {
benchmark("Generating the XML report") {
val loader = ExecFileLoader().apply {
arguments.getInputFiles()
.filterByFormat(ETestArtifactFormat.JACOCO)
.forEach { load(it) }
}
val sessionInfo = loader.sessionInfoStore.getMerged("merged")
val executionDataStore = loader.executionDataStore

try {
JaCoCoXmlReportGenerator(
arguments.getClassDirectoriesOrZips(),
wildcardIncludeExcludeFilter,
arguments.duplicateClassFileBehavior,
arguments.shouldIgnoreUncoveredClasses,
wrap(logger)
).convert(
Dump(sessionInfo, executionDataStore),
Paths.get(arguments.outputFile).toFile()
)
} catch (e: EmptyReportException) {
logger.warn(e.localizedMessage)
}
}
}

/** Converts one .exec binary coverage file, test details and test execution files to JSON testwise coverage. */
@Throws(IOException::class, AgentOptionParseException::class)
fun runTestwiseCoverageReportGeneration() {
val testDetails = arguments.getInputFiles().readObjects<TestDetails>(
ETestArtifactFormat.TEST_LIST
)
val testExecutions = arguments.getInputFiles().readObjects<TestExecution>(
ETestArtifactFormat.TEST_EXECUTION
)
val logger = CommandLineLogger()
benchmark("Generating the testwise coverage report") {
logger.info(
"Writing report with " + testDetails.size + " Details/" + testExecutions.size + " Results"
)

TestwiseCoverageReportWriter(
TestInfoFactory(testDetails, testExecutions),
arguments.getOutputFile(),
arguments.splitAfter
).use { coverageWriter ->
arguments.getInputFiles()
.filterByFormat(ETestArtifactFormat.JACOCO)
.forEach { executionDataFile ->
JaCoCoTestwiseReportGenerator(
arguments.getClassDirectoriesOrZips(),
wildcardIncludeExcludeFilter,
arguments.duplicateClassFileBehavior,
logger
).convertAndConsume(executionDataFile, coverageWriter)
}
}
}
}

private val wildcardIncludeExcludeFilter: ClasspathWildcardIncludeFilter
get() = ClasspathWildcardIncludeFilter(
arguments.locationIncludeFilters.joinToString(":"),
arguments.locationExcludeFilters.joinToString(":")
)
}
12 changes: 12 additions & 0 deletions agent/src/main/kotlin/com/teamscale/jacoco/agent/util/Benchmark.kt
@@ -0,0 +1,12 @@
package com.teamscale.jacoco.agent.util

import com.teamscale.jacoco.agent.util.Logging.logger
import kotlin.time.measureTime

object Benchmark {
/**
* Measures how long a certain piece of code takes and logs it to the debug log.
*/
fun Any.benchmark(name: String, block: () -> Unit) =
logger.debug("$name took ${measureTime(block)} ms")
}

0 comments on commit f968675

Please sign in to comment.