-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ignore offences from YML file using load_ignore_config
- Loading branch information
1 parent
c823cc7
commit d3a27ff
Showing
6 changed files
with
143 additions
and
0 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
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
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Isolator | ||
# Add .load_ignore_config function for ignoring patterns from file | ||
module Ignorer | ||
def load_ignore_config(path) | ||
return unless File.exist?(path) | ||
|
||
todos = YAML.load_file(path) | ||
|
||
adapters.each do |id, adapter| | ||
ignored_paths = todos.fetch(id, []) | ||
configure_adapter(adapter, ignored_paths) | ||
end | ||
end | ||
|
||
private | ||
|
||
def configure_adapter(adapter, ignored_paths) | ||
ignores = build_ignore_list(ignored_paths) | ||
return if ignores.blank? | ||
|
||
regex = Regexp.new("^.*(#{ignores.join('|')}):.*$") | ||
adapter.ignore_if { caller.any? { |row| regex =~ row } } | ||
end | ||
|
||
def build_ignore_list(ignored_paths) | ||
ignored_paths.each_with_object([]) do |path, result| | ||
ignored_files = Dir[path] | ||
|
||
if ignored_files.blank? | ||
result << path.to_s | ||
else | ||
result.concat(ignored_files) | ||
end | ||
end | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "Ignorer" do | ||
TODO_PATH = ".isolator_todo.yml" | ||
|
||
before(:all) do | ||
module ::Isolator::Danger # rubocop:disable Style/ClassAndModuleChildren | ||
def self.call_masked(a, b) | ||
a + b | ||
end | ||
|
||
def self.call_unmasked(a, b) | ||
a + b | ||
end | ||
end | ||
|
||
Isolator.isolate :todo_masked_adapter, | ||
target: ::Isolator::Danger.singleton_class, | ||
method_name: :call_masked | ||
|
||
Isolator.isolate :todo_unmasked_adapter, | ||
target: ::Isolator::Danger.singleton_class, | ||
method_name: :call_unmasked | ||
end | ||
|
||
after(:all) do | ||
Isolator.send(:remove_const, "Danger") | ||
Isolator.adapters.delete("todo_masked_adapter") | ||
Isolator.adapters.delete("todo_unmasked_adapter") | ||
end | ||
|
||
before do | ||
allow(Isolator).to receive(:within_transaction?) { true } | ||
end | ||
|
||
subject { ::Isolator::Danger } | ||
|
||
specify do | ||
expect { subject.call_masked(1, 2) }.to raise_error(Isolator::UnsafeOperationError) | ||
expect { subject.call_unmasked(1, 2) }.to raise_error(Isolator::UnsafeOperationError) | ||
end | ||
|
||
context "unmasked todos" do | ||
before(:each) do | ||
allow(File).to receive(:exist?).with(TODO_PATH).and_return(true) | ||
|
||
allow(YAML).to receive(:load_file).with(TODO_PATH).and_return( | ||
"todo_unmasked_adapter" => ["spec/isolator/ignorer_spec.rb:58"], | ||
"wrong_adapter" => ["spec/isolator/ignorer_spec.rb:62"] | ||
) | ||
|
||
Isolator.load_ignore_config(TODO_PATH) | ||
end | ||
|
||
it "doesn't raise when ignored" do | ||
expect { subject.call_unmasked(1, 2) }.not_to raise_error | ||
end | ||
|
||
it "raise when wrong operator is ignored" do | ||
expect { subject.call_unmasked(1, 2) }.to raise_error | ||
end | ||
end | ||
|
||
context "masked todos" do | ||
before(:each) do | ||
allow(File).to receive(:exist?).with(TODO_PATH).and_return(true) | ||
|
||
allow(YAML).to receive(:load_file).with(TODO_PATH).and_return( | ||
"todo_masked_adapter" => ["spec/isolator/**/*.rb"] | ||
) | ||
|
||
Isolator.load_ignore_config(TODO_PATH) | ||
end | ||
|
||
it "doesn't raise when ignored via mask" do | ||
expect { subject.call_masked(1, 2) }.not_to raise_error | ||
end | ||
end | ||
end |