forked from rubocop/rubocop-rspec
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`RSpec` provides the convenience method ```ruby file_fixture('path/some_file.csv') ``` which (by default) is equivalent to ```ruby Rails.root.join('spec/fixtures/files/path/some_file.csv') ``` We add a cop which detects and autocorrects verbose `Rails.root` constructions and works best in combination with [`Rails/RootPathnameMethods`](rubocop/rubocop-rails#587).
- Loading branch information
Showing
8 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1214](https://github.com/rubocop/rubocop-rails/pull/1214): Add new `RSpec/FileFixture` cop. ([@leoarnold][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Favor the use of `file_fixture`. | ||
# | ||
# `RSpec` provides the convenience method | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# file_fixture('path/some_file.csv') | ||
# ---- | ||
# | ||
# which (by default) is equivalent to | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# Rails.root.join('spec/fixtures/files/path/some_file.csv') | ||
# ---- | ||
# | ||
# @example | ||
# # bad | ||
# "#{Rails.root}/spec/fixtures/path/some_file.csv" | ||
# Rails.root.join('spec/fixtures/path/some_file.csv') | ||
# Rails.root.join('spec', 'fixtures', 'path', 'some_file.csv') | ||
# | ||
# "#{Rails.root}/spec/fixtures/files/some_file.csv" | ||
# Rails.root.join('spec/fixtures/files/some_file.csv') | ||
# Rails.root.join('spec', 'fixtures', 'files', 'some_file.csv') | ||
# | ||
# # good | ||
# file_fixture('../path/some_file.csv').path | ||
# file_fixture('../path/some_file.csv') | ||
# file_fixture('../path/some_file.csv') | ||
# | ||
# file_fixture('some_file.csv').path | ||
# file_fixture('some_file.csv') | ||
# file_fixture('some_file.csv') | ||
# | ||
class FileFixture < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `file_fixture`.' | ||
|
||
RESTRICT_ON_SEND = %i[join root].freeze | ||
|
||
DEFAULT_FILE_FIXTURE_PATTERN = %r{^spec/fixtures/files/}.freeze | ||
DEFAULT_FIXTURE_PATTERN = %r{^spec/fixtures/}.freeze | ||
|
||
# @!method file_io?(node) | ||
def_node_matcher :file_io?, <<~PATTERN | ||
(send | ||
(const {nil? cbase} :File) {:binread :binwrite :open :read :write} $...) | ||
PATTERN | ||
|
||
# @!method rails_root_join(node) | ||
def_node_matcher :rails_root_join, <<~PATTERN | ||
(send | ||
(send | ||
(const {nil? cbase} :Rails) :root) :join (str $_)+) | ||
PATTERN | ||
|
||
# @!method dstr_rails_root(node) | ||
def_node_matcher :dstr_rails_root, <<~PATTERN | ||
(dstr | ||
(begin | ||
(send | ||
(const {nil? cbase} :Rails) :root)) | ||
(str $_)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless (crime_scene, strings, method = evidence(node)) | ||
return unless (new_path = new_path(strings)) | ||
|
||
add_offense(crime_scene) do |corrector| | ||
replacement = "file_fixture('#{new_path}')" | ||
replacement += ".#{method}" if method | ||
|
||
corrector.replace(crime_scene, replacement) | ||
end | ||
end | ||
|
||
private | ||
|
||
def evidence(node) | ||
if !file_io?(node.parent) && (string = rails_root_join(node)) | ||
[node, string, nil] | ||
elsif (string = dstr_rails_root(node.parent.parent)) | ||
[node.parent.parent, string.gsub(%r{^/}, ''), :path] | ||
end | ||
end | ||
|
||
def new_path(strings) | ||
path = File.join(strings) | ||
|
||
if DEFAULT_FILE_FIXTURE_PATTERN.match?(path) | ||
path.gsub(DEFAULT_FILE_FIXTURE_PATTERN, '') | ||
elsif DEFAULT_FIXTURE_PATTERN.match?(path) | ||
path.gsub(DEFAULT_FIXTURE_PATTERN, '../') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::FileFixture, :config do | ||
it 'registers an offense when using ' \ | ||
'`"\#{Rails.root}/spec/fixtures/files/some_file.csv"`' do | ||
expect_offense(<<~RUBY) | ||
"\#{Rails.root}/spec/fixtures/files/some_file.csv" | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('some_file.csv').path | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
'`"\#{::Rails.root}/spec/fixtures/path/some_file.csv"`' do | ||
expect_offense(<<~RUBY) | ||
"\#{::Rails.root}/spec/fixtures/path/some_file.csv" | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv').path | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec/fixtures/files/some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec/fixtures/files/some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`::Rails.root.join('spec/fixtures/path/some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
::Rails.root.join('spec/fixtures/path/some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec', 'fixtures/path/some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec', 'fixtures/path/some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec/fixtures', 'path/some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec/fixtures', 'path/some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec/fixtures/path', 'some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec/fixtures/path', 'some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec', 'fixtures', 'path/some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec', 'fixtures', 'path/some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec', 'fixtures/path', 'some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec', 'fixtures/path', 'some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec/fixtures', 'path', 'some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec/fixtures', 'path', 'some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ' \ | ||
"`Rails.root.join('spec', 'fixtures', 'path', 'some_file.csv')`" do | ||
expect_offense(<<~RUBY) | ||
Rails.root.join('spec', 'fixtures', 'path', 'some_file.csv') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `file_fixture`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
file_fixture('../path/some_file.csv') | ||
RUBY | ||
end | ||
|
||
%i[binread binwrite open read write].each do |method| | ||
# This is handled by `Rails/RootPathnameMethods` | ||
it 'does not register an offense when using ' \ | ||
"`File.#{method}(Rails.root.join(...))`" do | ||
expect_no_offenses(<<~RUBY) | ||
File.#{method}(Rails.root.join('spec', 'fixtures', 'path', 'some_file.csv')) | ||
RUBY | ||
end | ||
end | ||
end |