|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module RSpec |
| 6 | + # Checks for outside local variables used in examples. |
| 7 | + # |
| 8 | + # Local variables assigned outside an example but used within it act |
| 9 | + # as shared state, which can make tests non-deterministic. |
| 10 | + # |
| 11 | + # @example |
| 12 | + # # bad - outside variable used in a hook |
| 13 | + # user = create(:user) |
| 14 | + # |
| 15 | + # before { user.update(admin: true) } |
| 16 | + # |
| 17 | + # # good |
| 18 | + # let(:user) { create(:user) } |
| 19 | + # |
| 20 | + # before { user.update(admin: true) } |
| 21 | + # |
| 22 | + # # bad - outside variable used in an example |
| 23 | + # user = create(:user) |
| 24 | + # |
| 25 | + # it 'is persisted' do |
| 26 | + # expect(user).to be_persisted |
| 27 | + # end |
| 28 | + # |
| 29 | + # # good |
| 30 | + # let(:user) { create(:user) } |
| 31 | + # |
| 32 | + # it 'is persisted' do |
| 33 | + # expect(user).to be_persisted |
| 34 | + # end |
| 35 | + # |
| 36 | + # # also good - assigning the variable within the example |
| 37 | + # it 'is persisted' do |
| 38 | + # user = create(:user) |
| 39 | + # |
| 40 | + # expect(user).to be_persisted |
| 41 | + # end |
| 42 | + # |
| 43 | + # # bad - outside variable passed to included examples |
| 44 | + # attrs = ['foo', 'bar'] |
| 45 | + # |
| 46 | + # it_behaves_like 'some examples', attrs |
| 47 | + # |
| 48 | + # # good |
| 49 | + # it_behaves_like 'some examples' do |
| 50 | + # let(:attrs) { ['foo', 'bar'] } |
| 51 | + # end |
| 52 | + # |
| 53 | + # # good - when variable is used only as example description |
| 54 | + # attribute = 'foo' |
| 55 | + # |
| 56 | + # it "#{attribute} is persisted" do |
| 57 | + # expectations |
| 58 | + # end |
| 59 | + # |
| 60 | + # # good - when variable is used only to include other examples |
| 61 | + # examples = foo ? 'some examples' : 'other examples' |
| 62 | + # |
| 63 | + # it_behaves_like examples, another_argument |
| 64 | + # |
| 65 | + class LeakyLocalVariable < Base |
| 66 | + MSG = 'Use `let` instead of a local variable which can leak ' \ |
| 67 | + 'between examples.' |
| 68 | + |
| 69 | + # @!method example_method?(node) |
| 70 | + def_node_matcher :example_method?, <<~PATTERN |
| 71 | + (send nil? #Examples.all _) |
| 72 | + PATTERN |
| 73 | + |
| 74 | + # @!method includes_method?(node) |
| 75 | + def_node_matcher :includes_method?, <<~PATTERN |
| 76 | + (send nil? #Includes.all ...) |
| 77 | + PATTERN |
| 78 | + |
| 79 | + def self.joining_forces |
| 80 | + VariableForce |
| 81 | + end |
| 82 | + |
| 83 | + def after_leaving_scope(scope, _variable_table) |
| 84 | + scope.variables.each_value { |variable| check_references(variable) } |
| 85 | + end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + def check_references(variable) |
| 90 | + variable.assignments.each do |assignment| |
| 91 | + next if part_of_example_scope?(assignment.node) |
| 92 | + |
| 93 | + assignment.references.each do |reference| |
| 94 | + next unless part_of_example_scope?(reference) |
| 95 | + next if permitted_argument?(reference) |
| 96 | + |
| 97 | + add_offense(assignment.node) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def permitted_argument?(node) |
| 103 | + node.each_ancestor.any? do |ancestor| |
| 104 | + next true if example_method?(ancestor) |
| 105 | + if includes_method?(ancestor) |
| 106 | + next includes_arguments_permitted?(ancestor, node) |
| 107 | + end |
| 108 | + |
| 109 | + false |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + def includes_arguments_permitted?(node, argument) |
| 114 | + node.arguments[1..].all? do |argument_node| |
| 115 | + permitted_include_argument?(argument_node, argument) |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + def permitted_include_argument?(argument_node, argument) |
| 120 | + return true if argument_node.type?(:dstr, :dsym) |
| 121 | + |
| 122 | + argument_node != argument && |
| 123 | + argument_node.each_descendant.none?(argument) |
| 124 | + end |
| 125 | + |
| 126 | + def part_of_example_scope?(node) |
| 127 | + node.each_ancestor.any? { |ancestor| example_scope?(ancestor) } |
| 128 | + end |
| 129 | + |
| 130 | + def example_scope?(node) |
| 131 | + subject?(node) || let?(node) || hook?(node) || example?(node) || |
| 132 | + include?(node) |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments