aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/options/OptionParserSpec.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/scala/firrtlTests/options/OptionParserSpec.scala')
-rw-r--r--src/test/scala/firrtlTests/options/OptionParserSpec.scala65
1 files changed, 28 insertions, 37 deletions
diff --git a/src/test/scala/firrtlTests/options/OptionParserSpec.scala b/src/test/scala/firrtlTests/options/OptionParserSpec.scala
index ae4899d4..0a9ac2d5 100644
--- a/src/test/scala/firrtlTests/options/OptionParserSpec.scala
+++ b/src/test/scala/firrtlTests/options/OptionParserSpec.scala
@@ -4,15 +4,13 @@ package firrtlTests.options
import firrtl.{AnnotationSeq, FIRRTLException}
import firrtl.annotations.{Annotation, NoTargetAnnotation}
-import firrtl.options.{DoNotTerminateOnExit, DuplicateHandling, OptionsException}
+import firrtl.options.{DoNotTerminateOnExit, DuplicateHandling, ExceptOnError, OptionsException}
import scopt.OptionParser
import org.scalatest.{FlatSpec, Matchers}
-import java.security.Permission
-
-class OptionParserSpec extends FlatSpec with Matchers {
+class OptionParserSpec extends FlatSpec with Matchers with firrtlTests.Utils {
case class IntAnnotation(x: Int) extends NoTargetAnnotation {
def extract: Int = x
@@ -32,50 +30,35 @@ class OptionParserSpec extends FlatSpec with Matchers {
opt[Int]("integer").abbr("m").unbounded.action( (x, c) => IntAnnotation(x) +: c )
}
- case class ExitException(status: Option[Int]) extends SecurityException("Found a sys.exit")
+ trait WithIntParser { val parser = new IntParser }
- /* Security manager that disallows calls to sys.exit */
- class ExceptOnExit extends SecurityManager {
- override def checkPermission(perm: Permission): Unit = {}
- override def checkPermission(perm: Permission, context: Object): Unit = {}
- override def checkExit(status: Int): Unit = {
- super.checkExit(status)
- throw ExitException(Some(status))
- }
- }
+ behavior of "A default OptionsParser"
- /* Tell a parser to terminate in an environment where sys.exit throws an exception */
- def catchStatus(parser: OptionParser[_], exitState: Either[String, Unit]): Option[Int] = {
- System.setSecurityManager(new ExceptOnExit())
- val status = try {
- parser.terminate(exitState)
- throw new ExitException(None)
- } catch {
- case ExitException(s) => s
- }
- System.setSecurityManager(null)
- status
- }
+ it should "call sys.exit if terminate is called" in new WithIntParser {
+ info("exit status of 1 for failure")
+ catchStatus { parser.terminate(Left("some message")) } should be (Left(1))
- behavior of "default OptionsParser"
-
- it should "terminate on exit" in {
- val parser = new IntParser
+ info("exit status of 0 for success")
+ catchStatus { parser.terminate(Right(Unit)) } should be (Left(0))
+ }
- info("By default, exit statuses are reported")
- catchStatus(parser, Left("some message")) should be (Some(1))
- catchStatus(parser, Right(Unit)) should be (Some(0))
+ it should "print to stderr on an invalid option" in new WithIntParser {
+ grabStdOutErr{ parser.parse(Array("--foo"), Seq[Annotation]()) }._2 should include ("Unknown option --foo")
}
- behavior of "DoNotTerminateOnExit"
+ behavior of "An OptionParser with DoNotTerminateOnExit mixed in"
it should "disable sys.exit for terminate method" in {
val parser = new IntParser with DoNotTerminateOnExit
- catchStatus(parser, Left("some message")) should be (None)
- catchStatus(parser, Right(Unit)) should be (None)
+
+ info("no exit for failure")
+ catchStatus { parser.terminate(Left("some message")) } should be (Right(()))
+
+ info("no exit for success")
+ catchStatus { parser.terminate(Right(Unit)) } should be (Right(()))
}
- behavior of "DuplicateHandling"
+ behavior of "An OptionParser with DuplicateHandling mixed in"
it should "detect short duplicates" in {
val parser = new IntParser with DuplicateHandling with DuplicateShortOption
@@ -89,4 +72,12 @@ class OptionParserSpec extends FlatSpec with Matchers {
.getMessage should startWith ("Duplicate long option")
}
+ behavior of "An OptionParser with ExceptOnError mixed in"
+
+ it should "cause an OptionsException on an invalid option" in {
+ val parser = new IntParser with ExceptOnError
+ intercept[OptionsException] { parser.parse(Array("--foo"), Seq[Annotation]()) }
+ .getMessage should include ("Unknown option")
+ }
+
}