sabat-rubocop 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. data/.gitignore +50 -0
  2. data/.rspec +1 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +7 -0
  5. data/.yardopts +2 -0
  6. data/CHANGELOG.md +268 -0
  7. data/CONTRIBUTING.md +16 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +324 -0
  11. data/Rakefile +29 -0
  12. data/bin/rubocop +22 -0
  13. data/config/default.yml +58 -0
  14. data/config/disabled.yml +5 -0
  15. data/config/enabled.yml +403 -0
  16. data/lib/rubocop.rb +116 -0
  17. data/lib/rubocop/cli.rb +407 -0
  18. data/lib/rubocop/config.rb +250 -0
  19. data/lib/rubocop/config_store.rb +39 -0
  20. data/lib/rubocop/cop/cop.rb +138 -0
  21. data/lib/rubocop/cop/lint/assignment_in_condition.rb +54 -0
  22. data/lib/rubocop/cop/lint/end_alignment.rb +189 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +30 -0
  24. data/lib/rubocop/cop/lint/ensure_return.rb +22 -0
  25. data/lib/rubocop/cop/lint/eval.rb +22 -0
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +20 -0
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +81 -0
  28. data/lib/rubocop/cop/lint/loop.rb +29 -0
  29. data/lib/rubocop/cop/lint/rescue_exception.rb +29 -0
  30. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +34 -0
  31. data/lib/rubocop/cop/lint/unreachable_code.rb +35 -0
  32. data/lib/rubocop/cop/lint/unused_local_variable.rb +32 -0
  33. data/lib/rubocop/cop/lint/void.rb +58 -0
  34. data/lib/rubocop/cop/offence.rb +136 -0
  35. data/lib/rubocop/cop/rails/validation.rb +30 -0
  36. data/lib/rubocop/cop/style/access_control.rb +58 -0
  37. data/lib/rubocop/cop/style/alias.rb +28 -0
  38. data/lib/rubocop/cop/style/align_parameters.rb +39 -0
  39. data/lib/rubocop/cop/style/and_or.rb +45 -0
  40. data/lib/rubocop/cop/style/ascii_comments.rb +21 -0
  41. data/lib/rubocop/cop/style/ascii_identifiers.rb +22 -0
  42. data/lib/rubocop/cop/style/attr.rb +20 -0
  43. data/lib/rubocop/cop/style/avoid_class_vars.rb +20 -0
  44. data/lib/rubocop/cop/style/avoid_for.rb +18 -0
  45. data/lib/rubocop/cop/style/avoid_global_vars.rb +65 -0
  46. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +21 -0
  47. data/lib/rubocop/cop/style/avoid_perlisms.rb +50 -0
  48. data/lib/rubocop/cop/style/begin_block.rb +18 -0
  49. data/lib/rubocop/cop/style/block_comments.rb +20 -0
  50. data/lib/rubocop/cop/style/block_nesting.rb +47 -0
  51. data/lib/rubocop/cop/style/blocks.rb +27 -0
  52. data/lib/rubocop/cop/style/case_equality.rb +22 -0
  53. data/lib/rubocop/cop/style/case_indentation.rb +28 -0
  54. data/lib/rubocop/cop/style/character_literal.rb +37 -0
  55. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +33 -0
  56. data/lib/rubocop/cop/style/class_methods.rb +22 -0
  57. data/lib/rubocop/cop/style/collection_methods.rb +56 -0
  58. data/lib/rubocop/cop/style/colon_method_call.rb +29 -0
  59. data/lib/rubocop/cop/style/constant_name.rb +31 -0
  60. data/lib/rubocop/cop/style/def_parentheses.rb +70 -0
  61. data/lib/rubocop/cop/style/documentation.rb +58 -0
  62. data/lib/rubocop/cop/style/dot_position.rb +25 -0
  63. data/lib/rubocop/cop/style/empty_line_between_defs.rb +26 -0
  64. data/lib/rubocop/cop/style/empty_lines.rb +40 -0
  65. data/lib/rubocop/cop/style/empty_literal.rb +53 -0
  66. data/lib/rubocop/cop/style/encoding.rb +29 -0
  67. data/lib/rubocop/cop/style/end_block.rb +18 -0
  68. data/lib/rubocop/cop/style/end_of_line.rb +23 -0
  69. data/lib/rubocop/cop/style/favor_join.rb +29 -0
  70. data/lib/rubocop/cop/style/favor_modifier.rb +118 -0
  71. data/lib/rubocop/cop/style/favor_sprintf.rb +28 -0
  72. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +54 -0
  73. data/lib/rubocop/cop/style/hash_syntax.rb +47 -0
  74. data/lib/rubocop/cop/style/if_then_else.rb +29 -0
  75. data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -0
  76. data/lib/rubocop/cop/style/lambda.rb +47 -0
  77. data/lib/rubocop/cop/style/leading_comment_space.rb +25 -0
  78. data/lib/rubocop/cop/style/line_continuation.rb +26 -0
  79. data/lib/rubocop/cop/style/line_length.rb +30 -0
  80. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +61 -0
  81. data/lib/rubocop/cop/style/method_call_parentheses.rb +22 -0
  82. data/lib/rubocop/cop/style/method_length.rb +57 -0
  83. data/lib/rubocop/cop/style/multiline_if_then.rb +47 -0
  84. data/lib/rubocop/cop/style/not.rb +24 -0
  85. data/lib/rubocop/cop/style/numeric_literals.rb +25 -0
  86. data/lib/rubocop/cop/style/one_line_conditional.rb +20 -0
  87. data/lib/rubocop/cop/style/op_method.rb +29 -0
  88. data/lib/rubocop/cop/style/parameter_lists.rb +42 -0
  89. data/lib/rubocop/cop/style/parentheses_around_condition.rb +42 -0
  90. data/lib/rubocop/cop/style/proc.rb +30 -0
  91. data/lib/rubocop/cop/style/reduce_arguments.rb +34 -0
  92. data/lib/rubocop/cop/style/regexp_literal.rb +39 -0
  93. data/lib/rubocop/cop/style/rescue_modifier.rb +55 -0
  94. data/lib/rubocop/cop/style/semicolon.rb +51 -0
  95. data/lib/rubocop/cop/style/single_line_methods.rb +48 -0
  96. data/lib/rubocop/cop/style/space_after_comma_etc.rb +69 -0
  97. data/lib/rubocop/cop/style/space_after_control_keyword.rb +32 -0
  98. data/lib/rubocop/cop/style/string_literals.rb +36 -0
  99. data/lib/rubocop/cop/style/surrounding_space.rb +314 -0
  100. data/lib/rubocop/cop/style/symbol_array.rb +31 -0
  101. data/lib/rubocop/cop/style/symbol_name.rb +27 -0
  102. data/lib/rubocop/cop/style/tab.rb +25 -0
  103. data/lib/rubocop/cop/style/ternary_operator.rb +49 -0
  104. data/lib/rubocop/cop/style/trailing_whitespace.rb +24 -0
  105. data/lib/rubocop/cop/style/trivial_accessors.rb +32 -0
  106. data/lib/rubocop/cop/style/unless_else.rb +26 -0
  107. data/lib/rubocop/cop/style/variable_interpolation.rb +32 -0
  108. data/lib/rubocop/cop/style/when_then.rb +25 -0
  109. data/lib/rubocop/cop/style/while_until_do.rb +45 -0
  110. data/lib/rubocop/cop/style/word_array.rb +44 -0
  111. data/lib/rubocop/cop/util.rb +27 -0
  112. data/lib/rubocop/cop/variable_inspector.rb +280 -0
  113. data/lib/rubocop/formatter/base_formatter.rb +119 -0
  114. data/lib/rubocop/formatter/clang_style_formatter.rb +21 -0
  115. data/lib/rubocop/formatter/emacs_style_formatter.rb +17 -0
  116. data/lib/rubocop/formatter/formatter_set.rb +77 -0
  117. data/lib/rubocop/formatter/json_formatter.rb +76 -0
  118. data/lib/rubocop/formatter/progress_formatter.rb +63 -0
  119. data/lib/rubocop/formatter/simple_text_formatter.rb +62 -0
  120. data/lib/rubocop/version.rb +21 -0
  121. data/rubocop.gemspec +36 -0
  122. data/spec/.rubocop.yml +5 -0
  123. data/spec/project_spec.rb +24 -0
  124. data/spec/rubocop/cli_spec.rb +906 -0
  125. data/spec/rubocop/config_spec.rb +470 -0
  126. data/spec/rubocop/config_store_spec.rb +66 -0
  127. data/spec/rubocop/cops/cop_spec.rb +38 -0
  128. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +111 -0
  129. data/spec/rubocop/cops/lint/end_alignment_spec.rb +333 -0
  130. data/spec/rubocop/cops/lint/end_in_method_spec.rb +35 -0
  131. data/spec/rubocop/cops/lint/ensure_return_spec.rb +37 -0
  132. data/spec/rubocop/cops/lint/eval_spec.rb +41 -0
  133. data/spec/rubocop/cops/lint/handle_exceptions_spec.rb +36 -0
  134. data/spec/rubocop/cops/lint/literal_in_condition_spec.rb +42 -0
  135. data/spec/rubocop/cops/lint/loop_spec.rb +33 -0
  136. data/spec/rubocop/cops/lint/rescue_exception_spec.rb +127 -0
  137. data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +243 -0
  138. data/spec/rubocop/cops/lint/unreachable_code_spec.rb +69 -0
  139. data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +497 -0
  140. data/spec/rubocop/cops/lint/void_spec.rb +63 -0
  141. data/spec/rubocop/cops/offence_spec.rb +133 -0
  142. data/spec/rubocop/cops/rails/validation_spec.rb +27 -0
  143. data/spec/rubocop/cops/style/access_control_spec.rb +142 -0
  144. data/spec/rubocop/cops/style/alias_spec.rb +47 -0
  145. data/spec/rubocop/cops/style/align_parameters_spec.rb +199 -0
  146. data/spec/rubocop/cops/style/and_or_spec.rb +39 -0
  147. data/spec/rubocop/cops/style/ascii_comments_spec.rb +28 -0
  148. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +28 -0
  149. data/spec/rubocop/cops/style/attr_spec.rb +20 -0
  150. data/spec/rubocop/cops/style/avoid_class_vars_spec.rb +27 -0
  151. data/spec/rubocop/cops/style/avoid_for_spec.rb +37 -0
  152. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +34 -0
  153. data/spec/rubocop/cops/style/avoid_perl_backrefs_spec.rb +20 -0
  154. data/spec/rubocop/cops/style/avoid_perlisms_spec.rb +47 -0
  155. data/spec/rubocop/cops/style/begin_block_spec.rb +19 -0
  156. data/spec/rubocop/cops/style/block_comments_spec.rb +27 -0
  157. data/spec/rubocop/cops/style/block_nesting_spec.rb +159 -0
  158. data/spec/rubocop/cops/style/blocks_spec.rb +35 -0
  159. data/spec/rubocop/cops/style/case_equality_spec.rb +18 -0
  160. data/spec/rubocop/cops/style/case_indentation_spec.rb +88 -0
  161. data/spec/rubocop/cops/style/character_literal_spec.rb +28 -0
  162. data/spec/rubocop/cops/style/class_and_module_camel_case_spec.rb +46 -0
  163. data/spec/rubocop/cops/style/class_methods_spec.rb +51 -0
  164. data/spec/rubocop/cops/style/collection_methods_spec.rb +41 -0
  165. data/spec/rubocop/cops/style/colon_method_call_spec.rb +55 -0
  166. data/spec/rubocop/cops/style/constant_name_spec.rb +56 -0
  167. data/spec/rubocop/cops/style/def_with_parentheses_spec.rb +40 -0
  168. data/spec/rubocop/cops/style/def_without_parentheses_spec.rb +34 -0
  169. data/spec/rubocop/cops/style/documentation_spec.rb +79 -0
  170. data/spec/rubocop/cops/style/dot_position_spec.rb +30 -0
  171. data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +85 -0
  172. data/spec/rubocop/cops/style/empty_lines_spec.rb +40 -0
  173. data/spec/rubocop/cops/style/empty_literal_spec.rb +91 -0
  174. data/spec/rubocop/cops/style/encoding_spec.rb +49 -0
  175. data/spec/rubocop/cops/style/end_block_spec.rb +19 -0
  176. data/spec/rubocop/cops/style/end_of_line_spec.rb +25 -0
  177. data/spec/rubocop/cops/style/favor_join_spec.rb +37 -0
  178. data/spec/rubocop/cops/style/favor_modifier_spec.rb +160 -0
  179. data/spec/rubocop/cops/style/favor_sprintf_spec.rb +53 -0
  180. data/spec/rubocop/cops/style/favor_unless_over_negated_if_spec.rb +64 -0
  181. data/spec/rubocop/cops/style/favor_until_over_negated_while_spec.rb +47 -0
  182. data/spec/rubocop/cops/style/hash_syntax_spec.rb +51 -0
  183. data/spec/rubocop/cops/style/if_with_semicolon_spec.rb +25 -0
  184. data/spec/rubocop/cops/style/lambda_spec.rb +45 -0
  185. data/spec/rubocop/cops/style/leading_comment_space_spec.rb +65 -0
  186. data/spec/rubocop/cops/style/line_continuation_spec.rb +26 -0
  187. data/spec/rubocop/cops/style/line_length_spec.rb +25 -0
  188. data/spec/rubocop/cops/style/method_and_variable_snake_case_spec.rb +95 -0
  189. data/spec/rubocop/cops/style/method_call_parentheses_spec.rb +25 -0
  190. data/spec/rubocop/cops/style/method_length_spec.rb +151 -0
  191. data/spec/rubocop/cops/style/multiline_if_then_spec.rb +97 -0
  192. data/spec/rubocop/cops/style/not_spec.rb +28 -0
  193. data/spec/rubocop/cops/style/numeric_literals_spec.rb +51 -0
  194. data/spec/rubocop/cops/style/one_line_conditional_spec.rb +18 -0
  195. data/spec/rubocop/cops/style/op_method_spec.rb +80 -0
  196. data/spec/rubocop/cops/style/parameter_lists_spec.rb +49 -0
  197. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +59 -0
  198. data/spec/rubocop/cops/style/proc_spec.rb +28 -0
  199. data/spec/rubocop/cops/style/reduce_arguments_spec.rb +59 -0
  200. data/spec/rubocop/cops/style/regexp_literal_spec.rb +83 -0
  201. data/spec/rubocop/cops/style/rescue_modifier_spec.rb +122 -0
  202. data/spec/rubocop/cops/style/semicolon_spec.rb +95 -0
  203. data/spec/rubocop/cops/style/single_line_methods_spec.rb +54 -0
  204. data/spec/rubocop/cops/style/space_after_colon_spec.rb +29 -0
  205. data/spec/rubocop/cops/style/space_after_comma_spec.rb +31 -0
  206. data/spec/rubocop/cops/style/space_after_control_keyword_spec.rb +69 -0
  207. data/spec/rubocop/cops/style/space_after_semicolon_spec.rb +24 -0
  208. data/spec/rubocop/cops/style/space_around_braces_spec.rb +49 -0
  209. data/spec/rubocop/cops/style/space_around_equals_in_default_parameter_spec.rb +34 -0
  210. data/spec/rubocop/cops/style/space_around_operators_spec.rb +216 -0
  211. data/spec/rubocop/cops/style/space_inside_brackets_spec.rb +51 -0
  212. data/spec/rubocop/cops/style/space_inside_hash_literal_braces_spec.rb +99 -0
  213. data/spec/rubocop/cops/style/space_inside_parens_spec.rb +33 -0
  214. data/spec/rubocop/cops/style/string_literals_spec.rb +62 -0
  215. data/spec/rubocop/cops/style/symbol_array_spec.rb +45 -0
  216. data/spec/rubocop/cops/style/symbol_name_spec.rb +122 -0
  217. data/spec/rubocop/cops/style/tab_spec.rb +23 -0
  218. data/spec/rubocop/cops/style/ternary_operator_spec.rb +42 -0
  219. data/spec/rubocop/cops/style/trailing_whitespace_spec.rb +29 -0
  220. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +338 -0
  221. data/spec/rubocop/cops/style/unless_else_spec.rb +31 -0
  222. data/spec/rubocop/cops/style/variable_interpolation_spec.rb +53 -0
  223. data/spec/rubocop/cops/style/when_then_spec.rb +40 -0
  224. data/spec/rubocop/cops/style/while_until_do_spec.rb +47 -0
  225. data/spec/rubocop/cops/style/word_array_spec.rb +61 -0
  226. data/spec/rubocop/cops/variable_inspector_spec.rb +374 -0
  227. data/spec/rubocop/formatter/base_formatter_spec.rb +190 -0
  228. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +70 -0
  229. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +32 -0
  230. data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
  231. data/spec/rubocop/formatter/json_formatter_spec.rb +142 -0
  232. data/spec/rubocop/formatter/progress_formatter_spec.rb +196 -0
  233. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +74 -0
  234. data/spec/spec_helper.rb +92 -0
  235. data/spec/support/file_helper.rb +21 -0
  236. data/spec/support/isolated_environment.rb +27 -0
  237. data/spec/support/mri_syntax_checker.rb +69 -0
  238. data/spec/support/shared_examples.rb +33 -0
  239. 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