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 #5: fix false negatives when checking test classes #81

Merged
merged 1 commit into from
May 7, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void execute( EnforcerRuleHelper helper )

MavenLogger logger = new MavenLogger( helper.getLog() );

final Set<String> ignoredPackages = buildPackageList( outputDirectory, project, logger );
final Set<String> ignoredPackages = buildPackageList( outputDirectory, testOutputDirectory, project, logger );

if ( ignores != null )
{
Expand Down Expand Up @@ -288,18 +289,22 @@ private static Dependency findMatchingDependency( Signature signature, List<Depe
* @param outputDirectory
* @param logger
*/
private Set<String> buildPackageList( File outputDirectory, MavenProject project, Logger logger )
private Set<String> buildPackageList( File outputDirectory, File testOutputDirectory, MavenProject project, Logger logger )
throws IOException
{
ClassListBuilder plb = new ClassListBuilder( logger );
apply( plb, outputDirectory, project, logger );
apply( plb, outputDirectory, testOutputDirectory, project, logger );
return plb.getPackages();
}

private void apply( ClassFileVisitor v, File outputDirectory, MavenProject project, Logger logger )
private void apply( ClassFileVisitor v, File outputDirectory, File testOutputDirectory, MavenProject project, Logger logger )
throws IOException
{
v.process( outputDirectory );
if ( checkTestClasses )
{
v.process( testOutputDirectory );
}
if ( ignoreDependencies )
{
PatternIncludesArtifactFilter includesFilter = includeDependencies == null
Expand All @@ -309,6 +314,13 @@ private void apply( ClassFileVisitor v, File outputDirectory, MavenProject proje
? null
: new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) );

Set<String> classpathScopes = new HashSet<>(
Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM ) );
if ( checkTestClasses )
{
classpathScopes.addAll( Arrays.asList( Artifact.SCOPE_TEST, Artifact.SCOPE_RUNTIME ) );
}

logger.debug( "Building list of classes from dependencies" );
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
{
Expand All @@ -321,11 +333,11 @@ private void apply( ClassFileVisitor v, File outputDirectory, MavenProject proje
continue;
}

if ( !( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) || Artifact.SCOPE_PROVIDED.equals(
artifact.getScope() ) || Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) )
if ( !classpathScopes.contains( artifact.getScope() ) )
{
logger.debug( "Skipping artifact " + artifactId( artifact )
+ " as it is not on the compile classpath." );
+ " as it is not on the "
+ ( checkTestClasses ? "test" : "compile" ) + " classpath." );
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@
<pluginVersion>@project.version@</pluginVersion>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Main
{
public static void main( String[] args )
{
org.junit.Assert.assertTrue( true );

if ( new java.util.concurrent.ConcurrentHashMap().isEmpty() )
{
System.out.println( "All is good" );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
File log = new File(basedir, 'build.log')

assert log.text.contains( 'Main.java:34: Undefined reference: java.util.concurrent.ConcurrentHashMap' )
assert !log.text.contains( 'Undefined reference: void org.junit.Assert.assertTrue(boolean)' )
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -363,6 +364,10 @@ private void apply( ClassFileVisitor v )
throws IOException
{
v.process( outputDirectory );
if ( checkTestClasses )
{
v.process( testOutputDirectory );
}
if ( ignoreDependencies )
{
PatternIncludesArtifactFilter includesFilter = includeDependencies == null
Expand All @@ -373,6 +378,14 @@ private void apply( ClassFileVisitor v )
: new PatternExcludesArtifactFilter( Arrays.asList( excludeDependencies ) );

getLog().debug( "Building list of classes from dependencies" );

Set<String> classpathScopes = new HashSet<>(
Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM ) );
if ( checkTestClasses )
{
classpathScopes.addAll( Arrays.asList( Artifact.SCOPE_TEST, Artifact.SCOPE_RUNTIME ) );
}

for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
{

Expand All @@ -384,11 +397,11 @@ private void apply( ClassFileVisitor v )
continue;
}

if ( !( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) || Artifact.SCOPE_PROVIDED.equals(
artifact.getScope() ) || Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) )
if ( !classpathScopes.contains( artifact.getScope() ) )
{
getLog().debug( "Skipping artifact " + BuildSignaturesMojo.artifactId( artifact )
+ " as it is not on the compile classpath." );
+ " as it is not on the "
+ ( checkTestClasses ? "test" : "compile" ) + " classpath." );
continue;
}

Expand Down