diff options
author | Rene Groeschke <rene@gradle.com> | 2015-11-22 00:50:45 +0000 |
---|---|---|
committer | Rene Groeschke <rene@gradle.com> | 2015-11-22 00:50:45 +0000 |
commit | fdc7b27d69b22e0ffa151d56c8f3812dcd229555 (patch) | |
tree | f67fb55cdf48e05596f6d71e2b7f11e6853f5f28 /src | |
parent | a676a61ddf3478356b43b4b7f947e6f769cde50e (diff) | |
download | frege-gradle-plugin-fdc7b27d69b22e0ffa151d56c8f3812dcd229555.tar.gz frege-gradle-plugin-fdc7b27d69b22e0ffa151d56c8f3812dcd229555.tar.bz2 frege-gradle-plugin-fdc7b27d69b22e0ffa151d56c8f3812dcd229555.zip |
some more work on coverage and some cleanup
- get packages for integ tests right
- introduce common AbstractFregeIntegrationSpec
- first stab of unit test coverage for FregeCompile
- minor cleanup on FregeCompile
- configure fregePath as part as part of base plugin convention
Diffstat (limited to 'src')
-rw-r--r-- | src/integTest/groovy/frege/gradle/integtest/fixtures/AbstractFregeIntegrationSpec.groovy | 68 | ||||
-rw-r--r-- | src/integTest/groovy/frege/gradle/plugins/FregePluginIntegTest.groovy | 128 | ||||
-rw-r--r-- | src/integTest/groovy/frege/gradle/tasks/FregeCompileIntegTest.groovy | 74 | ||||
-rw-r--r-- | src/integTest/groovy/frege/plugin/FregePluginIntegTest.groovy | 161 | ||||
-rw-r--r-- | src/main/groovy/frege/gradle/plugins/FregeBasePlugin.java | 9 | ||||
-rw-r--r-- | src/main/groovy/frege/gradle/tasks/FregeCompile.groovy | 20 | ||||
-rw-r--r-- | src/test/groovy/frege/gradle/plugins/FregeBasePluginTest.groovy (renamed from src/test/groovy/frege/gradle/FregeBasePluginTest.groovy) | 3 | ||||
-rw-r--r-- | src/test/groovy/frege/gradle/plugins/FregePluginTest.groovy (renamed from src/test/groovy/frege/gradle/FregePluginTest.groovy) | 4 | ||||
-rw-r--r-- | src/test/groovy/frege/gradle/tasks/FregeCompileTest.groovy | 39 |
9 files changed, 333 insertions, 173 deletions
diff --git a/src/integTest/groovy/frege/gradle/integtest/fixtures/AbstractFregeIntegrationSpec.groovy b/src/integTest/groovy/frege/gradle/integtest/fixtures/AbstractFregeIntegrationSpec.groovy new file mode 100644 index 0000000..6bcd339 --- /dev/null +++ b/src/integTest/groovy/frege/gradle/integtest/fixtures/AbstractFregeIntegrationSpec.groovy @@ -0,0 +1,68 @@ +package frege.gradle.integtest.fixtures + +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.BuildResult +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification + +class AbstractFregeIntegrationSpec extends Specification { + public static final String DEFAULT_FREGE_VERSION = "3.23.370-g898bc8c" + List<File> pluginClasspath + + @Rule + final TemporaryFolder testProjectDir = new TemporaryFolder() + File buildFile + + def setup() { + buildFile = testProjectDir.newFile('build.gradle') + + + testProjectDir.newFolder("src", "main", "java", "org", "frege") + testProjectDir.newFolder("src", "main", "frege", "org", "frege") + + def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") + if (pluginClasspathResource == null) { + // try again via file reference + pluginClasspathResource = new File("build/createClasspathManifest/plugin-classpath.txt") + if (pluginClasspathResource == null) { + throw new IllegalStateException("Did not find plugin classpath resource, run `integTestClasses` build task.") + } + } + pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) } + } + + + BuildResult run(String task) { + run(null, task); + } + + BuildResult run(String gradleVersion, String task) { + def writer = new StringWriter(); + GradleRunner runner = newRunner(task, writer, gradleVersion) + def result = runner.build() + println writer; + return result; + } + + BuildResult fail(String task) { + def writer = new StringWriter(); + GradleRunner runner = newRunner(task, writer, null) + def result = runner.buildAndFail() + println writer; + return result; + } + + private GradleRunner newRunner(String task, StringWriter writer, String gradleVersion) { + def runner = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withArguments(task) + .withPluginClasspath(pluginClasspath) + .forwardStdOutput(writer) + if (gradleVersion) { + runner.withGradleVersion(gradleVersion) + } + runner + } + +} diff --git a/src/integTest/groovy/frege/gradle/plugins/FregePluginIntegTest.groovy b/src/integTest/groovy/frege/gradle/plugins/FregePluginIntegTest.groovy new file mode 100644 index 0000000..e837a2a --- /dev/null +++ b/src/integTest/groovy/frege/gradle/plugins/FregePluginIntegTest.groovy @@ -0,0 +1,128 @@ +package frege.gradle.plugins + +import frege.gradle.integtest.fixtures.AbstractFregeIntegrationSpec +import org.gradle.testkit.runner.BuildResult +import spock.lang.Unroll +import static org.gradle.testkit.runner.TaskOutcome.* + +class FregePluginIntegTest extends AbstractFregeIntegrationSpec { + + def setup() { + buildFile << """ + plugins { + id 'org.frege-lang' + } + + repositories { + jcenter() + } + """ + } + + def "can handle non existing source directories"() { + given: + buildFile << """ + dependencies { + compile "org.frege-lang:frege:$DEFAULT_FREGE_VERSION" + } + """ + + when: + def result = run("classes") + then: + result.task(":compileFrege").outcome == UP_TO_DATE + } + + @Unroll + def "can compile and run frege code (gradle: #gradleVersion, frege: #fregeVersion)"() { + given: + buildFile << """ + dependencies { + compile "org.frege-lang:frege:$fregeVersion" + } + ${sayHelloTask()} + """ + + def fregeSourceFile = testProjectDir.newFile("src/main/frege/org/frege/HelloFrege.fr") + + fregeSourceFile << """ + module org.frege.HelloFrege where + + greeting = "Hello Frege!" + + main _ = do + println greeting + """ + + when: + def result = run(gradleVersion, "sayHello") + + then: + result.output.contains("Hello Frege!") + result.task(":sayHello").outcome == SUCCESS + + where: + fregeVersion | gradleVersion + DEFAULT_FREGE_VERSION | "2.9" + DEFAULT_FREGE_VERSION | "2.8" + "3.22.367-g2737683" | "2.9" + "3.22.367-g2737683" | "2.8" + } + + def "can reference java from frege"() { + given: + buildFile << """ + dependencies { + compile "org.frege-lang:frege:$DEFAULT_FREGE_VERSION" + } + ${sayHelloTask()} + """ + + and: + javaCode() + fregeCallingJava() + when: + BuildResult result = run("sayHello") + then: + result.task(":compileJava").outcome == SUCCESS + result.task(":compileFrege").outcome == SUCCESS + result.output.contains("hello from java") + } + + def fregeCallingJava() { + + File fregeSourceFile = testProjectDir.newFile("src/main/frege/org/frege/HelloFrege.fr") + fregeSourceFile << """ + module org.frege.HelloFrege where + + data StaticHello = pure native org.frege.StaticHello where + pure native helloJava org.frege.StaticHello.helloJava:: () -> String + + + main _ = do + println(StaticHello.helloJava()) + + """ + } + + def javaCode(String sourceRoot = "java") { + def javaSourceFile = testProjectDir.newFile("src/main/$sourceRoot/org/frege/StaticHello.java") + + javaSourceFile << """ + package org.frege; + + public class StaticHello { + public static String helloJava() { + return "hello from java"; + } + } + """ + } + + def sayHelloTask() { + return """ task sayHello(type: JavaExec) { + classpath = sourceSets.main.runtimeClasspath + main = 'org.frege.HelloFrege' + } """ + } +}
\ No newline at end of file diff --git a/src/integTest/groovy/frege/gradle/tasks/FregeCompileIntegTest.groovy b/src/integTest/groovy/frege/gradle/tasks/FregeCompileIntegTest.groovy new file mode 100644 index 0000000..58e21ba --- /dev/null +++ b/src/integTest/groovy/frege/gradle/tasks/FregeCompileIntegTest.groovy @@ -0,0 +1,74 @@ +package frege.gradle.tasks +import frege.gradle.integtest.fixtures.AbstractFregeIntegrationSpec + +import static org.gradle.testkit.runner.TaskOutcome.FAILED + +class FregeCompileIntegTest extends AbstractFregeIntegrationSpec { + + List<File> pluginClasspath + + def setup() { + buildFile << """ + plugins { + id 'org.frege-lang.base' + } + + import frege.gradle.tasks.FregeCompile + + repositories { jcenter() } + + configurations { frege {} } + + dependencies { + frege "org.frege-lang:frege:$DEFAULT_FREGE_VERSION" + } + + task compile(type: FregeCompile) { + destinationDir = file("frege-output") + source("frege-src") + module = "frege-src" + classpath = configurations.frege + fregePath = configurations.frege + } + """ + + testProjectDir.newFolder("frege-src") + } + + def "shows compile errors"() { + given: + simpleFrege() + failingFrege() + when: + def result = fail("compile") + + then: + result.task(":compile").outcome == FAILED + result.output.contains("Failing.fr:6: can't resolve `Hello`") + } + + def failingFrege() { + def failingFrege = testProjectDir.newFile("frege-src/Failing.fr") + failingFrege << """ + + module Failing where + + failingFun _ = do + println(Hello) + """ + } + + def simpleFrege() { + + def helloFrege = testProjectDir.newFile("frege-src/Hello.fr") + helloFrege << """ + + module Hello where + + import frege.prelude.PreludeBase + + main _ = do + println("Hello From Frege") + """ + } +} diff --git a/src/integTest/groovy/frege/plugin/FregePluginIntegTest.groovy b/src/integTest/groovy/frege/plugin/FregePluginIntegTest.groovy deleted file mode 100644 index 40afe4a..0000000 --- a/src/integTest/groovy/frege/plugin/FregePluginIntegTest.groovy +++ /dev/null @@ -1,161 +0,0 @@ -package frege.plugin -import org.gradle.testkit.runner.GradleRunner -import org.junit.Rule -import org.junit.rules.TemporaryFolder -import spock.lang.Specification -import spock.lang.Unroll - -import static org.gradle.testkit.runner.TaskOutcome.SUCCESS - -class FregePluginIntegTest extends Specification { - - public static final String DEFAULT_FREGE_VERSION = "3.23.370-g898bc8c" - @Rule - final TemporaryFolder testProjectDir = new TemporaryFolder() - File buildFile - - List<File> pluginClasspath - - def setup() { - buildFile = testProjectDir.newFile('build.gradle') - - buildFile << """ - plugins { - id 'org.frege-lang' - } - - repositories { - jcenter() - } - """ - def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") - if (pluginClasspathResource == null) { - // try again via file reference - pluginClasspathResource = new File("build/createClasspathManifest/plugin-classpath.txt") - if (pluginClasspathResource == null) { - throw new IllegalStateException("Did not find plugin classpath resource, run `integTestClasses` build task.") - } - } - pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) } - } - - def "can handle non existing source directories"() { - given: - buildFile << """ - dependencies { - compile "org.frege-lang:frege:$DEFAULT_FREGE_VERSION" - } - """ - - when: - def result = GradleRunner.create() - .withProjectDir(testProjectDir.root) - .withArguments('classes') - .withPluginClasspath(pluginClasspath) - .build() - then: - result.task(":compileFrege") != null - } - - @Unroll - def "can compile and run frege code (gradle: #gradleVersion, frege: #fregeVersion)"() { - given: - buildFile << """ - dependencies { - compile "org.frege-lang:frege:$fregeVersion" - } - ${sayHelloTask()} - """ - - testProjectDir.newFolder("src", "main", "frege", "org", "frege") - def fregeSourceFile = testProjectDir.newFile("src/main/frege/org/frege/HelloFrege.fr") - - fregeSourceFile << """ -module org.frege.HelloFrege where - -greeting = "Hello Frege!" - -main _ = do - println greeting -""" - - when: - def result = GradleRunner.create() - .withGradleVersion(gradleVersion) - .withProjectDir(testProjectDir.root) - .withArguments('sayHello') - .withPluginClasspath(pluginClasspath) - .build() - - then: - result.output.contains("Hello Frege!") - result.task(":sayHello").outcome == SUCCESS - - where: - fregeVersion | gradleVersion - DEFAULT_FREGE_VERSION | "2.9" - DEFAULT_FREGE_VERSION | "2.8" - "3.22.367-g2737683" | "2.9" - "3.22.367-g2737683" | "2.8" - } - - - def "can reference java from frege"() { - given: - buildFile << """ - dependencies { - compile "org.frege-lang:frege:$DEFAULT_FREGE_VERSION" - } - - ${sayHelloTask()} - """ - - and: - testProjectDir.newFolder("src", "main", "frege", "org", "frege") - - def fregeSourceFile = testProjectDir.newFile("src/main/frege/org/frege/HelloFrege.fr") - - fregeSourceFile << """ -module org.frege.HelloFrege where - -data StaticHello = pure native org.frege.StaticHello where - pure native helloJava org.frege.StaticHello.helloJava :: () -> String - -main _ = do - println(StaticHello.helloJava()) - -""" - testProjectDir.newFolder("src", "main", "java", "org", "frege") - def javaSourceFile = testProjectDir.newFile("src/main/java/org/frege/StaticHello.java") - - javaSourceFile << """ -package org.frege; - -public class StaticHello { - public static String helloJava() { - return "hello from java"; - } -} -""" - - when: - def result = GradleRunner.create() - .withProjectDir(testProjectDir.root) - .withArguments('sayHello') - .withPluginClasspath(pluginClasspath) - .build() - then: - result.task(":compileJava").outcome == SUCCESS - result.task(":compileFrege").outcome == SUCCESS - - result.output.contains("hello from java") - } - - - def sayHelloTask() { - return """task sayHello(type: JavaExec){ - classpath = sourceSets.main.runtimeClasspath - main = 'org.frege.HelloFrege' - }""" - } -}
\ No newline at end of file diff --git a/src/main/groovy/frege/gradle/plugins/FregeBasePlugin.java b/src/main/groovy/frege/gradle/plugins/FregeBasePlugin.java index 9f8ebd9..7ced11b 100644 --- a/src/main/groovy/frege/gradle/plugins/FregeBasePlugin.java +++ b/src/main/groovy/frege/gradle/plugins/FregeBasePlugin.java @@ -15,6 +15,7 @@ import org.gradle.api.specs.Spec; import org.gradle.api.tasks.SourceSet; import javax.inject.Inject; +import java.util.concurrent.Callable; public class FregeBasePlugin implements Plugin<Project> { private FileResolver fileResolver; @@ -42,7 +43,7 @@ public class FregeBasePlugin implements Plugin<Project> { private void configureSourceSetDefaults(final JavaBasePlugin javaBasePlugin) { project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() { - public void execute(SourceSet sourceSet) { + public void execute(final SourceSet sourceSet) { final DefaultFregeSourceSet fregeSourceSet = new DefaultFregeSourceSet(((DefaultSourceSet) sourceSet).getDisplayName(), fileResolver); new DslObject(sourceSet).getConvention().getPlugins().put("frege", fregeSourceSet); @@ -60,6 +61,12 @@ public class FregeBasePlugin implements Plugin<Project> { FregeCompile compile = project.getTasks().create(compileTaskName, FregeCompile.class); compile.setModule(project.file(defaultSourcePath).getAbsolutePath()); javaBasePlugin.configureForSourceSet(sourceSet, compile); + compile.getConventionMapping().map("classpath", new Callable() { + public Object call() throws Exception { + return sourceSet.getCompileClasspath(); + } + }); + compile.dependsOn(sourceSet.getCompileJavaTaskName()); compile.setDescription(String.format("Compiles the %s Frege source.", sourceSet.getName())); compile.setSource(fregeSourceSet.getFrege()); diff --git a/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy b/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy index 3da2b34..8c63df6 100644 --- a/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy +++ b/src/main/groovy/frege/gradle/tasks/FregeCompile.groovy @@ -1,8 +1,10 @@ package frege.gradle.tasks + import groovy.transform.TypeChecked import org.gradle.api.Action import org.gradle.api.file.FileCollection import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.compile.AbstractCompile @@ -54,8 +56,8 @@ class FregeCompile extends AbstractCompile { @Input String module = "" - @Input - List<File> fregePaths = [] + @Optional @InputFiles + FileCollection fregePath @Input String mainClass = "frege.compiler.Main" @@ -84,14 +86,20 @@ class FregeCompile extends AbstractCompile { logger.info("Calling Frege compiler with compilerArgs: '$compilerArgs'") //TODO integrate with gradle compiler daemon infrastructure and skip internal execution + + def errOutputStream = new ByteArrayOutputStream(); + def outOutputStream = new ByteArrayOutputStream(); project.javaexec(new Action<JavaExecSpec>() { @Override void execute(JavaExecSpec javaExecSpec) { javaExecSpec.args = compilerArgs javaExecSpec.classpath = FregeCompile.this.classpath javaExecSpec.main = mainClass + javaExecSpec.errorOutput = System.err; + javaExecSpec.standardOutput = System.out; } }); + } public FregeCompile source(Object... sources) { @@ -129,10 +137,10 @@ class FregeCompile extends AbstractCompile { if (verbose) args << "-v" - def fp = fregePaths - if (!fp.isEmpty()) { + + if (fregePath != null && !fregePath.isEmpty()) { args << "-fp" - args << fp.collect { f -> f.absolutePath }.join(File.pathSeparator) + args << fregePath.files.collect { f -> f.absolutePath }.join(File.pathSeparator) } if (sourcePaths != null && !sourcePaths.isEmpty()) { @@ -151,7 +159,7 @@ class FregeCompile extends AbstractCompile { } args << "-d" - args << getDestinationDir() + args << getDestinationDir().absolutePath if (!module.isEmpty()) { logger.info "compiling module '$module'" diff --git a/src/test/groovy/frege/gradle/FregeBasePluginTest.groovy b/src/test/groovy/frege/gradle/plugins/FregeBasePluginTest.groovy index 3ca1bb2..04b5587 100644 --- a/src/test/groovy/frege/gradle/FregeBasePluginTest.groovy +++ b/src/test/groovy/frege/gradle/plugins/FregeBasePluginTest.groovy @@ -1,6 +1,5 @@ -package frege.gradle +package frege.gradle.plugins -import frege.gradle.plugins.FregeBasePlugin import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification diff --git a/src/test/groovy/frege/gradle/FregePluginTest.groovy b/src/test/groovy/frege/gradle/plugins/FregePluginTest.groovy index 2fc6d86..1867c88 100644 --- a/src/test/groovy/frege/gradle/FregePluginTest.groovy +++ b/src/test/groovy/frege/gradle/plugins/FregePluginTest.groovy @@ -1,7 +1,5 @@ -package frege.gradle +package frege.gradle.plugins -import frege.gradle.plugins.FregeBasePlugin -import frege.gradle.plugins.FregePlugin import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification diff --git a/src/test/groovy/frege/gradle/tasks/FregeCompileTest.groovy b/src/test/groovy/frege/gradle/tasks/FregeCompileTest.groovy new file mode 100644 index 0000000..c9224ea --- /dev/null +++ b/src/test/groovy/frege/gradle/tasks/FregeCompileTest.groovy @@ -0,0 +1,39 @@ +package frege.gradle.tasks + +import org.gradle.api.Project +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification + +class FregeCompileTest extends Specification { + Project project = ProjectBuilder.builder().build() + FregeCompile compile + + def setup() { + when: + compile = project.tasks.create("fregeCompile", FregeCompile) + } + + + def "configured sourcePaths tracked"() { + when: + compile.source("someFolder") + then: + compile.sourcePaths == [project.file("someFolder")] + } + + + def "default assembleArguments"() { + given: + compile.destinationDir = project.file("testoutput") + expect: + compile.assembleArguments() == ["-inline", "-make", "-d", project.file("testoutput").absolutePath] + } + + def "with prefix"() { + given: + compile.destinationDir = project.file("testoutput") + compile.prefix = "somePrefix" + expect: + compile.assembleArguments() == ["-inline", "-make", "-prefix", "somePrefix", "-d", project.file("testoutput").absolutePath] + } +} |