-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run in background #738
Open
steinybot
wants to merge
4
commits into
scalameta:main
Choose a base branch
from
steinybot:run-in-background
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Run in background #738
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
ThisBuild / scalaVersion := "2.12.17" | ||
|
||
enablePlugins(MdocPlugin) | ||
|
||
TaskKey[Unit]("check") := { | ||
SbtTest.test( | ||
TestCommand("mdocBgStart", "Waiting for file changes (press enter to interrupt)"), | ||
TestCommand("show version", "[info] 0.1.0-SNAPSHOT"), | ||
TestCommand("mdocBgStart", "mdoc is already running in the background"), | ||
TestCommand("mdocBgStop", "stopping mdoc"), | ||
TestCommand("mdocBgStop", "mdoc is not running in the background"), | ||
TestCommand("exit") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```scala mdoc | ||
println(example.Example.greeting) | ||
``` |
77 changes: 77 additions & 0 deletions
77
mdoc-sbt/src/sbt-test/sbt-mdoc/bg-tasks/project/SbtTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import java.nio.charset.StandardCharsets | ||
import java.util.concurrent.LinkedBlockingQueue | ||
import scala.collection.mutable | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
import scala.sys.process._ | ||
|
||
object SbtTest { | ||
|
||
def test(commands: TestCommand*) = { | ||
val commandsToSend = new LinkedBlockingQueue[String]() | ||
def sendInput(output: java.io.OutputStream): Unit = { | ||
val newLine = "\n".getBytes(StandardCharsets.UTF_8) | ||
try { | ||
while (true) { | ||
val command = commandsToSend.take() | ||
output.write(command.getBytes(StandardCharsets.UTF_8)) | ||
output.write(newLine) | ||
output.flush() | ||
} | ||
} catch { | ||
case _: InterruptedException => // Ignore | ||
} finally { | ||
output.close() | ||
} | ||
} | ||
|
||
val commandQueue: mutable.Queue[TestCommand] = mutable.Queue(commands: _*) | ||
var expectedOutput: Option[String] = Some("[info] started sbt server") | ||
def processOut(out: String): Unit = { | ||
if (expectedOutput.forall(out.endsWith)) { | ||
if (commandQueue.nonEmpty) { | ||
val command = commandQueue.dequeue() | ||
Thread.sleep(command.delay.toMillis) | ||
commandsToSend.put(command.command) | ||
expectedOutput = command.expectedOutput | ||
} | ||
} | ||
println(s"[SbtTest] $out") | ||
} | ||
|
||
val error = new StringBuilder() | ||
def processError(err: String): Unit = { | ||
println(s"[SbtTest error] $err") | ||
error.append(err) | ||
} | ||
|
||
// TODO: Do we need the -Xmx setting and any other future options? | ||
val command = Seq( | ||
"sbt", | ||
s"-Dplugin.version=${sys.props("plugin.version")}", | ||
"--no-colors", | ||
"--supershell=never" | ||
) | ||
val logger = ProcessLogger(processOut, processError) | ||
val basicIO = BasicIO(withIn = false, logger) | ||
val io = new ProcessIO(sendInput, basicIO.processOutput, basicIO.processError) | ||
val p = command.run(io) | ||
|
||
val deadline = 30.seconds.fromNow | ||
Future { | ||
while (p.isAlive()) { | ||
if (deadline.isOverdue()) { | ||
p.destroy() | ||
} | ||
} | ||
} | ||
|
||
val code = p.exitValue() | ||
|
||
expectedOutput.foreach { expected => | ||
throw new AssertionError(s"Expected to find output: $expected") | ||
} | ||
assert(code == 0, s"Expected exit code 0 but got $code") | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
mdoc-sbt/src/sbt-test/sbt-mdoc/bg-tasks/project/TestCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.concurrent.duration._ | ||
|
||
/** | ||
* @param command the command to send | ||
* @param expectedOutput expected output of the command | ||
* @param delay time to wait before sending the command | ||
*/ | ||
final case class TestCommand(command: String, expectedOutput: Option[String], delay: FiniteDuration) | ||
|
||
object TestCommand { | ||
def apply(command: String, expectedOutput: String, delay: FiniteDuration): TestCommand = | ||
TestCommand(command, Some(expectedOutput), delay) | ||
|
||
def apply(command: String, expectedOutput: String): TestCommand = | ||
TestCommand(command, Some(expectedOutput), Duration.Zero) | ||
|
||
def apply(command: String, delay: FiniteDuration): TestCommand = | ||
TestCommand(command, None, delay) | ||
|
||
def apply(command: String): TestCommand = | ||
TestCommand(command, None, Duration.Zero) | ||
} |
1 change: 1 addition & 0 deletions
1
mdoc-sbt/src/sbt-test/sbt-mdoc/bg-tasks/project/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version = 1.8.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("org.scalameta" % "sbt-mdoc" % sys.props("plugin.version")) |
5 changes: 5 additions & 0 deletions
5
mdoc-sbt/src/sbt-test/sbt-mdoc/bg-tasks/src/main/scala/example/Example.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package example | ||
|
||
object Example { | ||
def greeting = "Hello world!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Not sure why we need to do this. If we don't it fails with java.nio.file.NoSuchFileException. | ||
$ mkdir target/mdoc | ||
> check |
21 changes: 21 additions & 0 deletions
21
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.concurrent.duration._ | ||
import MdocPlugin._ | ||
|
||
ThisBuild / scalaVersion := "2.12.17" | ||
|
||
enablePlugins(MdocPlugin) | ||
|
||
InputKey[Unit]("mdocBg") := Def.inputTaskDyn { | ||
validateSettings.value | ||
val parsed = sbt.complete.DefaultParsers.spaceDelimited("<arg>").parsed | ||
val args = (mdocExtraArguments.value ++ parsed).mkString(" ") | ||
(Compile / bgRunMain).toTask(s" mdoc.SbtMain $args") | ||
}.evaluated | ||
|
||
TaskKey[Unit]("check") := { | ||
SbtTest.test( | ||
TestCommand("mdocBg --watch --background", "Waiting for file changes (press enter to interrupt)"), | ||
TestCommand("show version", "[info] 0.1.0-SNAPSHOT", 3.seconds), | ||
TestCommand("exit") | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/docs/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```scala mdoc | ||
println(example.Example.greeting) | ||
``` |
77 changes: 77 additions & 0 deletions
77
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/project/SbtTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import java.nio.charset.StandardCharsets | ||
import java.util.concurrent.LinkedBlockingQueue | ||
import scala.collection.mutable | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
import scala.sys.process._ | ||
|
||
object SbtTest { | ||
|
||
def test(commands: TestCommand*) = { | ||
val commandsToSend = new LinkedBlockingQueue[String]() | ||
def sendInput(output: java.io.OutputStream): Unit = { | ||
val newLine = "\n".getBytes(StandardCharsets.UTF_8) | ||
try { | ||
while (true) { | ||
val command = commandsToSend.take() | ||
output.write(command.getBytes(StandardCharsets.UTF_8)) | ||
output.write(newLine) | ||
output.flush() | ||
} | ||
} catch { | ||
case _: InterruptedException => // Ignore | ||
} finally { | ||
output.close() | ||
} | ||
} | ||
|
||
val commandQueue: mutable.Queue[TestCommand] = mutable.Queue(commands: _*) | ||
var expectedOutput: Option[String] = Some("[info] started sbt server") | ||
def processOut(out: String): Unit = { | ||
if (expectedOutput.forall(out.endsWith)) { | ||
if (commandQueue.nonEmpty) { | ||
val command = commandQueue.dequeue() | ||
Thread.sleep(command.delay.toMillis) | ||
commandsToSend.put(command.command) | ||
expectedOutput = command.expectedOutput | ||
} | ||
} | ||
println(s"[SbtTest] $out") | ||
} | ||
|
||
val error = new StringBuilder() | ||
def processError(err: String): Unit = { | ||
println(s"[SbtTest error] $err") | ||
error.append(err) | ||
} | ||
|
||
// TODO: Do we need the -Xmx setting and any other future options? | ||
val command = Seq( | ||
"sbt", | ||
s"-Dplugin.version=${sys.props("plugin.version")}", | ||
"--no-colors", | ||
"--supershell=never" | ||
) | ||
val logger = ProcessLogger(processOut, processError) | ||
val basicIO = BasicIO(withIn = false, logger) | ||
val io = new ProcessIO(sendInput, basicIO.processOutput, basicIO.processError) | ||
val p = command.run(io) | ||
|
||
val deadline = 30.seconds.fromNow | ||
Future { | ||
while (p.isAlive()) { | ||
if (deadline.isOverdue()) { | ||
p.destroy() | ||
} | ||
} | ||
} | ||
|
||
val code = p.exitValue() | ||
|
||
expectedOutput.foreach { expected => | ||
throw new AssertionError(s"Expected to find output: $expected") | ||
} | ||
assert(code == 0, s"Expected exit code 0 but got $code") | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/project/TestCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.concurrent.duration._ | ||
|
||
/** | ||
* @param command the command to send | ||
* @param expectedOutput expected output of the command | ||
* @param delay time to wait before sending the command | ||
*/ | ||
final case class TestCommand(command: String, expectedOutput: Option[String], delay: FiniteDuration) | ||
|
||
object TestCommand { | ||
def apply(command: String, expectedOutput: String, delay: FiniteDuration): TestCommand = | ||
TestCommand(command, Some(expectedOutput), delay) | ||
|
||
def apply(command: String, expectedOutput: String): TestCommand = | ||
TestCommand(command, Some(expectedOutput), Duration.Zero) | ||
|
||
def apply(command: String, delay: FiniteDuration): TestCommand = | ||
TestCommand(command, None, delay) | ||
|
||
def apply(command: String): TestCommand = | ||
TestCommand(command, None, Duration.Zero) | ||
} |
1 change: 1 addition & 0 deletions
1
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/project/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version = 1.8.2 |
1 change: 1 addition & 0 deletions
1
mdoc-sbt/src/sbt-test/sbt-mdoc/run-in-background/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("org.scalameta" % "sbt-mdoc" % sys.props("plugin.version")) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need any other options for the test, should we remove the comment?