Skip to content

Commit f6c277f

Browse files
committed
Add Rubocop style standards to build.
- Move custom excludes and non-ideal settings to .rubocop_todo.yml - Inherit the todo file from the main rubocop config - Add generic description about matcher module. We should consider moving the individual generator methods into this module. This will allow us to properly document the matchers as in rspec-expectations. We can also apply autoload if necessary at that point.
1 parent eedb305 commit f6c277f

32 files changed

+1409
-1167
lines changed

.rubocop.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
AccessModifierIndentation:
4+
EnforcedStyle: outdent
5+
6+
# "Use alias_method instead of alias"
7+
# We're fine with `alias`.
8+
Alias:
9+
Enabled: false
10+
11+
AlignParameters:
12+
EnforcedStyle: with_first_parameter
13+
14+
# "Avoid the use of the case equality operator ==="
15+
# We prefer using `Class#===` over `Object#is_a?` because `Class#===`
16+
# is less likely to be monkey patched than `is_a?` on a user object.
17+
CaseEquality:
18+
Enabled: false
19+
20+
CollectionMethods:
21+
PreferredMethods:
22+
reduce: 'inject'
23+
24+
# We use YARD to enforce documentation. It works better than rubocop's
25+
# enforcement...rubocop complains about the places we re-open
26+
# `RSpec::Expectations` and `RSpec::Matchers` w/o having doc commments.
27+
Documentation:
28+
Enabled: false
29+
30+
# We still support 1.8.7 which requires trailing dots
31+
DotPosition:
32+
EnforcedStyle: trailing
33+
34+
DoubleNegation:
35+
Enabled: false
36+
37+
# each_with_object is unavailable on 1.8.7 so we have to disable this one.
38+
EachWithObject:
39+
Enabled: false
40+
41+
Encoding:
42+
EnforcedStyle: when_needed
43+
44+
FormatString:
45+
EnforcedStyle: percent
46+
47+
# As long as we support ruby 1.8.7 we have to use hash rockets.
48+
HashSyntax:
49+
EnforcedStyle: hash_rockets
50+
51+
# We can't use the new lambda syntax, since we still support 1.8.7.
52+
Lambda:
53+
Enabled: false
54+
55+
# Who cares what we all the argument for binary operator methods?
56+
OpMethod:
57+
Enabled: false
58+
59+
PercentLiteralDelimiters:
60+
PreferredDelimiters:
61+
'%': () # double-quoted string
62+
'%i': '[]' # array of symbols
63+
'%q': () # single-quoted string
64+
'%Q': () # double-quoted string
65+
'%r': '{}' # regular expression pattern
66+
'%s': () # a symbol
67+
'%w': '[]' # array of single-quoted strings
68+
'%W': '[]' # array of double-quoted strings
69+
'%x': () # a shell command as a string
70+
71+
# We have too many special cases where we allow generator methods or prefer a
72+
# prefixed predicate due to it's improved readability.
73+
PredicateName:
74+
Enabled: false
75+
76+
# On 1.8 `proc` is `lambda`, so we use `Proc.new` to ensure we get real procs
77+
# on all supported versions.
78+
# http://batsov.com/articles/2014/02/04/the-elements-of-style-in-ruby-number-12-proc-vs-proc-dot-new/
79+
Proc:
80+
Enabled: false
81+
82+
RedundantReturn:
83+
AllowMultipleReturnValues: true
84+
85+
# We haven't adopted the `fail` to signal exceptions vs `raise` for re-raises
86+
# convention.
87+
SignalException:
88+
Enabled: false
89+
90+
# We don't care about single vs double qoutes.
91+
StringLiterals:
92+
Enabled: false
93+
94+
TrivialAccessors:
95+
AllowDSLWriters: true
96+
AllowPredicates: true
97+
ExactNameMatch: true

.rubocop_todo.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Warns when the class is excessively long.
2+
ClassLength:
3+
Max: 100
4+
5+
# Over time we'd like to get this down, but this is what we're at now.
6+
CyclomaticComplexity:
7+
Max: 10
8+
9+
# Over time we'd like to get this down, but this is what we're at now.
10+
LineLength:
11+
Max: 186
12+
13+
# Over time we'd like to get this down, but this is what we're at now.
14+
MethodLength:
15+
Max: 50
16+
17+
################################################################################
18+
# Individual File Exclusions
19+
################################################################################
20+
21+
AllCops:
22+
Exclude:
23+
# Templates are really ERB which Rubocop does not parse
24+
- 'lib/generators/rspec/*/templates/**/*'
25+
26+
FileName:
27+
Exclude:
28+
# I'm not sure why yet but we break convention for this one file
29+
- lib/rspec-rails.rb
30+
31+
GuardClause:
32+
Exclude:
33+
# This is getting triggered since the `unless` clause is the last
34+
# statement, which doesn't matter since it's the initializer. If it's
35+
# possible to break this up cleanly we can remove this exclude
36+
- lib/rspec/rails/matchers/have_http_status.rb
37+
38+
HandleExceptions:
39+
Exclude:
40+
# RSpec is tightly coupled to capybara right now, this should be
41+
# re-evaluted in the future. For now we allow the empty rescue
42+
- lib/rspec/rails/vendor/capybara.rb
43+
44+
IfUnlessModifier:
45+
Exclude:
46+
# Allow single line statement as the style matches the remainder of the file
47+
- lib/rspec/rails/vendor/capybara.rb
48+
49+
PerlBackrefs:
50+
Exclude:
51+
# We probably can refactor the backref out, but for now excluding it since
52+
# we can't use named matches in 1.8.7
53+
- lib/generators/rspec/scaffold/scaffold_generator.rb

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ if RUBY_VERSION <= '1.8.7'
3333
gem 'rubyzip', '< 1.0'
3434
end
3535

36+
gem 'rubocop', "~> 0.23.0", :platform => [:ruby_19, :ruby_20, :ruby_21]
37+
3638
custom_gemfile = File.expand_path("../Gemfile-custom", __FILE__)
3739
eval_gemfile custom_gemfile if File.exist?(custom_gemfile)
3840

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,10 @@ end
142142

143143
task :build => :verify_private_key_present
144144

145+
if RUBY_VERSION.to_f > 1.8
146+
require 'rubocop/rake_task'
147+
desc 'Run RuboCop on the lib directory'
148+
RuboCop::RakeTask.new(:rubocop) do |task|
149+
task.patterns = ['lib/**/*.rb']
150+
end
151+
end

lib/generators/rspec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ module Generators
2727
class GeneratedAttribute
2828
def input_type
2929
@input_type ||= if type == :text
30-
"textarea"
31-
else
32-
"input"
33-
end
30+
"textarea"
31+
else
32+
"input"
33+
end
3434
end
3535
end
3636
end

lib/generators/rspec/install/install_generator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module Rspec
66
module Generators
77
# @private
88
class InstallGenerator < ::Rails::Generators::Base
9-
109
desc <<DESC
1110
Description:
1211
Copy rspec files to your application.

lib/generators/rspec/model/model_generator.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ module Rspec
44
module Generators
55
# @private
66
class ModelGenerator < Base
7-
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7+
argument :attributes,
8+
:type => :array,
9+
:default => [],
10+
:banner => "field:type field:type"
811
class_option :fixture, :type => :boolean
912

1013
def create_model_spec
11-
template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
14+
template_file = File.join(
15+
'spec/models',
16+
class_path,
17+
"#{file_name}_spec.rb"
18+
)
19+
template 'model_spec.rb', template_file
1220
end
1321

1422
hook_for :fixture_replacement
1523

1624
def create_fixture_file
17-
if options[:fixture] && options[:fixture_replacement].nil?
18-
template 'fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
19-
end
25+
return unless missing_fixture_replacement?
26+
template 'fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml")
27+
end
28+
29+
private
30+
31+
def missing_fixture_replacement?
32+
options[:fixture] && options[:fixture_replacement].nil?
2033
end
2134
end
2235
end

0 commit comments

Comments
 (0)