Skip to content

Commit 8068d2d

Browse files
author
Landon Wilkins
committed
ensure spec files are *_spec.rb, fixes SD-594
test plan: * see test commit Change-Id: I8483b6979629642f9524f4fa7fecd17e402d56b4 Reviewed-on: https://gerrit.instructure.com/72669 Tested-by: Jenkins Reviewed-by: Jon Jensen <jon@instructure.com> Product-Review: Landon Wilkins <lwilkins@instructure.com> QA-Review: Landon Wilkins <lwilkins@instructure.com>
1 parent 55b7113 commit 8068d2d

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

gems/rubocop-canvas/lib/rubocop_canvas.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
require 'rubocop_canvas/helpers/comments'
88
require 'rubocop_canvas/helpers/consts'
99
require 'rubocop_canvas/helpers/diff_parser'
10+
require 'rubocop_canvas/helpers/file_meta'
1011
require 'rubocop_canvas/helpers/file_sieve'
1112
require 'rubocop_canvas/helpers/git_proxy'
1213
require 'rubocop_canvas/helpers/migration_tags'
1314

1415
# cops
1516
require 'rubocop_canvas/cops/datafixup/find_ids'
1617
require 'rubocop_canvas/cops/lint/freeze_constants'
18+
require 'rubocop_canvas/cops/lint/specs_ensure_spec_extension'
1719
require 'rubocop_canvas/cops/lint/specs_f_over_fj'
1820
require 'rubocop_canvas/cops/migration/concurrent_index'
1921
require 'rubocop_canvas/cops/migration/primary_key'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module RuboCop
2+
module Cop
3+
module Lint
4+
# most of this has been stolen from:
5+
# https://github.com/nevir/rubocop-rspec/blob/master/lib/rubocop/rspec/top_level_describe.rb
6+
# https://github.com/nevir/rubocop-rspec/blob/9aa33ee7014e8d6d580b12fe2651b32ccdaa7a92/lib/rubocop/cop/rspec/file_path.rb
7+
class SpecsEnsureSpecExtension < Cop
8+
include RuboCop::Cop::FileMeta
9+
10+
MSG = "Spec files need to end with \"_spec.rb\" for rspec to find and run them."
11+
METHODS = [:context, :describe].freeze
12+
SPEC_FILE_NAME_REGEX = /_spec\.rb$/
13+
14+
def on_send(node)
15+
return unless top_level_describe?(node)
16+
return if file_name =~ SPEC_FILE_NAME_REGEX
17+
add_offense node, :expression, MSG
18+
end
19+
20+
private
21+
22+
def top_level_describe?(node)
23+
_receiver, method_name, *_args = *node
24+
return false unless METHODS.include?(method_name)
25+
top_level_nodes.include?(node)
26+
end
27+
28+
def top_level_nodes
29+
nodes = describe_statement_children(root_node)
30+
# If we have no top level describe statements, we need to check any
31+
# blocks on the top level (e.g. after a require).
32+
if nodes.empty?
33+
nodes = node_children(root_node).map do |child|
34+
describe_statement_children(child) if child.type == :block
35+
end.flatten.compact
36+
end
37+
38+
nodes
39+
end
40+
41+
def describe_statement_children(node)
42+
node_children(node).select do |element|
43+
element.type == :send && METHODS.include?(element.children[1])
44+
end
45+
end
46+
47+
def node_children(node)
48+
node.children.select { |e| e.is_a? Parser::AST::Node }
49+
end
50+
51+
def root_node
52+
processed_source.ast
53+
end
54+
end
55+
end
56+
end
57+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module RuboCop
2+
module Cop
3+
module FileMeta
4+
def file_name
5+
processed_source.buffer.name.split('/').last
6+
end
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)