<commit_before>package nu.studer.teamcity.buildscan.agent

import org.gradle.testkit.runner.BuildResult
import org.gradle.util.GradleVersion
import spock.lang.Ignore

import static org.junit.Assume.assumeTrue

class AutoApplicationTest extends BaseInitScriptTest {

    private static final String GE_VERSION = '3.10.1'
    private static final String CCUD_VERSION = '1.7'

    def "sends build scan url service message when GE plugin is applied by init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, GE_VERSION, null)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE plugin is applied by project and init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, GE_VERSION, null)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE and CCUD plugins are applied by init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, GE_VERSION, CCUD_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE and CCUD plugins are applied by project and init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, GE_VERSION, CCUD_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    @Ignore(value = "This behavior is desired but not yet implemented")
    def "degrades gracefully project applies GE and init script applies CCUD <= 1.6.5 (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, GE_VERSION, '1.6.5')
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "build succeeds when URL is set without applied GE plugin (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, null, null)

        then:
        run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "build doesn't apply CCUD is applied without GE plugin (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def jvmArgs = generateJvmArgs(mockScansServer.address, null, CCUD_VERSION)

        then:
        if (jdkCompatibleGradleVersion.gradleVersion < GradleVersion.version('5.2')) {
            run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)
        } else {
            def result = runAndFail(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)
            result.output.contains("Could not create plugin of type 'CommonCustomUserDataGradlePlugin'.")
        }

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "url system property overrides project server url (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def jvmArgs = generateJvmArgs(URI.create('https://ge-server.invalid'), null, null)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan to scans.gradle.com if no URL is given (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def jvmArgs = generateJvmArgs(null, GE_VERSION, null)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, jvmArgs)

        then:
        outputContainsTermsOfServiceDenial(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    List<String> generateJvmArgs(URI geUrl, String gePluginVersion, String ccudPluginVersion) {
        def jvmArgs = []

        if (geUrl) {
            jvmArgs << "-DteamCityBuildScanPlugin.gradle-enterprise.url=$geUrl".toString()
        }

        if (gePluginVersion) {
            jvmArgs << "-DteamCityBuildScanPlugin.gradle-enterprise.plugin.version=$gePluginVersion".toString()
        }

        if (ccudPluginVersion) {
            jvmArgs << "-DteamCityBuildScanPlugin.ccud.plugin.version=$ccudPluginVersion".toString()
        }

        jvmArgs
    }

    void outputContainsTermsOfServiceDenial(BuildResult result) {
        assert 1 == result.output.count('The Gradle Terms of Service have not been agreed to.')
    }

}<commit_msg>Introduce TcPluginConfig help class in tests<commit_after>package nu.studer.teamcity.buildscan.agent

import org.gradle.testkit.runner.BuildResult
import spock.lang.Ignore

import static org.junit.Assume.assumeTrue

class AutoApplicationTest extends BaseInitScriptTest {

    private static final String GE_VERSION = '3.10.1'
    private static final String CCUD_VERSION = '1.7'

    def "sends build scan url service message when GE plugin is applied by init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address, gePluginVersion: GE_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE plugin is applied by project and init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address, gePluginVersion: GE_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE and CCUD plugins are applied by init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address, gePluginVersion: GE_VERSION, ccudPluginVersion: CCUD_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan url service message when GE and CCUD plugins are applied by project and init script (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address, gePluginVersion: GE_VERSION, ccudPluginVersion: CCUD_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    @Ignore(value = "This behavior is desired but not yet implemented")
    def "degrades gracefully project applies GE and init script applies CCUD <= 1.6.5 (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address, gePluginVersion: GE_VERSION, ccudPluginVersion: '1.6.5')
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "build succeeds when URL is set without applied GE plugin (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: mockScansServer.address)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        result

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "url system property overrides project server url (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        given:
        declareGePluginApplication(jdkCompatibleGradleVersion.gradleVersion)

        when:
        def gePluginConfig = new TcPluginConfig(geUrl: URI.create('https://ge-server.invalid'))
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTeamCityServiceMessageBuildScanUrl(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    def "sends build scan to scans.gradle.com if no URL is given (#jdkCompatibleGradleVersion)"() {
        assumeTrue jdkCompatibleGradleVersion.isJvmVersionCompatible()

        when:
        def gePluginConfig = new TcPluginConfig(gePluginVersion: GE_VERSION)
        def result = run(jdkCompatibleGradleVersion.gradleVersion, gePluginConfig.toJvmArgs())

        then:
        outputContainsTermsOfServiceDenial(result)

        where:
        jdkCompatibleGradleVersion << SUPPORTED_GRADLE_VERSIONS
    }

    void outputContainsTermsOfServiceDenial(BuildResult result) {
        def tosWarning = 'The Gradle Terms of Service have not been agreed to.'
        assert result.output.contains(tosWarning)
        assert 1 == result.output.count(tosWarning)
    }

    static final class TcPluginConfig {

        URI geUrl
        String gePluginVersion
        String ccudPluginVersion

        List<String> toJvmArgs() {
            def jvmArgs = []
            if (geUrl) {
                jvmArgs << "-DteamCityBuildScanPlugin.gradle-enterprise.url=$geUrl".toString()
            }
            if (gePluginVersion) {
                jvmArgs << "-DteamCityBuildScanPlugin.gradle-enterprise.plugin.version=$gePluginVersion".toString()
            }
            if (ccudPluginVersion) {
                jvmArgs << "-DteamCityBuildScanPlugin.ccud.plugin.version=$ccudPluginVersion".toString()
            }
            jvmArgs
        }

    }

}
{'fraction_non_alphanumeric': 0.0670946527832146, 'fraction_numerical': 0.002192124011586941, 'mean_word_length': 3.412435233160622, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '5835348', 'n_tokens_mistral': 3699, 'n_tokens_neox': 3514, 'n_words': 736}