-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
root_pathname_methods_spec.rb
151 lines (127 loc) · 5.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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|
next if method == :glob
it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do
expect_offense(<<~RUBY, receiver: receiver, method: method)
%{receiver}.%{method}(Rails.public_path)
^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^ `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: receiver, method: method)
::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb'))
^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::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: receiver, method: method)
::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb'), 20, 5)
^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::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
context 'when using `Dir.glob`' do
it "registers an offense when using `Dir.glob(Rails.root.join('**/*.rb'))`" do
expect_offense(<<~RUBY)
Dir.glob(Rails.root.join('**/*.rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`.
RUBY
expect_correction(<<~RUBY)
Rails.root.glob('**/*.rb')
RUBY
end
it "registers an offense when using `::Dir.glob(Rails.root.join('**/*.rb'))`" do
expect_offense(<<~RUBY)
::Dir.glob(Rails.root.join('**/*.rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`.
RUBY
expect_correction(<<~RUBY)
Rails.root.glob('**/*.rb')
RUBY
end
it "registers an offense when using `Dir.glob(Rails.root.join('**/\#{path}/*.rb'))`" do
expect_offense(<<~'RUBY')
Dir.glob(Rails.root.join("**/#{path}/*.rb"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`.
RUBY
expect_correction(<<~'RUBY')
Rails.root.glob("**/#{path}/*.rb")
RUBY
end
it "registers an offense when using `Dir.glob(Rails.root.join('**', '*.rb'))`" do
expect_offense(<<~RUBY)
Dir.glob(Rails.root.join('**', '*.rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`.
RUBY
expect_correction(<<~RUBY)
Rails.root.glob('**/*.rb')
RUBY
end
it "registers an offense when using `Dir.glob(Rails.root.join('**', \"\#{path}\", '*.rb'))`" do
expect_offense(<<~'RUBY')
Dir.glob(Rails.root.join('**', "#{path}", '*.rb'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#glob`.
RUBY
expect_correction(<<~'RUBY')
Rails.root.glob("**/#{path}/*.rb")
RUBY
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