From 5c4fcca66d61bf59b59f1a45da2e0c6d0155721f Mon Sep 17 00:00:00 2001 From: Leo Arnold Date: Tue, 29 Nov 2022 12:25:41 +0100 Subject: [PATCH] Re-enable RuboCop --- .github/workflows/tests.yml | 21 +++++------ .rubocop.yml | 12 ++++++- Gemfile | 12 ++++--- lib/roar/json/json_api.rb | 6 ++-- lib/roar/json/json_api/declarative.rb | 2 +- lib/roar/json/json_api/single_resource.rb | 4 +-- lib/roar/json/json_api/version.rb | 2 +- rakelib/rubocop.rake | 8 +++++ test/jsonapi/collection_render_test.rb | 36 +++++++++++++++++-- test/jsonapi/fieldsets_test.rb | 4 +-- .../relationship_custom_naming_test.rb | 4 +-- test/jsonapi/render_test.rb | 15 ++++++-- test/jsonapi/representer.rb | 3 -- test/test_helper.rb | 30 +++++++++------- 14 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 rakelib/rubocop.rake diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 406d47c..cf0005a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,16 +11,17 @@ jobs: strategy: matrix: ruby: - - 1.9 - - 2.1 - - 2.2 - - 2.3 - - 2.4 - - 2.5 - - 2.6 - - 2.7 - - 3.0 - - 3.1 + - '1.9' + - '2.0' + - '2.1' + - '2.2' + - '2.3' + - '2.4' + - '2.5' + - '2.6' + - '2.7' + - '3.0' + - '3.1' experimental: - false include: diff --git a/.rubocop.yml b/.rubocop.yml index c9930f0..c598e51 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,7 @@ AllCops: TargetRubyVersion: 1.9 + DisplayCopNames: true + DisplayStyleGuide: true Style/AlignHash: EnforcedHashRocketStyle: table @@ -17,6 +19,9 @@ Style/BlockDelimiters: - it - link +Style/Documentation: + Enabled: false + Style/Lambda: Enabled: false EnforcedStyle: literal @@ -24,8 +29,12 @@ Style/Lambda: Style/LambdaCall: EnforcedStyle: braces +Metrics/ClassLength: + Exclude: + - 'test/**/*' + Metrics/LineLength: - Enabled: false + Max: 200 Metrics/MethodLength: Max: 15 @@ -35,3 +44,4 @@ Style/PredicateName: - is_a? - has_one - has_many + - has_relationship diff --git a/Gemfile b/Gemfile index b400ca8..169f577 100644 --- a/Gemfile +++ b/Gemfile @@ -5,10 +5,10 @@ end gemspec -case ENV["GEMS_SOURCE"] -when "local" - gem "roar", path: "../roar" -when "github" +case ENV['GEMS_SOURCE'] +when 'local' + gem 'roar', path: '../roar' +when 'github' gem 'roar', github: 'trailblazer/roar' end @@ -16,4 +16,8 @@ gem 'minitest-line' gem 'minitest-reporters', '<= 1.3.0' # Note 1.3.1 is broken see https://github.com/kern/minitest-reporters/issues/267 gem 'pry' +if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.6.0') + gem 'rubocop', '~> 0.41.0' # RuboCop dropped support for Ruby 1.9 after v0.41 +end + gem 'json_spec', require: false diff --git a/lib/roar/json/json_api.rb b/lib/roar/json/json_api.rb index 7a726e6..70de0a3 100644 --- a/lib/roar/json/json_api.rb +++ b/lib/roar/json/json_api.rb @@ -44,8 +44,8 @@ def included(base) base.send(:include, JSONAPI::Mixin) base.type(@type) base.property(@id_key, as: :id, render_nil: false, render_filter: ->(input, _opts) { - input.to_s - }) + input.to_s + }) end end @@ -96,7 +96,7 @@ module Mixin # # @api private # @see http://www.ruby-doc.org/core/Module.html#method-i-included - def self.included(base) + def self.included(base) # rubocop:disable Metrics/MethodLength base.class_eval do feature Roar::JSON feature Roar::Hypermedia diff --git a/lib/roar/json/json_api/declarative.rb b/lib/roar/json/json_api/declarative.rb index 3876610..b0093e5 100644 --- a/lib/roar/json/json_api/declarative.rb +++ b/lib/roar/json/json_api/declarative.rb @@ -134,7 +134,7 @@ def has_many(name, options = {}, &block) private - def has_relationship(name, options = {}, &block) + def has_relationship(name, options = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity resource_decorator = options.delete(:decorator) || options.delete(:extend) || Class.new(Roar::Decorator).tap { |decorator| diff --git a/lib/roar/json/json_api/single_resource.rb b/lib/roar/json/json_api/single_resource.rb index 629ccec..f5c7750 100644 --- a/lib/roar/json/json_api/single_resource.rb +++ b/lib/roar/json/json_api/single_resource.rb @@ -6,7 +6,7 @@ module JSONAPI # @api private module SingleResource # @see Document#to_hash - def to_hash(options = {}) + def to_hash(options = {}) # rubocop:disable Metrics/AbcSize document = super(Options::Include.(options, mappings)) unwrapped = options[:wrap] == false resource = unwrapped ? document : document['data'] @@ -43,7 +43,7 @@ def mappings end def find_id_mapping(klass) - self_id = klass.definitions.detect { |definition| + klass.definitions.detect { |definition| definition[:as] && definition[:as].(:value) == 'id' }.name end diff --git a/lib/roar/json/json_api/version.rb b/lib/roar/json/json_api/version.rb index d33b4fd..03b99d2 100644 --- a/lib/roar/json/json_api/version.rb +++ b/lib/roar/json/json_api/version.rb @@ -1,7 +1,7 @@ module Roar module JSON module JSONAPI - VERSION = '0.0.3' + VERSION = '0.0.3' # rubocop:disable Style/MutableConstant end end end diff --git a/rakelib/rubocop.rake b/rakelib/rubocop.rake new file mode 100644 index 0000000..300bfaf --- /dev/null +++ b/rakelib/rubocop.rake @@ -0,0 +1,8 @@ +begin + require 'rubocop/rake_task' + RuboCop::RakeTask.new(:rubocop) + + task default: [:rubocop] +rescue LoadError + puts 'RuboCop not available' +end diff --git a/test/jsonapi/collection_render_test.rb b/test/jsonapi/collection_render_test.rb index aee4725..ab73950 100644 --- a/test/jsonapi/collection_render_test.rb +++ b/test/jsonapi/collection_render_test.rb @@ -3,9 +3,39 @@ require 'json' class JsonapiCollectionRenderTest < MiniTest::Spec - let(:article) { Article.new(1, 'Health walk', Author.new(2, 'someone@author.com'), Author.new('editor:1'), [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], [Author.new('contributor:1'), Author.new('contributor:2')]) } - let(:article2) { Article.new(2, 'Virgin Ska', Author.new('author:1'), nil, [Comment.new('comment:3', 'Cool song!')], [Author.new('contributor:1'), Author.new('contributor:2')]) } - let(:article3) { Article.new(3, 'Gramo echo', Author.new('author:1'), nil, [Comment.new('comment:4', 'Skalar')], [Author.new('contributor:1'), Author.new('contributor:2')]) } + let(:article) do + Article.new( + 1, + 'Health walk', + Author.new(2, 'someone@author.com'), + Author.new('editor:1'), + [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], + [Author.new('contributor:1'), Author.new('contributor:2')] + ) + end + + let(:article2) do + Article.new( + 2, + 'Virgin Ska', + Author.new('author:1'), + nil, + [Comment.new('comment:3', 'Cool song!')], + [Author.new('contributor:1'), Author.new('contributor:2')] + ) + end + + let(:article3) do + Article.new( + 3, + 'Gramo echo', + Author.new('author:1'), + nil, + [Comment.new('comment:4', 'Skalar')], + [Author.new('contributor:1'), Author.new('contributor:2')] + ) + end + let(:decorator) { ArticleDecorator.for_collection.new([article, article2, article3]) } it 'renders full document' do diff --git a/test/jsonapi/fieldsets_test.rb b/test/jsonapi/fieldsets_test.rb index cb97454..83a1884 100644 --- a/test/jsonapi/fieldsets_test.rb +++ b/test/jsonapi/fieldsets_test.rb @@ -319,7 +319,7 @@ class DocumentResourceWithDifferentIdAtRoot < Roar::Decorator let(:article) { klass = Struct.new(:article_id, :title, :summary, :comments, :author) klass.new(1, 'My Article', 'An interesting read.', comments, - Author.new('a:1', 'Celso', 'celsito@trb.to')) + Author.new('a:1', 'Celso', 'celsito@trb.to')) } let(:document) { @@ -541,7 +541,7 @@ class DocumentAndRelationWithDifferentId < Roar::Decorator it do DocumentAndRelationWithDifferentId.new(article).to_json(include: 'comments') - .must_equal_json document + .must_equal_json document end end end diff --git a/test/jsonapi/relationship_custom_naming_test.rb b/test/jsonapi/relationship_custom_naming_test.rb index b88a032..f833878 100644 --- a/test/jsonapi/relationship_custom_naming_test.rb +++ b/test/jsonapi/relationship_custom_naming_test.rb @@ -24,8 +24,8 @@ class RecipeDecorator < Roar::Decorator property :name end - has_one :best_chef, as: "bestChefEver", extend: ChefDecorator - has_many :best_ingredients, as: "bestIngridients", extend: IngredientDecorator + has_one :best_chef, as: 'bestChefEver', extend: ChefDecorator + has_many :best_ingredients, as: 'bestIngridients', extend: IngredientDecorator end Recipe = Struct.new(:id, :name, :best_chef, :best_ingredients, :reviews) diff --git a/test/jsonapi/render_test.rb b/test/jsonapi/render_test.rb index e85bd47..bc04502 100644 --- a/test/jsonapi/render_test.rb +++ b/test/jsonapi/render_test.rb @@ -3,7 +3,17 @@ require 'json' class JsonapiRenderTest < MiniTest::Spec - let(:article) { Article.new(1, 'Health walk', Author.new(2), Author.new('editor:1'), [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], [Author.new('contributor:1'),Author.new('contributor:2')]) } + let(:article) do + Article.new( + 1, + 'Health walk', + Author.new(2), + Author.new('editor:1'), + [Comment.new('comment:1', 'Ice and Snow'), Comment.new('comment:2', 'Red Stripe Skank')], + [Author.new('contributor:1'), Author.new('contributor:2')] + ) + end + let(:decorator) { ArticleDecorator.new(article) } it 'renders full document' do @@ -311,7 +321,6 @@ class VisualArtistDecorator < Roar::Decorator it { VisualArtistDecorator.for_collection.new([painter]).to_json.must_equal_json collection_document } end - describe 'null/ empty attributes render correctly' do class ArtistDecorator < Roar::Decorator include Roar::JSON::JSONAPI.resource :artists @@ -324,7 +333,7 @@ class ArtistDecorator < Roar::Decorator property :genre, render_nil: false # tests that we can override default setting end - link(:self) { "http://artists/#{represented.id}" } + link(:self) { "http://artists/#{represented.id}" } end Painter = Struct.new(:id, :name, :known_aliases, :movement, :noteable_works, :genre) diff --git a/test/jsonapi/representer.rb b/test/jsonapi/representer.rb index a9feca2..60f3509 100644 --- a/test/jsonapi/representer.rb +++ b/test/jsonapi/representer.rb @@ -75,7 +75,6 @@ class ArticleDecorator < Roar::Decorator # relationships has_one :author, class: Author, decorator: AuthorDecorator, populator: ::Representable::FindOrInstantiate do # populator is for parsing, only. - relationship do link(:self) { "/articles/#{represented.id}/relationships/author" } link(:related) { "/articles/#{represented.id}/author" } @@ -99,7 +98,6 @@ class ArticleDecorator < Roar::Decorator has_many :comments, class: Comment, decorator: CommentDecorator, populator: ::Representable::FindOrInstantiate do - relationship do link(:self) { "/articles/#{represented.id}/relationships/comments" } link(:related) { "/articles/#{represented.id}/comments" } @@ -110,7 +108,6 @@ class ArticleDecorator < Roar::Decorator end end - # this relationship should be listed in relationships but no data included/sideloaded has_many :contributors, class: Author, included: false do type :authors diff --git a/test/test_helper.rb b/test/test_helper.rb index 9d13633..b0b42fb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -24,19 +24,25 @@ module JsonSpec self.excluded_keys = [] end -module MiniTest::Assertions - def assert_equal_json(actual, expected) - assert_equal scrub(actual), scrub(expected) - end - def scrub(json, path = nil) - JsonSpec::Helpers.generate_normalized_json( - JsonSpec::Exclusion.exclude_keys( - JsonSpec::Helpers.parse_json(json, path) - ) - ).chomp + "\n" +module MiniTest + module Assertions + def assert_equal_json(actual, expected) + assert_equal scrub(actual), scrub(expected) + end + + def scrub(json, path = nil) + JsonSpec::Helpers.generate_normalized_json( + JsonSpec::Exclusion.exclude_keys( + JsonSpec::Helpers.parse_json(json, path) + ) + ).chomp + "\n" + end end end -module Minitest::Expectations - infect_an_assertion :assert_equal_json, :must_equal_json + +module Minitest + module Expectations + infect_an_assertion :assert_equal_json, :must_equal_json + end end