Skip to content

Commit

Permalink
Refactor command execution mocking helper to be more self-contained a…
Browse files Browse the repository at this point in the history
…nd to be allowed per rspec context
  • Loading branch information
michaelbaudino committed Jun 9, 2017
1 parent 4f771ed commit e9795fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
61 changes: 40 additions & 21 deletions spec/helpers/command_mocking_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
class CommandExecutionNotAllowed < ::StandardError
module CommandExecutionMock
COMMAND_EXECUTORS = %i(system exec spawn `).freeze

attr_reader :method_name, :args
def initialize(method_name, *args)
@method_name = method_name
@args = args
def self.included(_base)
define_custom_matchers
disallow_command_execution
end

def to_s
"Command execution is not allowed. Please stub the following call: #{method_name}(#{args.map(&:inspect).join(', ')})"
def define_custom_matchers
RSpec::Matchers.define :execute_nothing do
match do
COMMAND_EXECUTORS.each do |command_executor|
expect(Kernel).to_not receive(command_executor)
end
end
end
end
end

RSpec.configure do |config|
config.before(:each) do
CommandExecutionNotAllowed::COMMAND_EXECUTORS.each do |command_executor|
allow_any_instance_of(Object).to receive(command_executor).and_wrap_original do |method, *args|
raise CommandExecutionNotAllowed.new(method.name, *args)
end
allow(Kernel).to receive(command_executor).and_wrap_original do |method, *args|
raise CommandExecutionNotAllowed.new(method.name, *args)
def disallow_command_execution
RSpec.configuration.before(:each) do |example|
unless example.metadata[:command_execution].to_s == 'allowed'
COMMAND_EXECUTORS.each do |executor|
stub_command_executor(executor)
end
end
end
end
end

RSpec::Matchers.define :execute_nothing do |_expected|
match do |_actual|
CommandExecutionNotAllowed::COMMAND_EXECUTORS.each do |command_executor|
expect(Kernel).to_not receive(command_executor)
def stub_command_executor(executor)
allow_any_instance_of(Object).to receive(executor).and_wrap_original do |method, *args|
raise CommandExecutionNotAllowed.new(method.name, *args)
end
allow(Kernel).to receive(executor).and_wrap_original do |method, *args|
raise CommandExecutionNotAllowed.new(method.name, *args)
end
end

class CommandExecutionNotAllowed < ::StandardError
attr_reader :method_name, :args
def initialize(method_name, *args)
@method_name = method_name
@args = args
end

def to_s
"Command execution is not allowed. Please stub the following call: #{method_name}(#{args.map(&:inspect).join(', ')})"
end
end
end

RSpec.configure do
include CommandExecutionMock
end
2 changes: 1 addition & 1 deletion spec/integration/unison_strategy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

RSpec.describe 'Unison strategy' do
RSpec.describe 'Unison strategy', command_execution: :allowed do
include Rspec::Bash

let(:bin) { 'bin/docker-sync' }
Expand Down

0 comments on commit e9795fc

Please sign in to comment.