From b273e7b67e805386ad4e099d0d340d8b91e356e3 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 19 Apr 2023 10:55:16 +0200 Subject: [PATCH] chore(rubocop): fix Rails/RootPathnameMethods and assimiled cops --- .rubocop.yml | 6 ++++++ app/controllers/ping_controller.rb | 2 +- app/services/zxcvbn_service.rb | 2 +- config/initializers/core_ext.rb | 2 +- lib/tasks/deployment/20220922151100_populate_zones.rake | 2 +- spec/controllers/ping_controller_spec.rb | 7 ++++++- spec/controllers/webhook_controller_spec.rb | 4 ++-- spec/lib/tasks/graphql_spec.rb | 2 +- 8 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index df2111204..ca7a3de9a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1152,6 +1152,12 @@ Style/EvenOdd: Style/ExpandPathArguments: Enabled: true +Style/FileRead: + Enabled: true + +Style/FileWrite: + Enabled: true + Style/For: Enabled: true diff --git a/app/controllers/ping_controller.rb b/app/controllers/ping_controller.rb index 6fadefcbe..348a1e4fe 100644 --- a/app/controllers/ping_controller.rb +++ b/app/controllers/ping_controller.rb @@ -1,7 +1,7 @@ class PingController < ApplicationController def index Rails.logger.silence do - status_code = if File.file?(Rails.root.join("maintenance")) + status_code = if Rails.root.join("maintenance").file? # See https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4.2-http-check%20disable-on-404 :not_found elsif (ActiveRecord::Base.connection.execute('select 1 as test;').first['test'] == 1) diff --git a/app/services/zxcvbn_service.rb b/app/services/zxcvbn_service.rb index a6097ccc0..1930b66fe 100644 --- a/app/services/zxcvbn_service.rb +++ b/app/services/zxcvbn_service.rb @@ -19,7 +19,7 @@ class ZxcvbnService # # This is slow: loading and parsing the dictionary may take around 1s. def build_tester - dictionaries = YAML.safe_load(File.read(Rails.root.join("config", "initializers", "zxcvbn_dictionnaries.yaml"))) + dictionaries = YAML.safe_load(Rails.root.join("config", "initializers", "zxcvbn_dictionnaries.yaml").read) tester = Zxcvbn::Tester.new tester.add_word_lists(dictionaries) diff --git a/config/initializers/core_ext.rb b/config/initializers/core_ext.rb index 99b320fcd..44ae562d2 100644 --- a/config/initializers/core_ext.rb +++ b/config/initializers/core_ext.rb @@ -1,3 +1,3 @@ -Dir[File.join(Rails.root, "lib", "core_ext", "*.rb")].each do |core_ext_file| +Dir[Rails.root.join("lib", "core_ext", "*.rb")].each do |core_ext_file| require core_ext_file end diff --git a/lib/tasks/deployment/20220922151100_populate_zones.rake b/lib/tasks/deployment/20220922151100_populate_zones.rake index 3b1b1ad12..3db312eb1 100644 --- a/lib/tasks/deployment/20220922151100_populate_zones.rake +++ b/lib/tasks/deployment/20220922151100_populate_zones.rake @@ -7,7 +7,7 @@ namespace :after_party do coll_label = collectivite.labels.find_or_initialize_by(designated_on: Date.parse('1977-07-30')) coll_label.update(name: 'Collectivité territoriale') - config = Psych.safe_load(File.read(Rails.root.join("config", "zones.yml"))) + config = Psych.safe_load(Rails.root.join("config", "zones.yml").read) config["ministeres"].each do |ministere| acronym = ministere.keys.first zone = Zone.find_or_create_by!(acronym: acronym) diff --git a/spec/controllers/ping_controller_spec.rb b/spec/controllers/ping_controller_spec.rb index c1c811c63..5b9e8c123 100644 --- a/spec/controllers/ping_controller_spec.rb +++ b/spec/controllers/ping_controller_spec.rb @@ -13,8 +13,13 @@ describe PingController, type: :controller do end context 'when a maintenance file is present' do + let(:filepath) { Rails.root.join('maintenance') } before do - allow(File).to receive(:file?).and_return(true) + filepath.write("") + end + + after do + filepath.delete end it 'tells HAProxy that the app is in maintenance, but will be available again soon' do diff --git a/spec/controllers/webhook_controller_spec.rb b/spec/controllers/webhook_controller_spec.rb index e95a00cfb..65931d4fd 100644 --- a/spec/controllers/webhook_controller_spec.rb +++ b/spec/controllers/webhook_controller_spec.rb @@ -6,7 +6,7 @@ describe WebhookController, type: :controller do describe '#helpscout_support_dev' do subject(:response) { post :helpscout_support_dev, params: payload } - let(:payload) { JSON.parse(File.read(Rails.root.join('spec', 'fixtures', 'files', 'helpscout', 'tagged-dev.json'))) } + let(:payload) { JSON.parse(Rails.root.join('spec', 'fixtures', 'files', 'helpscout', 'tagged-dev.json').read) } let(:webhook_url) { "https://notification_url" } it 'works' do allow(Rails.application.secrets).to receive(:dig).with(:mattermost, :support_webhook_url).and_return(webhook_url) @@ -68,7 +68,7 @@ describe WebhookController, type: :controller do describe '#sendinblue' do subject(:response) { post :sendinblue, params: payload } - let(:payload) { JSON.parse(File.read(Rails.root.join('spec', 'fixtures', 'files', 'sendinblue', 'incident.json'))) } + let(:payload) { JSON.parse(Rails.root.join('spec', 'fixtures', 'files', 'sendinblue', 'incident.json').read) } it 'sends notification to mattermost' do notification_url = "https://notification_url" diff --git a/spec/lib/tasks/graphql_spec.rb b/spec/lib/tasks/graphql_spec.rb index 24b772c9a..450a9d9e2 100644 --- a/spec/lib/tasks/graphql_spec.rb +++ b/spec/lib/tasks/graphql_spec.rb @@ -1,6 +1,6 @@ describe 'graphql' do let(:current_defn) { API::V2::Schema.to_definition } - let(:printout_defn) { File.read(Rails.root.join('app', 'graphql', 'schema.graphql')) } + let(:printout_defn) { Rails.root.join('app', 'graphql', 'schema.graphql').read } it "update the printed schema with `bin/rake graphql:schema:idl`" do result = GraphQL::SchemaComparator.compare(current_defn, printout_defn)