<commit_before>/*
 * This file is part of the gradle-release plugin.
 *
 * (c) <NAME>
 * (c) ResearchGate GmbH
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package net.researchgate.release

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

public class PluginHelperVersionPropertyFileTests extends Specification {

    Project project

    PluginHelper helper

    File testDir = new File("build/tmp/test/${getClass().simpleName}")

    def setup() {
        project = ProjectBuilder.builder().withName("ReleasePluginTest").withProjectDir(testDir).build()
        project.version = '1.1'
        project.apply plugin: ReleasePlugin
        project.release.scmAdapters = [NoSCMReleaseAdapter]

        helper = new PluginHelper(project: project, extension: project.extensions['release'] as ReleaseExtension)

        def props = project.file("gradle.properties")
        props.withWriter {it << "version=${project.version}"}
    }

    def cleanup() {
        if (testDir.exists()) testDir.deleteDir()
    }

    def 'should find gradle.properties by default'() {
        expect:
        helper.findPropertiesFile().name == 'gradle.properties'
    }

    def 'should find properties from convention'() {
        given:
        def props = project.file("custom.properties")
        props.withWriter {it << '@@@@'}
        project.release {
            versionPropertyFile = 'custom.properties'
        }
        expect:
        helper.findPropertiesFile().name == 'custom.properties'
    }

    def 'by default should update `version` property from props file'() {
        given:
        helper.updateVersionProperty("2.2")
        expect:
        project.file("gradle.properties").readLines()[1] == 'version=2.2'
    }

    def 'when configured then update `version` and additional properties from props file'() {
        given:
        def props = project.file("custom.properties")
        props.withWriter {
            it << "version=${project.version}\nversion1=${project.version}\nversion2=${project.version}\n"
        }
        project.release {
            versionPropertyFile = 'custom.properties'
            versionProperties = ['version1']
        }
        when:
        helper.updateVersionProperty("2.2")
        def lines = project.file("custom.properties").readLines()
        then:
        lines[1] == 'version=2.2'
        lines[2] == 'version1=2.2'
        lines[3] == 'version2=1.1'
    }

    def 'should update version of project and subprojects'() {
        given:
        def proj1 = ProjectBuilder.builder().withParent(project).withName("proj1").build()
        proj1.version = project.version
        def proj2 = ProjectBuilder.builder().withParent(project).withName("proj2").build()
        proj2.version = project.version
        when:
        helper.updateVersionProperty("2.2")
        then:
        assert project.version == '2.2'
        assert proj1.version == project.version
        assert proj2.version == project.version
    }

     def 'should not fail when version contains spaces'() {
         given:
         helper.updateVersionProperty(" 2.2 ")
         project.version = "2.2"
         when:
         helper.updateVersionProperty("2.3")
         then:
         assert project.version == '2.3'
     }

}<commit_msg>Add test for version with spaces.<commit_after>/*
 * This file is part of the gradle-release plugin.
 *
 * (c) <NAME>
 * (c) ResearchGate GmbH
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package net.researchgate.release

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

public class PluginHelperVersionPropertyFileTests extends Specification {

    Project project

    PluginHelper helper

    File testDir = new File("build/tmp/test/${getClass().simpleName}")

    def setup() {
        project = ProjectBuilder.builder().withName("ReleasePluginTest").withProjectDir(testDir).build()
        project.version = '1.1'
        project.apply plugin: ReleasePlugin
        project.release.scmAdapters = [NoSCMReleaseAdapter]

        helper = new PluginHelper(project: project, extension: project.extensions['release'] as ReleaseExtension)

        def props = project.file("gradle.properties")
        props.withWriter {it << "version=${project.version}"}
    }

    def cleanup() {
        if (testDir.exists()) testDir.deleteDir()
    }

    def 'should find gradle.properties by default'() {
        expect:
        helper.findPropertiesFile().name == 'gradle.properties'
    }

    def 'should find properties from convention'() {
        given:
        def props = project.file("custom.properties")
        props.withWriter {it << '@@@@'}
        project.release {
            versionPropertyFile = 'custom.properties'
        }
        expect:
        helper.findPropertiesFile().name == 'custom.properties'
    }

    def 'by default should update `version` property from props file'() {
        given:
        helper.updateVersionProperty("2.2")
        expect:
        project.file("gradle.properties").readLines()[1] == 'version=2.2'
    }

    def 'when configured then update `version` and additional properties from props file'() {
        given:
        def props = project.file("custom.properties")
        props.withWriter {
            it << "version=${project.version}\nversion1=${project.version}\nversion2=${project.version}\n"
        }
        project.release {
            versionPropertyFile = 'custom.properties'
            versionProperties = ['version1']
        }
        when:
        helper.updateVersionProperty("2.2")
        def lines = project.file("custom.properties").readLines()
        then:
        lines[1] == 'version=2.2'
        lines[2] == 'version1=2.2'
        lines[3] == 'version2=1.1'
    }

    def 'should update version of project and subprojects'() {
        given:
        def proj1 = ProjectBuilder.builder().withParent(project).withName("proj1").build()
        proj1.version = project.version
        def proj2 = ProjectBuilder.builder().withParent(project).withName("proj2").build()
        proj2.version = project.version
        when:
        helper.updateVersionProperty("2.2")
        then:
        assert project.version == '2.2'
        assert proj1.version == project.version
        assert proj2.version == project.version
    }

    def 'should not fail when version contains spaces'() {
        given:
        helper.updateVersionProperty(" 2.5 ")
        project.createScmAdapter.execute()
        when:
        project.initScmAdapter.execute()
        then:
        noExceptionThrown()
    }
}
{'fraction_non_alphanumeric': 0.11847507331378299, 'fraction_numerical': 0.011730205278592375, 'mean_word_length': 2.6128177966101696, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '7593859', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1809, 'n_words': 426}