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

[MENFORCER-389] Allow filtering of parent in requireReleaseDeps #157

Merged
merged 1 commit into from Jun 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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