Skip to content

Instantly share code, notes, and snippets.

@melix
Created December 11, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/e96fdc635a3e6fccdec4aa5b70609e8a to your computer and use it in GitHub Desktop.
Save melix/e96fdc635a3e6fccdec4aa5b70609e8a to your computer and use it in GitHub Desktop.
/*
* Copyright 2017 the original author or authors.
*
* Licensed 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.gradle.integtests.resolve
import org.gradle.integtests.fixtures.GradleMetadataResolveRunner
import org.gradle.integtests.fixtures.RequiredFeature
import org.gradle.integtests.fixtures.RequiredFeatures
import spock.lang.Unroll
@RequiredFeatures(
@RequiredFeature(feature = GradleMetadataResolveRunner.EXPERIMENTAL_RESOLVE_BEHAVIOR, value = "true")
)
class VariantAttributesRulesIntegrationTest extends AbstractModuleDependencyResolveTest {
@Override
String getTestConfiguration() { variantToTest }
/**
* Does the published metadata provide variants with attributes? Eventually all metadata should do that.
* For Ivy and Maven POM metadata, the variants and attributes should be derived from configurations and scopes.
*/
boolean getPublishedModulesHaveAttributes() { gradleMetadataEnabled }
String getVariantToTest() {
if (gradleMetadataEnabled || useIvy()) {
'customVariant'
} else {
'compile'
}
}
def setup() {
repository {
'org.test:moduleA:1.0'() {
variant 'customVariant', [format: 'custom']
dependsOn('org.test:moduleB:1.0')
}
}
buildFile << """
def testAttribute = Attribute.of("TEST_ATTRIBUTE", String)
def formatAttribute = Attribute.of('format', String)
configurations { $variantToTest { attributes { attribute(formatAttribute, 'custom') } } }
dependencies {
$variantToTest group: 'org.test', name: 'moduleA', version: '1.0' ${publishedModulesHaveAttributes ? "" : ", configuration: '$variantToTest'"}
}
"""
}
@Unroll
def "can add attributes"() {
given:
buildFile << """
dependencies {
components {
withModule('org.test:moduleB') {
withVariant("$variantToTest") {
withAttributes {
attribute(formatAttribute, "custom")
}
}
}
}
}
"""
repository {
'org.test:moduleB:1.0' {
variant 'customVariant', [:]
}
}
when:
repositoryInteractions {
'org.test:moduleA:1.0' {
expectGetMetadata()
expectGetArtifact()
}
'org.test:moduleB:1.0'() {
expectGetMetadata()
expectGetArtifact()
}
}
then:
succeeds 'checkDep'
def variantToTest = variantToTest
resolve.expectGraph {
root(':', ':test:') {
edge('org.test:moduleB:', 'org.test:moduleB:1.0')
module("org.test:moduleA:1.0:$variantToTest") {
module("org.test:moduleB:1.0:$variantToTest")
}
}
}
}
@Unroll
def "can override attributes"() {
given:
buildFile << """
dependencies {
components {
withModule('org.test:moduleB') {
withVariant("$variantToTest") {
withAttributes {
attribute(formatAttribute, "custom")
}
}
}
}
}
"""
repository {
'org.test:moduleB:1.0' {
variant 'customVariant', [format: 'will be overriden']
}
}
when:
repositoryInteractions {
'org.test:moduleA:1.0' {
expectGetMetadata()
expectGetArtifact()
}
'org.test:moduleB:1.0'() {
expectGetMetadata()
expectGetArtifact()
}
}
then:
succeeds 'checkDep'
def variantToTest = variantToTest
resolve.expectGraph {
root(':', ':test:') {
edge('org.test:moduleB:', 'org.test:moduleB:1.0')
module("org.test:moduleA:1.0:$variantToTest") {
module("org.test:moduleB:1.0:$variantToTest")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment