<commit_before>package com.github.blindpirate.gogradle

import com.github.blindpirate.gogradle.crossplatform.Arch
import com.github.blindpirate.gogradle.crossplatform.Os
import com.github.blindpirate.gogradle.support.AccessWeb
import com.github.blindpirate.gogradle.support.IntegrationTestSupport
import com.github.blindpirate.gogradle.support.OnlyWhen
import com.github.blindpirate.gogradle.util.IOUtils
import com.github.blindpirate.gogradle.util.ProcessUtils
import org.apache.commons.codec.digest.DigestUtils
import org.junit.Test
import org.junit.runner.RunWith

import java.nio.file.Path

@RunWith(GogradleRunner)
@OnlyWhen("System.getenv('GOGS_DIR')!=null&&'git version'.execute()")
class GogsBuild extends IntegrationTestSupport {
    File resource = new File(System.getenv('GOGS_DIR'))

    ProcessUtils processUtils = new ProcessUtils()

    String buildDotGradle = """
buildscript {
    dependencies {
        classpath files("${System.getProperty('GOGRADLE_ROOT')}/build/libs/gogradle-${GogradleGlobal.GOGRADLE_VERSION}-all.jar")
    }
}
apply plugin: 'com.github.blindpirate.gogradle'

golang {
    packagePath="github.com/gogits/gogs"
    goVersion='1.8'
}

vet {
    continueWhenFail = true
}

"""

    @Test
    @AccessWeb
    void 'gogs should be built successfully'() {
        // v0.9.113
        new ProcessUtils().run(['git', 'checkout', '114c179e5a50e3313f7a5894100693805e64e440'], null, resource)

        // I don't know why it will fail on Windows
        if (Os.getHostOs() == Os.WINDOWS) {
            writeBuildAndSettingsDotGradle(buildDotGradle + 'test.enabled = false')
        } else {
            writeBuildAndSettingsDotGradle(buildDotGradle)
        }

        firstBuild()

//        File firstBuildResult = getOutputExecutable()
//        long lastModified = firstBuildResult.lastModified()
//        String md5 = DigestUtils.md5Hex(new FileInputStream(firstBuildResult))
//        assert processUtils.getStdout(processUtils.run(firstBuildResult.absolutePath)).contains('Gogs')
        assert new File(resource, 'gogradle.lock').exists()

        secondBuild()

//        File secondBuildResult = getOutputExecutable()
//        assert secondBuildResult.lastModified() > lastModified
//        assert DigestUtils.md5Hex(new FileInputStream(secondBuildResult)) == md5
    }

    void firstBuild() {
        try {
            newBuild {
                it.forTasks('build', 'check', 'lock', 'dependencies')<commit_msg>Fix bug: wait until git checkout completes<commit_after>package com.github.blindpirate.gogradle

import com.github.blindpirate.gogradle.crossplatform.Arch
import com.github.blindpirate.gogradle.crossplatform.Os
import com.github.blindpirate.gogradle.support.AccessWeb
import com.github.blindpirate.gogradle.support.IntegrationTestSupport
import com.github.blindpirate.gogradle.support.OnlyWhen
import com.github.blindpirate.gogradle.util.IOUtils
import com.github.blindpirate.gogradle.util.ProcessUtils
import org.junit.Test
import org.junit.runner.RunWith

import java.nio.file.Path

@RunWith(GogradleRunner)
@OnlyWhen("System.getenv('GOGS_DIR')!=null&&'git version'.execute()")
class GogsBuild extends IntegrationTestSupport {
    File resource = new File(System.getenv('GOGS_DIR'))

    ProcessUtils processUtils = new ProcessUtils()

    String buildDotGradle = """
buildscript {
    dependencies {
        classpath files("${System.getProperty('GOGRADLE_ROOT')}/build/libs/gogradle-${GogradleGlobal.GOGRADLE_VERSION}-all.jar")
    }
}
apply plugin: 'com.github.blindpirate.gogradle'

golang {
    packagePath="github.com/gogits/gogs"
    goVersion='1.8'
}

vet {
    continueWhenFail = true
}

"""

    @Test
    @AccessWeb
    void 'gogs should be built successfully'() {
        // v0.9.113
        assert processUtils.run(['git', 'checkout', '114c179e5a50e3313f7a5894100693805e64e440'], null, resource).waitFor() == 0

        // I don't know why it will fail on Windows
        if (Os.getHostOs() == Os.WINDOWS) {
            writeBuildAndSettingsDotGradle(buildDotGradle + 'test.enabled = false')
        } else {
            writeBuildAndSettingsDotGradle(buildDotGradle)
        }

        firstBuild()

        File firstBuildResult = getOutputExecutable()
//        long lastModified = firstBuildResult.lastModified()
//        String md5 = DigestUtils.md5Hex(new FileInputStream(firstBuildResult))
        assert processUtils.getStdout(processUtils.run(firstBuildResult.absolutePath)).contains('Gogs')
        assert new File(resource, 'gogradle.lock').exists()

        secondBuild()

        File secondBuildResult = getOutputExecutable()
        assert processUtils.getStdout(processUtils.run(secondBuildResult.absolutePath)).contains('Gogs')
//        assert secondBuildResult.lastModified() > lastModified
//        assert DigestUtils.md5Hex(new FileInputStream(secondBuildResult)) == md5
    }

    void firstBuild() {
        try {
            newBuild {
                it.forTasks('build', 'check', 'lock', 'dependencies')
{'fraction_non_alphanumeric': 0.11302260452090418, 'fraction_numerical': 0.01740348069613923, 'mean_word_length': 3.730368968779565, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4156616', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1531, 'n_words': 250}