This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new fb7f749  [MENFORCER-466] Apply the all levels scope and optional 
selectors on RequireUpperBoundDeps (#254)
fb7f749 is described below

commit fb7f7494af19a60be4ebfaa87ad28dfa946dea24
Author: Andrzej Jarmoniuk <[email protected]>
AuthorDate: Fri Mar 17 21:54:06 2023 +0100

    [MENFORCER-466] Apply the all levels scope and optional selectors on 
RequireUpperBoundDeps (#254)
    
    * [MENFORCER-466] Apply the all levels (so, including direct) scope and 
optional selectors on RequireUpperBoundDeps
    
    Co-authored-by: Slawomir Jaranowski <[email protected]>
---
 .../rules/dependency/DependencyConvergence.java    | 35 ++++-----------
 .../rules/dependency/RequireUpperBoundDeps.java    | 11 ++++-
 .../AllLevelsOptionalDependencySelector.java       | 40 +++++++++++++++++
 .../selector/AllLevelsScopeDependencySelector.java | 50 ++++++++++++++++++++++
 .../dependency/RequireUpperBoundDepsTest.java      |  3 +-
 .../menforcer466_requires_api150-1.0.pom}          | 31 ++------------
 .../menforcer466_requires_utils30-1.0.pom}         | 33 +++-----------
 .../pom.xml                                        | 50 +++++++++++++++-------
 .../invoker.properties                             | 18 --------
 9 files changed, 155 insertions(+), 116 deletions(-)

diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java
 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java
index b235b66..21cac73 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java
@@ -22,21 +22,21 @@ import javax.inject.Inject;
 import javax.inject.Named;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
-import org.apache.maven.artifact.Artifact;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
+import 
org.apache.maven.enforcer.rules.dependency.selector.AllLevelsOptionalDependencySelector;
+import 
org.apache.maven.enforcer.rules.dependency.selector.AllLevelsScopeDependencySelector;
 import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
-import org.eclipse.aether.collection.DependencyCollectionContext;
-import org.eclipse.aether.collection.DependencySelector;
-import org.eclipse.aether.graph.Dependency;
 import org.eclipse.aether.graph.DependencyNode;
 import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
 
+import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
+import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
+
 /**
  * @author <a href="mailto:[email protected]";>Rex Hoffman</a>
  */
@@ -49,8 +49,6 @@ public final class DependencyConvergence extends 
AbstractStandardEnforcerRule {
 
     private List<String> excludes;
 
-    private List<String> scopes = Arrays.asList(Artifact.SCOPE_COMPILE, 
Artifact.SCOPE_RUNTIME, Artifact.SCOPE_SYSTEM);
-
     private DependencyVersionMap dependencyVersionMap;
 
     private final ResolveUtil resolveUtil;
@@ -64,23 +62,8 @@ public final class DependencyConvergence extends 
AbstractStandardEnforcerRule {
     public void execute() throws EnforcerRuleException {
 
         DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
-                // TODO: use a modified version of ExclusionDependencySelector 
to process excludes and includes
-                new DependencySelector() {
-                    @Override
-                    public boolean selectDependency(Dependency dependency) {
-                        // regular OptionalDependencySelector only 
discriminates optional dependencies at level 2+
-                        return !dependency.isOptional()
-                                // regular scope selectors only discard 
transitive dependencies
-                                // and always allow direct dependencies
-                                && scopes.contains(dependency.getScope());
-                    }
-
-                    @Override
-                    public DependencySelector 
deriveChildSelector(DependencyCollectionContext context) {
-                        return this;
-                    }
-                },
-                // process dependency exclusions
+                new AllLevelsOptionalDependencySelector(),
+                new AllLevelsScopeDependencySelector(SCOPE_TEST, 
SCOPE_PROVIDED),
                 new ExclusionDependencySelector());
         dependencyVersionMap = new 
DependencyVersionMap().setUniqueVersions(uniqueVersions);
         node.accept(dependencyVersionMap);
@@ -142,7 +125,7 @@ public final class DependencyConvergence extends 
AbstractStandardEnforcerRule {
     @Override
     public String toString() {
         return String.format(
-                "DependencyConvergence[includes=%s, excludes=%s, 
uniqueVersions=%b, scopes=%s]",
-                includes, excludes, uniqueVersions, String.join(",", scopes));
+                "DependencyConvergence[includes=%s, excludes=%s, 
uniqueVersions=%b]",
+                includes, excludes, uniqueVersions);
     }
 }
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
index 0a26be3..50e054b 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java
@@ -34,12 +34,18 @@ import 
org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
+import 
org.apache.maven.enforcer.rules.dependency.selector.AllLevelsOptionalDependencySelector;
+import 
org.apache.maven.enforcer.rules.dependency.selector.AllLevelsScopeDependencySelector;
 import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
 import org.apache.maven.enforcer.rules.utils.ParentNodeProvider;
 import org.apache.maven.enforcer.rules.utils.ParentsVisitor;
 import org.eclipse.aether.graph.DependencyNode;
 import org.eclipse.aether.graph.DependencyVisitor;
 import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
+import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
+
+import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
+import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
 
 /**
  * Rule to enforce that the resolved dependency is also the most recent one of 
all transitive dependencies.
@@ -97,7 +103,10 @@ public final class RequireUpperBoundDeps extends 
AbstractStandardEnforcerRule {
 
     @Override
     public void execute() throws EnforcerRuleException {
-        DependencyNode node = 
resolveUtil.resolveTransitiveDependenciesVerbose();
+        DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
+                new AllLevelsOptionalDependencySelector(),
+                new AllLevelsScopeDependencySelector(SCOPE_TEST, 
SCOPE_PROVIDED),
+                new ExclusionDependencySelector());
         upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
                 .setUniqueVersions(uniqueVersions)
                 .setIncludes(includes);
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java
 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java
new file mode 100644
index 0000000..4c9e2ad
--- /dev/null
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.enforcer.rules.dependency.selector;
+
+import org.eclipse.aether.collection.DependencyCollectionContext;
+import org.eclipse.aether.collection.DependencySelector;
+import org.eclipse.aether.graph.Dependency;
+
+/**
+ * Dependency selector discarding {@code optional} dependencies on all levels.
+ * The standard {@link 
org.eclipse.aether.util.graph.selector.OptionalDependencySelector}
+ * does not discard direct dependencies.
+ */
+public class AllLevelsOptionalDependencySelector implements DependencySelector 
{
+    @Override
+    public boolean selectDependency(Dependency dependency) {
+        return !dependency.isOptional();
+    }
+
+    @Override
+    public DependencySelector deriveChildSelector(DependencyCollectionContext 
context) {
+        return this;
+    }
+}
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java
 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java
new file mode 100644
index 0000000..7490edf
--- /dev/null
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.enforcer.rules.dependency.selector;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.eclipse.aether.collection.DependencyCollectionContext;
+import org.eclipse.aether.collection.DependencySelector;
+import org.eclipse.aether.graph.Dependency;
+
+/**
+ * Dependency selector discarding dependencies with the given scope on all 
levels.
+ * The standard {@link 
org.eclipse.aether.util.graph.selector.ScopeDependencySelector}
+ * does not discard direct dependencies.
+ */
+public class AllLevelsScopeDependencySelector implements DependencySelector {
+    private final Collection<String> excluded;
+
+    public AllLevelsScopeDependencySelector(String... excluded) {
+        this.excluded = excluded != null ? Arrays.asList(excluded) : 
Collections.emptyList();
+    }
+
+    @Override
+    public boolean selectDependency(Dependency dependency) {
+        return !excluded.contains(dependency.getScope());
+    }
+
+    @Override
+    public DependencySelector deriveChildSelector(DependencyCollectionContext 
context) {
+        return this;
+    }
+}
diff --git 
a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
 
b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
index 0cdd221..ac66f0d 100644
--- 
a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
+++ 
b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java
@@ -27,6 +27,7 @@ import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
@@ -41,7 +42,7 @@ class RequireUpperBoundDepsTest {
     @Test
     void testRule() throws Exception {
 
-        when(resolveUtil.resolveTransitiveDependenciesVerbose())
+        when(resolveUtil.resolveTransitiveDependenciesVerbose(any(), any(), 
any()))
                 .thenReturn(new DependencyNodeBuilder()
                         .withType(DependencyNodeBuilder.Type.POM)
                         .withChildNode(new DependencyNodeBuilder()
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
 
b/maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_api150-1.0.pom
similarity index 64%
copy from 
maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
copy to 
maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_api150-1.0.pom
index 2adf202..5df5742 100644
--- 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
+++ 
b/maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_api150-1.0.pom
@@ -21,37 +21,14 @@
 <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";>
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.maven.plugins.enforcer.its</groupId>
-  <artifactId>menforcer128</artifactId>
-  <version>1.0-SNAPSHOT</version>
-  <packaging>jar</packaging>
+  <artifactId>menforcer466_requires_api150</artifactId>
+  <version>1.0</version>
+
   <dependencies>
     <dependency>
       <groupId>org.apache.maven.plugins.enforcer.its</groupId>
       <artifactId>menforcer128_api</artifactId>
-      <version>1.4.0</version>
-      <scope>provided</scope>
+      <version>1.5.0</version>
     </dependency>
   </dependencies>
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-enforcer-plugin</artifactId>
-        <version>@project.version@</version>
-        <executions>
-          <execution>
-            <id>enforce</id>
-            <configuration>
-              <rules>
-                <RequireUpperBoundDeps/>
-              </rules>
-            </configuration>
-            <goals>
-              <goal>enforce</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
 </project>
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
 
b/maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_utils30-1.0.pom
similarity index 62%
copy from 
maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
copy to 
maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_utils30-1.0.pom
index 2adf202..26af710 100644
--- 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
+++ 
b/maven-enforcer-plugin/src/it/mrm/repository/menforcer466_requires_utils30-1.0.pom
@@ -21,37 +21,14 @@
 <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";>
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.maven.plugins.enforcer.its</groupId>
-  <artifactId>menforcer128</artifactId>
-  <version>1.0-SNAPSHOT</version>
-  <packaging>jar</packaging>
+  <artifactId>menforcer466_requires_utils30</artifactId>
+  <version>1.0</version>
+
   <dependencies>
     <dependency>
       <groupId>org.apache.maven.plugins.enforcer.its</groupId>
-      <artifactId>menforcer128_api</artifactId>
-      <version>1.4.0</version>
-      <scope>provided</scope>
+      <artifactId>menforcer138_utils</artifactId>
+      <version>3.0</version>
     </dependency>
   </dependencies>
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-enforcer-plugin</artifactId>
-        <version>@project.version@</version>
-        <executions>
-          <execution>
-            <id>enforce</id>
-            <configuration>
-              <rules>
-                <RequireUpperBoundDeps/>
-              </rules>
-            </configuration>
-            <goals>
-              <goal>enforce</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
 </project>
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
 
b/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-scope-optional/pom.xml
similarity index 71%
rename from 
maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
rename to 
maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-scope-optional/pom.xml
index 2adf202..b62f04a 100644
--- 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/pom.xml
+++ 
b/maven-enforcer-plugin/src/it/projects/require-upper-bound-dependencies-scope-optional/pom.xml
@@ -20,18 +20,10 @@
 -->
 <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";>
   <modelVersion>4.0.0</modelVersion>
-  <groupId>org.apache.maven.plugins.enforcer.its</groupId>
-  <artifactId>menforcer128</artifactId>
+  <groupId>org.apache.maven.enforcer.its</groupId>
+  <artifactId>require-upper-bound-dependencies-provided</artifactId>
   <version>1.0-SNAPSHOT</version>
-  <packaging>jar</packaging>
-  <dependencies>
-    <dependency>
-      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
-      <artifactId>menforcer128_api</artifactId>
-      <version>1.4.0</version>
-      <scope>provided</scope>
-    </dependency>
-  </dependencies>
+
   <build>
     <plugins>
       <plugin>
@@ -41,17 +33,45 @@
         <executions>
           <execution>
             <id>enforce</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
             <configuration>
               <rules>
-                <RequireUpperBoundDeps/>
+                <requireUpperBoundDeps/>
               </rules>
             </configuration>
-            <goals>
-              <goal>enforce</goal>
-            </goals>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer128_api</artifactId>
+      <version>1.4.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer138_utils</artifactId>
+      <version>3.0</version>
+      <optional>true</optional>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer466_requires_api150</artifactId>
+      <version>1.0</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer466_requires_utils30</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
 </project>
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/invoker.properties
 
b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/invoker.properties
deleted file mode 100644
index 1dcdc65..0000000
--- 
a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps-provided/invoker.properties
+++ /dev/null
@@ -1,18 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#  http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-invoker.buildResult = success

Reply via email to