-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from zombocom/schneems/background-stdin
Add ability to write to STDIN
- Loading branch information
Showing
9 changed files
with
213 additions
and
11 deletions.
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
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,36 @@ | ||
class Rundoc::CodeCommand::Background | ||
# Will send contents to the background process via STDIN along with a newline | ||
# | ||
# | ||
class StdinWrite < Rundoc::CodeCommand | ||
def initialize(contents, name:, wait:, timeout: 5, ending: $/) | ||
@contents = contents | ||
@ending = ending | ||
@spawn = Rundoc::CodeCommand::Background::ProcessSpawn.find(name) | ||
@wait = wait | ||
@timeout_value = Integer(timeout) | ||
@contents_written = nil | ||
end | ||
|
||
# The command is rendered (`:::>-`) by the output of the `def call` method. | ||
def to_md(env = {}) | ||
writecontents | ||
end | ||
|
||
# The contents produced by the command (`:::->`) are rendered by the `def to_md` method. | ||
def call(env = {}) | ||
writecontents | ||
@spawn.log.read | ||
end | ||
|
||
def writecontents | ||
@contents_written ||= @spawn.stdin_write( | ||
contents, | ||
wait: @wait, | ||
ending: @ending, | ||
timeout: @timeout_value | ||
) | ||
end | ||
end | ||
end | ||
Rundoc.register_code_command(:"background.stdin_write", Rundoc::CodeCommand::Background::StdinWrite) |
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ def to_md(env = {}) | |
|
||
def call(env = {}) | ||
@spawn.stop | ||
"" | ||
@spawn.log.read | ||
end | ||
end | ||
end | ||
|
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Rundoc | ||
VERSION = "3.1.2" | ||
VERSION = "4.0.0" | ||
end |
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,57 @@ | ||
require "test_helper" | ||
|
||
class BackgroundStdinTest < Minitest::Test | ||
def test_background_stdin_write | ||
Dir.mktmpdir do |dir| | ||
Dir.chdir(dir) do | ||
dir = Pathname(dir) | ||
script = dir.join("script.rb") | ||
script.write <<~'EOF' | ||
$stdout.sync = true | ||
print "> " | ||
while line = gets | ||
puts line | ||
if line.strip == "exit" | ||
puts "Bye" | ||
return | ||
else | ||
puts "You said: #{line}" | ||
end | ||
print "> " | ||
end | ||
EOF | ||
|
||
source_path = dir.join("RUNDOC.md") | ||
source_path.write <<~EOF | ||
``` | ||
:::>- background.start("ruby #{script}", | ||
name: "script", | ||
wait: ">", | ||
timeout: 15 | ||
) | ||
:::-- background.stdin_write("hello", name: "script", wait: "hello") | ||
:::-- background.stdin_write("exit", name: "script", wait: "exit") | ||
:::>> background.stop(name: "script") | ||
``` | ||
EOF | ||
|
||
io = StringIO.new | ||
Rundoc::CLI.new( | ||
io: io, | ||
source_path: source_path, | ||
on_success_dir: dir.join(SUCCESS_DIRNAME) | ||
).call | ||
|
||
readme = dir.join(SUCCESS_DIRNAME).join("README.md").read | ||
expected = <<~EOF | ||
> hello | ||
You said: hello | ||
> exit | ||
Bye | ||
EOF | ||
assert readme.include?(expected) | ||
end | ||
end | ||
end | ||
end |
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