Skip to content

Commit 556a8d4

Browse files
committed
adding more test cases and fixing classloader issue during groovy compile which also fixed spock not being executed
1 parent 154f51e commit 556a8d4

File tree

9 files changed

+100
-89
lines changed

9 files changed

+100
-89
lines changed

libs/groovylib/package.mill

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package build.libs.groovylib
22

3-
// imports
43
import mill.*
54
import mill.contrib.buildinfo.BuildInfo
65
import mill.scalalib.*
@@ -20,10 +19,7 @@ object `package` extends MillPublishScalaModule with BuildInfo {
2019

2120
trait MillGroovyModule extends MillPublishScalaModule {
2221
override def javacOptions = super.javacOptions() ++ {
23-
val release =
24-
if (scala.util.Properties.isJavaAtLeast(11)) Seq("-release", "8")
25-
else Seq("-source", "1.8", "-target", "1.8")
26-
release ++ Seq("-encoding", "UTF-8", "-deprecation")
22+
Seq("-release", "8", "-encoding", "UTF-8", "-deprecation")
2723
}
2824
}
2925

libs/groovylib/src/mill/groovylib/GroovyModule.scala

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
2626
*/
2727
def groovyLanguageVersion: T[String] = Task { groovyVersion().split("[.]").take(2).mkString(".") }
2828

29-
override def bomMvnDeps: T[Seq[Dep]] = super.bomMvnDeps() ++ Seq(
30-
mvn"org.apache.groovy:groovy-bom:${groovyVersion()}"
31-
)
29+
override def bomMvnDeps: T[Seq[Dep]] = super.bomMvnDeps() ++
30+
Seq(mvn"org.apache.groovy:groovy-bom:${groovyVersion()}")
3231

3332
/**
3433
* All individual source files fed into the compiler.
@@ -58,12 +57,15 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
5857
* Defaults to add the groovy dependency matching the [[groovyVersion]].
5958
*/
6059
override def mandatoryMvnDeps: T[Seq[Dep]] = Task {
61-
super.mandatoryMvnDeps() ++ Seq(
62-
mvn"org.apache.groovy:groovy:${groovyVersion()}"
63-
)
60+
super.mandatoryMvnDeps()
61+
++
62+
groovyCompilerMvnDeps()
63+
// Seq(
64+
// mvn"org.apache.groovy:groovy:${groovyVersion()}"
65+
// )
6466
}
6567

66-
private def jvmWorkerRef: ModuleRef[JvmWorkerModule] = jvmWorker
68+
def jvmWorkerRef: ModuleRef[JvmWorkerModule] = jvmWorker
6769

6870
override def checkGradleModules: T[Boolean] = true
6971

@@ -89,7 +91,8 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
8991
def groovyCompilerMvnDeps: T[Seq[Dep]] = Task {
9092
val gv = groovyVersion()
9193

92-
val compilerDep = mvn"org.apache.groovy:groovy:$gv"
94+
val compilerDep = mvn"org.apache.groovy:groovy-all:$gv"
95+
// val annotationDep = mvn"org.codehaus.groovy:groovy-all-annotations:$gv"
9396

9497
Seq(compilerDep)
9598
}
@@ -104,9 +107,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
104107
*/
105108
def groovyCompilerPluginJars: T[Seq[PathRef]] = Task {
106109
val jars = defaultResolver().classpath(
107-
groovycPluginMvnDeps()
108-
// Don't resolve transitive jars
109-
.map(d => d.exclude("*" -> "*")),
110+
allMvnDeps(),
110111
resolutionParamsMapOpt = None
111112
)
112113
jars.toSeq
@@ -122,6 +123,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
122123
/**
123124
* The actual Groovy compile task (used by [[compile]] and [[groovycHelp]]).
124125
*/
126+
// TODO joint compilation: generate groovy-stubs -> compile java -> compile groovy -> delete stubs (or keep for debugging)
125127
protected def groovyCompileTask(): Task[CompilationResult] =
126128
Task.Anon {
127129
val ctx = Task.ctx()
@@ -152,7 +154,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
152154
javaHome = javaHome().map(_.path),
153155
javacOptions = javacOptions(),
154156
compileProblemReporter = ctx.reporter(hashCode),
155-
reportOldProblems = internalReportOldProblems()
157+
reportOldProblems = zincReportCachedProblems()
156158
)
157159
}
158160

@@ -189,18 +191,6 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
189191
}
190192
}
191193

192-
/**
193-
* Additional Groovy compiler options to be used by [[compile]].
194-
*/
195-
def groovycOptions: T[Seq[String]] = Task { Seq.empty[String] }
196-
197-
/**
198-
* Aggregation of all the options passed to the Groovy compiler.
199-
* In most cases, instead of overriding this target you want to override `groovycOptions` instead.
200-
*/
201-
def allGroovycOptions: T[Seq[String]] = Task {
202-
groovycOptions()
203-
}
204194

205195
private[groovylib] def internalCompileJavaFiles(
206196
worker: JvmWorkerApi,
@@ -228,8 +218,6 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
228218
)
229219
}
230220

231-
private[groovylib] def internalReportOldProblems: Task[Boolean] = zincReportCachedProblems
232-
233221
@internal
234222
override def bspBuildTarget: BspBuildTarget = super.bspBuildTarget.copy(
235223
languageIds = Seq(
@@ -254,16 +242,7 @@ trait GroovyModule extends JavaModule with GroovyModuleApi { outer =>
254242

255243
override def groovyLanguageVersion: T[String] = outer.groovyLanguageVersion()
256244
override def groovyVersion: T[String] = Task { outer.groovyVersion() }
257-
override def groovycPluginMvnDeps: T[Seq[Dep]] =
258-
Task { outer.groovycPluginMvnDeps() }
259-
// TODO: make Xfriend-path an explicit setting
260-
override def groovycOptions: T[Seq[String]] = Task {
261-
// FIXME
262-
outer.groovycOptions().filterNot(_.startsWith("-Xcommon-sources")) ++
263-
Seq(s"-Xfriend-paths=${outer.compile().classes.path.toString()}")
264-
}
245+
override def bomMvnDeps: T[Seq[Dep]] = outer.bomMvnDeps()
246+
override def mandatoryMvnDeps: Task.Simple[Seq[Dep]] = outer.mandatoryMvnDeps
265247
}
266-
267248
}
268-
269-
// TODO maybe an StandaloneGroovyTestsModule

libs/groovylib/src/mill/groovylib/GroovyWorkerManager.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ object GroovyWorkerManager extends ExternalModule {
1717
}
1818

1919
def get(toolsClassLoader: ClassLoader)(implicit ctx: TaskCtx): GroovyWorker = {
20+
// TODO why not use ServiceLoader...investigate
2021
val className =
2122
classOf[GroovyWorker].getPackage().getName().split("\\.").dropRight(1).mkString(
2223
"."
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jointcompile
2+
3+
import jointCompile.JavaPrinter
4+
5+
class GroovyGreeter{
6+
7+
private final String toGreet;
8+
private final JavaPrinter printer;
9+
10+
GroovyGreeter(String toGreet){
11+
this.toGreet = toGreet
12+
this.printer = new JavaPrinter()
13+
}
14+
15+
16+
void greet(){
17+
printer.print("Hello $toGreet");
18+
}
19+
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package jointcompile;
2+
3+
import jointcompile.GroovyGreeter;
4+
5+
6+
public class JavaMain {
7+
8+
public static void main(String[] args) {
9+
var greeter = new GroovyGreeter("JointCompile");
10+
greeter.greet();
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package jointcompile;
2+
3+
public class JavaPrinter {
4+
public void print(String s) {
5+
System.out.println(s);
6+
}
7+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
println "This is a Groovy Script"
32
println getHelloString()
43

54
static String getHelloString() {
6-
return "Hello, world!"
5+
return "Hello, Scripting!"
76
}
87

libs/groovylib/test/src/mill/groovylib/HelloGroovyTests.scala

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ object HelloGroovyTests extends TestSuite {
3535

3636
}
3737

38+
object `joint-compile` extends GroovyModule {
39+
override def groovyVersion: T[String] = groovy4Version
40+
}
41+
3842
trait Test extends GroovyModule {
3943

4044
override def mainClass = Some("hello.Hello")
4145

4246
object test extends GroovyTests with TestModule.Junit5 {
43-
4447
override def depManagement = Seq(
4548
mvn"org.junit.jupiter:junit-jupiter-engine:5.13.4"
4649
)
47-
4850
override def jupiterVersion = "5.13.4"
4951
override def junitPlatformVersion = "1.13.4"
5052
}
@@ -100,14 +102,6 @@ object HelloGroovyTests extends TestSuite {
100102

101103
test("compile & run Groovy module") {
102104
testEval().scoped { eval =>
103-
val Right(compiler) = eval.apply(m.groovyCompilerMvnDeps): @unchecked
104-
105-
assert(
106-
compiler.value.map(_.dep.module)
107-
.map(m => m.organization.value -> m.name.value)
108-
.contains("org.apache.groovy" -> "groovy")
109-
)
110-
111105
val Right(result) = eval.apply(m.compile): @unchecked
112106

113107
assert(
@@ -134,16 +128,15 @@ object HelloGroovyTests extends TestSuite {
134128
}
135129
}
136130

137-
// test("compiling & running a statically compiled Groovy") {
138-
// testEval().scoped { eval =>
139-
// val Right(_) = eval.apply(m.staticcompile.showMvnDepsTree()): @unchecked
140-
// val Right(result) = eval.apply(m.staticcompile.compile): @unchecked
141-
// assert(
142-
// os.walk(result.value.classes.path).exists(_.last == "HelloStatic.class")
143-
// )
144-
// val Right(_) = eval.apply(m.staticcompile.run()): @unchecked
145-
// }
146-
// }
131+
test("compiling & running a statically compiled Groovy") {
132+
testEval().scoped { eval =>
133+
val Right(result) = eval.apply(m.staticcompile.compile): @unchecked
134+
assert(
135+
os.walk(result.value.classes.path).exists(_.last == "HelloStatic.class")
136+
)
137+
val Right(_) = eval.apply(m.staticcompile.run()): @unchecked
138+
}
139+
}
147140

148141
test("compile & run test-only Maven JUnit5 test") {
149142
testEval().scoped { eval =>
@@ -161,25 +154,22 @@ object HelloGroovyTests extends TestSuite {
161154
}
162155
}
163156

164-
// test("compile Spock test") {
165-
// testEval().scoped { eval =>
166-
//
167-
// val Right(result1) = eval.apply(m.spock.compile): @unchecked
168-
// assert(
169-
// os.walk(result1.value.classes.path).exists(_.last == "SpockTest.class")
170-
// )
171-
// }
172-
// }
173-
174-
// test("run Spock test") {
175-
// testEval().scoped { eval =>
176-
//
177-
// val Right(discovered) = eval.apply(m.spock.discoveredTestClasses): @unchecked
178-
// assert(discovered.value == Seq("hello.spock.SpockTest"))
179-
//
180-
// val Right(_) = eval.apply(m.spock.testForked()): @unchecked
181-
// }
182-
// }
157+
test("compile & run Spock test") {
158+
testEval().scoped { eval =>
159+
160+
val Right(result1) = eval.apply(m.spock.compile): @unchecked
161+
assert(
162+
os.walk(result1.value.classes.path).exists(_.last == "SpockTest.class")
163+
)
164+
165+
val Right(discovered) = eval.apply(m.spock.discoveredTestClasses): @unchecked
166+
assert(discovered.value == Seq("hello.spock.SpockTest"))
167+
168+
val Right(_) = eval.apply(m.spock.testForked()): @unchecked
169+
}
170+
}
171+
172+
183173

184174
}
185175
}

libs/groovylib/worker/src/mill/groovylib/worker/impl/GroovyWorkerImpl.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mill.groovylib.worker.impl
22

3+
import groovy.lang.GroovyClassLoader
34
import mill.api.Result
45
import mill.api.TaskCtx
56
import mill.javalib.api.CompilationResult
@@ -22,22 +23,28 @@ class GroovyWorkerImpl extends GroovyWorker {
2223
val config = new CompilerConfiguration()
2324
config.setTargetDirectory(outputDir.toIO)
2425
config.setClasspathList(classpath.map(_.toIO.getAbsolutePath).asJava)
26+
// TODO
27+
// config.setDisabledGlobalASTTransformations()
28+
// config.setJointCompilationOptions()
29+
// config.setSourceEncoding()
30+
31+
// we need to set the classloader for groovy to use the worker classloader
32+
val parentCl: ClassLoader = this.getClass.getClassLoader
33+
// config in the GroovyClassLoader is needed when the CL itself is compiling classes
34+
val gcl = new GroovyClassLoader(parentCl, config)
35+
// config for actual compilation
36+
val unit = new CompilationUnit(config, null, gcl)
2537

26-
val unit = new CompilationUnit(config)
27-
28-
// Add source files to compile
2938
sourceFiles.foreach { sourceFile =>
3039
unit.addSource(sourceFile.toIO)
3140
}
3241

33-
return Try {
42+
Try {
3443
unit.compile(Phases.OUTPUT)
35-
3644
CompilationResult(outputDir, mill.api.PathRef(outputDir))
3745
}.fold(
3846
exception => Result.Failure(s"Groovy compilation failed: ${exception.getMessage}"),
3947
result => Result.Success(result)
4048
)
4149
}
42-
4350
}

0 commit comments

Comments
 (0)