Skip to content

Commit

Permalink
[MENFORCER-389] Allow filtering of parent in requireReleaseDeps
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jun 2, 2022
1 parent fd574ec commit fa8f7f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Expand Up @@ -19,8 +19,10 @@
* under the License.
*/

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
Expand All @@ -43,8 +45,6 @@ public class RequireReleaseDeps
/**
* Allows this rule to execute only when this project is a release.
*
* @parameter
*
* @see {@link #setOnlyWhenRelease(boolean)}
* @see {@link #isOnlyWhenRelease()}
Expand All @@ -54,8 +54,6 @@ public class RequireReleaseDeps
/**
* Allows this rule to fail when the parent is defined as a snapshot.
*
* @parameter
*
* @see {@link #setFailWhenParentIsSnapshot(boolean)}
* @see {@link #isFailWhenParentIsSnapshot()}
*/
Expand Down Expand Up @@ -98,6 +96,7 @@ public void execute( EnforcerRuleHelper helper )
{
callSuper = true;
}

if ( callSuper )
{
super.execute( helper );
Expand All @@ -107,7 +106,17 @@ public void execute( EnforcerRuleHelper helper )
{
project = getProject( helper );
}

Artifact parentArtifact = project.getParentArtifact();

if ( parentArtifact != null )
{
Set<Artifact> artifacts = filterArtifacts( Collections.singleton( parentArtifact ) );
parentArtifact = Optional.ofNullable( artifacts )
.flatMap( s -> s.stream().findFirst() )
.orElse( null );
}

if ( parentArtifact != null && parentArtifact.isSnapshot() )
{
throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.io.IOException;
import java.util.Collections;
import java.util.Set;

Expand All @@ -30,12 +31,14 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* The Class TestNoSnapshots.
* The Class TestRequireReleaseDeps.
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public class TestRequireReleaseDeps
class TestRequireReleaseDeps
{

/**
Expand All @@ -44,7 +47,7 @@ public class TestRequireReleaseDeps
* @throws Exception if any occurs
*/
@Test
public void testRule()
void testRule()
throws Exception
{
ArtifactStubFactory factory = new ArtifactStubFactory();
Expand Down Expand Up @@ -90,7 +93,7 @@ public void testRule()
}

@Test
public void testWildcardIgnore()
void testWildcardIgnore()
throws Exception
{
RequireReleaseDeps rule = newRequireReleaseDeps();
Expand All @@ -112,12 +115,33 @@ public void testWildcardIgnore()
* Test id.
*/
@Test
public void testId()
void testId()
{
RequireReleaseDeps rule = newRequireReleaseDeps();
rule.getCacheId();
assertThat( rule.getCacheId() ).isEqualTo( "0" );
}

@Test
void parentShouldBeExcluded() throws IOException
{

ArtifactStubFactory factory = new ArtifactStubFactory();
MockProject project = new MockProject();
project.setArtifact( factory.getSnapshotArtifact() );

MavenProject parent = new MockProject();
parent.setArtifact( factory.getSnapshotArtifact() );
project.setParent( parent );

EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );

RequireReleaseDeps rule = newRequireReleaseDeps();
rule.setExcludes( Collections.singletonList( parent.getArtifact().getGroupId() + ":*" ) );

EnforcerRuleUtilsHelper.execute( rule, helper, false );
}


private RequireReleaseDeps newRequireReleaseDeps()
{
return new RequireReleaseDeps()
Expand Down

0 comments on commit fa8f7f2

Please sign in to comment.