-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 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
38 changes: 38 additions & 0 deletions
38
lib/rubocop/cop/discourse/plugins/check_plugin_is_enabled.rb
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Discourse | ||
module Plugins | ||
class CheckPluginIsEnabled < Base | ||
MSG = "Check the plugin is enabled." | ||
|
||
def_node_matcher :existing_class?, <<~MATCHER | ||
(class (const (cbase) _) ...) | ||
MATCHER | ||
|
||
def_node_matcher :plugin_enabled_check?, <<~MATCHER | ||
(def _ _ (begin (if (send (const nil? :SiteSetting) /_enabled\\?$/) nil? (return)) ...)) | ||
MATCHER | ||
|
||
def on_def(node) | ||
return unless in_plugin_rb_file? | ||
return unless in_existing_class?(node) | ||
return if plugin_enabled_check?(node) | ||
add_offense(node, message: MSG) | ||
end | ||
|
||
private | ||
|
||
def in_plugin_rb_file? | ||
processed_source.path.split("/").last == "plugin.rb" | ||
end | ||
|
||
def in_existing_class?(node) | ||
node.each_ancestor(:class).detect(&method(:existing_class?)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
68 changes: 68 additions & 0 deletions
68
spec/lib/rubocop/cop/plugins/check_plugin_is_enabled_spec.rb
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe RuboCop::Cop::Discourse::Plugins::CheckPluginIsEnabled, | ||
:config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
context "when outside `plugin.rb`" do | ||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY, "my_class.rb") | ||
class ::MyClass | ||
def my_method | ||
"my_value" | ||
end | ||
end | ||
class AnotherClass | ||
def my_method | ||
"my_value" | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when inside `plugin.rb`" do | ||
context "when opening an existing class" do | ||
it "registers an offense" do | ||
expect_offense(<<~RUBY, "plugin.rb") | ||
after_initialize do | ||
class ::Post | ||
after_commit :generate_zendesk_ticket, on: [:create] | ||
private | ||
def generate_zendesk_ticket | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/CheckPluginIsEnabled: Check the plugin is enabled. | ||
return unless DiscourseZendeskPlugin::Helper.autogeneration_category?(topic.category_id) | ||
Jobs.enqueue_in(5.seconds, :zendesk_job, post_id: id) | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "does not register an offense" do | ||
expect_no_offenses(<<~RUBY, "plugin.rb") | ||
after_initialize do | ||
class ::Post | ||
after_commit :generate_zendesk_ticket, on: [:create] | ||
private | ||
def generate_zendesk_ticket | ||
return unless SiteSetting.zendesk_enabled? | ||
return unless DiscourseZendeskPlugin::Helper.autogeneration_category?(topic.category_id) | ||
Jobs.enqueue_in(5.seconds, :zendesk_job, post_id: id) | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |