<commit_before>package org.gosulang.gradle.functional

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.gradle.testkit.runner.UnexpectedBuildSuccess
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import java.util.stream.Collectors

class GosuRuntimeInferenceTest extends Specification {

    private final URL _pluginClasspathResource = getClass().getClassLoader().getResource("plugin-classpath.txt")
    private final URL _gosuVersionResource = getClass().getClassLoader().getResource("gosuVersion.txt")

    public final TemporaryFolder _testProjectDir = new TemporaryFolder()

    def setup() {
        _testProjectDir.create()
        
        def _buildFile = _testProjectDir.newFile("build.gradle");
        
        def gosuVersion = getGosuVersion(_gosuVersionResource)
        def buildFileContent = 
"""
buildscript {
    dependencies {
        classpath files(${getClasspathWithoutGosuJars()})
    }
}
apply plugin: 'org.gosu-lang.gosu'
repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots' //for Gosu snapshot builds
    }
}

dependencies {
//    compile group: 'org.gosu-lang.gosu', name: 'gosu-core-api', version: '$gosuVersion' //intentionally commenting-out to cause build failure
    testCompile group: 'junit', name: 'junit', version: '4.11'
}
"""
        writeFile(_buildFile, buildFileContent)
        
        println("--- Dumping build.gradle ---")
        println(buildFileContent);
        println("--- Done dumping build.gradle ---")
        
        //make a POGO to compile
        String simplePogoContent = 'public class SimplePogo {}'
        File pogo = new File(_testProjectDir.newFolder('src', 'main', 'gosu'), 'SimplePogo.gs')
        pogo.getParentFile().mkdirs();
        writeFile(pogo, simplePogoContent)
    }
    
    def "Build throws when gosu-core-api jar is not declared as a dependency"() {

        when:
        GradleRunner runner = GradleRunner.create()
                .withProjectDir(_testProjectDir.getRoot())
                .withArguments('clean', 'compileGosu', '-is')
        
        BuildResult result = runner.buildAndFail()

        println("--- Dumping stdout ---")
        println(result.getStandardOutput())
        println("--- Done dumping stdout ---")
        println()
        println("--- Dumping stderr ---")
        println(result.getStandardError())
        println("--- Done dumping stderr ---")
                
        then:
        notThrown(UnexpectedBuildSuccess)
        result.getStandardError().contains('Cannot infer Gosu classpath because the Gosu Core API Jar was not found.')

    }

    /**
     * This method might be overkill
     * @return the classpath containing the bytecode of the gosu plugin before it is packaged, sans any dependencies on gosu JARs
     */
    protected String getClasspathWithoutGosuJars() {
        URL url = _pluginClasspathResource
        List<String> pluginClasspathRaw = new BufferedReader(new FileReader(url.getFile())).lines().collect(Collectors.toList())
        List<String> elementsToRemove = pluginClasspathRaw.findAll { it.contains('/org.gosu-lang.gosu/gosu-core-api/') || 
                                                                     it.contains('/org.gosu-lang.gosu/gosu-core/') ||
                                                                     it.contains('/org.gosu-lang.gosu/gosu-ant-compiler/') ||
                                                                     it.contains('/org.gosu-lang.gosu.managed/')}
        return "'" + String.join("', '", pluginClasspathRaw.minus(elementsToRemove)) + "'" //wrap each entry in single quotes
    }

    protected static String getGosuVersion(URL url) throws IOException {
        return new BufferedReader(new FileReader(url.getFile())).lines().findFirst().get()
    }

    protected static void writeFile(File destination, String content) throws IOException {
        BufferedWriter output = null
        try {
            output = new BufferedWriter(new FileWriter(destination))
            output.write(content)
        } finally {
            if (output != null) {
                output.close()
            }
        }
    }

}<commit_msg>Refactor to base on AbstractGosuPluginSpecification<commit_after>package org.gosulang.gradle.functional

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.gradle.testkit.runner.UnexpectedBuildSuccess
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

import java.util.stream.Collectors

class GosuRuntimeInferenceTest extends AbstractGosuPluginSpecification {

    File simplePogo
    
    def 'Build throws when gosu-core-api jar is not declared as a dependency'() {
        given:
        buildScript << 
            """
            plugins {
                id 'org.gosu-lang.gosu'
            }
            repositories {
                mavenLocal()
                mavenCentral()
                maven {
                    url 'https://oss.sonatype.org/content/repositories/snapshots' //for Gosu snapshot builds
                }
            }
            dependencies {
                //compile group: 'org.gosu-lang.gosu', name: 'gosu-core-api', version: '$gosuVersion' //intentionally commenting-out to cause build failure
                testCompile group: 'junit', name: 'junit', version: '4.11'
            }
            """
        
        simplePogo = new File(testProjectDir.newFolder('src', 'main', 'gosu'), 'SimplePogo.gs')
        simplePogo << """
            public class SimplePogo {
            }"""
        
        when:
        GradleRunner runner = GradleRunner.create()
                .withProjectDir(testProjectDir.root)
                .withPluginClasspath(pluginClasspath)
                .withArguments('clean', 'compileGosu', '-is')
        
        BuildResult result = runner.buildAndFail()

        println('--- Dumping stdout ---')
        println(result.getStandardOutput())
        println('--- Done dumping stdout ---')
        println()
        println('--- Dumping stderr ---')
        println(result.getStandardError())
        println('--- Done dumping stderr ---')
                
        then:
        notThrown(UnexpectedBuildSuccess)
        result.getStandardError().contains('Cannot infer Gosu classpath because the Gosu Core API Jar was not found.')
        !new File(testProjectDir.root, asPath('build', 'classes', 'main', 'SimplePogo.class')).exists()
    }


}
{'fraction_non_alphanumeric': 0.11008215085884988, 'fraction_numerical': 0.0008961911874533233, 'mean_word_length': 2.283962726826876, 'pattern_counts': {'":': 0, '<': 9, '<?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': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834663', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1748, 'n_words': 390}