From 8a31b0743753f080e0ba07c8d6d0da863a5c4726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 11 Oct 2017 13:25:40 +0200 Subject: Add a new RSpec::EnvAssignment cop to prevent assigning to ENV in specs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/rubocop/cop/rspec/env_assignment_spec.rb | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 spec/rubocop/cop/rspec/env_assignment_spec.rb (limited to 'spec/rubocop') diff --git a/spec/rubocop/cop/rspec/env_assignment_spec.rb b/spec/rubocop/cop/rspec/env_assignment_spec.rb new file mode 100644 index 00000000000..4e859b6f6fa --- /dev/null +++ b/spec/rubocop/cop/rspec/env_assignment_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/rspec/env_assignment' + +describe RuboCop::Cop::RSpec::EnvAssignment do + include CopHelper + + OFFENSE_CALL_SINGLE_QUOTES_KEY = %(ENV['FOO'] = 'bar').freeze + OFFENSE_CALL_DOUBLE_QUOTES_KEY = %(ENV["FOO"] = 'bar').freeze + + let(:source_file) { 'spec/foo_spec.rb' } + + subject(:cop) { described_class.new } + + shared_examples 'an offensive ENV#[]= call' do |content| + it "registers an offense for `#{content}`" do + inspect_source(cop, content, source_file) + + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([1]) + expect(cop.highlights).to eq([content]) + end + end + + shared_examples 'an autocorrected ENV#[]= call' do |content, autocorrected_content| + it "registers an offense for `#{content}` and autocorrects it to `#{autocorrected_content}`" do + autocorrected = autocorrect_source(cop, content, source_file) + + expect(autocorrected).to eql(autocorrected_content) + end + end + + context 'in a spec file' do + before do + allow(cop).to receive(:in_spec?).and_return(true) + end + + context 'with a key using single quotes' do + it_behaves_like 'an offensive ENV#[]= call', OFFENSE_CALL_SINGLE_QUOTES_KEY + it_behaves_like 'an autocorrected ENV#[]= call', OFFENSE_CALL_SINGLE_QUOTES_KEY, %(stub_env('FOO', 'bar')) + end + + context 'with a key using double quotes' do + it_behaves_like 'an offensive ENV#[]= call', OFFENSE_CALL_DOUBLE_QUOTES_KEY + it_behaves_like 'an autocorrected ENV#[]= call', OFFENSE_CALL_DOUBLE_QUOTES_KEY, %(stub_env("FOO", 'bar')) + end + end + + context 'outside of a spec file' do + it "does not register an offense for `#{OFFENSE_CALL_SINGLE_QUOTES_KEY}` in a non-spec file" do + inspect_source(cop, OFFENSE_CALL_SINGLE_QUOTES_KEY) + + expect(cop.offenses.size).to eq(0) + end + end +end -- cgit v1.2.1