Skip to content

Commit

Permalink
prevent BasicProcess#inspect from crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Mar 31, 2016
1 parent 65d637d commit 3502d8c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/aruba/processes/basic_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ def before_run; end
def after_run; end

def inspect
out = stdout(:wait_for_io => 0) + stderr(:wait_for_io => 0)
out = truncate("#{stdout(:wait_for_io => 0).inspect}", 35)
err = truncate("#{stderr(:wait_for_io => 0).inspect}", 35)

out = if out.length > 76
out[0, 75] + ' ...'
else
out
end

format '#<%s:%s commandline="%s": output="%s">', self.class, self.object_id, commandline, out
fmt = '#<%s:%s commandline="%s": stdout=%s stderr=%s>'
format fmt, self.class, self.object_id, commandline, out, err
end

alias to_s inspect

private
Expand All @@ -138,6 +135,11 @@ def arguments

[]
end

def truncate(string, max_length)
return string if string.length <= max_length
string[0, max_length - 1] + ' ...'
end
end
end
end
64 changes: 64 additions & 0 deletions spec/aruba/processes/basic_process_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'aruba/processes/basic_process'

RSpec.describe Aruba::Processes::BasicProcess do
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" }

subject 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

describe "#inspect" do
it "it shows useful info" do
expected = /#<#<Class:0x\h+>:\d+ commandline="foobar": stdout="foo output" stderr="foo error output"/
expect(subject.inspect).to match(expected)
end

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

it "it shows useful info" do
expected = /#<#<Class:0x\h+>:\d+ commandline="foobar": stdout=nil stderr="foo error output"/
expect(subject.inspect).to match(expected)
end
end

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

it "it shows useful info" do
expected = /#<#<Class:0x\h+>:\d+ commandline="foobar": stdout="foo output" stderr=nil/
expect(subject.inspect).to match(expected)
end
end

context "with neither stderr nor stdout" do
let(:stderr) { nil }
let(:stdout) { nil }

it "it shows useful info" do
expected = /#<#<Class:0x\h+>:\d+ commandline="foobar": stdout=nil stderr=nil/
expect(subject.inspect).to match(expected)
end
end
end
end

0 comments on commit 3502d8c

Please sign in to comment.