Skip to content

Commit

Permalink
Add error codes to failure paths and test them
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-peterson committed Jun 16, 2024
1 parent 36d3dc0 commit 60cad33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
34 changes: 30 additions & 4 deletions 004-cut/integration/src/test/scala/CutSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 =
Expand All @@ -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 {
Expand All @@ -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 =
Expand Down
5 changes: 2 additions & 3 deletions 004-cut/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -78,7 +78,6 @@ object Main extends App {
}

selections.foreach(println)
// TODO status code
}

private def selectionsFromFile(config: Config): Option[Selections] = {
Expand All @@ -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)
}
}
}

0 comments on commit 60cad33

Please sign in to comment.