<commit_before>/*
 * Copyright 2015 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.testkit.runner

import org.gradle.testkit.runner.daemon.GradleDaemon
import org.gradle.testkit.runner.daemon.GradleDaemonAnalyzer
import org.gradle.testkit.runner.internal.DefaultGradleRunner
import org.gradle.util.GFileUtils
import org.junit.Rule
import org.junit.rules.TemporaryFolder

import static org.gradle.testkit.runner.TaskResult.*

class GradleRunnerIsolatedDaemonIntegrationTest extends AbstractGradleRunnerIntegrationTest {
    @Rule TemporaryFolder testUserHomeDir = new TemporaryFolder()

    def "configuration in default Gradle user home directory is ignored for test execution with daemon"() {
        given:
        File defaultGradleUserHomeDir = new File(testUserHomeDir.root, '.gradle')

        and:
        String gradlePropertiesContent = 'myProp1=propertiesFile'
        writeGradlePropertiesFile(defaultGradleUserHomeDir, gradlePropertiesContent)

        and:
        String initScriptContent = "allprojects { ext.myProp2 = 'initScript' }"
        writeInitScriptFile(defaultGradleUserHomeDir, initScriptContent)

        and:
        buildFile << """
            task verifyProjectProperties {
                doLast {
                    assert !project.ext.has('myProp1')
                    assert !project.ext.has('myProp2')
                }
            }
        """

        when:
        DefaultGradleRunner gradleRunner = (DefaultGradleRunner)runner('verifyProjectProperties')
        gradleRunner.withJvmArguments("-Duser.home=$testUserHomeDir.root.canonicalPath")
        BuildResult result = gradleRunner.build()

        then:
        noExceptionThrown()
        result.tasks.collect { it.path } == [':verifyProjectProperties']
        result.taskPaths(SUCCESS) == [':verifyProjectProperties']
        result.taskPaths(SKIPPED).empty
        result.taskPaths(UP_TO_DATE).empty
        result.taskPaths(FAILED).empty
    }

    def "configuration in custom Gradle user home directory is used for test execution with daemon"() {
        setup:
        String gradlePropertiesContent = 'myProp1=propertiesFile'
        File gradlePropertiesFile = writeGradlePropertiesFile(buildContext.gradleUserHomeDir, gradlePropertiesContent)

        and:
        String initScriptContent = "allprojects { ext.myProp2 = 'initScript' }"
        File initScriptFile = writeInitScriptFile(buildContext.gradleUserHomeDir, initScriptContent)

        and:
        buildFile << """
            task verifyProjectProperties {
                doLast {
                    assert project.ext.has('myProp1')
                    assert project.ext.has('myProp2')
                }
            }
        """

        when:
        DefaultGradleRunner gradleRunner = runner('verifyProjectProperties')
        BuildResult result = gradleRunner.build()

        then:
        noExceptionThrown()
        result.tasks.collect { it.path } == [':verifyProjectProperties']
        result.taskPaths(SUCCESS) == [':verifyProjectProperties']
        result.taskPaths(SKIPPED).empty
        result.taskPaths(UP_TO_DATE).empty
        result.taskPaths(FAILED).empty

        cleanup:
        GFileUtils.forceDelete(gradlePropertiesFile)
        GFileUtils.forceDelete(initScriptFile)
    }

    def "daemon process is reused for test execution if one already exists"() {
        given:
        File customGradleUserHomeDir = new File(testUserHomeDir.root, UUID.randomUUID().toString())
        GradleDaemonAnalyzer gradleDaemonAnalyzer = new GradleDaemonAnalyzer(new File(customGradleUserHomeDir, 'daemon'), buildContext.version.version)
        gradleDaemonAnalyzer.daemons.empty
        buildFile << helloWorldTask()

        when:
        DefaultGradleRunner gradleRunner = runner('helloWorld')
        gradleRunner.withGradleUserHomeDir(customGradleUserHomeDir)
        gradleRunner.build()

        then:
        noExceptionThrown()
        List<GradleDaemon> initialDaemons = gradleDaemonAnalyzer.daemons
        initialDaemons.size() == 1
        String daemonPidInUse = initialDaemons[0].pid
        daemonPidInUse

        when:
        gradleRunner.build()

        then:
        noExceptionThrown()
        List<GradleDaemon> laterDaemons = gradleDaemonAnalyzer.daemons
        laterDaemons.size() == 1
        daemonPidInUse == laterDaemons[0].pid

        cleanup:
        GFileUtils.forceDelete(customGradleUserHomeDir)
    }

    private File writeGradlePropertiesFile(File gradleUserHomeDir, String content) {
        File gradlePropertiesFile = new File(gradleUserHomeDir, 'gradle.properties')
        GFileUtils.writeFile(content, gradlePropertiesFile)
        gradlePropertiesFile
    }

    private File writeInitScriptFile(File gradleUserHomeDir, String content) {
        File initScriptFile = new File(gradleUserHomeDir, 'init.gradle')
        GFileUtils.writeFile(content, initScriptFile)
        initScriptFile
    }
}<commit_msg>JUnit rule will already take care of deleting temp directory.<commit_after>/*
 * Copyright 2015 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.testkit.runner

import org.gradle.testkit.runner.daemon.GradleDaemon
import org.gradle.testkit.runner.daemon.GradleDaemonAnalyzer
import org.gradle.testkit.runner.internal.DefaultGradleRunner
import org.gradle.util.GFileUtils
import org.junit.Rule
import org.junit.rules.TemporaryFolder

import static org.gradle.testkit.runner.TaskResult.*

class GradleRunnerIsolatedDaemonIntegrationTest extends AbstractGradleRunnerIntegrationTest {
    @Rule TemporaryFolder testUserHomeDir = new TemporaryFolder()

    def "configuration in default Gradle user home directory is ignored for test execution with daemon"() {
        given:
        File defaultGradleUserHomeDir = new File(testUserHomeDir.root, '.gradle')

        and:
        String gradlePropertiesContent = 'myProp1=propertiesFile'
        writeGradlePropertiesFile(defaultGradleUserHomeDir, gradlePropertiesContent)

        and:
        String initScriptContent = "allprojects { ext.myProp2 = 'initScript' }"
        writeInitScriptFile(defaultGradleUserHomeDir, initScriptContent)

        and:
        buildFile << """
            task verifyProjectProperties {
                doLast {
                    assert !project.ext.has('myProp1')
                    assert !project.ext.has('myProp2')
                }
            }
        """

        when:
        DefaultGradleRunner gradleRunner = (DefaultGradleRunner)runner('verifyProjectProperties')
        gradleRunner.withJvmArguments("-Duser.home=$testUserHomeDir.root.canonicalPath")
        BuildResult result = gradleRunner.build()

        then:
        noExceptionThrown()
        result.tasks.collect { it.path } == [':verifyProjectProperties']
        result.taskPaths(SUCCESS) == [':verifyProjectProperties']
        result.taskPaths(SKIPPED).empty
        result.taskPaths(UP_TO_DATE).empty
        result.taskPaths(FAILED).empty
    }

    def "configuration in custom Gradle user home directory is used for test execution with daemon"() {
        setup:
        String gradlePropertiesContent = 'myProp1=propertiesFile'
        File gradlePropertiesFile = writeGradlePropertiesFile(buildContext.gradleUserHomeDir, gradlePropertiesContent)

        and:
        String initScriptContent = "allprojects { ext.myProp2 = 'initScript' }"
        File initScriptFile = writeInitScriptFile(buildContext.gradleUserHomeDir, initScriptContent)

        and:
        buildFile << """
            task verifyProjectProperties {
                doLast {
                    assert project.ext.has('myProp1')
                    assert project.ext.has('myProp2')
                }
            }
        """

        when:
        DefaultGradleRunner gradleRunner = runner('verifyProjectProperties')
        BuildResult result = gradleRunner.build()

        then:
        noExceptionThrown()
        result.tasks.collect { it.path } == [':verifyProjectProperties']
        result.taskPaths(SUCCESS) == [':verifyProjectProperties']
        result.taskPaths(SKIPPED).empty
        result.taskPaths(UP_TO_DATE).empty
        result.taskPaths(FAILED).empty

        cleanup:
        GFileUtils.forceDelete(gradlePropertiesFile)
        GFileUtils.forceDelete(initScriptFile)
    }

    def "daemon process is reused for test execution if one already exists"() {
        given:
        File customGradleUserHomeDir = new File(testUserHomeDir.root, UUID.randomUUID().toString())
        GradleDaemonAnalyzer gradleDaemonAnalyzer = new GradleDaemonAnalyzer(new File(customGradleUserHomeDir, 'daemon'), buildContext.version.version)
        gradleDaemonAnalyzer.daemons.empty
        buildFile << helloWorldTask()

        when:
        DefaultGradleRunner gradleRunner = runner('helloWorld')
        gradleRunner.withGradleUserHomeDir(customGradleUserHomeDir)
        gradleRunner.build()

        then:
        noExceptionThrown()
        List<GradleDaemon> initialDaemons = gradleDaemonAnalyzer.daemons
        initialDaemons.size() == 1
        String daemonPidInUse = initialDaemons[0].pid
        daemonPidInUse

        when:
        gradleRunner.build()

        then:
        noExceptionThrown()
        List<GradleDaemon> laterDaemons = gradleDaemonAnalyzer.daemons
        laterDaemons.size() == 1
        daemonPidInUse == laterDaemons[0].pid
    }

    private File writeGradlePropertiesFile(File gradleUserHomeDir, String content) {
        File gradlePropertiesFile = new File(gradleUserHomeDir, 'gradle.properties')
        GFileUtils.writeFile(content, gradlePropertiesFile)
        gradlePropertiesFile
    }

    private File writeInitScriptFile(File gradleUserHomeDir, String content) {
        File initScriptFile = new File(gradleUserHomeDir, 'init.gradle')
        GFileUtils.writeFile(content, initScriptFile)
        initScriptFile
    }
}
{'fraction_non_alphanumeric': 0.07907188353048225, 'fraction_numerical': 0.003639672429481347, 'mean_word_length': 2.995274445656125, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, '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': '3639886', 'n_tokens_mistral': 2977, 'n_tokens_neox': 2863, 'n_words': 651}