|
210 | 210 | file = Rails.root.join('docs', 'invoice.pdf').open
|
211 | 211 | RUBY
|
212 | 212 | end
|
| 213 | + |
| 214 | + it 'registers an offense when using only [] syntax' do |
| 215 | + expect_offense(<<~RUBY) |
| 216 | + File.join(Rails.root, ['app', 'models']) |
| 217 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*['app', 'models'])`. |
| 218 | + RUBY |
| 219 | + |
| 220 | + expect_correction(<<~RUBY) |
| 221 | + Rails.root.join(*['app', 'models']) |
| 222 | + RUBY |
| 223 | + end |
| 224 | + |
| 225 | + it 'registers an offense when using a leading string and an array using [] syntax' do |
| 226 | + expect_offense(<<~RUBY) |
| 227 | + File.join(Rails.root, "app", ["models", "goober"]) |
| 228 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *["models", "goober"])`. |
| 229 | + RUBY |
| 230 | + |
| 231 | + expect_correction(<<~RUBY) |
| 232 | + Rails.root.join("app", *["models", "goober"]) |
| 233 | + RUBY |
| 234 | + end |
| 235 | + |
| 236 | + it 'registers an offense when using an array using [] syntax and a trailing string' do |
| 237 | + expect_offense(<<~RUBY) |
| 238 | + File.join(Rails.root, ["app", "models"], "goober") |
| 239 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*["app", "models"], "goober")`. |
| 240 | + RUBY |
| 241 | + |
| 242 | + expect_correction(<<~RUBY) |
| 243 | + Rails.root.join(*["app", "models"], "goober") |
| 244 | + RUBY |
| 245 | + end |
| 246 | + |
| 247 | + it 'registers an offense when using only %w[] syntax' do |
| 248 | + expect_offense(<<~RUBY) |
| 249 | + File.join(Rails.root, %w[app models]) |
| 250 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models])`. |
| 251 | + RUBY |
| 252 | + |
| 253 | + expect_correction(<<~RUBY) |
| 254 | + Rails.root.join(*%w[app models]) |
| 255 | + RUBY |
| 256 | + end |
| 257 | + |
| 258 | + it 'registers an offense when using a leading string and an array using %w[] syntax' do |
| 259 | + expect_offense(<<~RUBY) |
| 260 | + File.join(Rails.root, "app", %w[models goober]) |
| 261 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *%w[models goober])`. |
| 262 | + RUBY |
| 263 | + |
| 264 | + expect_correction(<<~RUBY) |
| 265 | + Rails.root.join("app", *%w[models goober]) |
| 266 | + RUBY |
| 267 | + end |
| 268 | + |
| 269 | + it 'registers an offense when using an array using %w[] syntax and a trailing string' do |
| 270 | + expect_offense(<<~RUBY) |
| 271 | + File.join(Rails.root, %w[app models], "goober") |
| 272 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models], "goober")`. |
| 273 | + RUBY |
| 274 | + |
| 275 | + expect_correction(<<~RUBY) |
| 276 | + Rails.root.join(*%w[app models], "goober") |
| 277 | + RUBY |
| 278 | + end |
213 | 279 | end
|
0 commit comments