Skip to content

Commit 1edd59b

Browse files
rcarverdefunkt
authored andcommitted
simple linting to ensure your plugin names its hooks uniquely
1 parent eabb89f commit 1edd59b

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

lib/resque/plugin.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ module Resque
22
module Plugin
33
extend self
44

5+
LintError = Class.new(RuntimeError)
6+
7+
# Ensure that your plugin conforms to good hook naming conventions.
8+
#
9+
# Resque::Plugin.lint(MyResquePlugin)
10+
def lint(plugin)
11+
(before_hooks(plugin) + around_hooks(plugin) + after_hooks(plugin)).each do |hook|
12+
raise LintError, "#{plugin}.#{hook} is not namespaced" if hook =~ /perform$/
13+
end
14+
failure_hooks(plugin).each do |hook|
15+
raise LintError, "#{plugin}.#{hook} is not namespaced" if hook =~ /failure$/
16+
end
17+
end
18+
519
def before_hooks(job)
620
job.methods.grep(/^before_perform/).sort
721
end
@@ -17,6 +31,5 @@ def after_hooks(job)
1731
def failure_hooks(job)
1832
job.methods.grep(/^on_failure/).sort
1933
end
20-
2134
end
22-
end
35+
end

test/plugin_test.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,82 @@ def on_failure2; end
3535
end
3636
end
3737

38+
context "Resque::Plugin linting" do
39+
module BadBefore
40+
def self.before_perform; end
41+
end
42+
module BadAfter
43+
def self.after_perform; end
44+
end
45+
module BadAround
46+
def self.around_perform; end
47+
end
48+
module BadFailure
49+
def self.on_failure; end
50+
end
51+
52+
test "before_perform must be namespaced" do
53+
begin
54+
Resque::Plugin.lint(BadBefore)
55+
assert false, "should have failed"
56+
rescue Resque::Plugin::LintError => e
57+
assert_equal "BadBefore.before_perform is not namespaced", e.message
58+
end
59+
end
60+
61+
test "after_perform must be namespaced" do
62+
begin
63+
Resque::Plugin.lint(BadAfter)
64+
assert false, "should have failed"
65+
rescue Resque::Plugin::LintError => e
66+
assert_equal "BadAfter.after_perform is not namespaced", e.message
67+
end
68+
end
69+
70+
test "around_perform must be namespaced" do
71+
begin
72+
Resque::Plugin.lint(BadAround)
73+
assert false, "should have failed"
74+
rescue Resque::Plugin::LintError => e
75+
assert_equal "BadAround.around_perform is not namespaced", e.message
76+
end
77+
end
78+
79+
test "on_failure must be namespaced" do
80+
begin
81+
Resque::Plugin.lint(BadFailure)
82+
assert false, "should have failed"
83+
rescue Resque::Plugin::LintError => e
84+
assert_equal "BadFailure.on_failure is not namespaced", e.message
85+
end
86+
end
87+
88+
module GoodBefore
89+
def self.before_perform1; end
90+
end
91+
module GoodAfter
92+
def self.after_perform1; end
93+
end
94+
module GoodAround
95+
def self.around_perform1; end
96+
end
97+
module GoodFailure
98+
def self.on_failure1; end
99+
end
100+
101+
test "before_perform1 is an ok name" do
102+
Resque::Plugin.lint(GoodBefore)
103+
end
104+
105+
test "after_perform1 is an ok name" do
106+
Resque::Plugin.lint(GoodAfter)
107+
end
108+
109+
test "around_perform1 is an ok name" do
110+
Resque::Plugin.lint(GoodAround)
111+
end
112+
113+
test "on_failure1 is an ok name" do
114+
Resque::Plugin.lint(GoodFailure)
115+
end
116+
end

0 commit comments

Comments
 (0)