Skip to content

Commit

Permalink
Make Plugin class sortable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koic committed Feb 4, 2025
1 parent 7c1dc26 commit aecdd2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/lint_roller/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ def supported?(context)
def rules(context)
raise Error.new("Please implement `rules(context)` and return an instance of LintRoller::Rules")
end

def <=>(other)
about.name <=> other.about.name
end
end
end
36 changes: 36 additions & 0 deletions test/lib/plugin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ def rules(context)
end
end

class AnotherRoller < Plugin
ABOUT = About.new(
name: "another-roller",
version: "1.2.3",
homepage: "https://example.com",
description: "A sample lint roller for sort testing"
).freeze

def about
ABOUT
end

def supported?(context)
[:standard, :rubocop].include?(context.runner)
end

def rules(context)
if @config[:💥] == true
Rules.new(error: Error.new("Unexpected Boom"))
else
Rules.new(
type: :path,
config_format: :rubocop,
value: "/some/path/to/a/place"
)
end
end
end

def test_sample_roller
sample_roller = SampleRoller.new(:some_config)

Expand All @@ -67,5 +96,12 @@ def test_sample_roller
error: Error.new("Unexpected Boom")
), SampleRoller.new({:💥 => true}).rules(Context.new)
end

def test_sort_sample_roller_with_another_roller
sample_roller = SampleRoller.new(:some_config)
another_roller = AnotherRoller.new(:some_config)

assert_equal [another_roller, sample_roller], [sample_roller, another_roller].sort
end
end
end

0 comments on commit aecdd2e

Please sign in to comment.