diff --git a/.rubocop.yml b/.rubocop.yml index eeeea3dc4..b948f7af3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,6 +56,10 @@ Layout/DotPosition: StyleGuide: '#consistent-multi-line-chains' EnforcedStyle: leading +Lint/EmptyInPattern: + Description: 'Checks for the presence of `in` pattern branches without a body.' + Enabled: true + Layout/EmptyLinesAroundAttributeAccessor: Description: "Keep blank lines around attribute accessors." StyleGuide: '#empty-lines-around-attribute-accessor' @@ -536,6 +540,11 @@ Style/IfWithBooleanLiteralBranches: # Description: 'Avoid trailing inline comments.' # Enabled: false +Style/InPatternThen: + Description: 'Checks for `in;` uses in `case` expressions.' + StyleGuide: '#no-in-pattern-semicolons' + Enabled: true + # Style/IpAddresses: # Description: "Don't include literal IP addresses in code." # Enabled: false @@ -550,12 +559,25 @@ Style/IfWithBooleanLiteralBranches: # StyleGuide: '#single-line-blocks' # Enabled: false +Style/MultilineInPatternThen: + Description: 'Do not use `then` for multi-line `in` statement.' + Enabled: true + Style/NegatedIfElseCondition: Description: >- This cop checks for uses of `if-else` and ternary operators with a negated condition which can be simplified by inverting condition and swapping branches. Enabled: true +Style/Next: + Description: 'Use `next` to skip iteration instead of a condition at the end.' + StyleGuide: '#no-nested-conditionals' + Enabled: true + +Style/NilLambda: + Description: 'Prefer `-> {}` to `-> { nil }`.' + Enabled: true + Style/OptionalBooleanParameter: Description: 'Use keyword arguments when defining method with boolean argument.' StyleGuide: '#boolean-keyword-arguments' @@ -565,13 +587,8 @@ Style/OptionHash: Description: "Don't use option hashes when you can use keyword arguments." Enabled: true -Style/Next: - Description: 'Use `next` to skip iteration instead of a condition at the end.' - StyleGuide: '#no-nested-conditionals' - Enabled: true - -Style/NilLambda: - Description: 'Prefer `-> {}` to `-> { nil }`.' +Style/QuotedSymbols: + Description: 'Use a consistent style for quoted symbols.' Enabled: true Style/RedundantArgument: diff --git a/Gemfile b/Gemfile index 2ea18cd8b..9d0ed8db2 100644 --- a/Gemfile +++ b/Gemfile @@ -147,7 +147,7 @@ group :development, :test do gem "knapsack_pro", "~> 2.17.0" # Help parallelize Ruby spec builds gem "pry-byebug", "~> 3.8" # Combine 'pry' with 'byebug'. Adds 'step', 'next', 'finish', 'continue' and 'break' commands to control execution gem "rspec-rails", "~> 5.0" # rspec-rails is a testing framework for Rails 3+ - gem "rubocop", "~> 1.15", require: false # Automatic Ruby code style checking tool + gem "rubocop", "~> 1.16", require: false # Automatic Ruby code style checking tool gem "rubocop-performance", "~> 1.11", require: false # A collection of RuboCop cops to check for performance optimizations in Ruby code gem "rubocop-rails", "~> 2.10", require: false # Automatic Rails code style checking tool gem "rubocop-rspec", "~> 2.3", require: false # Code style checking for RSpec files diff --git a/Gemfile.lock b/Gemfile.lock index 29aa819d7..29c7b50e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -646,16 +646,16 @@ GEM rspec-retry (0.6.2) rspec-core (> 3.3) rspec-support (3.10.2) - rubocop (1.15.0) + rubocop (1.16.0) parallel (~> 1.10) parser (>= 3.0.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml - rubocop-ast (>= 1.5.0, < 2.0) + rubocop-ast (>= 1.7.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.5.0) + rubocop-ast (1.7.0) parser (>= 3.0.1.1) rubocop-performance (1.11.3) rubocop (>= 1.7.0, < 2.0) @@ -951,7 +951,7 @@ DEPENDENCIES rpush-redis (~> 1.1) rspec-rails (~> 5.0) rspec-retry (~> 0.6) - rubocop (~> 1.15) + rubocop (~> 1.16) rubocop-performance (~> 1.11) rubocop-rails (~> 2.10) rubocop-rspec (~> 2.3) diff --git a/app/liquid_tags/wikipedia_tag.rb b/app/liquid_tags/wikipedia_tag.rb index 503084a6f..b067852e1 100644 --- a/app/liquid_tags/wikipedia_tag.rb +++ b/app/liquid_tags/wikipedia_tag.rb @@ -42,7 +42,7 @@ class WikipediaTag < LiquidTagBase def parse_page_with_anchor(url, lang, title, anchor) api_url = "https://#{lang}.wikipedia.org/api/rest_v1/page/mobile-sections/#{title}" - response = HTTParty.get(api_url, headers: { 'user-agent': URL.url("/contact") }) + response = HTTParty.get(api_url, headers: { "user-agent": URL.url("/contact") }) handle_response_error(response, url) text, section_title = get_section_contents(response, anchor, url) @@ -57,7 +57,7 @@ class WikipediaTag < LiquidTagBase def parse_page(url, lang, title) api_url = "https://#{lang}.wikipedia.org/api/rest_v1/page/summary/#{title}" - response = HTTParty.get(api_url, headers: { 'user-agent': URL.url("/contact") }) + response = HTTParty.get(api_url, headers: { "user-agent": URL.url("/contact") }) handle_response_error(response, url) { diff --git a/app/models/device.rb b/app/models/device.rb index 1fb33e820..bea7e1573 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -38,7 +38,7 @@ class Device < ApplicationRecord subtitle: title, body: body }, - 'thread-id': Settings::Community.community_name + "thread-id": Settings::Community.community_name }, data: payload } diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb index cb29d6f6a..2d1434a0e 100644 --- a/config/initializers/carrierwave.rb +++ b/config/initializers/carrierwave.rb @@ -1,3 +1,4 @@ +# rubocop:disable Style/TopLevelMethodDefinition require "mini_magick" # Carrierwave uses MiniMagick for image processing. To prevent server timeouts @@ -59,3 +60,4 @@ Rails.application.reloader.to_prepare do local_storage_config end end +# rubocop:enable Style/TopLevelMethodDefinition diff --git a/config/initializers/slack_notifier.rb b/config/initializers/slack_notifier.rb index 5acfca0c5..9cf75bd0e 100644 --- a/config/initializers/slack_notifier.rb +++ b/config/initializers/slack_notifier.rb @@ -1,3 +1,4 @@ +# rubocop:disable Style/TopLevelMethodDefinition Rails.application.config.to_prepare do Dir.glob(Rails.root.join("lib/slack/notifier/util/*.rb")).each do |filename| require_dependency filename @@ -49,3 +50,4 @@ def init_slack_client end SlackClient = init_slack_client +# rubocop:enable Style/TopLevelMethodDefinition diff --git a/spec/initializers/rack/attack_spec.rb b/spec/initializers/rack/attack_spec.rb index f7c974126..2877fa9f7 100644 --- a/spec/initializers/rack/attack_spec.rb +++ b/spec/initializers/rack/attack_spec.rb @@ -132,9 +132,10 @@ describe Rack::Attack, type: :request, throttle: true do end # rubocop:disable RSpec/AnyInstance, RSpec/ExampleLength - it "throttles viewing tags" do + it "throttles viewing tags", :aggregate_failures do allow_any_instance_of(Stories::TaggedArticlesController).to receive(:tagged_count).and_return(0) - allow_any_instance_of(Stories::TaggedArticlesController).to receive(:stories_by_timeframe).and_return(Article.none) + allow_any_instance_of(Stories::TaggedArticlesController).to receive(:stories_by_timeframe) + .and_return(Article.none) allow_any_instance_of(Articles::Feeds::LargeForemExperimental).to receive( :published_articles_by_tag, ).and_return(Article.none) diff --git a/spec/lib/data_update_scripts/create_profile_fields_spec.rb b/spec/lib/data_update_scripts/create_profile_fields_spec.rb index 7dfdd3235..dd68be773 100644 --- a/spec/lib/data_update_scripts/create_profile_fields_spec.rb +++ b/spec/lib/data_update_scripts/create_profile_fields_spec.rb @@ -1,11 +1,11 @@ require "rails_helper" require Rails.root.join("lib/data_update_scripts/20200901040521_create_profile_fields.rb") -def profile_field_and_group_count - [ProfileField.count, ProfileFieldGroup.count] -end - describe DataUpdateScripts::CreateProfileFields do + def profile_field_and_group_count + [ProfileField.count, ProfileFieldGroup.count] + end + before do ProfileFieldGroup.destroy_all ProfileField.destroy_all diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1e23827a5..a25774a01 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,29 +1,29 @@ require "rails_helper" -def user_from_authorization_service(service_name, signed_in_resource = nil, cta_variant = "navbar_basic") - auth = OmniAuth.config.mock_auth[service_name] - Authentication::Authenticator.call( - auth, - current_user: signed_in_resource, - cta_variant: cta_variant, - ) -end - -def mock_username(provider_name, username) - if provider_name == :apple - OmniAuth.config.mock_auth[provider_name].info.first_name = username - else - OmniAuth.config.mock_auth[provider_name].info.nickname = username - end -end - -def provider_username(service_name) - auth_payload = OmniAuth.config.mock_auth[service_name] - provider_class = Authentication::Providers.get!(auth_payload.provider) - provider_class.new(auth_payload).user_nickname -end - RSpec.describe User, type: :model do + def user_from_authorization_service(service_name, signed_in_resource = nil, cta_variant = "navbar_basic") + auth = OmniAuth.config.mock_auth[service_name] + Authentication::Authenticator.call( + auth, + current_user: signed_in_resource, + cta_variant: cta_variant, + ) + end + + def mock_username(provider_name, username) + if provider_name == :apple + OmniAuth.config.mock_auth[provider_name].info.first_name = username + else + OmniAuth.config.mock_auth[provider_name].info.nickname = username + end + end + + def provider_username(service_name) + auth_payload = OmniAuth.config.mock_auth[service_name] + provider_class = Authentication::Providers.get!(auth_payload.provider) + provider_class.new(auth_payload).user_nickname + end + let(:user) { create(:user) } let(:other_user) { create(:user) } let(:org) { create(:organization) } diff --git a/spec/services/notifications/milestone/send_spec.rb b/spec/services/notifications/milestone/send_spec.rb index 5d5b775a5..b3d82fef5 100644 --- a/spec/services/notifications/milestone/send_spec.rb +++ b/spec/services/notifications/milestone/send_spec.rb @@ -1,6 +1,14 @@ require "rails_helper" RSpec.describe Notifications::Milestone::Send, type: :service do + def send_milestone_notification_view + described_class.call("View", article) + end + + def send_milestone_notification_reaction + described_class.call("Reaction", article) + end + let(:user) { create(:user) } let(:article) { create(:article, user_id: user.id, page_views_count: 4000, public_reactions_count: 70) } @@ -78,11 +86,3 @@ RSpec.describe Notifications::Milestone::Send, type: :service do end end end - -def send_milestone_notification_view - described_class.call("View", article) -end - -def send_milestone_notification_reaction - described_class.call("Reaction", article) -end diff --git a/spec/support/initializers/capybara.rb b/spec/support/initializers/capybara.rb index 09ec2b3af..2f41d890e 100644 --- a/spec/support/initializers/capybara.rb +++ b/spec/support/initializers/capybara.rb @@ -27,6 +27,7 @@ RSpec.configure do |config| end end +# rubocop:disable Style/TopLevelMethodDefinition # adapted from def wait_for_javascript max_time = Capybara::Helpers.monotonic_time + Capybara.default_max_wait_time @@ -42,3 +43,4 @@ def wait_for_javascript raise "wait_for_javascript timeout" unless finished end +# rubocop:enable Style/TopLevelMethodDefinition diff --git a/spec/system/homepage/user_visits_homepage_with_announcement_spec.rb b/spec/system/homepage/user_visits_homepage_with_announcement_spec.rb index d7ac189a9..2496ae1be 100644 --- a/spec/system/homepage/user_visits_homepage_with_announcement_spec.rb +++ b/spec/system/homepage/user_visits_homepage_with_announcement_spec.rb @@ -1,19 +1,19 @@ require "rails_helper" -def expect_broadcast_data(page) - within ".broadcast-wrapper" do - expect(page).to have_selector(".broadcast-data") - expect(page).to have_text("Hello, World!") - end -end - -def expect_no_broadcast_data(page) - expect(page).not_to have_css(".broadcast-wrapper") - expect(page).not_to have_selector(".broadcast-data") - expect(page).not_to have_text("Hello, World!") -end - RSpec.describe "User visits a homepage", type: :system do + def expect_broadcast_data(page) + within ".broadcast-wrapper" do + expect(page).to have_selector(".broadcast-data") + expect(page).to have_text("Hello, World!") + end + end + + def expect_no_broadcast_data(page) + expect(page).not_to have_css(".broadcast-wrapper") + expect(page).not_to have_selector(".broadcast-data") + expect(page).not_to have_text("Hello, World!") + end + context "when user hasn't logged in" do context "with an active announcement" do before do diff --git a/vendor/cache/rubocop-1.15.0.gem b/vendor/cache/rubocop-1.15.0.gem deleted file mode 100644 index 2bbe16518..000000000 Binary files a/vendor/cache/rubocop-1.15.0.gem and /dev/null differ diff --git a/vendor/cache/rubocop-1.16.0.gem b/vendor/cache/rubocop-1.16.0.gem new file mode 100644 index 000000000..3415ccd50 Binary files /dev/null and b/vendor/cache/rubocop-1.16.0.gem differ diff --git a/vendor/cache/rubocop-ast-1.5.0.gem b/vendor/cache/rubocop-ast-1.5.0.gem deleted file mode 100644 index 7343115f1..000000000 Binary files a/vendor/cache/rubocop-ast-1.5.0.gem and /dev/null differ diff --git a/vendor/cache/rubocop-ast-1.7.0.gem b/vendor/cache/rubocop-ast-1.7.0.gem new file mode 100644 index 000000000..c4d1a6287 Binary files /dev/null and b/vendor/cache/rubocop-ast-1.7.0.gem differ