Commit cd68bb18 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Simply the implementation and fix tests

We can much simply this now because call sites are no longer using long
form or other aliased names.

For `RuboCop::Cop::InjectEnterpriseEditionModule`, we change it that:

* `prepend_mod_with` must be used for extension modules, therefore
  we don't need to allow using it on regular modules.
parent d503dbe8
......@@ -51,4 +51,4 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
# rubocop: enable CodeReuse/ActiveRecord
end
Groups::AutocompleteSourcesController.prepend_if_ee('EE::Groups::AutocompleteSourcesController')
Groups::AutocompleteSourcesController.prepend_mod
......@@ -139,4 +139,4 @@ module Nav
end
end
Nav::TopNavHelper.prepend_ee_mod
Nav::TopNavHelper.prepend_mod
......@@ -105,4 +105,4 @@ module Terraform
end
end
Terraform::State.prepend_if_ee('EE::Terraform::State')
Terraform::State.prepend_mod
......@@ -46,4 +46,4 @@ module Groups
end
end
Groups::AutocompleteService.prepend_if_ee('EE::Groups::AutocompleteService')
Groups::AutocompleteService.prepend_mod
......@@ -145,4 +145,4 @@ module Namespaces
end
end
Namespaces::InProductMarketingEmailsService.prepend_ee_mod
Namespaces::InProductMarketingEmailsService.prepend_mod
......@@ -3,47 +3,40 @@
require 'active_support/inflector'
module InjectEnterpriseEditionModule
def prepend_mod_with(constant_with_prefix, namespace: Object, with_descendants: false)
prepend_mod_for(
constant_without_prefix(constant_with_prefix),
namespace: namespace,
with_descendants: with_descendants)
def prepend_mod_with(constant_name, namespace: Object, with_descendants: false)
each_extension_for(constant_name, namespace) do |constant|
prepend_module(constant, with_descendants)
end
end
def extend_mod_with(constant_with_prefix, namespace: Object)
def extend_mod_with(constant_name, namespace: Object)
each_extension_for(
constant_without_prefix(constant_with_prefix),
constant_name,
namespace,
&method(:extend))
end
def include_mod_with(constant_with_prefix, namespace: Object)
def include_mod_with(constant_name, namespace: Object)
each_extension_for(
constant_without_prefix(constant_with_prefix),
constant_name,
namespace,
&method(:include))
end
def prepend_mod(with_descendants: false)
prepend_mod_for(name, with_descendants: with_descendants)
prepend_mod_with(name, with_descendants: with_descendants) # rubocop: disable Cop/InjectEnterpriseEditionModule
end
def extend_mod
each_extension_for(name, Object, &method(:extend))
extend_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
end
def include_mod
each_extension_for(name, Object, &method(:include))
include_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
end
private
def prepend_mod_for(constant_name, namespace: Object, with_descendants: false)
each_extension_for(constant_name, namespace) do |constant|
prepend_module(constant, with_descendants)
end
end
def prepend_module(mod, with_descendants)
prepend(mod)
......@@ -75,12 +68,6 @@ module InjectEnterpriseEditionModule
rescue NameError
false
end
def constant_without_prefix(constant_with_prefix)
constant_with_prefix
.delete_prefix('::') # TODO: Some calling sites are passing this prefix
.delete_prefix('EE::')
end
end
Module.prepend(InjectEnterpriseEditionModule)
......@@ -175,4 +175,4 @@ module Banzai
end
end
Banzai::Filter::References::ReferenceCache.prepend_if_ee('EE::Banzai::Filter::References::ReferenceCache')
Banzai::Filter::References::ReferenceCache.prepend_mod
......@@ -2,16 +2,16 @@
module RuboCop
module Cop
# Cop that blacklists the injecting of EE specific modules before any lines which are not already injecting another module.
# Cop that blacklists the injecting of extension specific modules before any lines which are not already injecting another module.
# It allows multiple module injections as long as they're all at the end.
class InjectEnterpriseEditionModule < RuboCop::Cop::Cop
INVALID_LINE = 'Injecting EE modules must be done on the last line of this file' \
INVALID_LINE = 'Injecting extension modules must be done on the last line of this file' \
', outside of any class or module definitions'
DISALLOWED_METHOD =
'EE modules must be injected using `include_mod_with`, `extend_mod_with`, or `prepend_mod_with`'
INVALID_ARGUMENT = 'EE modules to inject must be specified as a String'
INVALID_ARGUMENT = 'extension modules to inject must be specified as a String'
CHECK_LINE_METHODS =
Set.new(%i[include_mod_with extend_mod_with prepend_mod_with]).freeze
......@@ -67,10 +67,10 @@ module RuboCop
def check_method?(node)
name = node.children[1]
if CHECK_LINE_METHODS.include?(name) || DISALLOW_METHODS.include?(name)
if DISALLOW_METHODS.include?(name)
ee_const?(node.children[2])
else
false
CHECK_LINE_METHODS.include?(name)
end
end
......
......@@ -28,19 +28,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name)
end
it "ignores EE prefix and calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", "EE::#{fish_name}")
end
it "ignores ::EE prefix and calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", "::EE::#{fish_name}")
fish_class.__send__("#{method}_mod_with", fish_name)
end
end
......@@ -55,7 +43,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "calls #{method} with the extension module from the additional namespace" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name, namespace: another_namespace)
fish_class.__send__("#{method}_mod_with", fish_name, namespace: another_namespace)
end
end
......@@ -67,7 +55,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "does not call #{method}" do
expect(fish_class).not_to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name)
fish_class.__send__("#{method}_mod_with", fish_name)
end
end
......@@ -75,7 +63,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "does not call #{method}" do
expect(fish_class).not_to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name)
fish_class.__send__("#{method}_mod_with", fish_name)
end
end
end
......
......@@ -6,129 +6,33 @@ require_relative '../../../rubocop/cop/inject_enterprise_edition_module'
RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
subject(:cop) { described_class.new }
it 'flags the use of `prepend_mod_with EE` in the middle of a file' do
it 'flags the use of `prepend_mod_with` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
prepend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `prepend_mod_with QA::EE` in the middle of a file' do
it 'flags the use of `include_mod_with` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with 'QA::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
include_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'does not flag the use of `prepend_mod_with EEFoo` in the middle of a file' do
expect_no_offenses(<<~SOURCE)
class Foo
prepend_mod_with 'EEFoo'
end
SOURCE
end
it 'flags the use of `prepend_mod_with EE::Foo::Bar` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with 'EE::Foo::Bar'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `prepend_mod_with(EE::Foo::Bar)` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with('Foo::Bar')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `prepend_mod_with EE::Foo::Bar::Baz` in the middle of a file' do
it 'flags the use of `extend_mod_with` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with 'EE::Foo::Bar::Baz'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `prepend_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `include_mod_with EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
include_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `include_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
include_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `extend_mod_with EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
extend_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'flags the use of `extend_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
extend_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'does not flag prepending of regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
prepend_mod_with 'Foo'
end
SOURCE
end
it 'does not flag including of regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
include_mod_with 'Foo'
end
SOURCE
end
it 'does not flag extending using regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
extend_mod_with 'Foo'
extend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'does not flag the use of `prepend_mod_with EE` on the last line' do
it 'does not flag the use of `prepend_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE)
class Foo
end
......@@ -137,7 +41,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE
end
it 'does not flag the use of `include_mod_with EE` on the last line' do
it 'does not flag the use of `include_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE)
class Foo
end
......@@ -146,7 +50,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE
end
it 'does not flag the use of `extend_mod_with EE` on the last line' do
it 'does not flag the use of `extend_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE)
class Foo
end
......@@ -155,7 +59,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE
end
it 'does not flag the double use of `X_if_ee` on the last line' do
it 'does not flag the double use of `X_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE)
class Foo
end
......@@ -166,7 +70,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE
end
it 'does not flag the use of `prepend_mod_with EE` as long as all injections are at the end of the file' do
it 'does not flag the use of `prepend_mod_with` as long as all injections are at the end of the file' do
expect_no_offenses(<<~SOURCE)
class Foo
end
......@@ -183,21 +87,21 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
it 'autocorrects offenses by just disabling the Cop' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
include_mod_with 'Bar'
prepend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
include Bar
end
SOURCE
expect_correction(<<~SOURCE)
class Foo
prepend_mod_with 'EE::Foo' # rubocop: disable Cop/InjectEnterpriseEditionModule
include_mod_with 'Bar'
prepend_mod_with('Foo') # rubocop: disable Cop/InjectEnterpriseEditionModule
include Bar
end
SOURCE
end
it 'disallows the use of prepend to inject an EE module' do
it 'disallows the use of prepend to inject an extension module' do
expect_offense(<<~SOURCE)
class Foo
end
......@@ -242,8 +146,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo
end
Foo.prepend_mod_with(EE::Foo)
^^^^^^^ EE modules to inject must be specified as a String
Foo.prepend_mod_with(Foo)
^^^ extension modules to inject must be specified as a String
SOURCE
end
......@@ -252,8 +156,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo
end
Foo.include_mod_with(EE::Foo)
^^^^^^^ EE modules to inject must be specified as a String
Foo.include_mod_with(Foo)
^^^ extension modules to inject must be specified as a String
SOURCE
end
......@@ -262,8 +166,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo
end
Foo.extend_mod_with(EE::Foo)
^^^^^^^ EE modules to inject must be specified as a String
Foo.extend_mod_with(Foo)
^^^ extension modules to inject must be specified as a String
SOURCE
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment