"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/subprojects/dependency-management/src/integTest/groovy/org/gradle/integtests/resolve/alignment/AbstractAlignmentSpec.groovy" (20 Apr 2023, 9255 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2018 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 package org.gradle.integtests.resolve.alignment
   18 
   19 import org.gradle.integtests.fixtures.publish.RemoteRepositorySpec
   20 import org.gradle.integtests.resolve.AbstractModuleDependencyResolveTest
   21 
   22 abstract class AbstractAlignmentSpec extends AbstractModuleDependencyResolveTest {
   23     static class ModuleAlignmentSpec {
   24         String group = 'org'
   25         String name
   26         List<String> seenVersions = []
   27         List<String> misses = []
   28         String alignsTo
   29         List<String> virtualPlatforms = []
   30         List<String> publishedPlatforms = []
   31 
   32         ModuleAlignmentSpec group(String group) {
   33             this.group = group
   34             this
   35         }
   36 
   37         ModuleAlignmentSpec name(String name) {
   38             this.name = name
   39             this
   40         }
   41 
   42         ModuleAlignmentSpec tries(String... versions) {
   43             Collections.addAll(seenVersions, versions)
   44             this
   45         }
   46 
   47         ModuleAlignmentSpec misses(String... versions) {
   48             Collections.addAll(misses, versions)
   49             this
   50         }
   51 
   52         ModuleAlignmentSpec alignsTo(String version) {
   53             this.alignsTo = version
   54             this
   55         }
   56 
   57         ModuleAlignmentSpec byVirtualPlatform(String group = 'org', String name = 'platform') {
   58             virtualPlatforms << "${group}:${name}"
   59             this
   60         }
   61 
   62         ModuleAlignmentSpec byPublishedPlatform(String group = 'org', String name = 'platform', String version = null) {
   63             if (version) {
   64                 publishedPlatforms << "${group}:${name}:${version}"
   65             } else {
   66                 publishedPlatforms << "${group}:${name}"
   67             }
   68             this
   69         }
   70 
   71 
   72         void applyTo(RemoteRepositorySpec spec) {
   73             def moduleName = name
   74             def alignedTo = alignsTo
   75             def otherVersions = seenVersions
   76             otherVersions.remove(alignedTo)
   77             def missedVersions = misses
   78             spec.group(group) {
   79                 module(moduleName) {
   80                     if (alignedTo) {
   81                         version(alignedTo) {
   82                             expectResolve()
   83                         }
   84                     }
   85                     otherVersions.each {
   86                         version(it) {
   87                             expectGetMetadata()
   88                         }
   89                     }
   90                     missedVersions.each {
   91                         version(it) {
   92                             expectGetMetadataMissing()
   93                         }
   94                     }
   95                 }
   96             }
   97         }
   98     }
   99 
  100     public final static Closure<Void> VIRTUAL_PLATFORM = {
  101         // If the platform is declared as virtual, we won't fetch metadata
  102     }
  103 
  104     void expectAlignment(@DelegatesTo(value = AlignmentSpec, strategy = Closure.DELEGATE_FIRST) Closure<?> spec) {
  105         def align = new AlignmentSpec()
  106         spec.delegate = align
  107         spec.resolveStrategy = Closure.DELEGATE_FIRST
  108         spec()
  109         repositoryInteractions {
  110             align.applyTo(repoSpec)
  111         }
  112     }
  113 
  114     static class AlignmentSpec {
  115         final List<ModuleAlignmentSpec> specs = []
  116         final Set<String> skipsPlatformMetadata = []
  117 
  118         AbstractAlignmentSpec.ModuleAlignmentSpec module(String name, @DelegatesTo(value=AbstractAlignmentSpec.ModuleAlignmentSpec, strategy = Closure.DELEGATE_FIRST) Closure<?> config = null) {
  119             def spec = new AbstractAlignmentSpec.ModuleAlignmentSpec(name: name)
  120             if (config) {
  121                 config.delegate = spec
  122                 config.resolveStrategy = Closure.DELEGATE_FIRST
  123                 config()
  124             }
  125             specs << spec
  126             spec
  127         }
  128 
  129         void doesNotGetPlatform(String group = 'org', String name = 'platform', String version = '1.0') {
  130             skipsPlatformMetadata << "$group:$name:$version"
  131         }
  132 
  133         void applyTo(RemoteRepositorySpec spec) {
  134             Set<String> virtualPlatforms = [] as Set
  135             Set<String> publishedPlatforms = [] as Set
  136             Set<String> resolvesToVirtual = [] as Set
  137 
  138             specs.each {
  139                 it.applyTo(spec)
  140                 if (it.virtualPlatforms) {
  141                     it.seenVersions.each { v ->
  142                         it.virtualPlatforms.each { vp ->
  143                             virtualPlatforms << "${vp}:$v"
  144                         }
  145                     }
  146                     it.virtualPlatforms.each { vp ->
  147                         resolvesToVirtual << "${vp}:$it.alignsTo"
  148                     }
  149                 }
  150                 if (it.publishedPlatforms) {
  151                     def exactPlatforms = it.publishedPlatforms.findAll { it.count(':') == 2 }
  152                     def inferredPlatforms = it.publishedPlatforms - exactPlatforms
  153                     // for published platforms, we know there's no artifacts, so it's actually easier
  154                     it.seenVersions.each { v ->
  155                         inferredPlatforms.each { pp ->
  156                             publishedPlatforms << "${pp}:$v"
  157                         }
  158                     }
  159                     inferredPlatforms.each { pp ->
  160                         publishedPlatforms << "${pp}:$it.alignsTo"
  161                     }
  162                     exactPlatforms.each { pp ->
  163                         publishedPlatforms << pp
  164                     }
  165                 }
  166             }
  167             virtualPlatforms.remove(resolvesToVirtual)
  168             virtualPlatforms.removeAll(skipsPlatformMetadata)
  169             resolvesToVirtual.removeAll(skipsPlatformMetadata)
  170             publishedPlatforms.removeAll(skipsPlatformMetadata)
  171             virtualPlatforms.each { p ->
  172                 spec."$p"(VIRTUAL_PLATFORM)
  173             }
  174             publishedPlatforms.each { p ->
  175                 spec."$p" {
  176                     expectGetMetadata()
  177                 }
  178             }
  179             resolvesToVirtual.each {
  180                 spec."$it"(VIRTUAL_PLATFORM)
  181             }
  182         }
  183 
  184     }
  185 
  186     protected void "a rule which infers module set from group and version"(boolean virtual = true) {
  187         buildFile << """
  188             dependencies {
  189                 components.all(InferModuleSetFromGroupAndVersion)
  190             }
  191             
  192             class InferModuleSetFromGroupAndVersion implements ComponentMetadataRule {
  193                 void execute(ComponentMetadataContext ctx) {
  194                     ctx.details.with {
  195                         belongsTo("\${id.group}:platform:\${id.version}", ${virtual})
  196                     }
  197                 }
  198             }
  199         """
  200     }
  201 
  202     protected void "align the 'org' group only"() {
  203         buildFile << """
  204             dependencies {
  205                 components.all(AlignOrgGroup)
  206             }
  207             
  208             class AlignOrgGroup implements ComponentMetadataRule {
  209                 void execute(ComponentMetadataContext ctx) {
  210                     ctx.details.with {
  211                         if ('org' == id.group) {
  212                            belongsTo("\${id.group}:platform:\${id.version}")
  213                         }
  214                     }
  215                 }
  216             }
  217         """
  218     }
  219 
  220     protected void "align the 'org' group to 2 different virtual platforms"() {
  221         buildFile << """
  222             dependencies {
  223                 components.all(AlignOrgGroupTo2Platforms)
  224             }
  225             
  226             class AlignOrgGroupTo2Platforms implements ComponentMetadataRule {
  227                 void execute(ComponentMetadataContext ctx) {
  228                     ctx.details.with {
  229                         if ('org' == id.group) {
  230                            belongsTo("\${id.group}:platform:\${id.version}")
  231                            belongsTo("\${id.group}:platform2:\${id.version}")
  232                         }
  233                     }
  234                 }
  235             }
  236         """
  237     }
  238 
  239     protected void 'a rule which declares that Groovy belongs to the Groovy and the Spring platforms'(boolean groovyVirtual=false, boolean springVirtual = false) {
  240         buildFile << """
  241             dependencies {
  242                 components.all(GroovyRule)
  243             }
  244             
  245             class GroovyRule implements ComponentMetadataRule {
  246                 void execute(ComponentMetadataContext ctx) {
  247                     ctx.details.with {
  248                         if ('org.apache.groovy' == id.group) {
  249                            belongsTo("org.apache.groovy:platform:\${id.version}", $groovyVirtual)
  250                            belongsTo("org.springframework:spring-platform:1.0", $springVirtual)
  251                         }
  252                     }
  253                 }
  254             }
  255         """
  256     }
  257 }