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
Binary file not being generated #597
Comments
Hi, Could you please clarify, what binary file do you have in mind and at what time do you expect it? |
What I want to accomplish
static String changedFiles() {
def gitOutput = 'git diff --name-only master...'.execute().text
def files = gitOutput.tokenize('\n').findAll { it.endsWith('.kt') }.join(',')
println "git diff files => $files"
return files
}
tasks.withType(KotlinCompile).configureEach {
doLast {
def outputDir = destinationDirectory.get().asFile
def cliConfig = configurations.named("koverCli")
def koverJar = cliConfig.get().filter { it.name.startsWith("kover-cli") }.singleFile
exec {
commandLine(
"java",
"-jar",
koverJar.canonicalPath,
"instrument",
outputDir,
"--dest",
outputDir,
"--include",
changedFiles()
)
}
}
tasks.withType(KotlinCompile).configureEach {
doLast {
def outputDir = destinationDirectory.get().asFile
def cliConfig = configurations.named("koverCli")
def koverJar = cliConfig.get().filter { it.name.startsWith("kover-cli") }.singleFile
if (koverJar) {
exec {
commandLine(
"java",
"-jar",
koverJar.canonicalPath,
"report",
"--classfiles",
outputDir,
"--src",
"${layout.buildDirectory}/kover/bin-reports/testBelgiumDevDebugUnitTest.ic",
"--html",
"${outputDir.absolutePath}/kover-report.html",
"--xml",
"${outputDir.absolutePath}/kover-report.xml",
)
}
} else {
println "Kover CLI JAR not found in dependencies"
}
}
}
|
Instrumentation is intended to modify classes. It cannot evaluate coverage, because Kover isn't a static analyzing tool, and it requires running instrumented classes. It is necessary to run tests over the instrumented classes. Please note that if the tests being run are connected tests (or are also called Android instrumented tests), then you will need to transfer the binary report from the emulator (or Android device) to the host machine so that this file can be read from the build script. |
First of all thanks a lot for prompt responses. Based on my understanding the sequence should be following:
Based on above hypothesis of mine I came up this below script looking at the examples. And I run unit tests with task dependencies {
add("koverCli", libs.kover.cli)
runtimeOnly(libs.kover.offline.runtime)
}
File koverCliJar() {
def cliConfig = configurations.named("koverCli")
return cliConfig.get().filter { it.name.startsWith("kover-cli") }.singleFile
}
tasks.withType(KotlinCompile).configureEach {
doLast {
def outputDir = destinationDirectory.get().asFile
def koverJar = koverCliJar()
if (koverJar) {
println "Kover Offline Runtime Execution starting"
def args = []
args << "java"
args << "-jar"
args << koverJar.canonicalPath
args << "instrument"
args << outputDir
args << "--dest"
args << outputDir
args << "--hits"
def changedFiles = changedFiles()
changedFiles.forEach { file ->
args << "--include"
args << file
}
exec { commandLine(args) }
println "Kover Offline Runtime Execution finished"
} else {
println "Kover CLI JAR not found in dependencies"
}
}
}
def binaryReport = layout.buildDirectory.file("kover/report.ic").get().asFile
tasks.withType(Test).configureEach {
// set system property for binary report path
systemProperty("kover.offline.report.path", binaryReport.absolutePath)
} |
Have you added an If yes, and the error still occurs, please send this error and its full stack-trace. |
Yes I did add the dependency. Here is the stacktrace
And if I look via html report. Here is one stacktrace for one failing test
|
Replace |
Actually above stacktrace is for when I used |
It may be worth completely rebuild the application, but this error usually indicates that runtime classes are unavailable |
Does it mean I must first run assemble task and then test task? |
Call |
I have always been running test as below: Interestingly if I comment out below script tests succeed tasks.withType(KotlinCompile).configureEach {
doLast {
def outputDir = destinationDirectory.get().asFile
def koverJar = koverCliJar()
if (koverJar) {
println "Kover Offline Runtime Execution starting"
def args = []
args << "java"
args << "-jar"
args << koverJar.canonicalPath
args << "instrument"
args << outputDir
args << "--dest"
args << outputDir
def changedFiles = changedFiles()
changedFiles.forEach { file ->
args << "--include"
args << file
}
exec { commandLine(args) }
println "Kover Offline Runtime Execution finished"
} else {
println "Kover CLI JAR not found in dependencies"
}
}
} |
One question. above script is expected to run after compilation finished or after unit test gradle task completion? |
In this case, the instrumentation will not occur and the
the test task depends on the compilation task. Therefore, compilation task will be called before running the tests, instrumentation will occur, and only after that the tests will be performed. However, it is not necessary to instrument the tasks of compiling tests. |
not really, you only need to instrument tasks for only compiling application classes, not compiling tests (in your case, it will be, for example,
It is not recommended to use the Kover Gradle Plugin and the offline instrumentation at the same time - this can lead to unexpected errors. |
This is new info to me. Indeed we're using Kover Gradle Plugin in combination with SonarQube to track projects code coverage. What I am trying to accomplish on top it is to have code coverage report only for changed files, which I get from git commit, and fail pipeline for Pull Request if coverage is below certain percentage, for instance 50%, but only for changed/added kotlin files not for entire project. I know that Kover Gradle Plugin minium coverage feature in place but checks for entire project coverage but I need is only for changed files. Could you give any advice on how I could do that? Is it even possible with Kover? |
Could you clarify what is the the number of modified files in your PR? Tens, hundreds or thousands? |
Normally it's between a range of 10 to 20 files. It's rare when we have 100 files affected in a single PR. Those things happen during big migrations only. |
But it is also not about an entire file which has changed but rather to focus only on change within that file. |
@shanshin any advice on how I can accomplish above or is it simply not possible? |
It looks like even if you set up offline instrumentation correctly, you will not be able to collect coverage in this way:
you use file names in it, however, only class names are accepted as input of parameter Also, it seems to me that using the approach of displaying coverage of only modified files is an ineffective means of controlling coverage.
Let's say in PR you change only class
according to the modified files report, the percentage of coverage remained unchanged, but in reality, coverage has dropped significantly (for class Therefore, I would recommend avoiding using the modified files report to analyze coverage changes. To do this, you may use other metrics: for example, compare the percentage of coverage of all classes before and after changes, and issue a warning if the newer class coverage has become less than a certain value, or if the coverage has decreased by a certain value |
@shanshin thanks a lot for the detailed explanation and guidance. Would you give high level advice on how I could approach in terms of implementation your suggestion above? How do I get coverage of all classes before the changes? Should I checkout to master and run |
Unfortunately, there is no universal solution here. It all depends on your workflow, CI/CD settings etc. However, in general terms, yes, you first need to run You can try using external tools, such as codecov, which have such functionality. However, there are some problems with codecov integration. |
When I run below command it succeeds but no binary file being generated
Am I missing something?
The text was updated successfully, but these errors were encountered: