<commit_before>/*
 * Copyright 2019 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.attributes

import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AbstractDependencyResolutionTest
import org.gradle.integtests.fixtures.resolve.ResolveTestFixture

class CrossProjectMultipleVariantSelectionIntegrationTest extends AbstractDependencyResolutionTest {

    ResolveTestFixture resolve

    def setup() {
        buildFile << """
            allprojects {
                apply plugin: 'java-library'
            }
        """
        settingsFile << """
            rootProject.name = 'test'
        """
        resolve = new ResolveTestFixture(buildFile, "compileClasspath")
        resolve.prepare()
    }

    def "can select both main variant and test fixtures with project dependencies"() {
        given:
        settingsFile << "include 'lib'"

        file("lib/build.gradle") << """
            configurations {
                testFixtures {
                    canBeResolved = false
                    canBeConsumed = true
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                        attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'jar'))
                        attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EXTERNAL))
                        attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, Integer.parseInt(JavaVersion.current().majorVersion))
                    }
                    outgoing.capability('org:lib-fixtures:1.0')
                }
            }

            artifacts {
                testFixtures file("lib-test-fixtures.jar")
            }
        """

        buildFile << """
            dependencies {
                implementation project(':lib')
                implementation (project(':lib')) {
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                    }
                    capabilities {
                        requireCapability('org:lib-fixtures')
                    }
                }
            }
        """

        when:
        succeeds ':checkDeps'

        then:
        resolve.expectGraph {
            root(":", ":test:") {
                project(":lib", "test:lib:") {
                    variant "apiElements", ['org.gradle.usage':'java-api', 'org.gradle.libraryelements': 'jar', 'org.gradle.category':'library', 'org.gradle.dependency.bundling':'external', 'org.gradle.jvm.version': JavaVersion.current().majorVersion]
                    artifact group:'', module:'', version: '', type: '', name: 'main', noType: true
                }
                project(":lib", "test:lib:") {
                    variant "testFixtures", ['org.gradle.usage':'java-api', 'org.gradle.libraryelements': 'jar', 'org.gradle.dependency.bundling':'external', 'org.gradle.jvm.version': JavaVersion.current().majorVersion]
                    artifact group:'test', module:'lib', version:'unspecified', classifier: 'test-fixtures'
                }
            }
        }
    }

    def "prefers the variant which strictly matches the requested capabilities"() {
        given:
        settingsFile << "include 'lib'"

        file("lib/build.gradle") << """
            configurations {                
                testFixtures {
                    canBeResolved = false
                    canBeConsumed = true
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                        attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'jar'))
                        attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category, Category.LIBRARY))
                        attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EXTERNAL))
                        attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, Integer.parseInt(JavaVersion.current().majorVersion))
                    }
                    outgoing.capability('test:lib:1.0')
                    outgoing.capability('test:lib-fixtures:1.0')
                }
            }

            artifacts {
                testFixtures file("lib-test-fixtures.jar")
            }
        """

        buildFile << """<commit_msg>test: Add disambiguating attributes to make tests pass.<commit_after>/*
 * Copyright 2019 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.attributes

import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AbstractDependencyResolutionTest
import org.gradle.integtests.fixtures.resolve.ResolveTestFixture

class CrossProjectMultipleVariantSelectionIntegrationTest extends AbstractDependencyResolutionTest {

    ResolveTestFixture resolve

    def setup() {
        buildFile << """
            allprojects {
                apply plugin: 'java-library'
            }
        """
        settingsFile << """
            rootProject.name = 'test'
        """
        resolve = new ResolveTestFixture(buildFile, "compileClasspath")
        resolve.prepare()
    }

    def "can select both main variant and test fixtures with project dependencies"() {
        given:
        settingsFile << "include 'lib'"

        file("lib/build.gradle") << """
            configurations {
                testFixtures {
                    canBeResolved = false
                    canBeConsumed = true
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                        attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'jar'))
                        attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EXTERNAL))
                        attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, Integer.parseInt(JavaVersion.current().majorVersion))
                    }
                    outgoing.capability('org:lib-fixtures:1.0')
                }
            }

            artifacts {
                testFixtures file("lib-test-fixtures.jar")
            }
        """

        buildFile << """
            dependencies {
                implementation project(':lib')
                implementation (project(':lib')) {
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                    }
                    capabilities {
                        requireCapability('org:lib-fixtures')
                    }
                }
            }
        """

        when:
        succeeds ':checkDeps'

        then:
        resolve.expectGraph {
            root(":", ":test:") {
                project(":lib", "test:lib:") {
                    variant "apiElements", ['org.gradle.usage':'java-api', 'org.gradle.libraryelements': 'jar', 'org.gradle.category':'library', 'org.gradle.dependency.bundling':'external', 'org.gradle.jvm.version': JavaVersion.current().majorVersion]
                    artifact group:'', module:'', version: '', type: '', name: 'main', noType: true
                }
                project(":lib", "test:lib:") {
                    variant "testFixtures", ['org.gradle.usage':'java-api', 'org.gradle.libraryelements': 'jar', 'org.gradle.dependency.bundling':'external', 'org.gradle.jvm.version': JavaVersion.current().majorVersion]
                    artifact group:'test', module:'lib', version:'unspecified', classifier: 'test-fixtures'
                }
            }
        }
    }

    def "prefers the variant which strictly matches the requested capabilities"() {
        given:
        settingsFile << "include 'lib'"

        file("lib/build.gradle") << """
            configurations {
                testFixtures {
                    canBeResolved = false
                    canBeConsumed = true
                    attributes {
                        attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, 'java-api'))
                        attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'jar'))
                        attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category, Category.LIBRARY))
                        attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EXTERNAL))
                        attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, Integer.parseInt(JavaVersion.current().majorVersion))

                        // Need to disambiguate testFixtures from apiElements
                        attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, 'test'))
                    }
                    outgoing.capability('test:lib:1.0')
                    outgoing.capability('test:lib-fixtures:1.0')
                }
            }

            artifacts {
                testFixtures file("lib-test-fixtures.jar")
            }
        """

        buildFile << """
{'fraction_non_alphanumeric': 0.10430926443651788, 'fraction_numerical': 0.002699315530704714, 'mean_word_length': 1.9173228346456692, 'pattern_counts': {'":': 8, '<': 35, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1572582', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2483, 'n_words': 518}