forked from bazelbuild/rules_kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Builds on previous work (bazelbuild#52) to add coverage support to Kotlin targets in Bazel. So far, it's working (in basic form), but seems to omit major swaths of our code when I test it on a live codebase. I'm not sure why that happens yet.
- Loading branch information
Sam Gammon
committed
Dec 16, 2020
1 parent
77b6458
commit 495db02
Showing
10 changed files
with
142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/JacocoProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.bazel.kotlin.builder.tasks.jvm | ||
|
||
import io.bazel.kotlin.builder.toolchain.KotlinToolchain | ||
import org.jacoco.core.instr.Instrumenter | ||
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator | ||
import java.io.BufferedInputStream | ||
import java.io.BufferedOutputStream | ||
import java.io.IOException | ||
import java.nio.file.FileVisitResult | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.nio.file.SimpleFileVisitor | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import io.bazel.kotlin.model.JvmCompilationTask | ||
import com.google.devtools.build.lib.view.proto.Deps | ||
import javax.inject.Inject | ||
|
||
|
||
/** Implements Jacoco instrumentation. */ | ||
class JacocoProcessor @Inject constructor(val compiler: KotlinToolchain.KotlincInvoker) { | ||
fun instrument(command: JvmCompilationTask) { | ||
val classDir = Paths.get(command.directories.classes) | ||
val instr = Instrumenter(OfflineInstrumentationAccessGenerator()) | ||
|
||
// Runs Jacoco instrumentation processor over all .class files. | ||
Files.walkFileTree( | ||
classDir, | ||
object : SimpleFileVisitor<Path>() { | ||
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { | ||
if (!file.fileName.toString().endsWith(".class")) { | ||
return FileVisitResult.CONTINUE | ||
} | ||
|
||
val uninstrumentedCopy = Paths.get(file.toString() + ".uninstrumented") | ||
Files.move(file, uninstrumentedCopy) | ||
BufferedInputStream(Files.newInputStream(uninstrumentedCopy)).use { input -> | ||
BufferedOutputStream(Files.newOutputStream(file)).use { output -> | ||
instr.instrument(input, output, file.toString()) | ||
} | ||
} | ||
return FileVisitResult.CONTINUE | ||
} | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters