<commit_before>/*
 * 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.fixtures

import org.gradle.api.JavaVersion
import org.gradle.testing.internal.util.RetryRule
import org.gradle.util.GradleVersion
import org.gradle.util.TestPrecondition
import spock.lang.Specification

class RetryRuleUtil {

    static private final String[] FILES_TO_PRESERVE = ['reproducible-archives-init.gradle']

    static RetryRule retryCrossVersionTestOnIssueWithReleasedGradleVersion(Specification specification) {
        RetryRule.retryIf(specification) { t ->
            def daemonsFixture = specification.hasProperty("daemonsFixture") ? specification.daemonsFixture : null
            Throwable failure = t

            return shouldRetry(specification, failure, daemonsFixture)
        }
    }

    private static boolean shouldRetry(Specification specification, Throwable failure, daemonsFixture) {
        String releasedGradleVersion = specification.hasProperty("releasedGradleVersion") ? specification.releasedGradleVersion : null
        def caughtGradleConnectionException = specification.hasProperty("caughtGradleConnectionException") ? specification.caughtGradleConnectionException : null

        println "Cross version test failure with target version " + releasedGradleVersion
        println "Failure: " + failure
        println "Cause  : " + failure?.cause

        if (caughtGradleConnectionException != null) {
            failure = caughtGradleConnectionException
            println "Failure (caught during test): " + failure
            println "Cause   (caught during test): " + failure?.cause
        }

        println "Daemons (potentially used): ${daemonsFixture?.daemons?.collect { "$it.context.pid (Port $it.port)"}} - ${daemonsFixture.daemonBaseDir}"

        if (releasedGradleVersion == null) {
            println "Can not retry cross version test because 'gradleVersion' is unknown"
            return false
        }

        def targetDistVersion = GradleVersion.version(releasedGradleVersion)

        // known issue with pre 1.3 daemon versions: https://github.com/gradle/gradle/commit/29d895bc086bc2bfcf1c96a6efad22c602441e26
        if (targetDistVersion < GradleVersion.version("1.3") &&<commit_msg>Do not try to access the daemon port property as it might fail<commit_after>/*
 * 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.fixtures

import org.gradle.api.JavaVersion
import org.gradle.testing.internal.util.RetryRule
import org.gradle.util.GradleVersion
import org.gradle.util.TestPrecondition
import spock.lang.Specification

class RetryRuleUtil {

    static private final String[] FILES_TO_PRESERVE = ['reproducible-archives-init.gradle']

    static RetryRule retryCrossVersionTestOnIssueWithReleasedGradleVersion(Specification specification) {
        RetryRule.retryIf(specification) { t ->
            def daemonsFixture = specification.hasProperty("daemonsFixture") ? specification.daemonsFixture : null
            Throwable failure = t

            return shouldRetry(specification, failure, daemonsFixture)
        }
    }

    private static boolean shouldRetry(Specification specification, Throwable failure, daemonsFixture) {
        String releasedGradleVersion = specification.hasProperty("releasedGradleVersion") ? specification.releasedGradleVersion : null
        def caughtGradleConnectionException = specification.hasProperty("caughtGradleConnectionException") ? specification.caughtGradleConnectionException : null

        println "Cross version test failure with target version " + releasedGradleVersion
        println "Failure: " + failure
        println "Cause  : " + failure?.cause

        if (caughtGradleConnectionException != null) {
            failure = caughtGradleConnectionException
            println "Failure (caught during test): " + failure
            println "Cause   (caught during test): " + failure?.cause
        }

        println "Daemons (potentially used): ${daemonsFixture?.daemons?.collect { it.context.pid }} - ${daemonsFixture.daemonBaseDir}"

        if (releasedGradleVersion == null) {
            println "Can not retry cross version test because 'gradleVersion' is unknown"
            return false
        }

        def targetDistVersion = GradleVersion.version(releasedGradleVersion)

        // known issue with pre 1.3 daemon versions: https://github.com/gradle/gradle/commit/29d895bc086bc2bfcf1c96a6efad22c602441e26
        if (targetDistVersion < GradleVersion.version("1.3") &&
{'fraction_non_alphanumeric': 0.07681440932368003, 'fraction_numerical': 0.012360939431396786, 'mean_word_length': 3.6617283950617283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3878579', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1431, 'n_words': 436}