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

Prevent duplicate output #517

Merged
merged 11 commits into from
Feb 7, 2018
1 change: 1 addition & 0 deletions lib/aruba/api/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def run_command_and_stop(cmd, opts = {})
expect(command).to be_successfully_executed
rescue ::RSpec::Expectations::ExpectationNotMetError => e
aruba.announcer.activate(aruba.config.activate_announcer_on_command_failure)
aruba.event_bus.notify Events::CommandStopped.new(command)
raise e
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/aruba/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def initialize(command, opts = {})

# Stop command
def stop(*)
return if __getobj__.stopped?

__getobj__.stop
event_bus.notify Events::CommandStopped.new(self)

Expand All @@ -56,6 +58,8 @@ def stop(*)

# Terminate command
def terminate(*)
return if __getobj__.stopped?

__getobj__.terminate
event_bus.notify Events::CommandStopped.new(self)

Expand Down
1 change: 1 addition & 0 deletions lib/aruba/processes/debug_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.match?(mode)
end

def start
@started = true
Dir.chdir @working_directory do
Aruba.platform.with_environment(environment) do
@exit_status = system(command, *arguments) ? 0 : 1
Expand Down
2 changes: 2 additions & 0 deletions lib/aruba/processes/in_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def initialize(cmd, exit_timeout, io_wait_timeout, working_directory, environmen
def start
fail "You need to call aruba.config.main_class = YourMainClass" unless main_class

@started = true

Dir.chdir @working_directory do
before_run

Expand Down
79 changes: 79 additions & 0 deletions spec/aruba/command_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'spec_helper'

RSpec.describe Aruba::Command do
subject(:command) do
described_class.new(
'true',
event_bus: event_bus,
exit_timeout: exit_timeout,
io_wait_timeout: io_wait_timeout,
working_directory: working_directory,
environment: environment,
main_class: main_class,
stop_signal: stop_signal,
startup_wait_time: startup_wait_time
)
end

let(:event_bus) { instance_double('Aruba::EventBus') }
let(:exit_timeout) { 1 }
let(:io_wait_timeout) { 1 }
let(:working_directory) { expand_path('.') }
let(:environment) { ENV.to_hash }
let(:main_class) { nil }
let(:stop_signal) { nil }
let(:startup_wait_time) { 1 }

describe '#start' do
before do
allow(event_bus).to receive(:notify).with(Events::CommandStarted)
command.start
end

it 'leaves the command in the started state' do
expect(command).to be_started
end
end

describe '#stop' do
before do
allow(event_bus).to receive(:notify).with(Events::CommandStarted)
allow(event_bus).to receive(:notify).with(Events::CommandStopped)
command.start
command.stop
end

it { is_expected.to be_stopped }

it 'notifies the event bus only once per run' do
command.stop
expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once
end

it 'prevents #terminate from notifying the event bus' do
command.terminate
expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once
end
end

describe '#terminate' do
before do
allow(event_bus).to receive(:notify).with(Events::CommandStarted)
allow(event_bus).to receive(:notify).with(Events::CommandStopped)
command.start
command.terminate
end

it { is_expected.to be_stopped }

it 'notifies the event bus only once per run' do
command.terminate
expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once
end

it 'prevents #stop from notifying the event bus' do
command.stop
expect(event_bus).to have_received(:notify).with(Events::CommandStopped).once
end
end
end
107 changes: 107 additions & 0 deletions spec/aruba/processes/in_process_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'spec_helper'

RSpec.describe Aruba::Processes::InProcess do
class Runner
def initialize(argv, stdin, stdout, stderr, kernel)
@stdout = stdout
@stderr = stderr
end

def execute!
end
end

class StdoutRunner < Runner
def execute!
@stdout.puts 'yo'
end
end

class StderrRunner < Runner
def execute!
@stderr.puts 'yo'
end
end

class FailedRunner < Runner
def execute!
raise 'Oops'
end
end

subject(:process) do
described_class.new(command, exit_timeout, io_wait, working_directory,
environment, main_class)
end

let(:command) { 'foo' }
let(:exit_timeout) { 1 }
let(:io_wait) { 1 }
let(:working_directory) { Dir.getwd }
let(:environment) { ENV.to_hash.dup }
let(:main_class) { Runner }

describe "#stdout" do
let(:main_class) { StdoutRunner }

before do
process.start
process.stop
end

context 'when invoked once' do
it { expect(process.stdout).to eq "yo\n" }
end

context 'when invoked twice' do
it { 2.times { expect(process.stdout).to eq "yo\n" } }
end
end

describe "#stderr" do
let(:main_class) { StderrRunner }

before do
process.start
process.stop
end

context 'when invoked once' do
it { expect(process.stderr).to eq "yo\n" }
end

context 'when invoked twice' do
it { 2.times { expect(process.stderr).to eq "yo\n" } }
end
end

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

context 'when stopped successfully' do
it { expect { process.stop }.not_to raise_error }

it 'makes the process stopped' do
process.stop
expect(process).to be_stopped
end
end
end

describe "#start" do
context "when process run succeeds" do
it { expect { process.start }.not_to raise_error }

it 'makes the process started' do
process.start
expect(process).to be_started
end
end

context "when process run fails" do
let(:main_class) { FailedRunner }

it { expect { process.start }.to raise_error RuntimeError, 'Oops' }
end
end
end
12 changes: 11 additions & 1 deletion spec/aruba/processes/spawn_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@
before(:each) { process.start }

context 'when stopped successfully' do
it { process.stop }
it { expect { process.stop }.not_to raise_error }

it 'makes the process stopped' do
process.stop
expect(process).to be_stopped
end
end
end

describe "#start" do
context "when process run succeeds" do
it { expect { process.start }.not_to raise_error }

it 'makes the process started' do
process.start
expect(process).to be_started
end
end

context "when process run fails" do
Expand Down