Skip to content

Commit

Permalink
Fix: don't prevent referencing/reopening constants that match partially
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Sep 4, 2021
1 parent 5f0eead commit 582e66d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def validate(parsed_command)
private
def contains_invalid_const_reference?(parsed_command, banned_constants)
parsed_command.constants.find do |constant_name|
banned_constants.find { |banned_constant| constant_name.start_with?(banned_constant.to_s) }
banned_constants.find { |banned_constant| "#{constant_name}::".start_with?("#{banned_constant}::") }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def contains_invalid_class_or_module_declaration?(parsed_command)

def banned?(class_or_module_name)
@banned_class_or_module_names.find do |banned_class_or_module_name|
class_or_module_name.start_with?(banned_class_or_module_name)
"#{class_or_module_name}::".start_with?("#{banned_class_or_module_name}::")
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class ForbiddenConstantReferenceValidationTest < ActiveSupport::TestCase
RUBY
end

test "doesn't prevent referencing constants that only match partially" do
run_validation <<~RUBY, always: ["SomeClass"]
SomeClass2.some_method
RUBY
end

private
def run_validation(command, shield: Console1984.shield, always: [], protected: [])
validation = Console1984::CommandValidator::ForbiddenConstantReferenceValidation.new \
Expand Down
7 changes: 7 additions & 0 deletions test/command_validator/forbidden_reopening_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ module Some::Base::Class
end
end

test "doesn't prevent reopening classes when the constant is a partial match" do
run_validation <<~RUBY, ["SomeClass"]
class SomeClass2
end
RUBY
end

private
def run_validation(command, banned_classes_or_modules)
validation = Console1984::CommandValidator::ForbiddenReopeningValidation.new(banned_classes_or_modules)
Expand Down

0 comments on commit 582e66d

Please sign in to comment.