From b6de1491af7ba88bb778aaeeba369b6b87151f9e Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Sat, 5 Sep 2020 20:00:35 -0700 Subject: Support binary files in CustomFileEmission (#1887) --- .../phases/WriteOutputAnnotationsSpec.scala | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/test') diff --git a/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala b/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala index 4fe16041..9e58585c 100644 --- a/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala +++ b/src/test/scala/firrtlTests/options/phases/WriteOutputAnnotationsSpec.scala @@ -142,6 +142,28 @@ class WriteOutputAnnotationsSpec extends AnyFlatSpec with Matchers with firrtl.t new File(serializedFileName) should (exist) } + it should "support CustomFileEmission to binary files" in new Fixture { + val file = new File("write-CustomFileEmission-binary-files.anno.json") + val data = Array[Byte](0x0a, 0xa0.toByte) + val annotations = Seq( + TargetDirAnnotation(dir), + OutputAnnotationFileAnnotation(file.toString), + WriteOutputAnnotationsSpec.Binary(data) + ) + + val serializedFileName = view[StageOptions](annotations).getBuildFileName("Binary", Some(".Emission")) + val out = phase.transform(annotations) + + info(s"file '$serializedFileName' exists") + new File(serializedFileName) should (exist) + + info(s"file '$serializedFileName' is correct") + val inputStream = new java.io.FileInputStream(serializedFileName) + val result = new Array[Byte](2) + inputStream.read(result) + result should equal(data) + } + it should "error if multiple annotations try to write to the same file" in new Fixture { val file = new File("write-CustomFileEmission-annotations-error.anno.json") val annotations = Seq( @@ -175,6 +197,15 @@ private object WriteOutputAnnotationsSpec { } + case class Binary(value: Array[Byte]) extends NoTargetAnnotation with CustomFileEmission { + + override protected def baseFileName(a: AnnotationSeq): String = "Binary" + + override protected def suffix: Option[String] = Some(".Emission") + + override def getBytes: Iterable[Byte] = value + } + case class Replacement(file: String) extends NoTargetAnnotation } -- cgit v1.2.3