New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new RSpec/FileFixture
cop
#1214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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][]) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,6 +353,12 @@ RSpec/ExpectOutput: | |
VersionAdded: '1.10' | ||
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectOutput | ||
|
||
RSpec/FileFixture: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have Rails sub-department. |
||
Description: Favor the use of `file_fixture`. | ||
Enabled: pending | ||
VersionAdded: 2.7.0 | ||
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FileFixture | ||
|
||
RSpec/FilePath: | ||
Description: Checks that spec file paths are consistent and well-formed. | ||
Enabled: true | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
# 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 $_)+) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it's not a literal string passed to |
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring just duplicates what the example itself tells. Does it make sense to write a free-form text explaining this case? |
||
expect_offense(<<~RUBY) | ||
"\#{Rails.root}/spec/fixtures/files/some_file.csv" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's possible to write heredoc as |
||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're still using the old approach with just one CHANGELOG.md in this repo - it's not a major pain to merge it.