diff --git a/004-cut/integration/src/test/scala/CutSpec.scala b/004-cut/integration/src/test/scala/CutSpec.scala index 95361b3..b4d7c5a 100644 --- a/004-cut/integration/src/test/scala/CutSpec.scala +++ b/004-cut/integration/src/test/scala/CutSpec.scala @@ -35,7 +35,33 @@ class CutSpec extends FixtureAnyWordSpec with Matchers { | |Column and field numbering start from 1. |""".stripMargin - (s"$cut -h").!! shouldBe expectedUsage + s"$cut -h".!! shouldBe expectedUsage + } + } + "provided invalid options" should { + "provide info and fail" in { cut => + val command = s"$cut -n" + val expectedOutput = + """Error: Unknown option -n + |Try --help for more information. + |""".stripMargin + val sb = new StringBuffer + val err = new StringBuilder() + val printer = ProcessLogger((e: String) => err.append(e + "\n")) + (command run BasicIO(withIn = false, sb, Some(printer))).exitValue() shouldBe 1 + sb.toString shouldBe "" + err.toString shouldBe expectedOutput + } + } + "provided invalid file" should { + "provided info and fail" in { cut => + val command = s"$cut nonexistent.file" + val expectedOutput = + """nonexistent.file: No such file. + |""".stripMargin + val sb = new StringBuffer + (command run BasicIO(withIn = false, sb, None)).exitValue shouldBe 1 + sb.toString shouldBe expectedOutput // TODO direct to STDERR instead } } "cutting single field from file" should { @@ -48,7 +74,7 @@ class CutSpec extends FixtureAnyWordSpec with Matchers { |15 |20 |""".stripMargin - (s"$cut -f 1 $sampleTsvPath").!! shouldBe expectedOutput + s"$cut -f 1 $sampleTsvPath".!! shouldBe expectedOutput } "yield f2" in { cut => val expectedOutput = @@ -59,7 +85,7 @@ class CutSpec extends FixtureAnyWordSpec with Matchers { |16 |21 |""".stripMargin - (s"$cut -f 2 $sampleTsvPath").!! shouldBe expectedOutput + s"$cut -f 2 $sampleTsvPath".!! shouldBe expectedOutput } } "given a delimiter" should { @@ -84,7 +110,7 @@ class CutSpec extends FixtureAnyWordSpec with Matchers { |15\t16 |20\t21 |""".stripMargin - (s"$cut -f 1,2 $sampleTsvPath").!! shouldBe expectedOutput + s"$cut -f 1,2 $sampleTsvPath".!! shouldBe expectedOutput } "return the correct fields separated by a single occurrence of the provided field delimiter" in { cut => val expectedOutput = diff --git a/004-cut/src/main/scala/Main.scala b/004-cut/src/main/scala/Main.scala index fdcbd93..4934530 100644 --- a/004-cut/src/main/scala/Main.scala +++ b/004-cut/src/main/scala/Main.scala @@ -68,7 +68,7 @@ object Main extends App { OParser.parse(parser, args, Config()) match { case Some(config) => run(config) - case _ => println("Invalid options provided") + case _ => sys.exit(1) } private def run(config: Config): Unit = { @@ -78,7 +78,6 @@ object Main extends App { } selections.foreach(println) - // TODO status code } private def selectionsFromFile(config: Config): Option[Selections] = { @@ -89,7 +88,7 @@ object Main extends App { bufferedSource.close() Some(selections) } catch { - case _: FileNotFoundException => println(s"$config.file: No such file."); None + case _: FileNotFoundException => println(s"${config.file}: No such file."); sys.exit(1) } } }