|
| 1 | +/* |
| 2 | + * Scala.js JS Envs (https://github.com/scala-js/scala-js-js-envs) |
| 3 | + * |
| 4 | + * Copyright EPFL. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (https://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package org.scalajs.jsenv |
| 14 | + |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.Assert._ |
| 17 | + |
| 18 | +import scala.concurrent.{Await, Future} |
| 19 | +import scala.concurrent.duration._ |
| 20 | + |
| 21 | +import scala.util.Failure |
| 22 | + |
| 23 | +class ExternalJSRunTest { |
| 24 | + |
| 25 | + private def assertFails(future: Future[Unit])( |
| 26 | + pf: PartialFunction[Throwable, Unit]): Unit = { |
| 27 | + Await.ready(future, 1.seconds).value.get match { |
| 28 | + case Failure(t) if pf.isDefinedAt(t) => // OK |
| 29 | + |
| 30 | + case result => |
| 31 | + result.get |
| 32 | + fail("run succeeded unexpectedly") |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private val silentConfig = { |
| 37 | + val runConfig = RunConfig() |
| 38 | + .withInheritOut(false) |
| 39 | + .withInheritErr(false) |
| 40 | + .withOnOutputStream((_, _) => ()) |
| 41 | + |
| 42 | + ExternalJSRun.Config() |
| 43 | + .withRunConfig(runConfig) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + def nonExistentCommand: Unit = { |
| 48 | + val cmd = List("nonexistent-cmd") |
| 49 | + val run = ExternalJSRun.start(cmd, silentConfig) { _ => |
| 50 | + fail("unexpected call to input") |
| 51 | + } |
| 52 | + |
| 53 | + assertFails(run.future) { |
| 54 | + case ExternalJSRun.FailedToStartException(`cmd`, _) => // OK |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + def failingCommand: Unit = { |
| 60 | + val run = ExternalJSRun.start(List("node", "non-existent-file.js"), |
| 61 | + silentConfig)(_.close()) |
| 62 | + |
| 63 | + assertFails(run.future) { |
| 64 | + case ExternalJSRun.NonZeroExitException(_) => // OK |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + def abortedCommand: Unit = { |
| 70 | + val run = ExternalJSRun.start(List("node"), silentConfig) { _ => |
| 71 | + // Do not close stdin so node keeps running |
| 72 | + } |
| 73 | + |
| 74 | + run.close() |
| 75 | + |
| 76 | + assertFails(run.future) { |
| 77 | + case ExternalJSRun.ClosedException() => // OK |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + def abortedCommandOK: Unit = { |
| 83 | + val config = silentConfig |
| 84 | + .withClosingFails(false) |
| 85 | + |
| 86 | + val run = ExternalJSRun.start(List("node"), config) { _ => |
| 87 | + // Do not close stdin so node keeps running |
| 88 | + } |
| 89 | + |
| 90 | + run.close() |
| 91 | + Await.result(run.future, 1.second) |
| 92 | + } |
| 93 | +} |
0 commit comments