blob: 96bd535df08eb16b76a6f843ff7f4339b33f2083 (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# frozen_string_literal: true
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
# Check for ENV mocking in specs.
# See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#persistent-in-memory-application-state
#
# @example
#
# # bad
# allow(ENV).to receive(:[]).with('FOO').and_return('bar')
# allow(ENV).to receive(:fetch).with('FOO').and_return('bar')
# allow(ENV).to receive(:[]).with(key).and_return(value)
# allow(ENV).to receive(:[]).with(fetch_key(object)).and_return(fetch_value(object))
#
# # good
# stub_env('FOO', 'bar')
# stub_env(key, value)
# stub_env(fetch_key(object), fetch_value(object))
class EnvMocking < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
MESSAGE = "Don't mock the ENV, use `stub_env` instead."
def_node_matcher :env_mocking?, <<~PATTERN
(send
(send nil? :allow
(const {nil? cbase} :ENV)
)
:to
(send
(send
(send nil? :receive (sym {:[] :fetch}))
:with $_
)
:and_return $_
)
...
)
PATTERN
def on_send(node)
env_mocking?(node) do |key, value|
add_offense(node, message: MESSAGE) do |corrector|
corrector.replace(node.loc.expression, stub_env(key.source, value.source))
end
end
end
def stub_env(key, value)
"stub_env(#{key}, #{value})"
end
end
end
end
end
|