Skip to content

Commit 89e7bda

Browse files
committed
Make LintRoller::Plugin sortable
Typically, it is expected that `LintRoller::Plugin#about` includes a name, and sorting would be performed based on that name. However, currently, executing `sort` on LintRoller results in the following error: ```console ArgumentError: comparison of LintRoller::PluginTest::SampleRoller with LintRoller::PluginTest::AnotherRoller failed ``` This PR makes `LintRoller::Plugin` sortable by name. I considered including the version as well, but I could not come up with any use cases where multiple plugins with the same name would need to be processed simultaneously.
1 parent 7c1dc26 commit 89e7bda

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/lint_roller/plugin.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ def supported?(context)
1818
def rules(context)
1919
raise Error.new("Please implement `rules(context)` and return an instance of LintRoller::Rules")
2020
end
21+
22+
def <=>(other)
23+
about.name <=> other.about.name
24+
end
2125
end
2226
end

test/lib/plugin_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ def rules(context)
4545
end
4646
end
4747

48+
class AnotherRoller < Plugin
49+
ABOUT = About.new(
50+
name: "another-roller",
51+
version: "1.2.3",
52+
homepage: "https://example.com",
53+
description: "A sample lint roller for sort testing"
54+
).freeze
55+
56+
def about
57+
ABOUT
58+
end
59+
60+
def supported?(context)
61+
[:standard, :rubocop].include?(context.runner)
62+
end
63+
64+
def rules(context)
65+
if @config[:💥] == true
66+
Rules.new(error: Error.new("Unexpected Boom"))
67+
else
68+
Rules.new(
69+
type: :path,
70+
config_format: :rubocop,
71+
value: "/some/path/to/a/place"
72+
)
73+
end
74+
end
75+
end
76+
4877
def test_sample_roller
4978
sample_roller = SampleRoller.new(:some_config)
5079

@@ -67,5 +96,12 @@ def test_sample_roller
6796
error: Error.new("Unexpected Boom")
6897
), SampleRoller.new({:💥 => true}).rules(Context.new)
6998
end
99+
100+
def test_sort_sample_roller_with_another_roller
101+
sample_roller = SampleRoller.new(:some_config)
102+
another_roller = AnotherRoller.new(:some_config)
103+
104+
assert_equal [another_roller, sample_roller], [sample_roller, another_roller].sort
105+
end
70106
end
71107
end

0 commit comments

Comments
 (0)