From 582e66d72b05388b1f5ccb54cf34bcc179219ad5 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Sat, 4 Sep 2021 17:37:15 +0200 Subject: [PATCH] Fix: don't prevent referencing/reopening constants that match partially --- .../forbidden_constant_reference_validation.rb | 2 +- .../command_validator/forbidden_reopening_validation.rb | 2 +- .../forbidden_constant_reference_validation_test.rb | 6 ++++++ .../forbidden_reopening_validation_test.rb | 7 +++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/console1984/command_validator/forbidden_constant_reference_validation.rb b/lib/console1984/command_validator/forbidden_constant_reference_validation.rb index 64a06d2..62670a9 100644 --- a/lib/console1984/command_validator/forbidden_constant_reference_validation.rb +++ b/lib/console1984/command_validator/forbidden_constant_reference_validation.rb @@ -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 diff --git a/lib/console1984/command_validator/forbidden_reopening_validation.rb b/lib/console1984/command_validator/forbidden_reopening_validation.rb index c4aff51..2cedd4c 100644 --- a/lib/console1984/command_validator/forbidden_reopening_validation.rb +++ b/lib/console1984/command_validator/forbidden_reopening_validation.rb @@ -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 diff --git a/test/command_validator/forbidden_constant_reference_validation_test.rb b/test/command_validator/forbidden_constant_reference_validation_test.rb index 7246aa8..b7da8db 100644 --- a/test/command_validator/forbidden_constant_reference_validation_test.rb +++ b/test/command_validator/forbidden_constant_reference_validation_test.rb @@ -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 \ diff --git a/test/command_validator/forbidden_reopening_validation_test.rb b/test/command_validator/forbidden_reopening_validation_test.rb index 292c6de..2265d26 100644 --- a/test/command_validator/forbidden_reopening_validation_test.rb +++ b/test/command_validator/forbidden_reopening_validation_test.rb @@ -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)