Skip to content

Commit

Permalink
Added the ability to access the full classpath from the REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
John Johnson II committed May 29, 2022
1 parent 00d6022 commit a9beb82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/main/scala/com/potenciasoftware/rebel/BaseRepl.scala
@@ -1,6 +1,7 @@
package com.potenciasoftware.rebel

import java.lang.reflect.Field
import java.net.URLClassLoader
import scala.reflect.ClassTag
import scala.reflect.runtime.universe.TypeTag
import scala.tools.nsc.Settings
Expand Down Expand Up @@ -34,7 +35,15 @@ class BaseRepl {
val sets = new Settings
updateSettings(sets)
if (sets.classpath.isDefault)
sets.classpath.value = sys.props("java.class.path")
Thread.currentThread.getContextClassLoader match {
case cl: URLClassLoader =>
sets.classpath.value =
cl.getURLs
.map(_.toString)
.distinct
.mkString(java.io.File.pathSeparator)
case _ => sys.error("classloader is not a URLClassLoader")
}
sets
}

Expand Down Expand Up @@ -105,8 +114,15 @@ object BaseRepl {
}

object Parameter {
def apply[A: TypeTag: ClassTag](name: String, value: A, modifiers: String*) =
new Parameter(name, TypeStrings.fromTag[A], value, modifiers.toList)
def apply[A: TypeTag: ClassTag](
name: String,
value: A,
modifiers: String*
) = new Parameter(
name,
TypeStrings.fromTag[A],
value,
modifiers.toList)
}
}

10 changes: 10 additions & 0 deletions src/test/scala/com/potenciasoftware/rebel/BaseReplTest.scala
Expand Up @@ -87,6 +87,14 @@ class BaseReplTest extends AnyFlatSpec with Matchers {
|scala> // mutated options.PS1
|test> println(s"[${options.PS1}]")[test> ]""".stripMargin
}

it should "use the full classpath" in {
replTest[BaseReplTest]("fullClasspath",
"import com.potenciasoftware.rebel.BaseReplTest.TestValue",
"val value = TestValue(42)",
"println(value)"
).all.printAll
}
}

object BaseReplTest {
Expand All @@ -100,5 +108,7 @@ object BaseReplTest {
def PS1: String = repl.prompt
def PS1_=(ps1: String): Unit = { repl.prompt = ps1 }
}

case class TestValue(value: Any)
}

6 changes: 3 additions & 3 deletions src/test/scala/com/potenciasoftware/rebel/TestUtils.scala
Expand Up @@ -70,11 +70,11 @@ object TestUtils {
.map(_.fold("[Error] " + _, identity))
}

/** In a separate system process, execute a REPL sending it input returning the
/** In a separate system process, execute a REPL sending it input returning the
* resulting output. This technique is necessary because, for some reason,
* instatiating the REPL instance in the same process that is running the
* unit tests casuse an exception.
*
*
* @tparam C The class where [[methodName]] is defined.
* @param methodName The name of a 0-arity method which sets up the REPL to
* test. If the method returns a [[BaseRepl]],
Expand All @@ -93,7 +93,7 @@ object TestUtils {
line => output.append(Left(line)))

val exitCode = Seq("java",
"-classpath", modifiedTestClasspath mkString ":",
"-classpath", modifiedTestClasspath mkString java.io.File.pathSeparator,
"com.potenciasoftware.rebel.TestUtils",
implicitly[ClassTag[C]].runtimeClass.getName(),
methodName) #< in !< logger
Expand Down

0 comments on commit a9beb82

Please sign in to comment.