Skip to content
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

Allow multiple arguments to xProcess#write #634

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/aruba/api/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def run_command_and_stop(cmd, opts = {})
def type(input)
return close_input if input == "\u0004"

last_command_started.write(input << "\n")
last_command_started.write(input, "\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to enumerate something like

input.times do |item|
  last_command_started.write(item, "\n")
end

end

# Close stdin
Expand Down
9 changes: 4 additions & 5 deletions lib/aruba/processes/in_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ def stderr(*)
@stderr.string
end

# Write strint to stdin
# Write strings to stdin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, again, tidy!

#
# @param [String] input
# Write string to stdin in
def write(input)
@stdin.write input
# @param *input [Array<String>]
def write(*input)
@stdin.write(*input)
end

# Close io
Expand Down
4 changes: 2 additions & 2 deletions lib/aruba/processes/spawn_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def stderr(opts = {})
end
end

def write(input)
def write(*input)
return if stopped?

@process.io.stdin.write(input)
@process.io.stdin.write(*input)
@process.io.stdin.flush

self
Expand Down
72 changes: 46 additions & 26 deletions spec/aruba/processes/basic_process_spec.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
require "spec_helper"

RSpec.describe Aruba::Processes::BasicProcess do
let(:process) do
Class.new(described_class) do
def initialize(*args)
@stdout = args.shift
@stderr = args.shift
super(*args)
end

def stdout(*_args)
@stdout
end

def stderr(*_args)
@stderr
end
end.new(stdout, stderr, cmd, exit_timeout, io_wait_timeout, working_directory)
end

let(:cmd) { "foobar" }
let(:exit_timeout) { 0 }
let(:io_wait_timeout) { 0 }
let(:working_directory) { Dir.pwd }
let(:stdout) { "foo output" }
let(:stderr) { "foo error output" }
let(:process) { described_class.new(cmd, exit_timeout, io_wait_timeout, working_directory) }

describe '#inspect' do
let(:stdout) { 'foo output' }
let(:stderr) { 'foo error output' }

let(:derived_process) do
Class.new(described_class) do
def initialize(*args)
@stdout = args.shift
@stderr = args.shift
super(*args)
end

describe "#inspect" do
it "shows useful info" do
def stdout(*_args)
@stdout
end

def stderr(*_args)
@stderr
end
end.new(stdout, stderr, cmd, exit_timeout, io_wait_timeout, working_directory)
end

it 'shows useful info' do
expected = /commandline="foobar": stdout="foo output" stderr="foo error output"/
expect(process.inspect).to match(expected)
expect(derived_process.inspect).to match(expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat update to the expect() clauses.

end

context "with no stdout" do
let(:stdout) { nil }

it "shows useful info" do
expected = /commandline="foobar": stdout=nil stderr="foo error output"/
expect(process.inspect).to match(expected)
expect(derived_process.inspect).to match(expected)
end
end

Expand All @@ -46,7 +48,7 @@ def stderr(*_args)

it "shows useful info" do
expected = /commandline="foobar": stdout="foo output" stderr=nil/
expect(process.inspect).to match(expected)
expect(derived_process.inspect).to match(expected)
end
end

Expand All @@ -56,8 +58,26 @@ def stderr(*_args)

it "shows useful info" do
expected = /commandline="foobar": stdout=nil stderr=nil/
expect(process.inspect).to match(expected)
expect(derived_process.inspect).to match(expected)
end
end
end

describe '#stdin' do
it 'raises NotImplementedError' do
expect { process.stdin }.to raise_error NotImplementedError
end
end

describe '#stdout' do
it 'raises NotImplementedError' do
expect { process.stdout }.to raise_error NotImplementedError
end
end

describe '#stderr' do
it 'raises NotImplementedError' do
expect { process.stderr }.to raise_error NotImplementedError
end
end
end
28 changes: 28 additions & 0 deletions spec/aruba/processes/in_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def execute!
end
end

let(:stdin_runner) do
Class.new(base_runner) do
def execute!
@stdin.rewind
item = @stdin.gets.to_s.chomp
@stdout.puts "Hello, #{item}!"
end
end
end

let(:failed_runner) do
Class.new(base_runner) do
def execute!
Expand Down Expand Up @@ -141,4 +151,22 @@ def run_process(&block)
.to raise_error(TypeError, "no implicit conversion of String into Integer")
end
end

describe '#write' do
let(:main_class) { stdin_runner }

it 'writes single strings to the process' do
process.write 'World'
process.start
process.stop
expect(process.stdout).to eq "Hello, World!\n"
end

it 'writes multiple strings to the process' do
process.write 'Wor', 'ld'
process.start
process.stop
expect(process.stdout).to eq "Hello, World!\n"
end
end
end
20 changes: 20 additions & 0 deletions spec/aruba/processes/spawn_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@
end
end

describe '#write' do
let(:command_line) { "ruby -e 'puts gets'" }

it 'writes single strings to the process' do
process.start
process.write "hello\n"
process.stop

expect(process.stdout).to eq "hello\n"
end

it 'writes multiple strings to the process' do
process.start
process.write 'hel', "lo\n"
process.stop

expect(process.stdout).to eq "hello\n"
end
end

describe "#stop" do
before { process.start }

Expand Down