Skip to content
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

Fix NPE during report aggregation when multiple modules are specified using -pl arg #104

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ or
There is no separate mojo for aggregated reports, there is `aggregate` parameter.
To additionally generate aggregated SCoverage report for root module, when generating regular reports,
set `aggregate` parameter value to `true`.
It works only in multimodule projects.
It works only in multimodule projects, the aggregated report will be generated in the current
execution root.

It can be configured by defining `aggregate` plugin configuration parameter or `scoverage.aggregate` project property.

Expand Down
1 change: 1 addition & 0 deletions src/it/test_report_for_some_submodules/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=clean scoverage:report -e -ntp -pl module01 -pl module02/submodule02_02
16 changes: 16 additions & 0 deletions src/it/test_report_for_some_submodules/module01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>test_report_for_some_submodules</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>module01</artifactId>
<name>Test Scoverage report works for specified submodules: Module 1</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg01

class HelloService1
{
def hello =
{
"Hello from module 1"
}

}

object HelloService1 extends HelloService1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg01

import org.junit.Test;
import org.junit.Assert.assertEquals

class HelloServiceTest
{
@Test
def test1()
{
assertEquals("Hello from module 1", HelloService1.hello)
}

}
23 changes: 23 additions & 0 deletions src/it/test_report_for_some_submodules/module02/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>test_report_for_some_submodules</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>module02</artifactId>
<packaging>pom</packaging>
<name>Test Scoverage report works for specified submodules: Module 2</name>

<modules>
<module>submodule02_01</module>
<module>submodule02_02</module>
</modules>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>module02</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>submodule02_01</artifactId>
<name>Test Scoverage report works for specified submodules: SubModule 2_1</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg02_01

class HelloService2
{
def hello =
{
"Hello from submodule02_01"
}

}

object HelloService2 extends HelloService2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg02_01

import org.junit.Test;
import org.junit.Assert.assertEquals

class HelloServiceTest
{
@Test
def test2()
{
assertEquals("Hello from submodule02_01", HelloService2.hello)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>module02</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>submodule02_02</artifactId>
<name>Test Scoverage report works for specified submodules: SubModule 2_2</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg02_02

class HelloService2
{
def hello =
{
"Hello from submodule02_02"
}

}

object HelloService2 extends HelloService2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg02_02

import org.junit.Test;
import org.junit.Assert.assertEquals

class HelloServiceTest
{
@Test
def test2()
{
assertEquals("Hello from submodule02_02", HelloService2.hello)
}

}
65 changes: 65 additions & 0 deletions src/it/test_report_for_some_submodules/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>it.scoverage-maven-plugin</groupId>
<artifactId>integration_tests_parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../integration_tests_parent/pom.xml</relativePath>
</parent>

<artifactId>test_report_for_some_submodules</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Test Scoverage report works for specified submodules</name>
<description>Test Scoverage report works for specified submodules</description>

<properties>
<scala.compat.version>2.13</scala.compat.version>
<scala.minor.version>12</scala.minor.version>
</properties>

<modules>
<module>module01</module>
<module>module02</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<configuration>
<aggregate>true</aggregate> <!-- for aggregated report -->
</configuration>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<configuration>
<aggregate>true</aggregate> <!-- for aggregated report -->
</configuration>
</plugin>
</plugins>
</reporting>
</project>
14 changes: 14 additions & 0 deletions src/it/test_report_for_some_submodules/validate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try {

def aggregatedScoverageFile = new File(basedir, "target/scoverage.xml")
assert aggregatedScoverageFile.exists()

def aggregatedReportFile = new File(basedir, "target/site/scoverage/index.html")
assert aggregatedReportFile.exists()

return true

} catch (Throwable e) {
e.printStackTrace()
return false
}
27 changes: 27 additions & 0 deletions src/main/java/org/scoverage/plugin/SCoverageReportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -535,6 +537,31 @@ else if ( !module.getPackaging().equals( "pom" ) )
scoverageDataDirs.size() ) );
}

/* traverse up the module tree until a module isExecutionRoot */
if ( topLevelModule == null )
{
Queue<MavenProject> candidateForTopLevelModules = new ArrayDeque<>(reactorProjects);
while ( !candidateForTopLevelModules.isEmpty() )
{
MavenProject module = candidateForTopLevelModules.poll();
if ( module.isExecutionRoot() )
{
topLevelModule = module;
break;
}
if ( module.hasParent() )
{
candidateForTopLevelModules.add(module.getParent());
}
}
}
if ( topLevelModule == null )
{
// This exception should never be thrown.
throw new IllegalStateException("Cannot find the top level module to write the " +
"aggregated reports.");
}

File topLevelModuleOutputDirectory = rebase( outputDirectory, topLevelModule );
File topLevelModuleXmlOutputDirectory = rebase( xmlOutputDirectory, topLevelModule );

Expand Down