diff options
| author | Schuyler Eldridge | 2019-08-05 18:43:22 -0400 |
|---|---|---|
| committer | GitHub | 2019-08-05 18:43:22 -0400 |
| commit | 0fe6aad23a4aee50119b9fe2645ba2ff833f65bb (patch) | |
| tree | afc9cfb9bc11c67a4a61253959c0223669baa3da | |
| parent | ac42287bc47fb8bc6695ae0aaf8f4fee61e129e5 (diff) | |
| parent | 8f3d510c27be7fe9dd6cca7f09803113d1ce6bf2 (diff) | |
Merge pull request #1144 from freechipsproject/fileutils-fix
Fix FileUtils.getLines, add simple FileUtils tests
| -rw-r--r-- | src/main/scala/firrtl/FileUtils.scala | 20 | ||||
| -rw-r--r-- | src/test/scala/firrtlTests/FileUtilsSpec.scala | 46 |
2 files changed, 51 insertions, 15 deletions
diff --git a/src/main/scala/firrtl/FileUtils.scala b/src/main/scala/firrtl/FileUtils.scala index 1007eb2a..8e73b4f9 100644 --- a/src/main/scala/firrtl/FileUtils.scala +++ b/src/main/scala/firrtl/FileUtils.scala @@ -97,12 +97,7 @@ object FileUtils { * * @param fileName The file to read */ - def getLines(fileName: String): Seq[String] = { - val source = scala.io.Source.fromFile(fileName) - val lines = source.getLines() - source.close() - lines.toSeq - } + def getLines(fileName: String): Seq[String] = getLines(new File(fileName)) /** Read a text file and return it as a Seq of strings * Closes the file after read to avoid dangling file handles @@ -111,9 +106,9 @@ object FileUtils { */ def getLines(file: File): Seq[String] = { val source = scala.io.Source.fromFile(file) - val lines = source.getLines() + val lines = source.getLines().toList source.close() - lines.toSeq + lines } /** Read a text file and return it as a single string @@ -121,12 +116,7 @@ object FileUtils { * * @param fileName The file to read */ - def getText(fileName: String): String = { - val source = scala.io.Source.fromFile(fileName) - val text = source.mkString - source.close() - text - } + def getText(fileName: String): String = getText(new File(fileName)) /** Read a text file and return it as a single string * Closes the file after read to avoid dangling file handles @@ -167,4 +157,4 @@ object FileUtils { inputStream.close() text } -}
\ No newline at end of file +} diff --git a/src/test/scala/firrtlTests/FileUtilsSpec.scala b/src/test/scala/firrtlTests/FileUtilsSpec.scala new file mode 100644 index 00000000..1a23fb48 --- /dev/null +++ b/src/test/scala/firrtlTests/FileUtilsSpec.scala @@ -0,0 +1,46 @@ +// See LICENSE for license details. + +package firrtlTests + +import org.scalatest.{FlatSpec, Matchers} + +import firrtl.FileUtils + +class FileUtilsSpec extends FlatSpec with Matchers { + + private val sampleAnnotations: String = "annotations/SampleAnnotations.anno" + private val sampleAnnotationsFileName: String = s"src/test/resources/$sampleAnnotations" + + behavior of "FileUtils.getLines" + + it should "read from a string filename" in { + FileUtils.getLines(sampleAnnotationsFileName).size should be > 0 + } + + it should "read from a Java file" in { + FileUtils.getLines(new java.io.File(sampleAnnotationsFileName)).size should be > 0 + } + + behavior of "FileUtils.getText" + + it should "read from a string filename" in { + FileUtils.getText(sampleAnnotationsFileName).size should be > 0 + } + + it should "read from a Java file" in { + FileUtils.getText(new java.io.File(sampleAnnotationsFileName)).size should be > 0 + } + + behavior of "FileUtils.getLinesResource" + + it should "read from a resource" in { + FileUtils.getLinesResource(s"/$sampleAnnotations").size should be > 0 + } + + behavior of "FileUtils.getTextResource" + + it should "read from a resource" in { + FileUtils.getTextResource(s"/$sampleAnnotations").split("\n").size should be > 0 + } + +} |
