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
|
# frozen_string_literal: true
require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/rake/require'
RSpec.describe RuboCop::Cop::Rake::Require do
let(:msg) { described_class::MSG }
describe '#in_rake_file?' do
context 'in a Rake file' do
let(:node) { double(location: double(expression: double(source_buffer: double(name: 'foo/bar.rake')))) } # rubocop:disable RSpec/VerifiedDoubles
it { expect(subject.__send__(:in_rake_file?, node)).to be(true) }
end
context 'when outside of a Rake file' do
let(:node) { double(location: double(expression: double(source_buffer: double(name: 'foo/bar.rb')))) } # rubocop:disable RSpec/VerifiedDoubles
it { expect(subject.__send__(:in_rake_file?, node)).to be(false) }
end
end
context 'in a Rake file' do
before do
allow(cop).to receive(:in_rake_file?).and_return(true)
end
it 'registers offenses for require methods' do
expect_offense(<<~RUBY)
require 'json'
^^^^^^^^^^^^^^ #{msg}
require_relative 'gitlab/json'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
RUBY
end
it 'registers offenses for require methods inside `namespace` definitions' do
expect_offense(<<~RUBY)
namespace :foo do
require 'json'
^^^^^^^^^^^^^^ #{msg}
task :parse do
end
end
namespace :bar do
require_relative 'gitlab/json'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
task :parse do
end
end
RUBY
end
it 'does not register offense inside `task` definition' do
expect_no_offenses(<<~RUBY)
task :parse do
require 'json'
end
namespace :some do
task parse: :env do
require_relative 'gitlab/json'
end
end
RUBY
end
it 'does not register offense inside a block definition' do
expect_no_offenses(<<~RUBY)
RSpec::Core::RakeTask.new(:parse_json) do |t, args|
require 'json'
end
RUBY
end
it 'does not register offense inside a method definition' do
expect_no_offenses(<<~RUBY)
def load_deps
require 'json'
end
def self.run
require 'yaml'
end
task :parse do
load_deps
run
end
RUBY
end
it 'does not register offense when require task related files' do
expect_no_offenses(<<~RUBY)
require 'rubocop/rake_tasks'
require 'gettext_i18n_rails/tasks'
require_relative '../../rubocop/check_graceful_task'
RUBY
end
end
context 'when outside of a Rake file' do
before do
allow(cop).to receive(:in_rake_file?).and_return(false)
end
it 'registers an offenses for require methods' do
expect_no_offenses("require 'json'")
end
end
end
|