Skip to content

Commit

Permalink
Switch to Cucumber standard MessageSpies cop configuration
Browse files Browse the repository at this point in the history
This replace #have_received style mocking with #received style, which is
the style used in other Ruby code in the Cucumber project
  • Loading branch information
mvz committed Oct 20, 2024
1 parent 3fe7c6e commit b1a4d27
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Metrics/BlockLength:
- '**/*.gemspec'
- 'spec/**/*'

# Stylistic preference for cucumber
RSpec/MessageSpies:
EnforcedStyle: receive

# This cop does not work properly with predicates that take arguments.
# TODO: Re-enable once https://github.com/rubocop/rubocop-rspec/issues/466 is resolved.
RSpec/PredicateMatcher:
Expand Down
6 changes: 2 additions & 4 deletions spec/aruba/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
end

it 'announces to stdout exactly once' do
expect(@aruba.aruba.announcer).to receive(:announce).with(:stdout).once
@aruba.run_command_and_stop('echo "hello world"', fail_on_error: false)

aggregate_failures do
expect(@aruba.last_command_started.output).to include('hello world')
expect(@aruba.aruba.announcer).to have_received(:announce).with(:stdout).once
end
expect(@aruba.last_command_started.output).to include('hello world')
end
end

Expand Down
51 changes: 20 additions & 31 deletions spec/aruba/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,86 @@
require 'spec_helper'

RSpec.describe Aruba::Command do
let(:event_bus) { instance_double(Aruba::EventBus) }
let(: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
)
described_class.new('true',
event_bus: event_bus,
startup_wait_time: 0.01, io_wait_timeout: 0.01, exit_timeout: 0.01,
working_directory: File.expand_path('.'),
environment: ENV.to_hash,
main_class: nil, stop_signal: nil)
end

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

describe '#start' do
before do
it 'leaves the command in the started state' do
allow(event_bus).to receive(:notify).with(Aruba::Events::CommandStarted)
command.start
expect(command).to be_started
end

it 'leaves the command in the started state' do
expect(command).to be_started
it 'notifies the event bus' do
expect(event_bus).to receive(:notify).with(Aruba::Events::CommandStarted)
command.start
end
end

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

it 'stops the command' do
allow(event_bus).to receive(:notify).with(Aruba::Events::CommandStopped)
command.stop
expect(command).to be_stopped
end

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

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

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

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

it 'stops the command' do
allow(event_bus).to receive(:notify).with(Aruba::Events::CommandStopped)
command.terminate
expect(command).to be_stopped
end

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

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

it 'prevents #stop from notifying the event bus' do
expect(event_bus).to receive(:notify).with(Aruba::Events::CommandStopped).once
command.terminate
command.stop
expect(event_bus).to have_received(:notify).with(Aruba::Events::CommandStopped).once
end
end
end
2 changes: 1 addition & 1 deletion spec/aruba/matchers/command/have_output_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

it 'emits a deprecation warning' do
aggregate_failures do
expect(Aruba.platform).to receive(:deprecated)
expect(obj).to have_output_size 6
expect(Aruba.platform).to have_received(:deprecated)
end
end
end
Expand Down
16 changes: 7 additions & 9 deletions spec/aruba/processes/spawn_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,18 @@
let(:command_path) { '/path with space/foo' }

it 'passes the command path as a single string to ProcessRunner.new' do
expect(Aruba::Processes::ProcessRunner).to receive(:new).with([command_path])
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new).with([command_path])
end
end

context 'with a command with arguments' do
let(:command_line) { 'foo -x "bar baz"' }

it 'passes the command and arguments as separate items to ProcessRunner.new' do
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
expect(Aruba::Processes::ProcessRunner).to receive(:new)
.with([command_path, '-x', 'bar baz'])
process.start
end
end
end
Expand Down Expand Up @@ -293,29 +293,27 @@

context 'with a command without a space in the path' do
it 'passes the command as-is' do
expect(Aruba::Processes::ProcessRunner).to receive(:new).with([command_path])
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
.with([command_path])
end
end

context 'with a command with a space in the path' do
let(:command_path) { 'D:\Bar Baz\foo' }

it 'passes the command as-is' do
expect(Aruba::Processes::ProcessRunner).to receive(:new).with([command_path])
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
.with([command_path])
end
end

context 'with a command with arguments' do
let(:command_line) { "foo -x 'bar \"baz\"'" }

it 'passes the command and arguments individually' do
process.start
expect(Aruba::Processes::ProcessRunner).to have_received(:new)
expect(Aruba::Processes::ProcessRunner).to receive(:new)
.with([command_path, '-x', 'bar "baz"'])
process.start
end
end
end
Expand Down

0 comments on commit b1a4d27

Please sign in to comment.