sabat-rubocop 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +50 -0
- data/.rspec +1 -0
- data/.rubocop.yml +7 -0
- data/.travis.yml +7 -0
- data/.yardopts +2 -0
- data/CHANGELOG.md +268 -0
- data/CONTRIBUTING.md +16 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +324 -0
- data/Rakefile +29 -0
- data/bin/rubocop +22 -0
- data/config/default.yml +58 -0
- data/config/disabled.yml +5 -0
- data/config/enabled.yml +403 -0
- data/lib/rubocop.rb +116 -0
- data/lib/rubocop/cli.rb +407 -0
- data/lib/rubocop/config.rb +250 -0
- data/lib/rubocop/config_store.rb +39 -0
- data/lib/rubocop/cop/cop.rb +138 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +54 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +189 -0
- data/lib/rubocop/cop/lint/end_in_method.rb +30 -0
- data/lib/rubocop/cop/lint/ensure_return.rb +22 -0
- data/lib/rubocop/cop/lint/eval.rb +22 -0
- data/lib/rubocop/cop/lint/handle_exceptions.rb +20 -0
- data/lib/rubocop/cop/lint/literal_in_condition.rb +81 -0
- data/lib/rubocop/cop/lint/loop.rb +29 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +29 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +34 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +35 -0
- data/lib/rubocop/cop/lint/unused_local_variable.rb +32 -0
- data/lib/rubocop/cop/lint/void.rb +58 -0
- data/lib/rubocop/cop/offence.rb +136 -0
- data/lib/rubocop/cop/rails/validation.rb +30 -0
- data/lib/rubocop/cop/style/access_control.rb +58 -0
- data/lib/rubocop/cop/style/alias.rb +28 -0
- data/lib/rubocop/cop/style/align_parameters.rb +39 -0
- data/lib/rubocop/cop/style/and_or.rb +45 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +21 -0
- data/lib/rubocop/cop/style/ascii_identifiers.rb +22 -0
- data/lib/rubocop/cop/style/attr.rb +20 -0
- data/lib/rubocop/cop/style/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/style/avoid_for.rb +18 -0
- data/lib/rubocop/cop/style/avoid_global_vars.rb +65 -0
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +21 -0
- data/lib/rubocop/cop/style/avoid_perlisms.rb +50 -0
- data/lib/rubocop/cop/style/begin_block.rb +18 -0
- data/lib/rubocop/cop/style/block_comments.rb +20 -0
- data/lib/rubocop/cop/style/block_nesting.rb +47 -0
- data/lib/rubocop/cop/style/blocks.rb +27 -0
- data/lib/rubocop/cop/style/case_equality.rb +22 -0
- data/lib/rubocop/cop/style/case_indentation.rb +28 -0
- data/lib/rubocop/cop/style/character_literal.rb +37 -0
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +33 -0
- data/lib/rubocop/cop/style/class_methods.rb +22 -0
- data/lib/rubocop/cop/style/collection_methods.rb +56 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +29 -0
- data/lib/rubocop/cop/style/constant_name.rb +31 -0
- data/lib/rubocop/cop/style/def_parentheses.rb +70 -0
- data/lib/rubocop/cop/style/documentation.rb +58 -0
- data/lib/rubocop/cop/style/dot_position.rb +25 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +26 -0
- data/lib/rubocop/cop/style/empty_lines.rb +40 -0
- data/lib/rubocop/cop/style/empty_literal.rb +53 -0
- data/lib/rubocop/cop/style/encoding.rb +29 -0
- data/lib/rubocop/cop/style/end_block.rb +18 -0
- data/lib/rubocop/cop/style/end_of_line.rb +23 -0
- data/lib/rubocop/cop/style/favor_join.rb +29 -0
- data/lib/rubocop/cop/style/favor_modifier.rb +118 -0
- data/lib/rubocop/cop/style/favor_sprintf.rb +28 -0
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +54 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +47 -0
- data/lib/rubocop/cop/style/if_then_else.rb +29 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -0
- data/lib/rubocop/cop/style/lambda.rb +47 -0
- data/lib/rubocop/cop/style/leading_comment_space.rb +25 -0
- data/lib/rubocop/cop/style/line_continuation.rb +26 -0
- data/lib/rubocop/cop/style/line_length.rb +30 -0
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +61 -0
- data/lib/rubocop/cop/style/method_call_parentheses.rb +22 -0
- data/lib/rubocop/cop/style/method_length.rb +57 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +47 -0
- data/lib/rubocop/cop/style/not.rb +24 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +20 -0
- data/lib/rubocop/cop/style/op_method.rb +29 -0
- data/lib/rubocop/cop/style/parameter_lists.rb +42 -0
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +42 -0
- data/lib/rubocop/cop/style/proc.rb +30 -0
- data/lib/rubocop/cop/style/reduce_arguments.rb +34 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +39 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +55 -0
- data/lib/rubocop/cop/style/semicolon.rb +51 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +48 -0
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +69 -0
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +32 -0
- data/lib/rubocop/cop/style/string_literals.rb +36 -0
- data/lib/rubocop/cop/style/surrounding_space.rb +314 -0
- data/lib/rubocop/cop/style/symbol_array.rb +31 -0
- data/lib/rubocop/cop/style/symbol_name.rb +27 -0
- data/lib/rubocop/cop/style/tab.rb +25 -0
- data/lib/rubocop/cop/style/ternary_operator.rb +49 -0
- data/lib/rubocop/cop/style/trailing_whitespace.rb +24 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +32 -0
- data/lib/rubocop/cop/style/unless_else.rb +26 -0
- data/lib/rubocop/cop/style/variable_interpolation.rb +32 -0
- data/lib/rubocop/cop/style/when_then.rb +25 -0
- data/lib/rubocop/cop/style/while_until_do.rb +45 -0
- data/lib/rubocop/cop/style/word_array.rb +44 -0
- data/lib/rubocop/cop/util.rb +27 -0
- data/lib/rubocop/cop/variable_inspector.rb +280 -0
- data/lib/rubocop/formatter/base_formatter.rb +119 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +21 -0
- data/lib/rubocop/formatter/emacs_style_formatter.rb +17 -0
- data/lib/rubocop/formatter/formatter_set.rb +77 -0
- data/lib/rubocop/formatter/json_formatter.rb +76 -0
- data/lib/rubocop/formatter/progress_formatter.rb +63 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +62 -0
- data/lib/rubocop/version.rb +21 -0
- data/rubocop.gemspec +36 -0
- data/spec/.rubocop.yml +5 -0
- data/spec/project_spec.rb +24 -0
- data/spec/rubocop/cli_spec.rb +906 -0
- data/spec/rubocop/config_spec.rb +470 -0
- data/spec/rubocop/config_store_spec.rb +66 -0
- data/spec/rubocop/cops/cop_spec.rb +38 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +111 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +333 -0
- data/spec/rubocop/cops/lint/end_in_method_spec.rb +35 -0
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/lint/eval_spec.rb +41 -0
- data/spec/rubocop/cops/lint/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/lint/literal_in_condition_spec.rb +42 -0
- data/spec/rubocop/cops/lint/loop_spec.rb +33 -0
- data/spec/rubocop/cops/lint/rescue_exception_spec.rb +127 -0
- data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +243 -0
- data/spec/rubocop/cops/lint/unreachable_code_spec.rb +69 -0
- data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +497 -0
- data/spec/rubocop/cops/lint/void_spec.rb +63 -0
- data/spec/rubocop/cops/offence_spec.rb +133 -0
- data/spec/rubocop/cops/rails/validation_spec.rb +27 -0
- data/spec/rubocop/cops/style/access_control_spec.rb +142 -0
- data/spec/rubocop/cops/style/alias_spec.rb +47 -0
- data/spec/rubocop/cops/style/align_parameters_spec.rb +199 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +39 -0
- data/spec/rubocop/cops/style/ascii_comments_spec.rb +28 -0
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +28 -0
- data/spec/rubocop/cops/style/attr_spec.rb +20 -0
- data/spec/rubocop/cops/style/avoid_class_vars_spec.rb +27 -0
- data/spec/rubocop/cops/style/avoid_for_spec.rb +37 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +34 -0
- data/spec/rubocop/cops/style/avoid_perl_backrefs_spec.rb +20 -0
- data/spec/rubocop/cops/style/avoid_perlisms_spec.rb +47 -0
- data/spec/rubocop/cops/style/begin_block_spec.rb +19 -0
- data/spec/rubocop/cops/style/block_comments_spec.rb +27 -0
- data/spec/rubocop/cops/style/block_nesting_spec.rb +159 -0
- data/spec/rubocop/cops/style/blocks_spec.rb +35 -0
- data/spec/rubocop/cops/style/case_equality_spec.rb +18 -0
- data/spec/rubocop/cops/style/case_indentation_spec.rb +88 -0
- data/spec/rubocop/cops/style/character_literal_spec.rb +28 -0
- data/spec/rubocop/cops/style/class_and_module_camel_case_spec.rb +46 -0
- data/spec/rubocop/cops/style/class_methods_spec.rb +51 -0
- data/spec/rubocop/cops/style/collection_methods_spec.rb +41 -0
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +55 -0
- data/spec/rubocop/cops/style/constant_name_spec.rb +56 -0
- data/spec/rubocop/cops/style/def_with_parentheses_spec.rb +40 -0
- data/spec/rubocop/cops/style/def_without_parentheses_spec.rb +34 -0
- data/spec/rubocop/cops/style/documentation_spec.rb +79 -0
- data/spec/rubocop/cops/style/dot_position_spec.rb +30 -0
- data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +85 -0
- data/spec/rubocop/cops/style/empty_lines_spec.rb +40 -0
- data/spec/rubocop/cops/style/empty_literal_spec.rb +91 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +49 -0
- data/spec/rubocop/cops/style/end_block_spec.rb +19 -0
- data/spec/rubocop/cops/style/end_of_line_spec.rb +25 -0
- data/spec/rubocop/cops/style/favor_join_spec.rb +37 -0
- data/spec/rubocop/cops/style/favor_modifier_spec.rb +160 -0
- data/spec/rubocop/cops/style/favor_sprintf_spec.rb +53 -0
- data/spec/rubocop/cops/style/favor_unless_over_negated_if_spec.rb +64 -0
- data/spec/rubocop/cops/style/favor_until_over_negated_while_spec.rb +47 -0
- data/spec/rubocop/cops/style/hash_syntax_spec.rb +51 -0
- data/spec/rubocop/cops/style/if_with_semicolon_spec.rb +25 -0
- data/spec/rubocop/cops/style/lambda_spec.rb +45 -0
- data/spec/rubocop/cops/style/leading_comment_space_spec.rb +65 -0
- data/spec/rubocop/cops/style/line_continuation_spec.rb +26 -0
- data/spec/rubocop/cops/style/line_length_spec.rb +25 -0
- data/spec/rubocop/cops/style/method_and_variable_snake_case_spec.rb +95 -0
- data/spec/rubocop/cops/style/method_call_parentheses_spec.rb +25 -0
- data/spec/rubocop/cops/style/method_length_spec.rb +151 -0
- data/spec/rubocop/cops/style/multiline_if_then_spec.rb +97 -0
- data/spec/rubocop/cops/style/not_spec.rb +28 -0
- data/spec/rubocop/cops/style/numeric_literals_spec.rb +51 -0
- data/spec/rubocop/cops/style/one_line_conditional_spec.rb +18 -0
- data/spec/rubocop/cops/style/op_method_spec.rb +80 -0
- data/spec/rubocop/cops/style/parameter_lists_spec.rb +49 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +59 -0
- data/spec/rubocop/cops/style/proc_spec.rb +28 -0
- data/spec/rubocop/cops/style/reduce_arguments_spec.rb +59 -0
- data/spec/rubocop/cops/style/regexp_literal_spec.rb +83 -0
- data/spec/rubocop/cops/style/rescue_modifier_spec.rb +122 -0
- data/spec/rubocop/cops/style/semicolon_spec.rb +95 -0
- data/spec/rubocop/cops/style/single_line_methods_spec.rb +54 -0
- data/spec/rubocop/cops/style/space_after_colon_spec.rb +29 -0
- data/spec/rubocop/cops/style/space_after_comma_spec.rb +31 -0
- data/spec/rubocop/cops/style/space_after_control_keyword_spec.rb +69 -0
- data/spec/rubocop/cops/style/space_after_semicolon_spec.rb +24 -0
- data/spec/rubocop/cops/style/space_around_braces_spec.rb +49 -0
- data/spec/rubocop/cops/style/space_around_equals_in_default_parameter_spec.rb +34 -0
- data/spec/rubocop/cops/style/space_around_operators_spec.rb +216 -0
- data/spec/rubocop/cops/style/space_inside_brackets_spec.rb +51 -0
- data/spec/rubocop/cops/style/space_inside_hash_literal_braces_spec.rb +99 -0
- data/spec/rubocop/cops/style/space_inside_parens_spec.rb +33 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +62 -0
- data/spec/rubocop/cops/style/symbol_array_spec.rb +45 -0
- data/spec/rubocop/cops/style/symbol_name_spec.rb +122 -0
- data/spec/rubocop/cops/style/tab_spec.rb +23 -0
- data/spec/rubocop/cops/style/ternary_operator_spec.rb +42 -0
- data/spec/rubocop/cops/style/trailing_whitespace_spec.rb +29 -0
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +338 -0
- data/spec/rubocop/cops/style/unless_else_spec.rb +31 -0
- data/spec/rubocop/cops/style/variable_interpolation_spec.rb +53 -0
- data/spec/rubocop/cops/style/when_then_spec.rb +40 -0
- data/spec/rubocop/cops/style/while_until_do_spec.rb +47 -0
- data/spec/rubocop/cops/style/word_array_spec.rb +61 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +374 -0
- data/spec/rubocop/formatter/base_formatter_spec.rb +190 -0
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +70 -0
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +32 -0
- data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
- data/spec/rubocop/formatter/json_formatter_spec.rb +142 -0
- data/spec/rubocop/formatter/progress_formatter_spec.rb +196 -0
- data/spec/rubocop/formatter/simple_text_formatter_spec.rb +74 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- data/spec/support/mri_syntax_checker.rb +69 -0
- data/spec/support/shared_examples.rb +33 -0
- metadata +517 -0
@@ -0,0 +1,470 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
DEFAULT_CONFIG = Rubocop::Config.load_file('config/default.yml')
|
6
|
+
|
7
|
+
describe Rubocop::Config do
|
8
|
+
include FileHelper
|
9
|
+
|
10
|
+
subject(:configuration) { Rubocop::Config.new(hash, loaded_path) }
|
11
|
+
let(:hash) { {} }
|
12
|
+
let(:loaded_path) { 'example/.rubocop.yml' }
|
13
|
+
|
14
|
+
before { Rubocop::ConfigStore.prepare }
|
15
|
+
|
16
|
+
describe '.configuration_file_for', :isolated_environment do
|
17
|
+
subject(:configuration_file_for) do
|
18
|
+
Rubocop::Config.configuration_file_for(dir_path)
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when no config file exists in ancestor directories' do
|
22
|
+
let(:dir_path) { 'dir' }
|
23
|
+
before { create_file('dir/example.rb', '') }
|
24
|
+
|
25
|
+
context 'but a config file exists in home directory' do
|
26
|
+
before { create_file('~/.rubocop.yml', '') }
|
27
|
+
|
28
|
+
it 'returns the path to the file in home directory' do
|
29
|
+
expect(configuration_file_for).to end_with('home/.rubocop.yml')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'and no config file exists in home directory' do
|
34
|
+
it 'falls back to the provided default file' do
|
35
|
+
expect(configuration_file_for).to end_with('config/default.yml')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when a config file exists in the parent directory' do
|
41
|
+
let(:dir_path) { 'dir' }
|
42
|
+
|
43
|
+
before do
|
44
|
+
create_file('dir/example.rb', '')
|
45
|
+
create_file('.rubocop.yml', '')
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns the path to that configuration file' do
|
49
|
+
expect(configuration_file_for).to end_with('work/.rubocop.yml')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when multiple config files exist in ancestor directories' do
|
54
|
+
let(:dir_path) { 'dir' }
|
55
|
+
|
56
|
+
before do
|
57
|
+
create_file('dir/example.rb', '')
|
58
|
+
create_file('dir/.rubocop.yml', '')
|
59
|
+
create_file('.rubocop.yml', '')
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'prefers closer config file' do
|
63
|
+
expect(configuration_file_for).to end_with('dir/.rubocop.yml')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '.configuration_from_file', :isolated_environment do
|
69
|
+
subject(:configuration_from_file) do
|
70
|
+
Rubocop::Config.configuration_from_file(file_path)
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with any config file' do
|
74
|
+
let(:file_path) { '.rubocop.yml' }
|
75
|
+
|
76
|
+
before do
|
77
|
+
create_file(file_path, ['Encoding:',
|
78
|
+
' Enabled: false'])
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'returns a configuration inheriting from default.yml' do
|
82
|
+
expect(configuration_from_file)
|
83
|
+
.to eq(DEFAULT_CONFIG.merge('Encoding' => { 'Enabled' => false }))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when multiple config files exist in ancestor directories' do
|
88
|
+
let(:file_path) { 'dir/.rubocop.yml' }
|
89
|
+
|
90
|
+
before do
|
91
|
+
create_file('.rubocop.yml',
|
92
|
+
['AllCops:',
|
93
|
+
' Excludes:',
|
94
|
+
' - vendor/**',
|
95
|
+
])
|
96
|
+
|
97
|
+
create_file(file_path,
|
98
|
+
['AllCops:',
|
99
|
+
' Excludes: []',
|
100
|
+
])
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'gets AllCops/Exclude from the highest directory level' do
|
104
|
+
excludes = configuration_from_file['AllCops']['Excludes']
|
105
|
+
expect(excludes).to eq(['../vendor/**'])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'when a file inherits from a parent file' do
|
110
|
+
let(:file_path) { 'dir/.rubocop.yml' }
|
111
|
+
|
112
|
+
before do
|
113
|
+
create_file('.rubocop.yml',
|
114
|
+
['AllCops:',
|
115
|
+
' Excludes:',
|
116
|
+
' - vendor/**',
|
117
|
+
' - !ruby/regexp /[A-Z]/',
|
118
|
+
])
|
119
|
+
|
120
|
+
create_file(file_path, ['inherit_from: ../.rubocop.yml'])
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'gets AllCops/Exclude relative to its own directory' do
|
124
|
+
excludes = configuration_from_file['AllCops']['Excludes']
|
125
|
+
expect(excludes).to eq(['../vendor/**', /[A-Z]/])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'when a file inherits from a sibling file' do
|
130
|
+
let(:file_path) { 'dir/.rubocop.yml' }
|
131
|
+
|
132
|
+
before do
|
133
|
+
create_file('src/.rubocop.yml',
|
134
|
+
['AllCops:',
|
135
|
+
' Excludes:',
|
136
|
+
' - vendor/**',
|
137
|
+
])
|
138
|
+
|
139
|
+
create_file(file_path, ['inherit_from: ../src/.rubocop.yml'])
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'gets AllCops/Exclude relative to its own directory' do
|
143
|
+
excludes = configuration_from_file['AllCops']['Excludes']
|
144
|
+
expect(excludes).to eq(['../src/vendor/**'])
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'when a file inherits from a parent and grandparent file' do
|
149
|
+
let(:file_path) { 'dir/subdir/.rubocop.yml' }
|
150
|
+
|
151
|
+
before do
|
152
|
+
create_file('dir/subdir/example.rb', '')
|
153
|
+
|
154
|
+
create_file('.rubocop.yml',
|
155
|
+
['LineLength:',
|
156
|
+
' Enabled: false',
|
157
|
+
' Max: 77'])
|
158
|
+
|
159
|
+
create_file('dir/.rubocop.yml',
|
160
|
+
['inherit_from: ../.rubocop.yml',
|
161
|
+
'',
|
162
|
+
'MethodLength:',
|
163
|
+
' Enabled: true',
|
164
|
+
' CountComments: false',
|
165
|
+
' Max: 10'
|
166
|
+
])
|
167
|
+
|
168
|
+
create_file(file_path,
|
169
|
+
['inherit_from: ../.rubocop.yml',
|
170
|
+
'',
|
171
|
+
'LineLength:',
|
172
|
+
' Enabled: true',
|
173
|
+
'',
|
174
|
+
'MethodLength:',
|
175
|
+
' Max: 5'
|
176
|
+
])
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'returns the ancestor configuration plus local overrides' do
|
180
|
+
expect(configuration_from_file)
|
181
|
+
.to eq(DEFAULT_CONFIG.merge('LineLength' => {
|
182
|
+
'Enabled' => true,
|
183
|
+
'Max' => 77
|
184
|
+
},
|
185
|
+
'MethodLength' => {
|
186
|
+
'Enabled' => true,
|
187
|
+
'CountComments' => false,
|
188
|
+
'Max' => 5
|
189
|
+
}))
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'when a file inherits from two configurations' do
|
194
|
+
let(:file_path) { '.rubocop.yml' }
|
195
|
+
|
196
|
+
before do
|
197
|
+
create_file('example.rb', '')
|
198
|
+
|
199
|
+
create_file('normal.yml',
|
200
|
+
['MethodLength:',
|
201
|
+
' Enabled: false',
|
202
|
+
' CountComments: true',
|
203
|
+
' Max: 79'])
|
204
|
+
|
205
|
+
create_file('special.yml',
|
206
|
+
['MethodLength:',
|
207
|
+
' Enabled: false',
|
208
|
+
' Max: 200'])
|
209
|
+
|
210
|
+
create_file(file_path,
|
211
|
+
['inherit_from:',
|
212
|
+
' - normal.yml',
|
213
|
+
' - special.yml',
|
214
|
+
'',
|
215
|
+
'MethodLength:',
|
216
|
+
' Enabled: true'
|
217
|
+
])
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'returns values from the last one when possible' do
|
221
|
+
expect(configuration_from_file['MethodLength'])
|
222
|
+
.to eq('Enabled' => true, # overridden in .rubocop.yml
|
223
|
+
'CountComments' => true, # only defined in normal.yml
|
224
|
+
'Max' => 200 # special.yml takes precedence
|
225
|
+
)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe '.load_file', :isolated_environment do
|
231
|
+
subject(:load_file) do
|
232
|
+
Rubocop::Config.load_file(configuration_path)
|
233
|
+
end
|
234
|
+
|
235
|
+
let(:configuration_path) { '.rubocop.yml' }
|
236
|
+
|
237
|
+
it 'returns a configuration loaded from the passed path' do
|
238
|
+
create_file(configuration_path, [
|
239
|
+
'Encoding:',
|
240
|
+
' Enabled: true',
|
241
|
+
])
|
242
|
+
configuration = load_file
|
243
|
+
expect(configuration['Encoding']).to eq({
|
244
|
+
'Enabled' => true
|
245
|
+
})
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe '.merge' do
|
250
|
+
subject(:merge) { Rubocop::Config.merge(base, derived) }
|
251
|
+
|
252
|
+
let(:base) do
|
253
|
+
{
|
254
|
+
'AllCops' => {
|
255
|
+
'Includes' => ['**/*.gemspec', '**/Rakefile'],
|
256
|
+
'Excludes' => []
|
257
|
+
}
|
258
|
+
}
|
259
|
+
end
|
260
|
+
let(:derived) do
|
261
|
+
{ 'AllCops' => { 'Excludes' => ['example.rb', 'exclude_*'] } }
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'returns a recursive merge of its two arguments' do
|
265
|
+
expect(merge).to eq('AllCops' => {
|
266
|
+
'Includes' => ['**/*.gemspec', '**/Rakefile'],
|
267
|
+
'Excludes' => ['example.rb', 'exclude_*']
|
268
|
+
})
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
describe '#validate', :isolated_environment do
|
273
|
+
# TODO: Because Config.load_file now outputs the validation warning,
|
274
|
+
# it is inserting text into the rspec test output here.
|
275
|
+
# The next 2 lines should be removed eventually.
|
276
|
+
before(:each) { $stdout = StringIO.new }
|
277
|
+
after(:each) { $stdout = STDOUT }
|
278
|
+
|
279
|
+
subject(:configuration) do
|
280
|
+
Rubocop::Config.load_file(configuration_path)
|
281
|
+
end
|
282
|
+
|
283
|
+
let(:configuration_path) { '.rubocop.yml' }
|
284
|
+
|
285
|
+
context 'when the configuration includes any unrecognized cop name' do
|
286
|
+
before do
|
287
|
+
create_file(configuration_path, [
|
288
|
+
'LyneLenth:',
|
289
|
+
' Enabled: true',
|
290
|
+
' Max: 100',
|
291
|
+
])
|
292
|
+
end
|
293
|
+
|
294
|
+
it 'raises validation error' do
|
295
|
+
expect do
|
296
|
+
configuration.validate
|
297
|
+
end.to raise_error(Rubocop::Config::ValidationError) do |error|
|
298
|
+
expect(error.message).to start_with('unrecognized cop LyneLenth')
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
context 'when the configuration includes any unrecognized parameter' do
|
304
|
+
before do
|
305
|
+
create_file(configuration_path, [
|
306
|
+
'LineLength:',
|
307
|
+
' Enabled: true',
|
308
|
+
' Min: 10',
|
309
|
+
])
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'raises validation error' do
|
313
|
+
expect do
|
314
|
+
configuration.validate
|
315
|
+
end.to raise_error(Rubocop::Config::ValidationError) do |error|
|
316
|
+
expect(error.message).to
|
317
|
+
start_with('unrecognized parameter LineLength:Min')
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
describe '#file_to_include?' do
|
324
|
+
let(:hash) do
|
325
|
+
{
|
326
|
+
'AllCops' => {
|
327
|
+
'Includes' => ['Gemfile', 'config/unicorn.rb.example']
|
328
|
+
}
|
329
|
+
}
|
330
|
+
end
|
331
|
+
|
332
|
+
let(:loaded_path) { '/home/foo/project/.rubocop.yml' }
|
333
|
+
|
334
|
+
context 'when the passed path matches any of patterns to include' do
|
335
|
+
it 'returns true' do
|
336
|
+
file_path = '/home/foo/project/Gemfile'
|
337
|
+
expect(configuration.file_to_include?(file_path)).to be_true
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
context 'when the passed path does not match any of patterns to include' do
|
342
|
+
it 'returns false' do
|
343
|
+
file_path = '/home/foo/project/Gemfile.lock'
|
344
|
+
expect(configuration.file_to_include?(file_path)).to be_false
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
describe '#file_to_exclude?' do
|
350
|
+
let(:hash) do
|
351
|
+
{
|
352
|
+
'AllCops' => {
|
353
|
+
'Excludes' => ['log/*']
|
354
|
+
}
|
355
|
+
}
|
356
|
+
end
|
357
|
+
|
358
|
+
let(:loaded_path) { '/home/foo/project/.rubocop.yml' }
|
359
|
+
|
360
|
+
context 'when the passed path matches any of patterns to exclude' do
|
361
|
+
it 'returns true' do
|
362
|
+
file_path = '/home/foo/project/log/foo.rb'
|
363
|
+
expect(configuration.file_to_exclude?(file_path)).to be_true
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
context 'when the passed path does not match any of patterns to exclude' do
|
368
|
+
it 'returns false' do
|
369
|
+
file_path = '/home/foo/project/log_file.rb'
|
370
|
+
expect(configuration.file_to_exclude?(file_path)).to be_false
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe '#patterns_to_include' do
|
376
|
+
subject(:patterns_to_include) do
|
377
|
+
configuration = Rubocop::Config.new(hash, loaded_path)
|
378
|
+
configuration.patterns_to_include
|
379
|
+
end
|
380
|
+
|
381
|
+
let(:hash) { {} }
|
382
|
+
let(:loaded_path) { 'example/.rubocop.yml' }
|
383
|
+
|
384
|
+
context 'when config file has AllCops => Includes key' do
|
385
|
+
let(:hash) do
|
386
|
+
{
|
387
|
+
'AllCops' => {
|
388
|
+
'Includes' => ['Gemfile', 'config/unicorn.rb.example']
|
389
|
+
}
|
390
|
+
}
|
391
|
+
end
|
392
|
+
|
393
|
+
it 'returns the Includes value' do
|
394
|
+
expect(patterns_to_include).to eq([
|
395
|
+
'Gemfile',
|
396
|
+
'config/unicorn.rb.example'
|
397
|
+
])
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe '#patterns_to_exclude' do
|
403
|
+
subject(:patterns_to_exclude) do
|
404
|
+
configuration = Rubocop::Config.new(hash, loaded_path)
|
405
|
+
configuration.patterns_to_exclude
|
406
|
+
end
|
407
|
+
|
408
|
+
let(:hash) { {} }
|
409
|
+
let(:loaded_path) { 'example/.rubocop.yml' }
|
410
|
+
|
411
|
+
context 'when config file has AllCops => Excludes key' do
|
412
|
+
let(:hash) do
|
413
|
+
{
|
414
|
+
'AllCops' => {
|
415
|
+
'Excludes' => ['log/*']
|
416
|
+
}
|
417
|
+
}
|
418
|
+
end
|
419
|
+
|
420
|
+
it 'returns the Excludes value' do
|
421
|
+
expect(patterns_to_exclude).to eq(['log/*'])
|
422
|
+
end
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
describe 'configuration for SymbolArray', :isolated_environment do
|
427
|
+
before do
|
428
|
+
create_file('example.rb', '# encoding: utf-8')
|
429
|
+
end
|
430
|
+
|
431
|
+
context 'when no config file exists for the target file' do
|
432
|
+
it 'is disabled' do
|
433
|
+
configuration = Rubocop::ConfigStore.for('example.rb')
|
434
|
+
expect(configuration.cop_enabled?('SymbolArray')).to be_false
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
context 'when a config file which does not mention SymbolArray exists' do
|
439
|
+
it 'is disabled' do
|
440
|
+
create_file('.rubocop.yml', [
|
441
|
+
'LineLength:',
|
442
|
+
' Max: 79'
|
443
|
+
])
|
444
|
+
configuration = Rubocop::ConfigStore.for('example.rb')
|
445
|
+
expect(configuration.cop_enabled?('SymbolArray')).to be_false
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
context 'when a config file which explicitly enables SymbolArray exists' do
|
450
|
+
it 'is enabled' do
|
451
|
+
create_file('.rubocop.yml', [
|
452
|
+
'SymbolArray:',
|
453
|
+
' Enabled: true'
|
454
|
+
])
|
455
|
+
configuration = Rubocop::ConfigStore.for('example.rb')
|
456
|
+
expect(configuration.cop_enabled?('SymbolArray')).to be_true
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
describe 'configuration for SymbolName' do
|
462
|
+
describe 'AllowCamelCase' do
|
463
|
+
it 'is enabled by default' do
|
464
|
+
default_config = Rubocop::Config.default_configuration
|
465
|
+
symbol_name_config = default_config.for_cop('SymbolName')
|
466
|
+
expect(symbol_name_config['AllowCamelCase']).to be_true
|
467
|
+
end
|
468
|
+
end
|
469
|
+
end
|
470
|
+
end
|