Skip to content

Conversation

rrourke
Copy link

@rrourke rrourke commented Nov 8, 2022

This fixes a problem with submodules where if it was previously built even using mvn clean it will look in other sub modules for their class files. Class files should be isolated to the module that the diff is being performed on.
This will also skip running if a jacoco file is not found.

Copy link
Owner

@SurpSG SurpSG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rrourke

Thank you for your contribution.
I leaved a few comments in your PR. Just trying to understand what issue(s) you want to fix

return reactorProjects.asSequence()
.map { project -> File(project.build.outputDirectory) }
.filter { outputDirectory -> outputDirectory.exists() }
.filter { outputDirectory -> outputDirectory.exists() && outputDirectory.path.contains(rootProjectDir.path)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot understand why do we need this check. Could you explain?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not need this, was from before. I'll remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested it, this is still needed. Without this the class files are not filtered and all classes for all modules are exposed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, still cannot get it. Could you provide a sample project or integration tests, so I can see a project setup?

Existing IT here: https://github.com/SurpSG/diff-coverage-maven-plugin/tree/master/src/it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is going to take me a while to make to make a project that hits this criteria but think of it like this.
project demo
Sub modules:

  • A
  • B
  • C
    Module build order is project demo -> a -> b ->c.
    Code changes are made to sub module a, unit tests are added passes code coverage.
    B now runs and it detects the classes from module a, its new classes from module b(self) and will pass jacoc but will fail code coverage because there are no tests for classes from a in its jacoco that only ran on b.

if you print https://github.com/SurpSG/diff-coverage-maven-plugin/blob/master/src/main/kotlin/com/github/surpsg/DiffCoverageMojo.kt#L91

you will see:

Classes
[INFO] [/Users/.../demo/target/classes, /Users/.../demo/target/a/classes, /Users/.../demo/b/target/classes, /Users/.../demo/b/target/classes]

so when the code coverage runs it will look at the classes in a see that no tests were run in jacoco and then fail.
My change makes it so what is seen is this

Classes
[INFO] [/Users/.../demo/b/target/classes]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SurpSG https://github.com/rrourke/mvn-example/tree/quickTest here is an example repo. Checkout the quickTest repo and run mvn install clean you will see it fail for the reasons i mentioned above. You can then use my code for diff-covrage-maven-plugin and you will see it pass.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I got it, thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, just let me know what else I need to do to get this merged

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add several integration tests

https://github.com/SurpSG/diff-coverage-maven-plugin/tree/master/src/it

}
if (!found) {
log.info("No exec files found skipping")
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain your motivation?

Exec files are always appear if tests are run. From my experience, missed exec files it's misconfiguration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all modules might have unit tests, this might be redundant since I changed how it scans.


private val rootProjectDir: File
get() = reactorProjects[0].basedir
get() = project.basedir
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there are sub modules it scans for all class files or if using the -rf flag it will use the base dir of the first module ran. In either case the class files it scans for are either just from the first module or it looks at all modules. This causes problems as if you have sub module a, b, and c if you make changes to b than when c runs the code change was in b but the jacoco for c doesn't have the unit tests and thus fails. Making this change makes it so it only looks at the class files for the module that is running the tests.

@rrourke
Copy link
Author

rrourke commented Nov 16, 2022

@SurpSG I've made some changes let me know what you think

@rrourke
Copy link
Author

rrourke commented Nov 17, 2022

@SurpSG saw the test failed added missing path files that were not added

@SurpSG
Copy link
Owner

SurpSG commented Nov 21, 2022

Hi @rrourke

Thank you for this awesome work!

Still investigating your PR.

@rrourke
Copy link
Author

rrourke commented Nov 22, 2022

@SurpSG looks like it failed on itself, what do you want to do from here?

@SurpSG
Copy link
Owner

SurpSG commented Nov 22, 2022

The plugin is applied to itself. It reported that coverage is not enough.
But please, don't increase the coverage ritght now. Wait until I finish my investigation.
Seems, Maven doesn't provide a clear way to solve this issue.

Suppose, we have a structure:

root
  |__ module_1
  |__module_2
  |__pom.xml

What I expect:

  • Jacoco maven plugin is aplied to submodules
  • Diff Coverage is applied to the root only
  • When we run install or verify:
    ** tests are run in module_1(coverage is collected by jacoco)
    ** tests are run in module_2(coverage is collected by jacoco)
    ** Diff Coverage is run:
    *** collected .exec files from all projects
    *** collected classes from all projects
    *** Run diff coverage analyze. Finally, receive aggregated diff coverage report from entire project

Currently, it has a different behavior.

How it was implemented in jacoco maven plugin to receive the aggregated report? Recommendations from the jacoco team:

  • create a separate subproject(reporting-module) that depends on all another projects
  • apply Jacoco aggregate to the reporting-module
    When we call install:
  • all tests are run(coverage collected)
  • at the end reporting-module is invoked with aggregation task

DiffCoverage maven plugin currently can work in the same way. See integration test: (multi-module-reports-generation-check)[https://github.com/SurpSG/diff-coverage-maven-plugin/tree/master/src/it/multi-module-reports-generation-check]
But I don't like such approach. We have a separate module for reporting needs.

What we can do:

  • declare the DiffCoverageMojo.kt as aggregator(see about aggregator here)
    But the verification on root is invoked earlier than on subpojects, so we don't have coverage info at that moment. Somehow we should force invoking diff coverage that applied to the root after subproject's tests.

  • Another solutiong - convert DiffCoverageMojo to report plugin. I have a draft code for this approach and seems it works as I expect. I need more time to investigate my approach and your solution as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants