Skip to content

Commit a30e7e1

Browse files
bell-dbthesamet
authored andcommitted
Add PosixPluginFrontendSpec
1 parent 34921bb commit a30e7e1

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package protocbridge.frontend
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.must.Matchers
5+
import protocbridge.{ExtraEnv, ProtocCodeGenerator}
6+
7+
import java.io.ByteArrayOutputStream
8+
import scala.sys.process.ProcessIO
9+
import scala.util.Random
10+
11+
class OsSpecificFrontendSpec extends AnyFlatSpec with Matchers {
12+
13+
protected def testPluginFrontend(frontend: PluginFrontend): Array[Byte] = {
14+
val random = new Random()
15+
val toSend = Array.fill(123)(random.nextInt(256).toByte)
16+
val toReceive = Array.fill(456)(random.nextInt(256).toByte)
17+
val env = new ExtraEnv(secondaryOutputDir = "tmp")
18+
19+
val fakeGenerator = new ProtocCodeGenerator {
20+
override def run(request: Array[Byte]): Array[Byte] = {
21+
request mustBe (toSend ++ env.toByteArrayAsField)
22+
toReceive
23+
}
24+
}
25+
val (path, state) = frontend.prepare(
26+
fakeGenerator,
27+
env
28+
)
29+
val actualOutput = new ByteArrayOutputStream()
30+
val process = sys.process
31+
.Process(path.toAbsolutePath.toString)
32+
.run(
33+
new ProcessIO(
34+
writeInput => {
35+
writeInput.write(toSend)
36+
writeInput.close()
37+
},
38+
processOutput => {
39+
val buffer = new Array[Byte](4096)
40+
var bytesRead = 0
41+
while (bytesRead != -1) {
42+
bytesRead = processOutput.read(buffer)
43+
if (bytesRead != -1) {
44+
actualOutput.write(buffer, 0, bytesRead)
45+
}
46+
}
47+
processOutput.close()
48+
},
49+
_.close()
50+
)
51+
)
52+
process.exitValue()
53+
frontend.cleanup(state)
54+
actualOutput.toByteArray
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package protocbridge.frontend
2+
3+
class PosixPluginFrontendSpec extends OsSpecificFrontendSpec {
4+
if (!PluginFrontend.isWindows) {
5+
it must "execute a program that forwards input and output to given stream" in {
6+
testPluginFrontend(PosixPluginFrontend)
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
11
package protocbridge.frontend
22

3-
import java.io.ByteArrayInputStream
4-
5-
import protocbridge.{ProtocCodeGenerator, ExtraEnv}
6-
7-
import scala.sys.process.ProcessLogger
8-
import org.scalatest.flatspec.AnyFlatSpec
9-
import org.scalatest.matchers.must.Matchers
10-
11-
class WindowsPluginFrontendSpec extends AnyFlatSpec with Matchers {
3+
class WindowsPluginFrontendSpec extends OsSpecificFrontendSpec {
124
if (PluginFrontend.isWindows) {
135
it must "execute a program that forwards input and output to given stream" in {
14-
val toSend = "ping"
15-
val toReceive = "pong"
16-
val env = new ExtraEnv(secondaryOutputDir = "tmp")
17-
18-
val fakeGenerator = new ProtocCodeGenerator {
19-
override def run(request: Array[Byte]): Array[Byte] = {
20-
request mustBe (toSend.getBytes ++ env.toByteArrayAsField)
21-
toReceive.getBytes
22-
}
23-
}
24-
val (path, state) = WindowsPluginFrontend.prepare(
25-
fakeGenerator,
26-
env
27-
)
28-
val actualOutput = scala.collection.mutable.Buffer.empty[String]
29-
val process = sys.process
30-
.Process(path.toAbsolutePath.toString)
31-
.#<(new ByteArrayInputStream(toSend.getBytes))
32-
.run(ProcessLogger(o => actualOutput.append(o)))
33-
process.exitValue()
34-
actualOutput.mkString mustBe toReceive
35-
WindowsPluginFrontend.cleanup(state)
6+
testPluginFrontend(WindowsPluginFrontend)
367
}
378
}
389
}

0 commit comments

Comments
 (0)