forked from EugenMayer/docker-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor command execution mocking helper to be more self-contained a…
…nd to be allowed per rspec context
- Loading branch information
1 parent
4f771ed
commit e9795fc
Showing
2 changed files
with
41 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters