Skip to content

Commit 7dfbb34

Browse files
author
Landon Wilkins
committed
lint: discourage stubbing in before(:once) blocks, fixes SD-861
test plan: * see test commit Change-Id: I34433152aae370618c6bae1a23078e7c99c8b084 Reviewed-on: https://gerrit.instructure.com/74741 Tested-by: Jenkins Reviewed-by: Jon Jensen <jon@instructure.com> Product-Review: Jon Jensen <jon@instructure.com> QA-Review: Jon Jensen <jon@instructure.com>
1 parent 2666b07 commit 7dfbb34

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

gems/rubocop-canvas/lib/rubocop_canvas.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'rubocop_canvas/cops/lint/freeze_constants'
1818
require 'rubocop_canvas/cops/lint/sleep'
1919
require 'rubocop_canvas/cops/lint/specs_before_all'
20+
require 'rubocop_canvas/cops/lint/specs_before_once_stubs'
2021
require 'rubocop_canvas/cops/lint/specs_ensure_spec_extension'
2122
require 'rubocop_canvas/cops/lint/specs_execute_script'
2223
require 'rubocop_canvas/cops/lint/specs_f_over_fj'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module RuboCop
2+
module Cop
3+
module Lint
4+
class SpecsBeforeOnceStubs < Cop
5+
MSG = "Stubs in a `before(:once)` block won't carry over to the examples; you should move this to a `before(:each)`"
6+
# http://gofreerange.com/mocha/docs/Mocha/Mock.html
7+
# - stubs
8+
# - returns
9+
# homegrown:
10+
# - stub_file_data
11+
# - stub_kaltura
12+
# - stub_png_data
13+
STUB_METHODS = %i[
14+
stubs
15+
returns
16+
stub_file_data
17+
stub_kaltura
18+
stub_png_data
19+
].freeze
20+
21+
BLOCK_METHOD = :before
22+
BLOCK_ARG = :once
23+
24+
def on_send(node)
25+
_receiver, method_name, *_args = *node
26+
return unless STUB_METHODS.include? method_name
27+
return unless node.ancestors.find do |ancestor|
28+
ancestor.children[0].to_a[1] == BLOCK_METHOD &&
29+
ancestor.children[0].to_a[2].children[0] == BLOCK_ARG
30+
end
31+
add_offense node, :expression, MSG
32+
end
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)