<commit_before>/*
 * Copyright 2014 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.plugins.ide.internal.tooling

import org.gradle.api.DefaultTask
import org.gradle.api.internal.project.DefaultProjectTaskLister
import org.gradle.tooling.model.gradle.BuildInvocations
import org.gradle.util.TestUtil
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class BuildInvocationsBuilderTest extends Specification {
    @Shared
    def project = TestUtil.builder().withName("root").build()
    @Shared
    def child1 = TestUtil.builder().withName("child1").withParent(project).build()
    @Shared
    def child2 = TestUtil.builder().withName("child2").withParent(project).build()
    @Shared
    def grandChildOfChild1 = TestUtil.builder().withName("grandChild").withParent(child1).build()

    def setupSpec() {
        // create a project/task tree:
        //   root (t1, t2)
        //   +--- child 1 (t2, t3)
        //        +-- grand child (t3, t4)
        //   +-- child 2 (t2)

        // root tasks (one public, one private)
        def task1OfRoot = project.tasks.create('t1', DefaultTask)
        task1OfRoot.group = 'build'
        task1OfRoot.description = 'T1 from root'

        def task2OfRoot = project.tasks.create('t2', DefaultTask)
        task2OfRoot.group = null
        task2OfRoot.description = null

        // child tasks (one public, one private)
        def task1OfChild1 = child1.tasks.create('t2', DefaultTask)
        task1OfChild1.group = 'build'
        task1OfChild1.description = 'T2 from child 1'

        def task2OfChild1 = child1.tasks.create('t3', DefaultTask)
        task2OfChild1.group = null
        task2OfChild1.description = 'T3 from child'

        def task1OfChild2 = child2.tasks.create('t2', DefaultTask)
        task1OfChild2.group = 'build'
        task1OfChild2.description = 'T2 from child 2'

        // grand child tasks (one public, one private)
        def task1OfGrandChild = grandChildOfChild1.tasks.create('t3', DefaultTask)
        task1OfGrandChild.group = 'build'
        task1OfGrandChild.description = null

        def task2OfGrandChild = grandChildOfChild1.tasks.create('t4', DefaultTask)
        task2OfGrandChild.group = ''
        task2OfGrandChild.description = 'T4 from grand child'
    }

    def "canBuild"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def canBuild = builder.canBuild(BuildInvocations.name)

        then:
        canBuild
    }

    @Unroll("tasks and selectors for #startProject")
    def "tasksAndSelectorsAndTheirVisibility"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", startProject)

        then:
        model.taskSelectors*.projectPath as Set == [startProject.path] as Set
        model.taskSelectors*.name as Set == selectorNames as Set
        model.tasks*.name as Set == taskNames as Set

        model.taskSelectors.findAll { it.public }*.name as Set == visibleSelectors as Set
        model.tasks.findAll { it.public }*.name as Set == visibleTasks as Set

        where:
        startProject       | selectorNames            | taskNames     | visibleSelectors   | visibleTasks
        project            | ['t1', 't2', 't3', 't4'] | ['t1', 't2']  | ['t1', 't2', 't3'] | ['t1']
        child1             | ['t2', 't3', 't4']       | ['t2', 't3',] | ['t2', 't3']       | ['t2']
        child2             | ['t2']                   | ['t2']        | ['t2']             | ['t2']
        grandChildOfChild1 | ['t3', 't4']             | ['t3', 't4',] | ['t3']             | ['t3']
    }

    def "implicitProjectFlagWins"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", child1, true)

        then:
        model.taskSelectors*.name as Set == ['t1', 't2', 't3', 't4'] as Set
        model.taskSelectors.each { it ->
            assert it.projectPath == ':'
        }
    }

    def "nonNullDescriptionFromSmallestProjectPathAlwaysWins"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", project)

        then:
        assert model.taskSelectors.find { it.name == 't1' }.description == 'T1 from root'
        assert model.taskSelectors.find { it.name == 't2' }.description == ''
        assert model.taskSelectors.find { it.name == 't3' }.description == 'T3 from child'
        assert model.taskSelectors.find { it.name == 't4' }.description == 'T4 from grand child'
    }<commit_msg>Add more coverage for task description precedence<commit_after>/*
 * Copyright 2014 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.plugins.ide.internal.tooling

import org.gradle.api.DefaultTask
import org.gradle.api.internal.project.DefaultProjectTaskLister
import org.gradle.tooling.model.gradle.BuildInvocations
import org.gradle.util.TestUtil
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

class BuildInvocationsBuilderTest extends Specification {
    @Shared
    def project = TestUtil.builder().withName("root").build()
    @Shared
    def child = TestUtil.builder().withName("child").withParent(project).build()
    @Shared
    def grandChild1OfChild = TestUtil.builder().withName("grandChild1").withParent(child).build()
    @Shared
    def grandChild2OfChild = TestUtil.builder().withName("grandChild2").withParent(child).build()

    def setupSpec() {
        // create a project/task tree:
        //   root (t1, t2)
        //   +--- child 1 (t2, t3)
        //        +-- grand child (t3, t4)
        //        +-- grand child (t4)

        // root tasks (one public, one private)
        def task1OfRoot = project.tasks.create('t1', DefaultTask)
        task1OfRoot.group = 'build'
        task1OfRoot.description = 'T1 from root'

        def task2OfRoot = project.tasks.create('t2', DefaultTask)
        task2OfRoot.group = null
        task2OfRoot.description = null

        // child tasks (one public, one private)
        def task1OfChild1 = child.tasks.create('t2', DefaultTask)
        task1OfChild1.group = 'build'
        task1OfChild1.description = 'T2 from child'

        def task2OfChild1 = child.tasks.create('t3', DefaultTask)
        task2OfChild1.group = null
        task2OfChild1.description = 'T3 from child'

        // grand child tasks (one public, one private)
        def task1OfGrandChild = grandChild1OfChild.tasks.create('t3', DefaultTask)
        task1OfGrandChild.group = 'build'
        task1OfGrandChild.description = null

        def task2OfGrandChild = grandChild1OfChild.tasks.create('t4', DefaultTask)
        task2OfGrandChild.group = ''
        task2OfGrandChild.description = 'T4 from grand child 1'

        def taskOfGrandChild2 = grandChild2OfChild.tasks.create('t4', DefaultTask)
        taskOfGrandChild2.group = ''
        taskOfGrandChild2.description = 'T4 from grand child 2'
    }

    def "canBuild"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def canBuild = builder.canBuild(BuildInvocations.name)

        then:
        canBuild
    }

    @Unroll("tasks and selectors for #startProject")
    def "tasksAndSelectorsAndTheirVisibility"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", startProject)

        then:
        model.taskSelectors*.projectPath as Set == [startProject.path] as Set
        model.taskSelectors*.name as Set == selectorNames as Set
        model.tasks*.name as Set == taskNames as Set

        model.taskSelectors.findAll { it.public }*.name as Set == visibleSelectors as Set
        model.tasks.findAll { it.public }*.name as Set == visibleTasks as Set

        where:
        startProject       | selectorNames            | taskNames     | visibleSelectors   | visibleTasks
        project            | ['t1', 't2', 't3', 't4'] | ['t1', 't2']  | ['t1', 't2', 't3'] | ['t1']
        child              | ['t2', 't3', 't4']       | ['t2', 't3',] | ['t2', 't3']       | ['t2']
        grandChild1OfChild | ['t3', 't4']             | ['t3', 't4',] | ['t3']             | ['t3']
        grandChild2OfChild | ['t4']                   | ['t4',]       | []                 | []
    }

    def "implicitProjectFlagWins"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", child, true)

        then:
        model.taskSelectors*.name as Set == ['t1', 't2', 't3', 't4'] as Set
        model.taskSelectors.each { it ->
            assert it.projectPath == ':'
        }
    }

    def "nonNullDescriptionFromSmallestProjectPathAlwaysWins"() {
        given:
        def builder = new BuildInvocationsBuilder(new DefaultProjectTaskLister())

        when:
        def model = builder.buildAll("org.gradle.tooling.model.gradle.BuildInvocations", project)

        then:
        assert model.taskSelectors.find { it.name == 't1' }.description == 'T1 from root'
        assert model.taskSelectors.find { it.name == 't2' }.description == ''
        assert model.taskSelectors.find { it.name == 't3' }.description == 'T3 from child'
        assert model.taskSelectors.find { it.name == 't4' }.description == 'T4 from grand child 1'
    }
{'fraction_non_alphanumeric': 0.11658741770299927, 'fraction_numerical': 0.020025603511338697, 'mean_word_length': 2.6191264063534083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2410893', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3157, 'n_words': 817}