Skip to content

Commit 34606e3

Browse files
committed
Add PosixPluginFrontendSpec
1 parent a845105 commit 34606e3

File tree

3 files changed

+62
-31
lines changed

3 files changed

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