summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/env_assignment_spec.rb
blob: 0fd09eeae1110b1de276dea6bd969f385704541a (plain)
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
# frozen_string_literal: true

require 'fast_spec_helper'

require_relative '../../../../rubocop/cop/rspec/env_assignment'

RSpec.describe RuboCop::Cop::RSpec::EnvAssignment do
  offense_call_single_quotes_key = %(ENV['FOO'] = 'bar')
  offense_call_double_quotes_key = %(ENV["FOO"] = 'bar')

  let(:source_file) { 'spec/foo_spec.rb' }

  subject(:cop) { described_class.new }

  shared_examples 'an offensive and correction ENV#[]= call' do |content, autocorrected_content|
    it "registers an offense for `#{content}` and corrects", :aggregate_failures do
      expect_offense(<<~CODE)
        #{content}
        ^^^^^^^^^^^^^^^^^^ Don't assign to ENV, use `stub_env` instead.
      CODE

      expect_correction(<<~CODE)
        #{autocorrected_content}
      CODE
    end
  end

  context 'with a key using single quotes' do
    it_behaves_like 'an offensive and correction 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 and correction ENV#[]= call', offense_call_double_quotes_key, %(stub_env("FOO", 'bar'))
  end
end