|
244 | 244 | RUBY
|
245 | 245 | end
|
246 | 246 | end
|
| 247 | + |
| 248 | + context 'when using only [] syntax' do |
| 249 | + it 'registers an offense once' do |
| 250 | + expect_offense(<<~RUBY) |
| 251 | + File.join(Rails.root, ['app', 'models']) |
| 252 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 253 | + RUBY |
| 254 | + |
| 255 | + expect_correction(<<~RUBY) |
| 256 | + Rails.root.join(*['app', 'models']).to_s |
| 257 | + RUBY |
| 258 | + end |
| 259 | + end |
| 260 | + |
| 261 | + context 'with a leading string and an array using [] syntax' do |
| 262 | + it 'registers an offense once' do |
| 263 | + expect_offense(<<~RUBY) |
| 264 | + File.join(Rails.root, "app", ["models", "goober"]) |
| 265 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 266 | + RUBY |
| 267 | + |
| 268 | + expect_correction(<<~RUBY) |
| 269 | + Rails.root.join("app", *["models", "goober"]).to_s |
| 270 | + RUBY |
| 271 | + end |
| 272 | + end |
| 273 | + |
| 274 | + context 'with an array using [] syntax and a trailing string' do |
| 275 | + it 'registers an offense once' do |
| 276 | + expect_offense(<<~RUBY) |
| 277 | + File.join(Rails.root, ["app", "models"], "goober") |
| 278 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 279 | + RUBY |
| 280 | + |
| 281 | + expect_correction(<<~RUBY) |
| 282 | + Rails.root.join(*["app", "models"], "goober").to_s |
| 283 | + RUBY |
| 284 | + end |
| 285 | + end |
| 286 | + |
| 287 | + context 'when using only %w[] syntax' do |
| 288 | + it 'registers an offense once' do |
| 289 | + expect_offense(<<~RUBY) |
| 290 | + File.join(Rails.root, %w[app models]) |
| 291 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 292 | + RUBY |
| 293 | + |
| 294 | + expect_correction(<<~RUBY) |
| 295 | + Rails.root.join(*%w[app models]).to_s |
| 296 | + RUBY |
| 297 | + end |
| 298 | + end |
| 299 | + |
| 300 | + context 'with a leading string and an array using %w[] syntax' do |
| 301 | + it 'registers an offense once' do |
| 302 | + expect_offense(<<~RUBY) |
| 303 | + File.join(Rails.root, "app", %w[models goober]) |
| 304 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 305 | + RUBY |
| 306 | + |
| 307 | + expect_correction(<<~RUBY) |
| 308 | + Rails.root.join("app", *%w[models goober]).to_s |
| 309 | + RUBY |
| 310 | + end |
| 311 | + end |
| 312 | + |
| 313 | + context 'with an array using %w[] syntax and a trailing string' do |
| 314 | + it 'registers an offense once' do |
| 315 | + expect_offense(<<~RUBY) |
| 316 | + File.join(Rails.root, %w[app models], "goober") |
| 317 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`. |
| 318 | + RUBY |
| 319 | + |
| 320 | + expect_correction(<<~RUBY) |
| 321 | + Rails.root.join(*%w[app models], "goober").to_s |
| 322 | + RUBY |
| 323 | + end |
| 324 | + end |
247 | 325 | end
|
248 | 326 |
|
249 | 327 | context 'when EnforcedStyle is `arguments`' do
|
|
436 | 514 | RUBY
|
437 | 515 | end
|
438 | 516 | end
|
| 517 | + |
| 518 | + context 'when using only [] syntax' do |
| 519 | + it 'registers an offense once' do |
| 520 | + expect_offense(<<~RUBY) |
| 521 | + File.join(Rails.root, ['app', 'models']) |
| 522 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 523 | + RUBY |
| 524 | + |
| 525 | + expect_correction(<<~RUBY) |
| 526 | + Rails.root.join(*['app', 'models']).to_s |
| 527 | + RUBY |
| 528 | + end |
| 529 | + end |
| 530 | + |
| 531 | + context 'with a leading string and an array using [] syntax' do |
| 532 | + it 'registers an offense once' do |
| 533 | + expect_offense(<<~RUBY) |
| 534 | + File.join(Rails.root, "app", ["models", "goober"]) |
| 535 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 536 | + RUBY |
| 537 | + |
| 538 | + expect_correction(<<~RUBY) |
| 539 | + Rails.root.join("app", *["models", "goober"]).to_s |
| 540 | + RUBY |
| 541 | + end |
| 542 | + end |
| 543 | + |
| 544 | + context 'with an array using [] syntax and a trailing string' do |
| 545 | + it 'registers an offense once' do |
| 546 | + expect_offense(<<~RUBY) |
| 547 | + File.join(Rails.root, ["app", "models"], "goober") |
| 548 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 549 | + RUBY |
| 550 | + |
| 551 | + expect_correction(<<~RUBY) |
| 552 | + Rails.root.join(*["app", "models"], "goober").to_s |
| 553 | + RUBY |
| 554 | + end |
| 555 | + end |
| 556 | + |
| 557 | + context 'when using only %w[] syntax' do |
| 558 | + it 'registers an offense once' do |
| 559 | + expect_offense(<<~RUBY) |
| 560 | + File.join(Rails.root, %w[app models]) |
| 561 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 562 | + RUBY |
| 563 | + |
| 564 | + expect_correction(<<~RUBY) |
| 565 | + Rails.root.join(*%w[app models]).to_s |
| 566 | + RUBY |
| 567 | + end |
| 568 | + end |
| 569 | + |
| 570 | + context 'with a leading string and an array using %w[] syntax' do |
| 571 | + it 'registers an offense once' do |
| 572 | + expect_offense(<<~RUBY) |
| 573 | + File.join(Rails.root, "app", %w[models goober]) |
| 574 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 575 | + RUBY |
| 576 | + |
| 577 | + expect_correction(<<~RUBY) |
| 578 | + Rails.root.join("app", *%w[models goober]).to_s |
| 579 | + RUBY |
| 580 | + end |
| 581 | + end |
| 582 | + |
| 583 | + context 'with an array using %w[] syntax and a trailing string' do |
| 584 | + it 'registers an offense once' do |
| 585 | + expect_offense(<<~RUBY) |
| 586 | + File.join(Rails.root, %w[app models], "goober") |
| 587 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`. |
| 588 | + RUBY |
| 589 | + |
| 590 | + expect_correction(<<~RUBY) |
| 591 | + Rails.root.join(*%w[app models], "goober").to_s |
| 592 | + RUBY |
| 593 | + end |
| 594 | + end |
439 | 595 | end
|
440 | 596 | end
|
0 commit comments