From 7b2f3bbd3741450992d4152d8c64c45b41734d74 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Mon, 7 Nov 2016 02:43:41 -0500 Subject: Add cbt build for Eval and remove dependency on twitter util-core --- libraries/eval/Eval.scala | 16 +++++++++----- libraries/eval/build/build.scala | 14 ++++++++++++ libraries/eval/build/build/build.scala | 7 ++++++ libraries/eval/test/EvalTest.scala | 40 ++++++++++++++++++++-------------- 4 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 libraries/eval/build/build.scala create mode 100644 libraries/eval/build/build/build.scala (limited to 'libraries/eval') diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala index 1ab5762..057fd53 100644 --- a/libraries/eval/Eval.scala +++ b/libraries/eval/Eval.scala @@ -16,8 +16,6 @@ package com.twitter.util -import com.twitter.conversions.string._ -import com.twitter.io.StreamIO import java.io._ import java.math.BigInteger import java.net.URLClassLoader @@ -294,9 +292,9 @@ class Eval(target: Option[File]) { case -1 => fileName case dot => fileName.substring(0, dot) } - baseName.regexSub(Eval.classCleaner) { m => - "$%02x".format(m.group(0).charAt(0).toInt) - } + Eval.classCleaner.replaceAllIn( baseName, { m => + Regex.quoteReplacement( "$%02x".format(m.group(0).charAt(0).toInt) ) + }) } /* @@ -433,7 +431,13 @@ class Eval(target: Option[File]) { if (maxDepth == 0) { throw new IllegalStateException("Exceeded maximum recusion depth") } else { - apply(StreamIO.buffer(r.get(path)).toString, maxDepth - 1) + val inputStream = r.get(path) + val string = new String( + Iterator.continually( + inputStream.read() + ).takeWhile(_ != -1).map(_.toByte).toArray + ) + apply(string, maxDepth - 1) } } case _ => diff --git a/libraries/eval/build/build.scala b/libraries/eval/build/build.scala new file mode 100644 index 0000000..8dcaabd --- /dev/null +++ b/libraries/eval/build/build.scala @@ -0,0 +1,14 @@ +import cbt._ +class Build(val context: Context) extends BaseBuild{ + outer => + override def dependencies = super.dependencies :+ + new ScalaCompilerDependency( context.cbtHasChanged, context.paths.mavenCache, scalaVersion ) + + override def test: Option[ExitCode] = Some{ + new BasicBuild(context.copy(projectDirectory = projectDirectory ++ "/test")) with ScalaTest{ + override def dependencies = super.dependencies ++ Seq( + DirectoryDependency(projectDirectory++"/..") + ) + }.run + } +} diff --git a/libraries/eval/build/build/build.scala b/libraries/eval/build/build/build.scala new file mode 100644 index 0000000..22349c7 --- /dev/null +++ b/libraries/eval/build/build/build.scala @@ -0,0 +1,7 @@ +import cbt._ + +class Build(val context: Context) extends BuildBuild{ + override def dependencies = super.dependencies ++ Seq( + plugins.scalaTest + ) +} diff --git a/libraries/eval/test/EvalTest.scala b/libraries/eval/test/EvalTest.scala index d445424..b2d53bc 100644 --- a/libraries/eval/test/EvalTest.scala +++ b/libraries/eval/test/EvalTest.scala @@ -1,18 +1,26 @@ package com.twitter.util -import com.twitter.io.TempFile import java.io.{File, FileWriter} -import org.junit.runner.RunWith import org.scalatest.WordSpec -import org.scalatest.junit.JUnitRunner import scala.io.Source import scala.language.reflectiveCalls import scala.reflect.internal.util.Position import scala.tools.nsc.Settings import scala.tools.nsc.reporters.{AbstractReporter, Reporter} +import java.nio.file._ -@RunWith(classOf[JUnitRunner]) class EvalTest extends WordSpec { + def fromResourcePath(path: String): File = { + assert(path.endsWith(".scala")) + val tmpFile = File.createTempFile(path.stripSuffix(".scala"),"scala") + Files.copy( + Paths.get( getClass.getResource(path).getFile), + tmpFile.toPath, + StandardCopyOption.REPLACE_EXISTING + ) + tmpFile.deleteOnExit() + tmpFile + } "Evaluator" should { "apply('expression')" in { @@ -20,13 +28,13 @@ class EvalTest extends WordSpec { } "apply(new File(...))" in { - assert((new Eval).apply[Int](TempFile.fromResourcePath("/OnePlusOne.scala")) == 2) + assert((new Eval).apply[Int](fromResourcePath("/OnePlusOne.scala")) == 2) } "apply(new File(...), new File(...))" in { val derived = (new Eval).apply[() => String]( - TempFile.fromResourcePath("/Base.scala"), - TempFile.fromResourcePath("/Derived.scala")) + fromResourcePath("/Base.scala"), + fromResourcePath("/Derived.scala")) assert(derived() == "hello") } @@ -35,7 +43,7 @@ class EvalTest extends WordSpec { f.delete() f.mkdir() val e = new Eval(Some(f)) - val sourceFile = TempFile.fromResourcePath("/file-with-dash.scala") + val sourceFile = fromResourcePath("/file-with-dash.scala") val res: String = e(sourceFile) assert(res == "hello") val className = e.fileToClassName(sourceFile) @@ -52,7 +60,7 @@ class EvalTest extends WordSpec { f.delete() f.mkdir() val e = new Eval(Some(f)) - val sourceFile = TempFile.fromResourcePath("/OnePlusOne.scala") + val sourceFile = fromResourcePath("/OnePlusOne.scala") val res: Int = e(sourceFile) assert(res == 2) @@ -101,7 +109,7 @@ class EvalTest extends WordSpec { "uses deprecated" in { val deprecated = (new Eval).apply[() => String]( - TempFile.fromResourcePath("/Deprecated.scala")) + fromResourcePath("/Deprecated.scala")) assert(deprecated() == "hello") } @@ -126,22 +134,22 @@ class EvalTest extends WordSpec { "#include" in { val derived = Eval[() => String]( - TempFile.fromResourcePath("/Base.scala"), - TempFile.fromResourcePath("/DerivedWithInclude.scala")) + fromResourcePath("/Base.scala"), + fromResourcePath("/DerivedWithInclude.scala")) assert(derived() == "hello") assert(derived.toString == "hello, joe") } "recursive #include" in { val derived = Eval[() => String]( - TempFile.fromResourcePath("/Base.scala"), - TempFile.fromResourcePath("/IncludeInclude.scala")) + fromResourcePath("/Base.scala"), + fromResourcePath("/IncludeInclude.scala")) assert(derived() == "hello") assert(derived.toString == "hello, joe; hello, joe") } "toSource returns post-processed code" in { - val derived = Eval.toSource(TempFile.fromResourcePath("/DerivedWithInclude.scala")) + val derived = Eval.toSource(fromResourcePath("/DerivedWithInclude.scala")) assert(derived.contains("hello, joe")) assert(derived.contains("new Base")) } @@ -149,7 +157,7 @@ class EvalTest extends WordSpec { "throws a compilation error when Ruby is #included" in { intercept[Throwable] { Eval[() => String]( - TempFile.fromResourcePath("RubyInclude.scala") + fromResourcePath("RubyInclude.scala") ) } } -- cgit v1.2.3