/
root_pathname_methods_spec.rb
92 lines (79 loc) · 3.5 KB
/
root_pathname_methods_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::RootPathnameMethods, :config do
{
Dir: described_class::DIR_METHODS,
File: described_class::FILE_METHODS,
FileTest: described_class::FILE_TEST_METHODS,
FileUtils: described_class::FILE_UTILS_METHODS,
IO: described_class::FILE_METHODS
}.each do |receiver, methods|
methods.each do |method|
it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do
expect_offense(<<~RUBY)
#{receiver}.#{method}(Rails.public_path)
#{'^' * receiver.size}^#{'^' * method.size}^^^^^^^^^^^^^^^^^^^ `Rails.public_path` is a `Pathname` so you can just append `##{method}`.
RUBY
expect_correction(<<~RUBY)
Rails.public_path.#{method}
RUBY
end
it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...))` (if arity exists)" do
expect_offense(<<~RUBY)
::#{receiver}.#{method}(::Rails.root.join('db', 'schema.rb'))
^^#{'^' * receiver.size}^#{'^' * method.size}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `##{method}`.
RUBY
expect_correction(<<~RUBY)
::Rails.root.join('db', 'schema.rb').#{method}
RUBY
end
it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...), ...)` (if arity exists)" do
expect_offense(<<~RUBY)
::#{receiver}.#{method}(::Rails.root.join('db', 'schema.rb'), 20, 5)
^^#{'^' * receiver.size}^#{'^' * method.size}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `##{method}`.
RUBY
expect_correction(<<~RUBY)
::Rails.root.join('db', 'schema.rb').#{method}(20, 5)
RUBY
end
end
end
# This is handled by `Rails/RootJoinChain`
it 'does not register an offense when using `File.read(Rails.root.join(...).join(...))`' do
expect_no_offenses(<<~RUBY)
File.read(Rails.root.join('db').join('schema.rb'))
RUBY
end
# This is handled by `Style/FileRead`
it 'does not register an offense when using `File.open(Rails.root.join(...)).read`' do
expect_no_offenses(<<~RUBY)
File.open(Rails.root.join('db', 'schema.rb')).read
RUBY
end
# This is handled by `Style/FileRead`
it 'does not register an offense when using `File.open(Rails.root.join(...)).binread`' do
expect_no_offenses(<<~RUBY)
File.open(Rails.root.join('db', 'schema.rb')).binread
RUBY
end
# This is handled by `Style/FileWrite`
it 'does not register an offense when using `File.open(Rails.root.join(...)).write(content)`' do
expect_no_offenses(<<~RUBY)
File.open(Rails.root.join('db', 'schema.rb')).write(content)
RUBY
end
# This is handled by `Style/FileWrite`
it 'does not register an offense when using `File.open(Rails.root.join(...)).binwrite(content)`' do
expect_no_offenses(<<~RUBY)
File.open(Rails.root.join('db', 'schema.rb')).binwrite(content)
RUBY
end
it 'registers an offense when using `File.open(Rails.root.join(...), ...)` inside an iterator' do
expect_offense(<<~RUBY)
files.map { |file| File.open(Rails.root.join('db', file), 'wb') }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`.
RUBY
expect_correction(<<~RUBY)
files.map { |file| Rails.root.join('db', file).open('wb') }
RUBY
end
end