rubocop 0.29.1 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (341) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -1
  3. data/README.md +11 -2
  4. data/config/default.yml +194 -6
  5. data/config/disabled.yml +25 -1
  6. data/config/enabled.yml +74 -12
  7. data/lib/rubocop.rb +19 -2
  8. data/lib/rubocop/config.rb +23 -3
  9. data/lib/rubocop/config_loader.rb +9 -1
  10. data/lib/rubocop/cop/autocorrect_logic.rb +26 -0
  11. data/lib/rubocop/cop/cop.rb +33 -17
  12. data/lib/rubocop/cop/lint/assignment_in_condition.rb +16 -4
  13. data/lib/rubocop/cop/lint/block_alignment.rb +5 -14
  14. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  15. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +61 -0
  16. data/lib/rubocop/cop/lint/void.rb +11 -3
  17. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +17 -0
  18. data/lib/rubocop/cop/performance/detect.rb +72 -0
  19. data/lib/rubocop/cop/performance/flat_map.rb +64 -0
  20. data/lib/rubocop/cop/performance/reverse_each.rb +43 -0
  21. data/lib/rubocop/cop/performance/sample.rb +58 -0
  22. data/lib/rubocop/cop/performance/size.rb +64 -0
  23. data/lib/rubocop/cop/rails/date.rb +121 -0
  24. data/lib/rubocop/cop/rails/find_by.rb +52 -0
  25. data/lib/rubocop/cop/rails/find_each.rb +41 -0
  26. data/lib/rubocop/cop/rails/time_zone.rb +93 -0
  27. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +45 -0
  28. data/lib/rubocop/cop/style/block_delimiters.rb +186 -0
  29. data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +76 -0
  30. data/lib/rubocop/cop/style/command_literal.rb +120 -0
  31. data/lib/rubocop/cop/style/copyright.rb +87 -0
  32. data/lib/rubocop/cop/style/documentation.rb +32 -0
  33. data/lib/rubocop/cop/style/empty_else.rb +85 -16
  34. data/lib/rubocop/cop/style/encoding.rb +14 -2
  35. data/lib/rubocop/cop/style/end_of_line.rb +5 -0
  36. data/lib/rubocop/cop/style/hash_syntax.rb +86 -23
  37. data/lib/rubocop/cop/style/if_unless_modifier.rb +15 -0
  38. data/lib/rubocop/cop/style/indentation_consistency.rb +13 -6
  39. data/lib/rubocop/cop/style/indentation_width.rb +34 -10
  40. data/lib/rubocop/cop/style/lambda.rb +32 -17
  41. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -0
  42. data/lib/rubocop/cop/style/missing_else.rb +72 -0
  43. data/lib/rubocop/cop/style/regexp_literal.rb +89 -58
  44. data/lib/rubocop/cop/style/space_around_operators.rb +13 -2
  45. data/lib/rubocop/cop/style/space_before_block_braces.rb +3 -3
  46. data/lib/rubocop/cop/style/space_inside_block_braces.rb +9 -5
  47. data/lib/rubocop/cop/style/symbol_literal.rb +36 -0
  48. data/lib/rubocop/cop/style/trailing_blank_lines.rb +7 -0
  49. data/lib/rubocop/cop/style/trailing_comma.rb +21 -7
  50. data/lib/rubocop/cop/style/trivial_accessors.rb +17 -3
  51. data/lib/rubocop/cop/style/unneeded_capital_w.rb +4 -6
  52. data/lib/rubocop/cop/style/while_until_modifier.rb +10 -0
  53. data/lib/rubocop/cop/util.rb +24 -19
  54. data/lib/rubocop/cop/variable_force.rb +14 -0
  55. data/lib/rubocop/cop/variable_force/reference.rb +10 -4
  56. data/lib/rubocop/formatter/html_formatter.rb +10 -4
  57. data/lib/rubocop/formatter/text_util.rb +5 -7
  58. data/lib/rubocop/options.rb +47 -45
  59. data/lib/rubocop/path_util.rb +12 -8
  60. data/lib/rubocop/processed_source.rb +12 -1
  61. data/lib/rubocop/rake_task.rb +2 -2
  62. data/lib/rubocop/version.rb +1 -1
  63. data/relnotes/v0.30.0.md +145 -0
  64. data/rubocop.gemspec +15 -3
  65. metadata +22 -545
  66. data/.gitignore +0 -54
  67. data/.rspec +0 -1
  68. data/.rubocop.yml +0 -11
  69. data/.rubocop_todo.yml +0 -28
  70. data/.travis.yml +0 -22
  71. data/Gemfile +0 -15
  72. data/Rakefile +0 -41
  73. data/lib/rubocop/cop/style/blocks.rb +0 -80
  74. data/lib/rubocop/cop/style/unneeded_percent_x.rb +0 -25
  75. data/logo/rubocop-final-logo.eps +0 -0
  76. data/spec/.rubocop.yml +0 -5
  77. data/spec/fixtures/html_formatter/expected.html +0 -495
  78. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +0 -5
  79. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +0 -74
  80. data/spec/fixtures/html_formatter/project/app/models/book.rb +0 -5
  81. data/spec/isolated_environment_spec.rb +0 -24
  82. data/spec/project_spec.rb +0 -119
  83. data/spec/rubocop/cli_spec.rb +0 -2740
  84. data/spec/rubocop/comment_config_spec.rb +0 -105
  85. data/spec/rubocop/config_loader_spec.rb +0 -377
  86. data/spec/rubocop/config_spec.rb +0 -235
  87. data/spec/rubocop/config_store_spec.rb +0 -53
  88. data/spec/rubocop/cop/commissioner_spec.rb +0 -85
  89. data/spec/rubocop/cop/cop_spec.rb +0 -135
  90. data/spec/rubocop/cop/corrector_spec.rb +0 -59
  91. data/spec/rubocop/cop/force_spec.rb +0 -29
  92. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +0 -113
  93. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +0 -35
  94. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -130
  95. data/spec/rubocop/cop/lint/block_alignment_spec.rb +0 -533
  96. data/spec/rubocop/cop/lint/condition_position_spec.rb +0 -51
  97. data/spec/rubocop/cop/lint/debugger_spec.rb +0 -73
  98. data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +0 -108
  99. data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +0 -38
  100. data/spec/rubocop/cop/lint/duplicate_methods_spec.rb +0 -189
  101. data/spec/rubocop/cop/lint/else_layout_spec.rb +0 -65
  102. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +0 -27
  103. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +0 -18
  104. data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -91
  105. data/spec/rubocop/cop/lint/end_in_method_spec.rb +0 -29
  106. data/spec/rubocop/cop/lint/ensure_return_spec.rb +0 -39
  107. data/spec/rubocop/cop/lint/eval_spec.rb +0 -33
  108. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +0 -30
  109. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +0 -33
  110. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +0 -172
  111. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +0 -31
  112. data/spec/rubocop/cop/lint/loop_spec.rb +0 -27
  113. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +0 -57
  114. data/spec/rubocop/cop/lint/require_parentheses_spec.rb +0 -82
  115. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -123
  116. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +0 -237
  117. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +0 -66
  118. data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +0 -51
  119. data/spec/rubocop/cop/lint/syntax_spec.rb +0 -34
  120. data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +0 -179
  121. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +0 -63
  122. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +0 -231
  123. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +0 -278
  124. data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +0 -192
  125. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +0 -1778
  126. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +0 -30
  127. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +0 -48
  128. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +0 -211
  129. data/spec/rubocop/cop/lint/void_spec.rb +0 -64
  130. data/spec/rubocop/cop/metrics/abc_size_spec.rb +0 -109
  131. data/spec/rubocop/cop/metrics/block_nesting_spec.rb +0 -156
  132. data/spec/rubocop/cop/metrics/class_length_spec.rb +0 -131
  133. data/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb +0 -204
  134. data/spec/rubocop/cop/metrics/line_length_spec.rb +0 -136
  135. data/spec/rubocop/cop/metrics/method_length_spec.rb +0 -147
  136. data/spec/rubocop/cop/metrics/parameter_lists_spec.rb +0 -44
  137. data/spec/rubocop/cop/metrics/perceived_complexity_spec.rb +0 -222
  138. data/spec/rubocop/cop/offense_spec.rb +0 -133
  139. data/spec/rubocop/cop/rails/action_filter_spec.rb +0 -64
  140. data/spec/rubocop/cop/rails/default_scope_spec.rb +0 -37
  141. data/spec/rubocop/cop/rails/delegate_spec.rb +0 -177
  142. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +0 -13
  143. data/spec/rubocop/cop/rails/output_spec.rb +0 -37
  144. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +0 -130
  145. data/spec/rubocop/cop/rails/scope_args_spec.rb +0 -25
  146. data/spec/rubocop/cop/rails/validation_spec.rb +0 -28
  147. data/spec/rubocop/cop/severity_spec.rb +0 -113
  148. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +0 -421
  149. data/spec/rubocop/cop/style/accessor_method_name_spec.rb +0 -81
  150. data/spec/rubocop/cop/style/alias_spec.rb +0 -59
  151. data/spec/rubocop/cop/style/align_array_spec.rb +0 -123
  152. data/spec/rubocop/cop/style/align_hash_spec.rb +0 -411
  153. data/spec/rubocop/cop/style/align_parameters_spec.rb +0 -431
  154. data/spec/rubocop/cop/style/and_or_spec.rb +0 -235
  155. data/spec/rubocop/cop/style/array_join_spec.rb +0 -29
  156. data/spec/rubocop/cop/style/ascii_comments_spec.rb +0 -21
  157. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +0 -36
  158. data/spec/rubocop/cop/style/attr_spec.rb +0 -29
  159. data/spec/rubocop/cop/style/bare_percent_literals_spec.rb +0 -132
  160. data/spec/rubocop/cop/style/begin_block_spec.rb +0 -13
  161. data/spec/rubocop/cop/style/block_comments_spec.rb +0 -45
  162. data/spec/rubocop/cop/style/block_end_newline_spec.rb +0 -61
  163. data/spec/rubocop/cop/style/blocks_spec.rb +0 -121
  164. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +0 -288
  165. data/spec/rubocop/cop/style/case_equality_spec.rb +0 -13
  166. data/spec/rubocop/cop/style/case_indentation_spec.rb +0 -293
  167. data/spec/rubocop/cop/style/character_literal_spec.rb +0 -37
  168. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +0 -40
  169. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +0 -133
  170. data/spec/rubocop/cop/style/class_check_spec.rb +0 -41
  171. data/spec/rubocop/cop/style/class_methods_spec.rb +0 -80
  172. data/spec/rubocop/cop/style/class_vars_spec.rb +0 -19
  173. data/spec/rubocop/cop/style/collection_methods_spec.rb +0 -48
  174. data/spec/rubocop/cop/style/colon_method_call_spec.rb +0 -65
  175. data/spec/rubocop/cop/style/comment_annotation_spec.rb +0 -86
  176. data/spec/rubocop/cop/style/comment_indentation_spec.rb +0 -179
  177. data/spec/rubocop/cop/style/constant_name_spec.rb +0 -65
  178. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +0 -43
  179. data/spec/rubocop/cop/style/deprecated_hash_methods_spec.rb +0 -45
  180. data/spec/rubocop/cop/style/documentation_spec.rb +0 -146
  181. data/spec/rubocop/cop/style/dot_position_spec.rb +0 -132
  182. data/spec/rubocop/cop/style/double_negation_spec.rb +0 -22
  183. data/spec/rubocop/cop/style/each_with_object_spec.rb +0 -73
  184. data/spec/rubocop/cop/style/else_alignment_spec.rb +0 -506
  185. data/spec/rubocop/cop/style/empty_else_spec.rb +0 -100
  186. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +0 -135
  187. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +0 -178
  188. data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +0 -103
  189. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +0 -135
  190. data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +0 -90
  191. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +0 -79
  192. data/spec/rubocop/cop/style/empty_lines_spec.rb +0 -51
  193. data/spec/rubocop/cop/style/empty_literal_spec.rb +0 -117
  194. data/spec/rubocop/cop/style/encoding_spec.rb +0 -139
  195. data/spec/rubocop/cop/style/end_block_spec.rb +0 -13
  196. data/spec/rubocop/cop/style/end_of_line_spec.rb +0 -65
  197. data/spec/rubocop/cop/style/even_odd_spec.rb +0 -164
  198. data/spec/rubocop/cop/style/extra_spacing_spec.rb +0 -68
  199. data/spec/rubocop/cop/style/file_name_spec.rb +0 -84
  200. data/spec/rubocop/cop/style/first_parameter_indentation_spec.rb +0 -304
  201. data/spec/rubocop/cop/style/flip_flop_spec.rb +0 -23
  202. data/spec/rubocop/cop/style/for_spec.rb +0 -105
  203. data/spec/rubocop/cop/style/format_string_spec.rb +0 -184
  204. data/spec/rubocop/cop/style/global_vars_spec.rb +0 -34
  205. data/spec/rubocop/cop/style/guard_clause_spec.rb +0 -172
  206. data/spec/rubocop/cop/style/hash_syntax_spec.rb +0 -137
  207. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +0 -146
  208. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +0 -24
  209. data/spec/rubocop/cop/style/indent_array_spec.rb +0 -173
  210. data/spec/rubocop/cop/style/indent_hash_spec.rb +0 -367
  211. data/spec/rubocop/cop/style/indentation_consistency_spec.rb +0 -509
  212. data/spec/rubocop/cop/style/indentation_width_spec.rb +0 -885
  213. data/spec/rubocop/cop/style/infinite_loop_spec.rb +0 -48
  214. data/spec/rubocop/cop/style/inline_comment_spec.rb +0 -13
  215. data/spec/rubocop/cop/style/lambda_call_spec.rb +0 -65
  216. data/spec/rubocop/cop/style/lambda_spec.rb +0 -76
  217. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +0 -64
  218. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +0 -184
  219. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +0 -59
  220. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +0 -60
  221. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +0 -108
  222. data/spec/rubocop/cop/style/method_name_spec.rb +0 -125
  223. data/spec/rubocop/cop/style/module_function_spec.rb +0 -24
  224. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +0 -78
  225. data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +0 -199
  226. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +0 -134
  227. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +0 -464
  228. data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +0 -18
  229. data/spec/rubocop/cop/style/negated_if_spec.rb +0 -102
  230. data/spec/rubocop/cop/style/negated_while_spec.rb +0 -64
  231. data/spec/rubocop/cop/style/nested_ternary_operator_spec.rb +0 -21
  232. data/spec/rubocop/cop/style/next_spec.rb +0 -301
  233. data/spec/rubocop/cop/style/nil_comparison_spec.rb +0 -29
  234. data/spec/rubocop/cop/style/non_nil_check_spec.rb +0 -132
  235. data/spec/rubocop/cop/style/not_spec.rb +0 -28
  236. data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -80
  237. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +0 -13
  238. data/spec/rubocop/cop/style/op_method_spec.rb +0 -85
  239. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +0 -161
  240. data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +0 -316
  241. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +0 -122
  242. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +0 -22
  243. data/spec/rubocop/cop/style/predicate_name_spec.rb +0 -57
  244. data/spec/rubocop/cop/style/proc_spec.rb +0 -27
  245. data/spec/rubocop/cop/style/raise_args_spec.rb +0 -87
  246. data/spec/rubocop/cop/style/redundant_begin_spec.rb +0 -121
  247. data/spec/rubocop/cop/style/redundant_exception_spec.rb +0 -59
  248. data/spec/rubocop/cop/style/redundant_return_spec.rb +0 -171
  249. data/spec/rubocop/cop/style/redundant_self_spec.rb +0 -186
  250. data/spec/rubocop/cop/style/regexp_literal_spec.rb +0 -188
  251. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +0 -116
  252. data/spec/rubocop/cop/style/self_assignment_spec.rb +0 -49
  253. data/spec/rubocop/cop/style/semicolon_spec.rb +0 -114
  254. data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -289
  255. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +0 -82
  256. data/spec/rubocop/cop/style/single_line_methods_spec.rb +0 -94
  257. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +0 -63
  258. data/spec/rubocop/cop/style/space_after_colon_spec.rb +0 -55
  259. data/spec/rubocop/cop/style/space_after_comma_spec.rb +0 -30
  260. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +0 -84
  261. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +0 -70
  262. data/spec/rubocop/cop/style/space_after_not_spec.rb +0 -22
  263. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +0 -23
  264. data/spec/rubocop/cop/style/space_around_block_parameters_spec.rb +0 -155
  265. data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +0 -95
  266. data/spec/rubocop/cop/style/space_around_operators_spec.rb +0 -330
  267. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +0 -73
  268. data/spec/rubocop/cop/style/space_before_comma_spec.rb +0 -42
  269. data/spec/rubocop/cop/style/space_before_comment_spec.rb +0 -34
  270. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +0 -70
  271. data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +0 -28
  272. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +0 -317
  273. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +0 -83
  274. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +0 -147
  275. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +0 -44
  276. data/spec/rubocop/cop/style/space_inside_range_literal_spec.rb +0 -64
  277. data/spec/rubocop/cop/style/special_global_vars_spec.rb +0 -62
  278. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +0 -99
  279. data/spec/rubocop/cop/style/string_literals_spec.rb +0 -228
  280. data/spec/rubocop/cop/style/struct_inheritance_spec.rb +0 -44
  281. data/spec/rubocop/cop/style/symbol_array_spec.rb +0 -37
  282. data/spec/rubocop/cop/style/symbol_proc_spec.rb +0 -100
  283. data/spec/rubocop/cop/style/tab_spec.rb +0 -47
  284. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +0 -83
  285. data/spec/rubocop/cop/style/trailing_comma_spec.rb +0 -390
  286. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +0 -31
  287. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +0 -558
  288. data/spec/rubocop/cop/style/unless_else_spec.rb +0 -23
  289. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +0 -90
  290. data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +0 -127
  291. data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +0 -34
  292. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +0 -76
  293. data/spec/rubocop/cop/style/variable_name_spec.rb +0 -107
  294. data/spec/rubocop/cop/style/when_then_spec.rb +0 -42
  295. data/spec/rubocop/cop/style/while_until_do_spec.rb +0 -55
  296. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +0 -93
  297. data/spec/rubocop/cop/style/word_array_spec.rb +0 -117
  298. data/spec/rubocop/cop/team_spec.rb +0 -190
  299. data/spec/rubocop/cop/util_spec.rb +0 -102
  300. data/spec/rubocop/cop/variable_force/assignment_spec.rb +0 -200
  301. data/spec/rubocop/cop/variable_force/locatable_spec.rb +0 -699
  302. data/spec/rubocop/cop/variable_force/reference_spec.rb +0 -19
  303. data/spec/rubocop/cop/variable_force/scope_spec.rb +0 -319
  304. data/spec/rubocop/cop/variable_force/variable_spec.rb +0 -59
  305. data/spec/rubocop/cop/variable_force/variable_table_spec.rb +0 -276
  306. data/spec/rubocop/cop/variable_force_spec.rb +0 -26
  307. data/spec/rubocop/formatter/base_formatter_spec.rb +0 -191
  308. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +0 -114
  309. data/spec/rubocop/formatter/colorizable_spec.rb +0 -107
  310. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +0 -50
  311. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -67
  312. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +0 -63
  313. data/spec/rubocop/formatter/file_list_formatter_spec.rb +0 -33
  314. data/spec/rubocop/formatter/formatter_set_spec.rb +0 -141
  315. data/spec/rubocop/formatter/fuubar_style_formatter_spec.rb +0 -129
  316. data/spec/rubocop/formatter/html_formatter_spec.rb +0 -48
  317. data/spec/rubocop/formatter/json_formatter_spec.rb +0 -152
  318. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -75
  319. data/spec/rubocop/formatter/progress_formatter_spec.rb +0 -182
  320. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +0 -135
  321. data/spec/rubocop/formatter/text_util_spec.rb +0 -55
  322. data/spec/rubocop/options_spec.rb +0 -181
  323. data/spec/rubocop/path_util_spec.rb +0 -77
  324. data/spec/rubocop/processed_source_spec.rb +0 -184
  325. data/spec/rubocop/rake_task_spec.rb +0 -122
  326. data/spec/rubocop/runner_spec.rb +0 -99
  327. data/spec/rubocop/string_util_spec.rb +0 -46
  328. data/spec/rubocop/target_finder_spec.rb +0 -245
  329. data/spec/rubocop/token_spec.rb +0 -29
  330. data/spec/spec_helper.rb +0 -39
  331. data/spec/support/cop_helper.rb +0 -75
  332. data/spec/support/cops/class_must_be_a_module_cop.rb +0 -19
  333. data/spec/support/cops/module_must_be_a_class_cop.rb +0 -19
  334. data/spec/support/coverage.rb +0 -15
  335. data/spec/support/custom_matchers.rb +0 -58
  336. data/spec/support/file_helper.rb +0 -23
  337. data/spec/support/jruby_workaround.rb +0 -15
  338. data/spec/support/mri_syntax_checker.rb +0 -73
  339. data/spec/support/shared_contexts.rb +0 -53
  340. data/spec/support/shared_examples.rb +0 -55
  341. data/spec/support/statement_modifier_helper.rb +0 -41
@@ -1,5 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- # Prevent CSRF attacks by raising an exception.
3
- # For APIs, you may want to use :null_session instead.
4
- protect_from_forgery with: :exception
5
- end
@@ -1,74 +0,0 @@
1
- class BooksController < ApplicationController
2
- before_action :set_book, only: [:show, :edit, :update, :destroy]
3
-
4
- # GET /books
5
- # GET /books.json
6
- def index
7
- @books = Book.all
8
- end
9
-
10
- # GET /books/1
11
- # GET /books/1.json
12
- def show
13
- end
14
-
15
- # GET /books/new
16
- def new
17
- @book = Book.new
18
- end
19
-
20
- # GET /books/1/edit
21
- def edit
22
- end
23
-
24
- # POST /books
25
- # POST /books.json
26
- def create
27
- @book = Book.new(book_params)
28
-
29
- respond_to do |format|
30
- if @book.save
31
- format.html { redirect_to @book, notice: 'Book was successfully created.' }
32
- format.json { render :show, status: :created, location: @book }
33
- else
34
- format.html { render :new }
35
- format.json { render json: @book.errors, status: :unprocessable_entity }
36
- end
37
- end
38
- end
39
-
40
- # PATCH/PUT /books/1
41
- # PATCH/PUT /books/1.json
42
- def update
43
- respond_to do |format|
44
- if @book.update(book_params)
45
- format.html { redirect_to @book, notice: 'Book was successfully updated.' }
46
- format.json { render :show, status: :ok, location: @book }
47
- else
48
- format.html { render :edit }
49
- format.json { render json: @book.errors, status: :unprocessable_entity }
50
- end
51
- end
52
- end
53
-
54
- # DELETE /books/1
55
- # DELETE /books/1.json
56
- def destroy
57
- @book.destroy
58
- respond_to do |format|
59
- format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
60
- format.json { head :no_content }
61
- end
62
- end
63
-
64
- private
65
- # Use callbacks to share common setup or constraints between actions.
66
- def set_book
67
- @book = Book.find(params[:id])
68
- end
69
-
70
- # Never trust parameters from the scary internet, only allow the white list through.
71
- def book_params
72
- params.require(:book).permit(:name)
73
- end
74
- end
@@ -1,5 +0,0 @@
1
- class Book < ActiveRecord::Base
2
- def someMethod
3
- foo = bar = baz
4
- end
5
- end
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe 'isolated environment', :isolated_environment do
6
- include FileHelper
7
-
8
- let(:cli) { RuboCop::CLI.new }
9
-
10
- before(:each) { $stdout = StringIO.new }
11
- after(:each) { $stdout = STDOUT }
12
-
13
- # Configuration files above the work directory shall not disturb the
14
- # tests. This is especially important on Windows where the temporary
15
- # directory is under the user's home directory. On any platform we don't want
16
- # a .rubocop.yml file in the temporary directory to affect the outcome of
17
- # rspec.
18
- it 'is not affected by a config file above the work directory' do
19
- create_file('../.rubocop.yml', ['inherit_from: missing_file.yml'])
20
- create_file('ex.rb', ['# encoding: utf-8'])
21
- # A return value of 0 means that the erroneous config file was not read.
22
- expect(cli.run([])).to eq(0)
23
- end
24
- end
@@ -1,119 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe 'RuboCop Project' do
6
- describe 'default configuration file' do
7
- let(:cop_names) { RuboCop::Cop::Cop.all.map(&:cop_name) }
8
-
9
- subject(:default_config) do
10
- RuboCop::ConfigLoader.load_file('config/default.yml')
11
- end
12
-
13
- it 'has configuration for all cops' do
14
- expect(default_config.keys.sort).to eq((['AllCops'] + cop_names).sort)
15
- end
16
-
17
- it 'has a nicely formatted description for all cops' do
18
- cop_names.each do |name|
19
- description = default_config[name]['Description']
20
- expect(description).not_to be_nil
21
- expect(description).not_to include("\n")
22
- end
23
- end
24
- end
25
-
26
- describe 'changelog' do
27
- subject(:changelog) do
28
- path = File.join(File.dirname(__FILE__), '..', 'CHANGELOG.md')
29
- File.read(path)
30
- end
31
-
32
- it 'has link definitions for all implicit links' do
33
- implicit_link_names = changelog.scan(/\[([^\]]+)\]\[\]/).flatten.uniq
34
- implicit_link_names.each do |name|
35
- expect(changelog).to include("[#{name}]: http")
36
- end
37
- end
38
-
39
- describe 'entry' do
40
- subject(:entries) { lines.grep(/^\*/).map(&:chomp) }
41
- let(:lines) { changelog.each_line }
42
-
43
- it 'has a whitespace between the * and the body' do
44
- entries.each do |entry|
45
- expect(entry).to match(/^\* \S/)
46
- end
47
- end
48
-
49
- context 'after version 0.14.0' do
50
- let(:lines) do
51
- changelog.each_line.take_while do |line|
52
- !line.start_with?('## 0.14.0')
53
- end
54
- end
55
-
56
- it 'has a link to the contributors at the end' do
57
- entries.each do |entry|
58
- expect(entry).to match(/\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/)
59
- end
60
- end
61
- end
62
-
63
- describe 'link to related issue' do
64
- let(:issues) do
65
- entries.map do |entry|
66
- entry.match(/\[(?<number>[#\d]+)\]\((?<url>[^\)]+)\)/)
67
- end.compact
68
- end
69
-
70
- it 'has an issue number prefixed with #' do
71
- issues.each do |issue|
72
- expect(issue[:number]).to match(/^#\d+$/)
73
- end
74
- end
75
-
76
- it 'has a valid URL' do
77
- issues.each do |issue|
78
- number = issue[:number].gsub(/\D/, '')
79
- pattern = %r{^https://github\.com/bbatsov/rubocop/(?:issues|pull)/#{number}$} # rubocop:disable Metrics/LineLength
80
- expect(issue[:url]).to match(pattern)
81
- end
82
- end
83
-
84
- it 'has a colon and a whitespace at the end' do
85
- entries_including_issue_link = entries.select do |entry|
86
- entry.match(/^\*\s*\[/)
87
- end
88
-
89
- entries_including_issue_link.each do |entry|
90
- expect(entry).to include('): ')
91
- end
92
- end
93
- end
94
-
95
- describe 'body' do
96
- let(:bodies) do
97
- entries.map do |entry|
98
- entry
99
- .gsub(/`[^`]+`/, '``')
100
- .sub(/^\*\s*(?:\[.+?\):\s*)?/, '')
101
- .sub(/\s*\([^\)]+\)$/, '')
102
- end
103
- end
104
-
105
- it 'does not start with a lower case' do
106
- bodies.each do |body|
107
- expect(body).not_to match(/^[a-z]/)
108
- end
109
- end
110
-
111
- it 'ends with a punctuation' do
112
- bodies.each do |body|
113
- expect(body).to match(/[\.\!]$/)
114
- end
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,2740 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'fileutils'
4
- require 'tmpdir'
5
- require 'spec_helper'
6
- require 'timeout'
7
-
8
- describe RuboCop::CLI, :isolated_environment do
9
- include FileHelper
10
-
11
- subject(:cli) { described_class.new }
12
-
13
- before(:each) do
14
- $stdout = StringIO.new
15
- $stderr = StringIO.new
16
- RuboCop::ConfigLoader.debug = false
17
- end
18
-
19
- after(:each) do
20
- $stdout = STDOUT
21
- $stderr = STDERR
22
- end
23
-
24
- def abs(path)
25
- File.expand_path(path)
26
- end
27
-
28
- describe 'option' do
29
- describe '--version' do
30
- it 'exits cleanly' do
31
- expect { cli.run ['-v'] }.to exit_with_code(0)
32
- expect { cli.run ['--version'] }.to exit_with_code(0)
33
- expect($stdout.string).to eq((RuboCop::Version::STRING + "\n") * 2)
34
- end
35
- end
36
-
37
- describe '--auto-correct' do
38
- it 'corrects SymbolProc and SpaceBeforeBlockBraces offenses' do
39
- source = ['foo.map{ |a| a.nil? }']
40
- create_file('example.rb', source)
41
- expect(cli.run(['-D', '--auto-correct'])).to eq(0)
42
- corrected = "foo.map(&:nil?)\n"
43
- expect(IO.read('example.rb')).to eq(corrected)
44
- uncorrected = $stdout.string.split($RS).select do |line|
45
- line.include?('example.rb:') && !line.include?('[Corrected]')
46
- end
47
- expect(uncorrected).to be_empty # Hence exit code 0.
48
- end
49
-
50
- it 'corrects only IndentationWidth without crashing' do
51
- source = ['foo = if bar',
52
- ' something',
53
- 'elsif baz',
54
- ' other_thing',
55
- 'else',
56
- ' fail',
57
- 'end']
58
- create_file('example.rb', source)
59
- expect(cli.run(%w(--only IndentationWidth --auto-correct))).to eq(0)
60
- corrected = ['foo = if bar',
61
- ' something',
62
- 'elsif baz',
63
- ' other_thing',
64
- 'else',
65
- ' fail',
66
- 'end',
67
- ''].join("\n")
68
- expect(IO.read('example.rb')).to eq(corrected)
69
- end
70
-
71
- it 'crashes on infinite loop but prints offenses' do
72
- create_file('example.rb', '3.times{ something;other_thing;}')
73
- # This configuration makes --auto-correct impossible to finish since a
74
- # space will be added after each ; but then removed again for the one
75
- # that's inside }.
76
- create_file('.rubocop.yml', ['SpaceInsideBlockBraces:',
77
- ' EnforcedStyle: no_space',
78
- ' SpaceBeforeBlockParameters: false'])
79
- cmd = %w(--only SpaceAfterSemicolon,SpaceInsideBlockBraces
80
- --auto-correct --format simple)
81
- expect { cli.run(cmd) }.to raise_error(RuboCop::Runner::
82
- InfiniteCorrectionLoop)
83
- expect(IO.read('example.rb'))
84
- .to eq("3.times{something; other_thing;}\n")
85
-
86
- expected_output = [
87
- '== example.rb ==',
88
- 'C: 1: 9: [Corrected] Space inside { detected.',
89
- 'C: 1: 19: [Corrected] Space missing after semicolon.',
90
- 'C: 1: 31: [Corrected] Space missing after semicolon.',
91
- 'C: 1: 32: [Corrected] Space inside } detected.',
92
- 'C: 1: 33: [Corrected] Space inside } detected.',
93
- '',
94
- # We're interrupted during inspection, hence 0 files inspected.
95
- '0 files inspected, 5 offenses detected, 5 offenses corrected',
96
- ''
97
- ]
98
- expect($stdout.string).to eq(expected_output.join("\n"))
99
- end
100
-
101
- it 'corrects complicated cases conservatively' do
102
- # Two cops make corrections here; Style/BracesAroundHashParameters, and
103
- # Style/AlignHash. Because they make minimal corrections relating only
104
- # to their specific areas, and stay away from cleaning up extra
105
- # whitespace in the process, the combined changes don't interfere with
106
- # each other and the result is semantically the same as the starting
107
- # point.
108
- source = ['# encoding: utf-8',
109
- 'expect(subject[:address]).to eq({',
110
- " street1: '1 Market',",
111
- " street2: '#200',",
112
- " city: 'Some Town',",
113
- " state: 'CA',",
114
- " postal_code: '99999-1111'",
115
- '})']
116
- create_file('example.rb', source)
117
- expect(cli.run(['-D', '--auto-correct'])).to eq(0)
118
- corrected =
119
- ['# encoding: utf-8',
120
- "expect(subject[:address]).to eq(street1: '1 Market',",
121
- " street2: '#200',",
122
- " city: 'Some Town',",
123
- " state: 'CA',",
124
- " postal_code: '99999-1111')"]
125
- expect(IO.read('example.rb')).to eq(corrected.join("\n") + "\n")
126
- end
127
-
128
- it 'honors Exclude settings in individual cops' do
129
- source = ['# encoding: utf-8',
130
- 'puts %x(ls)']
131
- create_file('example.rb', source)
132
- create_file('.rubocop.yml', ['Style/UnneededPercentX:',
133
- ' Exclude:',
134
- ' - example.rb'])
135
- expect(cli.run(['--auto-correct'])).to eq(0)
136
- expect($stdout.string).to include('no offenses detected')
137
- expect(IO.read('example.rb')).to eq(source.join("\n") + "\n")
138
- end
139
-
140
- it 'corrects code with indentation problems' do
141
- create_file('example.rb', ['# encoding: utf-8',
142
- 'module Bar',
143
- 'class Goo',
144
- ' def something',
145
- ' first call',
146
- " do_other 'things'",
147
- ' if other > 34',
148
- ' more_work',
149
- ' end',
150
- ' end',
151
- 'end',
152
- 'end',
153
- '',
154
- 'module Foo',
155
- 'class Bar',
156
- '',
157
- ' stuff = [',
158
- ' {',
159
- " some: 'hash',",
160
- ' },',
161
- ' {',
162
- " another: 'hash',",
163
- " with: 'more'",
164
- ' },',
165
- ' ]',
166
- 'end',
167
- 'end'
168
- ])
169
- expect(cli.run(['--auto-correct'])).to eq(1)
170
- expect(IO.read('example.rb'))
171
- .to eq(['# encoding: utf-8',
172
- 'module Bar',
173
- ' class Goo',
174
- ' def something',
175
- ' first call',
176
- " do_other 'things'",
177
- ' if other > 34',
178
- ' more_work',
179
- ' end',
180
- ' end',
181
- ' end',
182
- 'end',
183
- '',
184
- 'module Foo',
185
- ' class Bar',
186
- ' stuff = [',
187
- ' {',
188
- " some: 'hash'",
189
- ' },',
190
- ' {',
191
- " another: 'hash',",
192
- " with: 'more'",
193
- ' }',
194
- ' ]',
195
- ' end',
196
- 'end',
197
- ''].join("\n"))
198
- end
199
-
200
- it 'can change block comments and indent them' do
201
- create_file('example.rb', ['# encoding: utf-8',
202
- 'module Foo',
203
- 'class Bar',
204
- '=begin',
205
- 'This is a nice long',
206
- 'comment',
207
- 'which spans a few lines',
208
- '=end',
209
- ' def baz',
210
- ' do_something',
211
- ' end',
212
- 'end',
213
- 'end'])
214
- expect(cli.run(['--auto-correct'])).to eq(1)
215
- expect(IO.read('example.rb'))
216
- .to eq(['# encoding: utf-8',
217
- 'module Foo',
218
- ' class Bar',
219
- ' # This is a nice long',
220
- ' # comment',
221
- ' # which spans a few lines',
222
- ' def baz',
223
- ' do_something',
224
- ' end',
225
- ' end',
226
- 'end',
227
- ''].join("\n"))
228
- end
229
-
230
- it 'can correct two problems with blocks' do
231
- # {} should be do..end and space is missing.
232
- create_file('example.rb', ['# encoding: utf-8',
233
- '(1..10).each{ |i|',
234
- ' puts i',
235
- '}'])
236
- expect(cli.run(['--auto-correct'])).to eq(0)
237
- expect(IO.read('example.rb'))
238
- .to eq(['# encoding: utf-8',
239
- '(1..10).each do |i|',
240
- ' puts i',
241
- 'end',
242
- ''].join("\n"))
243
- end
244
-
245
- it 'can handle spaces when removing braces' do
246
- create_file('example.rb',
247
- ['# encoding: utf-8',
248
- "assert_post_status_code 400, 's', {:type => 'bad'}"])
249
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
250
- expect(IO.read('example.rb'))
251
- .to eq(['# encoding: utf-8',
252
- "assert_post_status_code 400, 's', type: 'bad'",
253
- ''].join("\n"))
254
- e = abs('example.rb')
255
- expect($stdout.string)
256
- .to eq(["#{e}:2:35: C: [Corrected] Redundant curly braces around " \
257
- 'a hash parameter.',
258
- "#{e}:2:35: C: [Corrected] Use the new Ruby 1.9 hash " \
259
- 'syntax.',
260
- # TODO: Don't report that a problem is corrected when it
261
- # actually went away due to another correction.
262
- "#{e}:2:35: C: [Corrected] Space inside { missing.",
263
- # TODO: Don't report duplicates (HashSyntax in this case).
264
- "#{e}:2:36: C: [Corrected] Use the new Ruby 1.9 hash " \
265
- 'syntax.',
266
- "#{e}:2:50: C: [Corrected] Space inside } missing.",
267
- ''].join("\n"))
268
- end
269
-
270
- # A case where two cops, EmptyLinesAroundBody and EmptyLines, try to
271
- # remove the same line in autocorrect.
272
- it 'can correct two empty lines at end of class body' do
273
- create_file('example.rb', ['class Test',
274
- ' def f',
275
- ' end',
276
- '',
277
- '',
278
- 'end'])
279
- expect(cli.run(['--auto-correct'])).to eq(1)
280
- expect($stderr.string).to eq('')
281
- expect(IO.read('example.rb')).to eq(['class Test',
282
- ' def f',
283
- ' end',
284
- 'end',
285
- ''].join("\n"))
286
- end
287
-
288
- # A case where WordArray's correction can be clobbered by
289
- # AccessModifierIndentation's correction.
290
- it 'can correct indentation and another thing' do
291
- create_file('example.rb', ['# encoding: utf-8',
292
- 'class Dsl',
293
- 'private',
294
- ' A = ["git", "path",]',
295
- 'end'])
296
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(1)
297
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
298
- 'class Dsl',
299
- ' private',
300
- '',
301
- ' A = %w(git path)',
302
- 'end',
303
- ''].join("\n"))
304
- e = abs('example.rb')
305
- expect($stdout.string)
306
- .to eq(["#{e}:2:1: C: Missing top-level class documentation " \
307
- 'comment.',
308
- "#{e}:3:1: C: [Corrected] Indent access modifiers like " \
309
- '`private`.',
310
- "#{e}:3:1: C: [Corrected] Keep a blank line before and " \
311
- 'after `private`.',
312
- "#{e}:3:3: W: Useless `private` access modifier.",
313
- "#{e}:3:3: C: [Corrected] Keep a blank line before and " \
314
- 'after `private`.',
315
- "#{e}:4:7: C: [Corrected] Use `%w` or `%W` " \
316
- 'for array of words.',
317
- "#{e}:4:8: C: [Corrected] Prefer single-quoted strings " \
318
- "when you don't need string interpolation or special " \
319
- 'symbols.',
320
- "#{e}:4:15: C: [Corrected] Prefer single-quoted strings " \
321
- "when you don't need string interpolation or special " \
322
- 'symbols.',
323
- "#{e}:4:21: C: [Corrected] Avoid comma after the last item " \
324
- 'of an array.',
325
- "#{e}:5:7: C: [Corrected] Use `%w` or `%W` " \
326
- 'for array of words.',
327
- "#{e}:5:8: C: [Corrected] Prefer single-quoted strings " \
328
- "when you don't need string interpolation or special " \
329
- 'symbols.',
330
- "#{e}:5:15: C: [Corrected] Prefer single-quoted strings " \
331
- "when you don't need string interpolation or special " \
332
- 'symbols.',
333
- "#{e}:5:21: C: [Corrected] Avoid comma after the last item " \
334
- 'of an array.',
335
- ''].join("\n"))
336
- end
337
-
338
- # A case where the same cop could try to correct an offense twice in one
339
- # place.
340
- it 'can correct empty line inside special form of nested modules' do
341
- create_file('example.rb', ['module A module B',
342
- '',
343
- 'end end'])
344
- expect(cli.run(['--auto-correct'])).to eq(1)
345
- expect(IO.read('example.rb')).to eq(['module A module B',
346
- 'end end',
347
- ''].join("\n"))
348
- uncorrected = $stdout.string.split($RS).select do |line|
349
- line.include?('example.rb:') && !line.include?('[Corrected]')
350
- end
351
- expect(uncorrected).not_to be_empty # Hence exit code 1.
352
- end
353
-
354
- it 'can correct single line methods' do
355
- create_file('example.rb', ['# encoding: utf-8',
356
- 'def func1; do_something end # comment',
357
- 'def func2() do_1; do_2; end'])
358
- expect(cli.run(%w(--auto-correct --format offenses))).to eq(0)
359
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
360
- '# comment',
361
- 'def func1',
362
- ' do_something',
363
- 'end',
364
- '',
365
- 'def func2',
366
- ' do_1',
367
- ' do_2',
368
- 'end',
369
- ''].join("\n"))
370
- expect($stdout.string).to eq(['',
371
- '10 Style/TrailingWhitespace',
372
- '3 Style/Semicolon',
373
- '3 Style/SingleLineMethods',
374
- '1 Style/DefWithParentheses',
375
- '1 Style/EmptyLineBetweenDefs',
376
- '--',
377
- '18 Total',
378
- '',
379
- ''].join("\n"))
380
- end
381
-
382
- # In this example, the auto-correction (changing "raise" to "fail")
383
- # creates a new problem (alignment of parameters), which is also
384
- # corrected automatically.
385
- it 'can correct a problems and the problem it creates' do
386
- create_file('example.rb',
387
- ['# encoding: utf-8',
388
- 'raise NotImplementedError,',
389
- " 'Method should be overridden in child classes'"])
390
- expect(cli.run(['--auto-correct'])).to eq(0)
391
- expect(IO.read('example.rb'))
392
- .to eq(['# encoding: utf-8',
393
- 'fail NotImplementedError,',
394
- " 'Method should be overridden in child classes'",
395
- ''].join("\n"))
396
- expect($stdout.string)
397
- .to eq(['Inspecting 1 file',
398
- 'C',
399
- '',
400
- 'Offenses:',
401
- '',
402
- 'example.rb:2:1: C: [Corrected] Use fail instead of ' \
403
- 'raise to signal exceptions.',
404
- 'raise NotImplementedError,',
405
- '^^^^^',
406
- 'example.rb:3:7: C: [Corrected] Align the parameters of a ' \
407
- 'method call if they span more than one line.',
408
- " 'Method should be overridden in child classes'",
409
- ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
410
- '',
411
- '1 file inspected, 2 offenses detected, 2 offenses ' \
412
- 'corrected',
413
- ''].join("\n"))
414
- end
415
-
416
- # Thanks to repeated auto-correction, we can get rid of the trailing
417
- # spaces, and then the extra empty line.
418
- it 'can correct two problems in the same place' do
419
- create_file('example.rb',
420
- ['# encoding: utf-8',
421
- '# Example class.',
422
- 'class Klass',
423
- ' ',
424
- ' def f',
425
- ' end',
426
- 'end'])
427
- expect(cli.run(['--auto-correct'])).to eq(0)
428
- expect(IO.read('example.rb'))
429
- .to eq(['# encoding: utf-8',
430
- '# Example class.',
431
- 'class Klass',
432
- ' def f',
433
- ' end',
434
- 'end',
435
- ''].join("\n"))
436
- expect($stderr.string).to eq('')
437
- expect($stdout.string)
438
- .to eq(['Inspecting 1 file',
439
- 'C',
440
- '',
441
- 'Offenses:',
442
- '',
443
- 'example.rb:4:1: C: [Corrected] Extra empty line detected ' \
444
- 'at class body beginning.',
445
- 'example.rb:4:1: C: [Corrected] Trailing whitespace ' \
446
- 'detected.',
447
- '',
448
- '1 file inspected, 2 offenses detected, 2 offenses ' \
449
- 'corrected',
450
- ''].join("\n"))
451
- end
452
-
453
- it 'can correct MethodDefParentheses and other offense' do
454
- create_file('example.rb',
455
- ['# encoding: utf-8',
456
- 'def primes limit',
457
- ' 1.upto(limit).select { |i| i.even? }',
458
- 'end'])
459
- expect(cli.run(%w(-D --auto-correct))).to eq(0)
460
- expect($stderr.string).to eq('')
461
- expect(IO.read('example.rb'))
462
- .to eq(['# encoding: utf-8',
463
- 'def primes(limit)',
464
- ' 1.upto(limit).select(&:even?)',
465
- 'end',
466
- ''].join("\n"))
467
- expect($stdout.string)
468
- .to eq(['Inspecting 1 file',
469
- 'C',
470
- '',
471
- 'Offenses:',
472
- '',
473
- 'example.rb:2:12: C: [Corrected] ' \
474
- 'Style/MethodDefParentheses: ' \
475
- 'Use def with parentheses when there are parameters.',
476
- 'def primes limit',
477
- ' ^^^^^',
478
- 'example.rb:3:3: C: [Corrected] Style/SymbolProc: ' \
479
- 'Pass &:even? as an argument to select instead of a block.',
480
- ' 1.upto(limit).select { |i| i.even? }',
481
- ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
482
- '',
483
- '1 file inspected, 2 offenses detected, 2 offenses ' \
484
- 'corrected',
485
- ''].join("\n"))
486
- end
487
-
488
- it 'can correct WordArray and SpaceAfterComma offenses' do
489
- create_file('example.rb',
490
- ['# encoding: utf-8',
491
- "f(type: ['offline','offline_payment'],",
492
- " bar_colors: ['958c12','953579','ff5800','0085cc'])"])
493
- expect(cli.run(%w(-D --auto-correct --format o))).to eq(0)
494
- expect($stdout.string)
495
- .to eq(['',
496
- '4 Style/SpaceAfterComma',
497
- '2 Style/WordArray',
498
- '--',
499
- '6 Total',
500
- '',
501
- ''].join("\n"))
502
- expect(IO.read('example.rb'))
503
- .to eq(['# encoding: utf-8',
504
- 'f(type: %w(offline offline_payment),',
505
- ' bar_colors: %w(958c12 953579 ff5800 0085cc))',
506
- ''].join("\n"))
507
- end
508
-
509
- it 'can correct SpaceAfterComma and HashSyntax offenses' do
510
- create_file('example.rb',
511
- ['# encoding: utf-8',
512
- "I18n.t('description',:property_name => property.name)"])
513
- expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(0)
514
- expect($stdout.string)
515
- .to eq(["#{abs('example.rb')}:2:21: C: [Corrected] " \
516
- 'Style/SpaceAfterComma: Space missing after comma.',
517
- "#{abs('example.rb')}:2:22: C: [Corrected] " \
518
- 'Style/HashSyntax: Use the new Ruby 1.9 hash syntax.',
519
- ''].join("\n"))
520
- expect(IO.read('example.rb'))
521
- .to eq(['# encoding: utf-8',
522
- "I18n.t('description', property_name: property.name)",
523
- ''].join("\n"))
524
- end
525
-
526
- it 'can correct HashSyntax and SpaceAroundOperators offenses' do
527
- create_file('example.rb',
528
- ['# encoding: utf-8',
529
- '{ :b=>1 }'])
530
- expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(0)
531
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
532
- '{ b: 1 }',
533
- ''].join("\n"))
534
- expect($stdout.string)
535
- .to eq(["#{abs('example.rb')}:2:3: C: [Corrected] " \
536
- 'Style/HashSyntax: Use the new Ruby 1.9 hash syntax.',
537
- "#{abs('example.rb')}:2:5: C: [Corrected] " \
538
- 'Style/SpaceAroundOperators: Surrounding space missing for ' \
539
- "operator '=>'.",
540
- ''].join("\n"))
541
- end
542
-
543
- it 'can correct HashSyntax when --only is used' do
544
- create_file('example.rb',
545
- ['# encoding: utf-8',
546
- '{ :b=>1 }'])
547
- expect(cli.run(%w(--auto-correct -f emacs
548
- --only Style/HashSyntax))).to eq(0)
549
- expect($stderr.string).to eq('')
550
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
551
- '{ b: 1 }',
552
- ''].join("\n"))
553
- expect($stdout.string)
554
- .to eq(["#{abs('example.rb')}:2:3: C: [Corrected] Use the new " \
555
- 'Ruby 1.9 hash syntax.',
556
- ''].join("\n"))
557
- end
558
-
559
- it 'can correct TrailingBlankLines and TrailingWhitespace offenses' do
560
- create_file('example.rb',
561
- ['# encoding: utf-8',
562
- '',
563
- ' ',
564
- '',
565
- ''])
566
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
567
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
568
- ''].join("\n"))
569
- expect($stdout.string)
570
- .to eq(["#{abs('example.rb')}:2:1: C: [Corrected] 3 trailing " \
571
- 'blank lines detected.',
572
- "#{abs('example.rb')}:3:1: C: [Corrected] Trailing " \
573
- 'whitespace detected.',
574
- ''].join("\n"))
575
- end
576
-
577
- it 'can correct MethodCallParentheses and EmptyLiteral offenses' do
578
- create_file('example.rb',
579
- ['# encoding: utf-8',
580
- 'Hash.new()'])
581
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
582
- expect($stderr.string).to eq('')
583
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
584
- '{}',
585
- ''].join("\n"))
586
- expect($stdout.string)
587
- .to eq(["#{abs('example.rb')}:2:1: C: [Corrected] Use hash " \
588
- 'literal `{}` instead of `Hash.new`.',
589
- "#{abs('example.rb')}:2:9: C: [Corrected] Do not use " \
590
- 'parentheses for method calls with no arguments.',
591
- ''].join("\n"))
592
- end
593
-
594
- it 'can correct IndentHash offenses with separator style' do
595
- create_file('example.rb',
596
- ['# encoding: utf-8',
597
- 'CONVERSION_CORRESPONDENCE = {',
598
- ' match_for_should: :match,',
599
- ' match_for_should_not: :match_when_negated,',
600
- ' failure_message_for_should: :failure_message,',
601
- 'failure_message_for_should_not: :failure_message_when',
602
- '}'])
603
- create_file('.rubocop.yml',
604
- ['Style/AlignHash:',
605
- ' EnforcedColonStyle: separator'])
606
- expect(cli.run(%w(--auto-correct))).to eq(0)
607
- expect(IO.read('example.rb'))
608
- .to eq(['# encoding: utf-8',
609
- 'CONVERSION_CORRESPONDENCE = {',
610
- ' match_for_should: :match,',
611
- ' match_for_should_not: :match_when_negated,',
612
- ' failure_message_for_should: :failure_message,',
613
- ' failure_message_for_should_not: :failure_message_when',
614
- '}',
615
- ''].join("\n"))
616
- end
617
-
618
- it 'does not say [Corrected] if correction was avoided' do
619
- create_file('example.rb', ['# encoding: utf-8',
620
- 'a = c and b',
621
- 'not a && b',
622
- 'func a do b end',
623
- "Signal.trap('TERM') { system(cmd); exit }"])
624
- expect(cli.run(%w(-a -f simple))).to eq(1)
625
- expect($stderr.string).to eq('')
626
- expect(IO.read('example.rb'))
627
- .to eq(['# encoding: utf-8',
628
- 'a = c and b',
629
- 'not a && b',
630
- 'func a do b end',
631
- "Signal.trap('TERM') { system(cmd); exit }",
632
- ''].join("\n"))
633
- expect($stdout.string)
634
- .to eq(['== example.rb ==',
635
- 'C: 2: 7: Use && instead of and.',
636
- 'C: 3: 1: Use ! instead of not.',
637
- 'C: 4: 8: Prefer {...} over do...end for single-line ' \
638
- 'blocks.',
639
- 'C: 5: 34: Do not use semicolons to terminate expressions.',
640
- '',
641
- '1 file inspected, 4 offenses detected',
642
- ''].join("\n"))
643
- end
644
-
645
- it 'does not hang SpaceAfterPunctuation and SpaceInsideParens' do
646
- create_file('example.rb',
647
- ['# encoding: utf-8',
648
- 'some_method(a, )'])
649
- Timeout.timeout(10) do
650
- expect(cli.run(%w(--auto-correct))).to eq(0)
651
- end
652
- expect($stderr.string).to eq('')
653
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
654
- 'some_method(a)',
655
- ''].join("\n"))
656
- end
657
-
658
- it 'does not hang SpaceAfterPunctuation and SpaceInsideBrackets' do
659
- create_file('example.rb',
660
- ['# encoding: utf-8',
661
- 'puts [1, ]'])
662
- Timeout.timeout(10) do
663
- expect(cli.run(%w(--auto-correct))).to eq(0)
664
- end
665
- expect($stderr.string).to eq('')
666
- expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
667
- 'puts [1]',
668
- ''].join("\n"))
669
- end
670
- end
671
-
672
- describe '--auto-gen-config' do
673
- before(:each) do
674
- RuboCop::Formatter::DisabledConfigFormatter
675
- .config_to_allow_offenses = {}
676
- end
677
-
678
- it 'overwrites an existing todo file' do
679
- create_file('example1.rb', ['# encoding: utf-8',
680
- 'x= 0 ',
681
- '#' * 85,
682
- 'y ',
683
- 'puts x'])
684
- create_file('.rubocop_todo.yml', ['Metrics/LineLength:',
685
- ' Enabled: false'])
686
- create_file('.rubocop.yml', ['inherit_from: .rubocop_todo.yml'])
687
- expect(cli.run(['--auto-gen-config'])).to eq(1)
688
- expect(IO.readlines('.rubocop_todo.yml')[7..-1].map(&:chomp))
689
- .to eq(['# Offense count: 1',
690
- '# Configuration parameters: AllowURI, URISchemes.',
691
- 'Metrics/LineLength:',
692
- ' Max: 85',
693
- '',
694
- '# Offense count: 1',
695
- '# Cop supports --auto-correct.',
696
- 'Style/SpaceAroundOperators:',
697
- ' Enabled: false',
698
- '',
699
- '# Offense count: 2',
700
- '# Cop supports --auto-correct.',
701
- 'Style/TrailingWhitespace:',
702
- ' Enabled: false'])
703
-
704
- # Create new CLI instance to avoid using cached configuration.
705
- new_cli = described_class.new
706
-
707
- expect(new_cli.run(['example1.rb'])).to eq(0)
708
- end
709
-
710
- it 'exits with error if file arguments are given' do
711
- create_file('example1.rb', ['# encoding: utf-8',
712
- 'x= 0 ',
713
- '#' * 85,
714
- 'y ',
715
- 'puts x'])
716
- expect { cli.run(['--auto-gen-config', 'example1.rb']) }
717
- .to exit_with_code(1)
718
- expect($stderr.string)
719
- .to eq(['--auto-gen-config can not be combined with any other ' \
720
- 'arguments.',
721
- ''].join("\n"))
722
- expect($stdout.string).to eq('')
723
- end
724
-
725
- it 'can generate a todo list' do
726
- create_file('example1.rb', ['# encoding: utf-8',
727
- '$x= 0 ',
728
- '#' * 90,
729
- '#' * 85,
730
- 'y ',
731
- 'puts x'])
732
- create_file('example2.rb', ['# encoding: utf-8',
733
- "\tx = 0",
734
- 'puts x'])
735
- expect(cli.run(['--auto-gen-config'])).to eq(1)
736
- expect($stderr.string).to eq('')
737
- expect($stdout.string)
738
- .to include(['Created .rubocop_todo.yml.',
739
- 'Run `rubocop --config .rubocop_todo.yml`, or',
740
- 'add inherit_from: .rubocop_todo.yml in a ' \
741
- '.rubocop.yml file.',
742
- ''].join("\n"))
743
- expected =
744
- ['# This configuration was generated by `rubocop --auto-gen-config`',
745
- /# on .* using RuboCop version .*/,
746
- '# The point is for the user to remove these configuration records',
747
- '# one by one as the offenses are removed from the code base.',
748
- '# Note that changes in the inspected code, or installation of new',
749
- '# versions of RuboCop, may require this file to be generated ' \
750
- 'again.',
751
- '',
752
- '# Offense count: 2',
753
- '# Configuration parameters: AllowURI, URISchemes.',
754
- 'Metrics/LineLength:',
755
- ' Max: 90',
756
- '',
757
- '# Offense count: 1',
758
- '# Cop supports --auto-correct.',
759
- 'Style/CommentIndentation:',
760
- ' Enabled: false',
761
- '',
762
- '# Offense count: 1',
763
- '# Configuration parameters: AllowedVariables.',
764
- 'Style/GlobalVars:',
765
- ' Enabled: false',
766
- '',
767
- '# Offense count: 1',
768
- '# Cop supports --auto-correct.',
769
- 'Style/IndentationConsistency:',
770
- ' Enabled: false',
771
- '',
772
- '# Offense count: 1',
773
- '# Cop supports --auto-correct.',
774
- 'Style/SpaceAroundOperators:',
775
- ' Enabled: false',
776
- '',
777
- '# Offense count: 1',
778
- '# Cop supports --auto-correct.',
779
- 'Style/Tab:',
780
- ' Enabled: false',
781
- '',
782
- '# Offense count: 2',
783
- '# Cop supports --auto-correct.',
784
- 'Style/TrailingWhitespace:',
785
- ' Enabled: false']
786
- actual = IO.read('.rubocop_todo.yml').split($RS)
787
- expected.each_with_index do |line, ix|
788
- if line.is_a?(String)
789
- expect(actual[ix]).to eq(line)
790
- else
791
- expect(actual[ix]).to match(line)
792
- end
793
- end
794
- end
795
-
796
- it 'does not generate configuration for the Syntax cop' do
797
- create_file('example1.rb', ['# encoding: utf-8',
798
- 'x = < ', # Syntax error
799
- 'puts x'])
800
- create_file('example2.rb', ['# encoding: utf-8',
801
- "\tx = 0",
802
- 'puts x'])
803
- expect(cli.run(['--auto-gen-config'])).to eq(1)
804
- expect($stderr.string).to eq('')
805
- expected =
806
- ['# This configuration was generated by `rubocop --auto-gen-config`',
807
- /# on .* using RuboCop version .*/,
808
- '# The point is for the user to remove these configuration records',
809
- '# one by one as the offenses are removed from the code base.',
810
- '# Note that changes in the inspected code, or installation of new',
811
- '# versions of RuboCop, may require this file to be generated ' \
812
- 'again.',
813
- '',
814
- '# Offense count: 1',
815
- '# Cop supports --auto-correct.',
816
- 'Style/CommentIndentation:',
817
- ' Enabled: false',
818
- '',
819
- '# Offense count: 1',
820
- '# Cop supports --auto-correct.',
821
- 'Style/IndentationConsistency:',
822
- ' Enabled: false',
823
- '',
824
- '# Offense count: 1',
825
- '# Cop supports --auto-correct.',
826
- 'Style/Tab:',
827
- ' Enabled: false']
828
- actual = IO.read('.rubocop_todo.yml').split($RS)
829
- expect(actual.length).to eq(expected.length)
830
- expected.each_with_index do |line, ix|
831
- if line.is_a?(String)
832
- expect(actual[ix]).to eq(line)
833
- else
834
- expect(actual[ix]).to match(line)
835
- end
836
- end
837
- end
838
-
839
- it 'generates a todo list that removes the reports' do
840
- RuboCop::Cop::Style::RegexpLiteral.slash_count = 0
841
- create_file('example.rb', ['# encoding: utf-8',
842
- 'y.gsub!(%r{abc/xyz}, "#{x}")'])
843
- expect(cli.run(%w(--format emacs))).to eq(1)
844
- expect($stdout.string)
845
- .to eq(["#{abs('example.rb')}:2:9: C: Use %r only for regular " \
846
- "expressions matching more than 1 '/' character.",
847
- ''].join("\n"))
848
- expect(cli.run(['--auto-gen-config'])).to eq(1)
849
- expected =
850
- ['# This configuration was generated by `rubocop --auto-gen-config`',
851
- /# on .* using RuboCop version .*/,
852
- '# The point is for the user to remove these configuration records',
853
- '# one by one as the offenses are removed from the code base.',
854
- '# Note that changes in the inspected code, or installation of new',
855
- '# versions of RuboCop, may require this file to be generated ' \
856
- 'again.',
857
- '',
858
- '# Offense count: 1',
859
- '# Configuration parameters: MaxSlashes.',
860
- 'Style/RegexpLiteral:',
861
- ' Enabled: false']
862
- actual = IO.read('.rubocop_todo.yml').split($RS)
863
- expected.each_with_index do |line, ix|
864
- if line.is_a?(String)
865
- expect(actual[ix]).to eq(line)
866
- else
867
- expect(actual[ix]).to match(line)
868
- end
869
- end
870
- $stdout = StringIO.new
871
- result = cli.run(%w(--config .rubocop_todo.yml --format emacs))
872
- expect($stdout.string).to eq('')
873
- expect(result).to eq(0)
874
- end
875
- end
876
-
877
- describe '--only' do
878
- context 'when one cop is given' do
879
- it 'runs just one cop' do
880
- create_file('example.rb', ['if x== 0 ',
881
- "\ty",
882
- 'end'])
883
- # IfUnlessModifier depends on the configuration of LineLength.
884
-
885
- expect(cli.run(['--format', 'simple',
886
- '--only', 'Style/IfUnlessModifier',
887
- 'example.rb'])).to eq(1)
888
- expect($stdout.string)
889
- .to eq(['== example.rb ==',
890
- 'C: 1: 1: Favor modifier if usage when ' \
891
- 'having a single-line body. Another good alternative is ' \
892
- 'the usage of control flow &&/||.',
893
- '',
894
- '1 file inspected, 1 offense detected',
895
- ''].join("\n"))
896
- end
897
-
898
- it 'exits with error if an incorrect cop name is passed' do
899
- create_file('example.rb', ['if x== 0 ',
900
- "\ty",
901
- 'end'])
902
- expect(cli.run(['--only', 'Style/123'])).to eq(1)
903
- expect($stderr.string)
904
- .to include('Unrecognized cop or namespace: Style/123.')
905
- end
906
-
907
- it 'accepts cop names from plugins' do
908
- create_file('.rubocop.yml', ['require: rubocop_ext',
909
- '',
910
- 'Style/SomeCop:',
911
- ' Description: Something',
912
- ' Enabled: true'])
913
- create_file('rubocop_ext.rb', ['module RuboCop',
914
- ' module Cop',
915
- ' module Style',
916
- ' class SomeCop < Cop',
917
- ' end',
918
- ' end',
919
- ' end',
920
- 'end'])
921
- create_file('redirect.rb', '$stderr = STDOUT')
922
- rubocop = "#{RuboCop::ConfigLoader::RUBOCOP_HOME}/bin/rubocop"
923
- # Since we define a new cop class, we have to do this in a separate
924
- # process. Otherwise, the extra cop will affect other specs.
925
- output =
926
- `ruby -I . #{rubocop} --require redirect.rb --only Style/SomeCop`
927
- expect($CHILD_STATUS.success?).to be_truthy
928
- # The warning about the unrecognized cop is expected. It's given due
929
- # to the fact that we haven't supplied any default configuration for
930
- # rubocop_ext in this example.
931
- expect(output)
932
- .to eq(['Warning: unrecognized cop Style/SomeCop found in ' \
933
- "#{abs('.rubocop.yml')}",
934
- 'Inspecting 2 files',
935
- '..',
936
- '',
937
- '2 files inspected, no offenses detected',
938
- ''].join("\n"))
939
- end
940
-
941
- context 'without using namespace' do
942
- it 'runs just one cop' do
943
- create_file('example.rb', ['if x== 0 ',
944
- "\ty",
945
- 'end'])
946
-
947
- expect(cli.run(['--format', 'simple',
948
- '--display-cop-names',
949
- '--only', 'IfUnlessModifier',
950
- 'example.rb'])).to eq(1)
951
- expect($stdout.string)
952
- .to eq(['== example.rb ==',
953
- 'C: 1: 1: Style/IfUnlessModifier: Favor modifier if ' \
954
- 'usage when having a single-line body. Another good ' \
955
- 'alternative is the usage of control flow &&/||.',
956
- '',
957
- '1 file inspected, 1 offense detected',
958
- ''].join("\n"))
959
- end
960
- end
961
-
962
- it 'enables the given cop' do
963
- create_file('example.rb',
964
- ['x = 0 ',
965
- # Disabling comments still apply.
966
- '# rubocop:disable Style/TrailingWhitespace',
967
- 'y = 1 '])
968
-
969
- create_file('.rubocop.yml', ['Style/TrailingWhitespace:',
970
- ' Enabled: false'])
971
-
972
- expect(cli.run(['--format', 'simple',
973
- '--only', 'Style/TrailingWhitespace',
974
- 'example.rb'])).to eq(1)
975
- expect($stderr.string).to eq('')
976
- expect($stdout.string)
977
- .to eq(['== example.rb ==',
978
- 'C: 1: 6: Trailing whitespace detected.',
979
- '',
980
- '1 file inspected, 1 offense detected',
981
- ''].join("\n"))
982
- end
983
- end
984
-
985
- context 'when several cops are given' do
986
- it 'runs the given cops' do
987
- create_file('example.rb', ['if x== 100000000000000 ',
988
- "\ty",
989
- 'end'])
990
- expect(cli.run(['--format', 'simple',
991
- '--only',
992
- 'Style/IfUnlessModifier,Style/Tab,' \
993
- 'Style/SpaceAroundOperators',
994
- 'example.rb'])).to eq(1)
995
- expect($stderr.string).to eq('')
996
- expect($stdout.string)
997
- .to eq(['== example.rb ==',
998
- 'C: 1: 1: Favor modifier if usage when ' \
999
- 'having a single-line body. Another good alternative is ' \
1000
- 'the usage of control flow &&/||.',
1001
- "C: 1: 5: Surrounding space missing for operator '=='.",
1002
- 'C: 2: 1: Tab detected.',
1003
- '',
1004
- '1 file inspected, 3 offenses detected',
1005
- ''].join("\n"))
1006
- end
1007
-
1008
- context 'and --lint' do
1009
- it 'runs the given cops plus all enabled lint cops' do
1010
- create_file('example.rb', ['if x== 100000000000000 ',
1011
- "\ty = 3",
1012
- ' end'])
1013
- create_file('.rubocop.yml', ['Lint/EndAlignment:',
1014
- ' Enabled: false'])
1015
- expect(cli.run(['--format', 'simple',
1016
- '--only', 'Style/Tab,Style/SpaceAroundOperators',
1017
- '--lint',
1018
- 'example.rb'])).to eq(1)
1019
- expect($stdout.string)
1020
- .to eq(['== example.rb ==',
1021
- 'C: 1: 5: Surrounding space missing for operator ' \
1022
- "'=='.",
1023
- 'C: 2: 1: Tab detected.',
1024
- 'W: 2: 2: Useless assignment to variable - y.',
1025
- '',
1026
- '1 file inspected, 3 offenses detected',
1027
- ''].join("\n"))
1028
- end
1029
- end
1030
- end
1031
-
1032
- context 'when a namespace is given' do
1033
- it 'runs all enabled cops in that namespace' do
1034
- create_file('example.rb', ['if x== 100000000000000 ',
1035
- ' ' + '#' * 100,
1036
- "\ty",
1037
- 'end'])
1038
- expect(cli.run(%w(-f offenses --only Metrics example.rb))).to eq(1)
1039
- expect($stdout.string).to eq(['',
1040
- '1 Metrics/LineLength',
1041
- '--',
1042
- '1 Total',
1043
- '',
1044
- ''].join("\n"))
1045
- end
1046
- end
1047
-
1048
- context 'when two namespaces are given' do
1049
- it 'runs all enabled cops in those namespaces' do
1050
- create_file('example.rb', ['# encoding: utf-8',
1051
- 'if x== 100000000000000 ',
1052
- ' # ' + '-' * 98,
1053
- "\ty",
1054
- 'end'])
1055
- create_file('.rubocop.yml', ['Style/SpaceAroundOperators:',
1056
- ' Enabled: false'])
1057
- expect(cli.run(%w(-f o --only Metrics,Style example.rb))).to eq(1)
1058
- expect($stdout.string)
1059
- .to eq(['',
1060
- '1 Metrics/LineLength',
1061
- '1 Style/CommentIndentation',
1062
- '1 Style/IndentationWidth',
1063
- '1 Style/NumericLiterals',
1064
- '1 Style/Tab',
1065
- '1 Style/TrailingWhitespace',
1066
- '--',
1067
- '6 Total',
1068
- '',
1069
- ''].join("\n"))
1070
- end
1071
- end
1072
- end
1073
-
1074
- describe '--except' do
1075
- context 'when one name is given' do
1076
- it 'exits with error if the cop name is incorrect' do
1077
- create_file('example.rb', ['if x== 0 ',
1078
- "\ty",
1079
- 'end'])
1080
- expect(cli.run(['--except', 'Style/123'])).to eq(1)
1081
- expect($stderr.string)
1082
- .to include('Unrecognized cop or namespace: Style/123.')
1083
- end
1084
- end
1085
-
1086
- context 'when one cop plus one namespace are given' do
1087
- it 'runs all cops except the given' do
1088
- create_file('example.rb', ['if x== 0 ',
1089
- "\ty = 3",
1090
- 'end'])
1091
- expect(cli.run(['--format', 'offenses',
1092
- '--except', 'Style/IfUnlessModifier,Lint',
1093
- 'example.rb'])).to eq(1)
1094
- expect($stdout.string)
1095
- .to eq(['',
1096
- # Note: No Lint/UselessAssignment offense.
1097
- '1 Style/IndentationWidth',
1098
- '1 Style/SpaceAroundOperators',
1099
- '1 Style/Tab',
1100
- '1 Style/TrailingWhitespace',
1101
- '--',
1102
- '4 Total',
1103
- '',
1104
- ''].join("\n"))
1105
- end
1106
- end
1107
-
1108
- context 'when one cop is given without namespace' do
1109
- it 'disables the given cop' do
1110
- create_file('example.rb', ['if x== 0 ',
1111
- "\ty",
1112
- 'end'])
1113
-
1114
- cli.run(['--format', 'offenses',
1115
- '--except', 'IfUnlessModifier',
1116
- 'example.rb'])
1117
- with_option = $stdout.string
1118
- $stdout = StringIO.new
1119
- cli.run(['--format', 'offenses',
1120
- 'example.rb'])
1121
- without_option = $stdout.string
1122
-
1123
- expect(without_option.split($RS) - with_option.split($RS))
1124
- .to eq(['1 Style/IfUnlessModifier', '5 Total'])
1125
- end
1126
- end
1127
-
1128
- context 'when several cops are given' do
1129
- it 'disables the given cops' do
1130
- create_file('example.rb', ['if x== 100000000000000 ',
1131
- "\ty",
1132
- 'end'])
1133
- expect(cli.run(['--format', 'offenses',
1134
- '--except',
1135
- 'Style/IfUnlessModifier,Style/Tab,' \
1136
- 'Style/SpaceAroundOperators',
1137
- 'example.rb'])).to eq(1)
1138
- expect($stderr.string).to eq('')
1139
- expect($stdout.string)
1140
- .to eq(['',
1141
- '1 Style/IndentationWidth',
1142
- '1 Style/NumericLiterals',
1143
- '1 Style/TrailingWhitespace',
1144
- '--',
1145
- '3 Total',
1146
- '',
1147
- ''].join("\n"))
1148
- end
1149
- end
1150
- end
1151
-
1152
- describe '--lint' do
1153
- it 'runs only lint cops' do
1154
- create_file('example.rb', ['if 0 ',
1155
- "\ty",
1156
- 'end'])
1157
- # IfUnlessModifier depends on the configuration of LineLength.
1158
-
1159
- expect(cli.run(['--format', 'simple', '--lint',
1160
- 'example.rb'])).to eq(1)
1161
- expect($stdout.string)
1162
- .to eq(['== example.rb ==',
1163
- 'W: 1: 4: Literal 0 appeared in a condition.',
1164
- '',
1165
- '1 file inspected, 1 offense detected',
1166
- ''].join("\n"))
1167
- end
1168
- end
1169
-
1170
- describe '-d/--debug' do
1171
- it 'shows config files' do
1172
- create_file('example1.rb', "\tputs 0")
1173
- expect(cli.run(['--debug', 'example1.rb'])).to eq(1)
1174
- home = File.dirname(File.dirname(File.dirname(__FILE__)))
1175
- expect($stdout.string.lines.grep(/configuration/).map(&:chomp))
1176
- .to eq(["For #{abs('')}:" \
1177
- " configuration from #{home}/config/default.yml",
1178
- "Inheriting configuration from #{home}/config/enabled.yml",
1179
- "Inheriting configuration from #{home}/config/disabled.yml"
1180
- ])
1181
- end
1182
-
1183
- it 'shows cop names' do
1184
- create_file('example1.rb', "\tputs 0")
1185
- expect(cli.run(['--format',
1186
- 'emacs',
1187
- '--debug',
1188
- 'example1.rb'])).to eq(1)
1189
- expect($stdout.string.lines.to_a[-1])
1190
- .to eq(["#{abs('example1.rb')}:1:1: C: Style/Tab: Tab detected.",
1191
- ''].join("\n"))
1192
- end
1193
- end
1194
-
1195
- describe '-D/--display-cop-names' do
1196
- it 'shows cop names' do
1197
- create_file('example1.rb', "\tputs 0")
1198
- expect(cli.run(['--format',
1199
- 'emacs',
1200
- '--debug',
1201
- 'example1.rb'])).to eq(1)
1202
- expect($stdout.string.lines.to_a[-1])
1203
- .to eq(["#{abs('example1.rb')}:1:1: C: Style/Tab: Tab detected.",
1204
- ''].join("\n"))
1205
- end
1206
- end
1207
-
1208
- describe '--show-cops' do
1209
- shared_examples(:prints_config) do
1210
- it 'prints the current configuration' do
1211
- out = stdout.lines.to_a
1212
- printed_config = YAML.load(out.join)
1213
- cop_names = (cop_list[0] || '').split(',')
1214
- cop_names.each do |cop_name|
1215
- global_conf[cop_name].each do |key, value|
1216
- printed_value = printed_config[cop_name][key]
1217
- expect(printed_value).to eq(value)
1218
- end
1219
- end
1220
- end
1221
- end
1222
-
1223
- let(:cops) { RuboCop::Cop::Cop.all }
1224
-
1225
- let(:global_conf) do
1226
- config_path =
1227
- RuboCop::ConfigLoader.configuration_file_for(Dir.pwd.to_s)
1228
- RuboCop::ConfigLoader.configuration_from_file(config_path)
1229
- end
1230
-
1231
- let(:stdout) { $stdout.string }
1232
-
1233
- before do
1234
- create_file('.rubocop.yml', ['Metrics/LineLength:',
1235
- ' Max: 110'])
1236
- expect { cli.run ['--show-cops'] + cop_list }.to exit_with_code(0)
1237
- end
1238
-
1239
- context 'with no args' do
1240
- let(:cop_list) { [] }
1241
-
1242
- # Extracts the first line out of the description
1243
- def short_description_of_cop(cop)
1244
- desc = full_description_of_cop(cop)
1245
- desc ? desc.lines.first.strip : ''
1246
- end
1247
-
1248
- # Gets the full description of the cop or nil if no description is set.
1249
- def full_description_of_cop(cop)
1250
- cop_config = global_conf.for_cop(cop)
1251
- cop_config['Description']
1252
- end
1253
-
1254
- it 'prints all available cops and their description' do
1255
- cops.each do |cop|
1256
- expect(stdout).to include cop.cop_name
1257
- # Because of line breaks, we will only find the beginning.
1258
- expect(stdout).to include short_description_of_cop(cop)[0..60]
1259
- end
1260
- end
1261
-
1262
- it 'prints all types' do
1263
- cops
1264
- .types
1265
- .map(&:to_s)
1266
- .map(&:capitalize)
1267
- .each { |type| expect(stdout).to include(type) }
1268
- end
1269
-
1270
- it 'prints all cops in their right type listing' do
1271
- lines = stdout.lines
1272
- lines.slice_before(/Type /).each do |slice|
1273
- types = cops.types.map(&:to_s).map(&:capitalize)
1274
- current = types.delete(slice.shift[/Type '(?<c>[^']+)'/, 'c'])
1275
- # all cops in their type listing
1276
- cops.with_type(current).each do |cop|
1277
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_truthy
1278
- end
1279
-
1280
- # no cop in wrong type listing
1281
- types.each do |type|
1282
- cops.with_type(type).each do |cop|
1283
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_falsey
1284
- end
1285
- end
1286
- end
1287
- end
1288
-
1289
- include_examples :prints_config
1290
- end
1291
-
1292
- context 'with one cop given' do
1293
- let(:cop_list) { ['Style/Tab'] }
1294
-
1295
- it 'prints that cop and nothing else' do
1296
- expect(stdout).to eq(
1297
- ['# Supports --auto-correct',
1298
- 'Style/Tab:',
1299
- ' Description: No hard tabs.',
1300
- ' StyleGuide: ' \
1301
- 'https://github.com/bbatsov/ruby-style-guide#spaces-indentation',
1302
- ' Enabled: true',
1303
- '',
1304
- ''].join("\n"))
1305
- end
1306
-
1307
- include_examples :prints_config
1308
- end
1309
-
1310
- context 'with two cops given' do
1311
- let(:cop_list) { ['Style/Tab,Metrics/LineLength'] }
1312
- include_examples :prints_config
1313
- end
1314
-
1315
- context 'with one of the cops misspelled' do
1316
- let(:cop_list) { ['Style/Tab,Lint/X123'] }
1317
-
1318
- it 'skips the unknown cop' do
1319
- expect(stdout).to eq(
1320
- ['# Supports --auto-correct',
1321
- 'Style/Tab:',
1322
- ' Description: No hard tabs.',
1323
- ' StyleGuide: ' \
1324
- 'https://github.com/bbatsov/ruby-style-guide#spaces-indentation',
1325
- ' Enabled: true',
1326
- '',
1327
- ''].join("\n"))
1328
- end
1329
- end
1330
- end
1331
-
1332
- describe '-f/--format' do
1333
- let(:target_file) { 'example.rb' }
1334
-
1335
- before do
1336
- create_file(target_file, ['# encoding: utf-8',
1337
- '#' * 90])
1338
- end
1339
-
1340
- describe 'builtin formatters' do
1341
- context 'when simple format is specified' do
1342
- it 'outputs with simple format' do
1343
- cli.run(['--format', 'simple', 'example.rb'])
1344
- expect($stdout.string)
1345
- .to include(["== #{target_file} ==",
1346
- 'C: 2: 81: Line is too long. [90/80]'].join("\n"))
1347
- end
1348
- end
1349
-
1350
- context 'when clang format is specified' do
1351
- it 'outputs with clang format' do
1352
- create_file('example1.rb', ['# encoding: utf-8',
1353
- 'x= 0 ',
1354
- '#' * 85,
1355
- 'y ',
1356
- 'puts x'])
1357
- create_file('example2.rb', ['# encoding: utf-8',
1358
- "\tx",
1359
- 'def a',
1360
- ' puts',
1361
- 'end'])
1362
- create_file('example3.rb', ['# encoding: utf-8',
1363
- 'def badName',
1364
- ' if something',
1365
- ' test',
1366
- ' end',
1367
- 'end'])
1368
- expect(cli.run(['--format', 'clang', 'example1.rb',
1369
- 'example2.rb', 'example3.rb']))
1370
- .to eq(1)
1371
- expect($stdout.string)
1372
- .to eq(['example1.rb:2:2: C: Surrounding space missing for ' \
1373
- "operator '='.",
1374
- 'x= 0 ',
1375
- ' ^',
1376
- 'example1.rb:2:5: C: Trailing whitespace detected.',
1377
- 'x= 0 ',
1378
- ' ^',
1379
- 'example1.rb:3:81: C: Line is too long. [85/80]',
1380
- '###################################################' \
1381
- '##################################',
1382
- ' ' \
1383
- ' ^^^^^',
1384
- 'example1.rb:4:2: C: Trailing whitespace detected.',
1385
- 'y ',
1386
- ' ^',
1387
- 'example2.rb:1:1: C: Incorrect indentation detected' \
1388
- ' (column 0 instead of 1).',
1389
- '# encoding: utf-8',
1390
- '^^^^^^^^^^^^^^^^^',
1391
- 'example2.rb:2:1: C: Tab detected.',
1392
- "\tx",
1393
- '^',
1394
- 'example2.rb:3:1: C: Inconsistent indentation ' \
1395
- 'detected.',
1396
- 'def a',
1397
- '^^^^^',
1398
- 'example2.rb:4:1: C: Use 2 (not 3) spaces for ' \
1399
- 'indentation.',
1400
- ' puts',
1401
- '^^^',
1402
- 'example3.rb:2:5: C: Use snake_case for method names.',
1403
- 'def badName',
1404
- ' ^^^^^^^',
1405
- 'example3.rb:3:3: C: Use a guard clause instead of ' \
1406
- 'wrapping the code inside a conditional expression.',
1407
- ' if something',
1408
- ' ^^',
1409
- 'example3.rb:3:3: C: Favor modifier if usage ' \
1410
- 'when having a single-line body. Another good ' \
1411
- 'alternative is the usage of control flow &&/||.',
1412
- ' if something',
1413
- ' ^^',
1414
- 'example3.rb:5:5: W: end at 5, 4 is not aligned ' \
1415
- 'with if at 3, 2',
1416
- ' end',
1417
- ' ^^^',
1418
- '',
1419
- '3 files inspected, 12 offenses detected',
1420
- ''].join("\n"))
1421
- end
1422
- end
1423
-
1424
- context 'when emacs format is specified' do
1425
- it 'outputs with emacs format' do
1426
- create_file('example1.rb', ['# encoding: utf-8',
1427
- 'x= 0 ',
1428
- 'y ',
1429
- 'puts x'])
1430
- create_file('example2.rb', ['# encoding: utf-8',
1431
- "\tx = 0",
1432
- 'puts x'])
1433
- expect(cli.run(['--format', 'emacs', 'example1.rb',
1434
- 'example2.rb'])).to eq(1)
1435
- expected_output =
1436
- ["#{abs('example1.rb')}:2:2: C: Surrounding space missing" \
1437
- " for operator '='.",
1438
- "#{abs('example1.rb')}:2:5: C: Trailing whitespace detected.",
1439
- "#{abs('example1.rb')}:3:2: C: Trailing whitespace detected.",
1440
- "#{abs('example2.rb')}:1:1: C: Incorrect indentation detected" \
1441
- ' (column 0 instead of 1).',
1442
- "#{abs('example2.rb')}:2:1: C: Tab detected.",
1443
- "#{abs('example2.rb')}:3:1: C: Inconsistent indentation " \
1444
- 'detected.',
1445
- ''].join("\n")
1446
- expect($stdout.string).to eq(expected_output)
1447
- end
1448
- end
1449
-
1450
- context 'when unknown format name is specified' do
1451
- it 'aborts with error message' do
1452
- expect { cli.run(['--format', 'unknown', 'example.rb']) }
1453
- .to exit_with_code(1)
1454
- expect($stderr.string)
1455
- .to include('No formatter for "unknown"')
1456
- end
1457
- end
1458
-
1459
- context 'when ambiguous format name is specified' do
1460
- it 'aborts with error message' do
1461
- # Both 'files' and 'fuubar' start with an 'f'.
1462
- expect { cli.run(['--format', 'f', 'example.rb']) }
1463
- .to exit_with_code(1)
1464
- expect($stderr.string)
1465
- .to include('Cannot determine formatter for "f"')
1466
- end
1467
- end
1468
- end
1469
-
1470
- describe 'custom formatter' do
1471
- let(:target_file) { abs('example.rb') }
1472
-
1473
- context 'when a class name is specified' do
1474
- it 'uses the class as a formatter' do
1475
- module MyTool
1476
- class RuboCopFormatter < RuboCop::Formatter::BaseFormatter
1477
- def started(all_files)
1478
- output.puts "started: #{all_files.join(',')}"
1479
- end
1480
-
1481
- def file_started(file, _options)
1482
- output.puts "file_started: #{file}"
1483
- end
1484
-
1485
- def file_finished(file, _offenses)
1486
- output.puts "file_finished: #{file}"
1487
- end
1488
-
1489
- def finished(processed_files)
1490
- output.puts "finished: #{processed_files.join(',')}"
1491
- end
1492
- end
1493
- end
1494
-
1495
- cli.run(['--format', 'MyTool::RuboCopFormatter', 'example.rb'])
1496
- expect($stdout.string).to eq(["started: #{target_file}",
1497
- "file_started: #{target_file}",
1498
- "file_finished: #{target_file}",
1499
- "finished: #{target_file}",
1500
- ''].join("\n"))
1501
- end
1502
- end
1503
-
1504
- context 'when unknown class name is specified' do
1505
- it 'aborts with error message' do
1506
- args = '--format UnknownFormatter example.rb'
1507
- expect { cli.run(args.split) }.to exit_with_code(1)
1508
- expect($stderr.string).to include('UnknownFormatter')
1509
- end
1510
- end
1511
- end
1512
-
1513
- it 'can be used multiple times' do
1514
- cli.run(['--format', 'simple', '--format', 'emacs', 'example.rb'])
1515
- expect($stdout.string)
1516
- .to include(["== #{target_file} ==",
1517
- 'C: 2: 81: Line is too long. [90/80]',
1518
- "#{abs(target_file)}:2:81: C: Line is too long. " \
1519
- '[90/80]'].join("\n"))
1520
- end
1521
- end
1522
-
1523
- describe '-o/--out option' do
1524
- let(:target_file) { 'example.rb' }
1525
-
1526
- before do
1527
- create_file(target_file, ['# encoding: utf-8',
1528
- '#' * 90])
1529
- end
1530
-
1531
- it 'redirects output to the specified file' do
1532
- cli.run(['--out', 'output.txt', target_file])
1533
- expect(File.read('output.txt')).to include('Line is too long.')
1534
- end
1535
-
1536
- it 'is applied to the previously specified formatter' do
1537
- cli.run(['--format', 'simple',
1538
- '--format', 'emacs', '--out', 'emacs_output.txt',
1539
- target_file])
1540
-
1541
- expect($stdout.string).to eq(["== #{target_file} ==",
1542
- 'C: 2: 81: Line is too long. [90/80]',
1543
- '',
1544
- '1 file inspected, 1 offense detected',
1545
- ''].join("\n"))
1546
-
1547
- expect(File.read('emacs_output.txt'))
1548
- .to eq(["#{abs(target_file)}:2:81: C: Line is too long. [90/80]",
1549
- ''].join("\n"))
1550
- end
1551
- end
1552
-
1553
- describe '--fail-level option' do
1554
- let(:target_file) { 'example.rb' }
1555
-
1556
- before do
1557
- create_file(target_file, ['# encoding: utf-8',
1558
- 'def f',
1559
- ' x',
1560
- 'end'])
1561
- end
1562
-
1563
- after do
1564
- expect($stderr.string).to eq('')
1565
- expect($stdout.string)
1566
- .to include('1 file inspected, 1 offense detected')
1567
- end
1568
-
1569
- it 'fails when option is less than the severity level' do
1570
- expect(cli.run(['--fail-level', 'refactor', target_file])).to eq(1)
1571
- expect(cli.run(['--fail-level', 'autocorrect', target_file])).to eq(1)
1572
- end
1573
-
1574
- it 'fails when option is equal to the severity level' do
1575
- expect(cli.run(['--fail-level', 'convention', target_file])).to eq(1)
1576
- end
1577
-
1578
- it 'succeeds when option is greater than the severity level' do
1579
- expect(cli.run(['--fail-level', 'warning', target_file])).to eq(0)
1580
- end
1581
-
1582
- context 'with --auto-correct' do
1583
- after do
1584
- expect($stdout.string.lines.to_a.last)
1585
- .to eq('1 file inspected, 1 offense detected, 1 offense corrected' \
1586
- "\n")
1587
- end
1588
-
1589
- it 'fails when option is autocorrect and all offenses are ' \
1590
- 'autocorrected' do
1591
- expect(cli.run(['--auto-correct', '--format', 'simple',
1592
- '--fail-level', 'autocorrect',
1593
- target_file])).to eq(1)
1594
- end
1595
-
1596
- it 'fails when option is A and all offenses are autocorrected' do
1597
- expect(cli.run(['--auto-correct', '--format', 'simple',
1598
- '--fail-level', 'A',
1599
- target_file])).to eq(1)
1600
- end
1601
-
1602
- it 'succeeds when option is not given and all offenses are ' \
1603
- 'autocorrected' do
1604
- expect(cli.run(['--auto-correct', '--format', 'simple',
1605
- target_file])).to eq(0)
1606
- end
1607
-
1608
- it 'succeeds when option is refactor and all offenses are ' \
1609
- 'autocorrected' do
1610
- expect(cli.run(['--auto-correct', '--format', 'simple',
1611
- '--fail-level', 'refactor',
1612
- target_file])).to eq(0)
1613
- end
1614
- end
1615
- end
1616
-
1617
- describe '--force-exclusion' do
1618
- let(:target_file) { 'example.rb' }
1619
-
1620
- before do
1621
- create_file(target_file, ['# encoding: utf-8',
1622
- '#' * 90])
1623
-
1624
- create_file('.rubocop.yml', ['AllCops:',
1625
- ' Exclude:',
1626
- " - #{target_file}"])
1627
- end
1628
-
1629
- it 'excludes files specified in the configuration Exclude ' \
1630
- 'even if they are explicitly passed as arguments' do
1631
- expect(cli.run(['--force-exclusion', target_file])).to eq(0)
1632
- end
1633
- end
1634
- end
1635
-
1636
- context 'when interrupted' do
1637
- it 'returns 1' do
1638
- allow_any_instance_of(RuboCop::Runner)
1639
- .to receive(:aborting?).and_return(true)
1640
- create_file('example.rb', '# encoding: utf-8')
1641
- expect(cli.run(['example.rb'])).to eq(1)
1642
- end
1643
- end
1644
-
1645
- describe '#trap_interrupt' do
1646
- let(:runner) { RuboCop::Runner.new({}, RuboCop::ConfigStore.new) }
1647
- let(:interrupt_handlers) { [] }
1648
-
1649
- before do
1650
- allow(Signal).to receive(:trap).with('INT') do |&block|
1651
- interrupt_handlers << block
1652
- end
1653
- end
1654
-
1655
- def interrupt
1656
- interrupt_handlers.each(&:call)
1657
- end
1658
-
1659
- it 'adds a handler for SIGINT' do
1660
- expect(interrupt_handlers).to be_empty
1661
- cli.trap_interrupt(runner)
1662
- expect(interrupt_handlers.size).to eq(1)
1663
- end
1664
-
1665
- context 'with SIGINT once' do
1666
- it 'aborts processing' do
1667
- cli.trap_interrupt(runner)
1668
- expect(runner).to receive(:abort)
1669
- interrupt
1670
- end
1671
-
1672
- it 'does not exit immediately' do
1673
- cli.trap_interrupt(runner)
1674
- expect_any_instance_of(Object).not_to receive(:exit)
1675
- expect_any_instance_of(Object).not_to receive(:exit!)
1676
- interrupt
1677
- end
1678
- end
1679
-
1680
- context 'with SIGINT twice' do
1681
- it 'exits immediately' do
1682
- cli.trap_interrupt(runner)
1683
- expect_any_instance_of(Object).to receive(:exit!).with(1)
1684
- interrupt
1685
- interrupt
1686
- end
1687
- end
1688
- end
1689
-
1690
- context 'when given a file/directory that is not under the current dir' do
1691
- shared_examples 'checks Rakefile' do
1692
- it 'checks a Rakefile but Style/FileName does not report' do
1693
- create_file('Rakefile', 'x = 1')
1694
- create_file('other/empty', '')
1695
- Dir.chdir('other') do
1696
- expect(cli.run(['--format', 'simple', checked_path])).to eq(1)
1697
- end
1698
- expect($stdout.string)
1699
- .to eq(["== #{abs('Rakefile')} ==",
1700
- 'W: 1: 1: Useless assignment to variable - x.',
1701
- '',
1702
- '1 file inspected, 1 offense detected',
1703
- ''].join("\n"))
1704
- end
1705
- end
1706
-
1707
- context 'and the directory is absolute' do
1708
- let(:checked_path) { abs('..') }
1709
- include_examples 'checks Rakefile'
1710
- end
1711
-
1712
- context 'and the directory is relative' do
1713
- let(:checked_path) { '..' }
1714
- include_examples 'checks Rakefile'
1715
- end
1716
-
1717
- context 'and the Rakefile path is absolute' do
1718
- let(:checked_path) { abs('../Rakefile') }
1719
- include_examples 'checks Rakefile'
1720
- end
1721
-
1722
- context 'and the Rakefile path is relative' do
1723
- let(:checked_path) { '../Rakefile' }
1724
- include_examples 'checks Rakefile'
1725
- end
1726
- end
1727
-
1728
- it 'checks a given correct file and returns 0' do
1729
- create_file('example.rb', ['# encoding: utf-8',
1730
- 'x = 0',
1731
- 'puts x'])
1732
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
1733
- expect($stdout.string)
1734
- .to eq(['',
1735
- '1 file inspected, no offenses detected',
1736
- ''].join("\n"))
1737
- end
1738
-
1739
- it 'checks a given file with faults and returns 1' do
1740
- create_file('example.rb', ['# encoding: utf-8',
1741
- 'x = 0 ',
1742
- 'puts x'])
1743
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
1744
- expect($stdout.string)
1745
- .to eq ['== example.rb ==',
1746
- 'C: 2: 6: Trailing whitespace detected.',
1747
- '',
1748
- '1 file inspected, 1 offense detected',
1749
- ''].join("\n")
1750
- end
1751
-
1752
- it 'registers an offense for a syntax error' do
1753
- create_file('example.rb', ['# encoding: utf-8',
1754
- 'class Test',
1755
- 'en'])
1756
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1757
- expect($stdout.string)
1758
- .to eq(["#{abs('example.rb')}:4:1: E: unexpected " \
1759
- 'token $end',
1760
- ''].join("\n"))
1761
- end
1762
-
1763
- it 'registers an offense for Parser warnings' do
1764
- create_file('example.rb', ['# encoding: utf-8',
1765
- 'puts *test',
1766
- 'if a then b else c end'])
1767
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1768
- expect($stdout.string)
1769
- .to eq(["#{abs('example.rb')}:2:6: W: " \
1770
- 'Ambiguous splat operator. Parenthesize the method arguments ' \
1771
- "if it's surely a splat operator, or add a whitespace to the " \
1772
- 'right of the `*` if it should be a multiplication.',
1773
- "#{abs('example.rb')}:3:1: C: " \
1774
- 'Favor the ternary operator (?:) over if/then/else/end ' \
1775
- 'constructs.',
1776
- ''].join("\n"))
1777
- end
1778
-
1779
- it 'can process a file with an invalid UTF-8 byte sequence' do
1780
- create_file('example.rb', ['# encoding: utf-8',
1781
- "# #{'f9'.hex.chr}#{'29'.hex.chr}"])
1782
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1783
- expect($stdout.string)
1784
- .to eq(["#{abs('example.rb')}:1:1: F: Invalid byte sequence in utf-8.",
1785
- ''].join("\n"))
1786
- end
1787
-
1788
- context 'when errors are raised while processing files due to bugs' do
1789
- let(:errors) do
1790
- ['An error occurred while Encoding cop was inspecting file.rb.']
1791
- end
1792
-
1793
- before do
1794
- allow_any_instance_of(RuboCop::Runner)
1795
- .to receive(:errors).and_return(errors)
1796
- end
1797
-
1798
- it 'displays an error message to stderr' do
1799
- cli.run([])
1800
- expect($stderr.string)
1801
- .to include('1 error occurred:').and include(errors.first)
1802
- end
1803
- end
1804
-
1805
- describe 'rubocop:disable comment' do
1806
- it 'can disable all cops in a code section' do
1807
- src = ['# encoding: utf-8',
1808
- '# rubocop:disable all',
1809
- '#' * 90,
1810
- 'x(123456)',
1811
- 'y("123")',
1812
- 'def func',
1813
- ' # rubocop: enable Metrics/LineLength,Style/StringLiterals',
1814
- ' ' + '#' * 93,
1815
- ' x(123456)',
1816
- ' y("123")',
1817
- 'end']
1818
- create_file('example.rb', src)
1819
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1820
- # all cops were disabled, then 2 were enabled again, so we
1821
- # should get 2 offenses reported.
1822
- expect($stdout.string)
1823
- .to eq(["#{abs('example.rb')}:8:81: C: Line is too long. [95/80]",
1824
- "#{abs('example.rb')}:10:5: C: Prefer single-quoted " \
1825
- "strings when you don't need string interpolation or " \
1826
- 'special symbols.',
1827
- ''].join("\n"))
1828
- end
1829
-
1830
- it 'can disable selected cops in a code section' do
1831
- create_file('example.rb',
1832
- ['# encoding: utf-8',
1833
- '# rubocop:disable Style/LineLength,' \
1834
- 'Style/NumericLiterals,Style/StringLiterals',
1835
- '#' * 90,
1836
- 'x(123456)',
1837
- 'y("123")',
1838
- 'def func',
1839
- ' # rubocop: enable Metrics/LineLength, ' \
1840
- 'Style/StringLiterals',
1841
- ' ' + '#' * 93,
1842
- ' x(123456)',
1843
- ' y("123")',
1844
- 'end'])
1845
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1846
- expect($stderr.string)
1847
- .to eq(["#{abs('example.rb')}: Style/LineLength has the wrong " \
1848
- 'namespace - should be Metrics',
1849
- ''].join("\n"))
1850
- # 3 cops were disabled, then 2 were enabled again, so we
1851
- # should get 2 offenses reported.
1852
- expect($stdout.string)
1853
- .to eq(["#{abs('example.rb')}:8:81: C: Line is too long. [95/80]",
1854
- "#{abs('example.rb')}:10:5: C: Prefer single-quoted " \
1855
- "strings when you don't need string interpolation or " \
1856
- 'special symbols.',
1857
- ''].join("\n"))
1858
- end
1859
-
1860
- it 'can disable all cops on a single line' do
1861
- create_file('example.rb', ['# encoding: utf-8',
1862
- 'y("123", 123456) # rubocop:disable all'
1863
- ])
1864
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
1865
- expect($stdout.string).to be_empty
1866
- end
1867
-
1868
- it 'can disable selected cops on a single line' do
1869
- create_file('example.rb',
1870
- ['# encoding: utf-8',
1871
- 'a' * 90 + ' # rubocop:disable Metrics/LineLength',
1872
- '#' * 95,
1873
- 'y("123") # rubocop:disable Metrics/LineLength,' \
1874
- 'Style/StringLiterals'
1875
- ])
1876
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1877
- expect($stdout.string)
1878
- .to eq(["#{abs('example.rb')}:3:81: C: Line is too long. [95/80]",
1879
- ''].join("\n"))
1880
- end
1881
-
1882
- context 'without using namespace' do
1883
- it 'can disable selected cops on a single line' do
1884
- create_file('example.rb',
1885
- ['# encoding: utf-8',
1886
- 'a' * 90 + ' # rubocop:disable LineLength',
1887
- '#' * 95,
1888
- 'y("123") # rubocop:disable StringLiterals'
1889
- ])
1890
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
1891
- expect($stdout.string)
1892
- .to eq(["#{abs('example.rb')}:3:81: C: Line is too long. [95/80]",
1893
- ''].join("\n"))
1894
- end
1895
- end
1896
- end
1897
-
1898
- it 'finds a file with no .rb extension but has a shebang line' do
1899
- create_file('example', ['#!/usr/bin/env ruby',
1900
- '# encoding: utf-8',
1901
- 'x = 0',
1902
- 'puts x'
1903
- ])
1904
- expect(cli.run(%w(--format simple))).to eq(0)
1905
- expect($stdout.string)
1906
- .to eq(['', '1 file inspected, no offenses detected', ''].join("\n"))
1907
- end
1908
-
1909
- it 'does not register any offenses for an empty file' do
1910
- create_file('example.rb', '')
1911
- expect(cli.run(%w(--format simple))).to eq(0)
1912
- expect($stdout.string)
1913
- .to eq(['', '1 file inspected, no offenses detected', ''].join("\n"))
1914
- end
1915
-
1916
- describe 'style guide only usage' do
1917
- context 'via the cli option' do
1918
- describe '--only-guide-cops' do
1919
- it 'skips cops that have no link to a style guide' do
1920
- create_file('example.rb', 'fail')
1921
- create_file('.rubocop.yml', ['Metrics/LineLength:',
1922
- ' Enabled: true',
1923
- ' StyleGuide: ~',
1924
- ' Max: 2'])
1925
-
1926
- expect(cli.run(['--format', 'simple', '--only-guide-cops',
1927
- 'example.rb'])).to eq(0)
1928
- end
1929
-
1930
- it 'runs cops for rules that link to a style guide' do
1931
- create_file('example.rb', 'fail')
1932
- create_file('.rubocop.yml', ['Metrics/LineLength:',
1933
- ' Enabled: true',
1934
- ' StyleGuide: "http://an.example/url"',
1935
- ' Max: 2'])
1936
-
1937
- expect(cli.run(['--format', 'simple', '--only-guide-cops',
1938
- 'example.rb'])).to eq(1)
1939
-
1940
- expect($stdout.string)
1941
- .to eq(['== example.rb ==',
1942
- 'C: 1: 3: Line is too long. [4/2]',
1943
- '',
1944
- '1 file inspected, 1 offense detected',
1945
- ''].join("\n"))
1946
- end
1947
-
1948
- it 'overrides configuration of AllCops/StyleGuideCopsOnly' do
1949
- create_file('example.rb', 'fail')
1950
- create_file('.rubocop.yml', ['AllCops:',
1951
- ' StyleGuideCopsOnly: false',
1952
- 'Metrics/LineLength:',
1953
- ' Enabled: true',
1954
- ' StyleGuide: ~',
1955
- ' Max: 2'])
1956
-
1957
- expect(cli.run(['--format', 'simple', '--only-guide-cops',
1958
- 'example.rb'])).to eq(0)
1959
- end
1960
- end
1961
- end
1962
-
1963
- context 'via the config' do
1964
- before do
1965
- create_file('example.rb', 'fail')
1966
- create_file('.rubocop.yml', ['AllCops:',
1967
- " StyleGuideCopsOnly: #{guide_cops_only}",
1968
- 'Metrics/LineLength:',
1969
- ' Enabled: true',
1970
- ' StyleGuide: ~',
1971
- ' Max: 2'])
1972
- end
1973
-
1974
- describe 'AllCops/StyleGuideCopsOnly' do
1975
- context 'when it is true' do
1976
- let(:guide_cops_only) { 'true' }
1977
-
1978
- it 'skips cops that have no link to a style guide' do
1979
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
1980
- end
1981
- end
1982
-
1983
- context 'when it is false' do
1984
- let(:guide_cops_only) { 'false' }
1985
-
1986
- it 'runs cops for rules regardless of any link to the style guide' do
1987
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
1988
-
1989
- expect($stdout.string)
1990
- .to eq(['== example.rb ==',
1991
- 'C: 1: 3: Line is too long. [4/2]',
1992
- '',
1993
- '1 file inspected, 1 offense detected',
1994
- ''].join("\n"))
1995
- end
1996
- end
1997
- end
1998
- end
1999
- end
2000
-
2001
- describe 'rails cops' do
2002
- describe 'enabling/disabling' do
2003
- it 'by default does not run rails cops' do
2004
- create_file('app/models/example1.rb', ['# encoding: utf-8',
2005
- 'read_attribute(:test)'])
2006
- expect(cli.run(['--format', 'simple', 'app/models/example1.rb']))
2007
- .to eq(0)
2008
- end
2009
-
2010
- it 'with -R given runs rails cops' do
2011
- create_file('app/models/example1.rb', ['# encoding: utf-8',
2012
- 'read_attribute(:test)'])
2013
- expect(cli.run(['--format', 'simple', '-R', 'app/models/example1.rb']))
2014
- .to eq(1)
2015
- expect($stdout.string).to include('Prefer self[:attr]')
2016
- end
2017
-
2018
- it 'with configuration option true in one dir runs rails cops there' do
2019
- source = ['# encoding: utf-8',
2020
- 'read_attribute(:test)']
2021
- create_file('dir1/app/models/example1.rb', source)
2022
- create_file('dir1/.rubocop.yml', ['AllCops:',
2023
- ' RunRailsCops: true',
2024
- '',
2025
- 'Rails/ReadWriteAttribute:',
2026
- ' Include:',
2027
- ' - app/models/**/*.rb'])
2028
- create_file('dir2/app/models/example2.rb', source)
2029
- create_file('dir2/.rubocop.yml', ['AllCops:',
2030
- ' RunRailsCops: false',
2031
- '',
2032
- 'Rails/ReadWriteAttribute:',
2033
- ' Include:',
2034
- ' - app/models/**/*.rb'])
2035
- expect(cli.run(%w(--format simple dir1 dir2))).to eq(1)
2036
- expect($stdout.string)
2037
- .to eq(['== dir1/app/models/example1.rb ==',
2038
- 'C: 2: 1: Prefer self[:attr] over read_attribute' \
2039
- '(:attr).',
2040
- '',
2041
- '2 files inspected, 1 offense detected',
2042
- ''].join("\n"))
2043
- end
2044
-
2045
- it 'with configuration option false but -R given runs rails cops' do
2046
- create_file('app/models/example1.rb', ['# encoding: utf-8',
2047
- 'read_attribute(:test)'])
2048
- create_file('.rubocop.yml', ['AllCops:',
2049
- ' RunRailsCops: false'])
2050
- expect(cli.run(['--format', 'simple', '-R', 'app/models/example1.rb']))
2051
- .to eq(1)
2052
- expect($stdout.string).to include('Prefer self[:attr]')
2053
- end
2054
- end
2055
-
2056
- describe 'including/excluding' do
2057
- it 'includes some directories by default' do
2058
- source = ['# encoding: utf-8',
2059
- 'read_attribute(:test)',
2060
- "default_scope order: 'position'"]
2061
- # Several rails cops include app/models by default.
2062
- create_file('dir1/app/models/example1.rb', source)
2063
- create_file('dir1/app/models/example2.rb', source)
2064
- # No rails cops include app/views by default.
2065
- create_file('dir1/app/views/example3.rb', source)
2066
- # The .rubocop.yml file inherits from default.yml where the Include
2067
- # config parameter is set for the rails cops. The paths are interpreted
2068
- # as relative to dir1 because .rubocop.yml is placed there.
2069
- create_file('dir1/.rubocop.yml', ['AllCops:',
2070
- ' RunRailsCops: true',
2071
- '',
2072
- 'Rails/ReadWriteAttribute:',
2073
- ' Exclude:',
2074
- ' - "**/example2.rb"',
2075
- '',
2076
- 'Rails/DefaultScope:',
2077
- ' Exclude:',
2078
- ' - "**/example2.rb"'])
2079
- # No .rubocop.yml file in dir2 means that the paths from default.yml
2080
- # are interpreted as relative to the current directory, so they don't
2081
- # match.
2082
- create_file('dir2/app/models/example4.rb', source)
2083
-
2084
- expect(cli.run(%w(--format simple dir1 dir2))).to eq(1)
2085
- expect($stdout.string)
2086
- .to eq(['== dir1/app/models/example1.rb ==',
2087
- 'C: 2: 1: Prefer self[:attr] over read_attribute' \
2088
- '(:attr).',
2089
- 'C: 3: 15: default_scope expects a block as its sole' \
2090
- ' argument.',
2091
- '',
2092
- '4 files inspected, 2 offenses detected',
2093
- ''].join("\n"))
2094
- end
2095
- end
2096
- end
2097
-
2098
- describe 'cops can exclude files based on config' do
2099
- it 'ignores excluded files' do
2100
- create_file('example.rb', ['# encoding: utf-8',
2101
- 'x = 0'])
2102
- create_file('regexp.rb', ['# encoding: utf-8',
2103
- 'x = 0'])
2104
- create_file('exclude_glob.rb', ['#!/usr/bin/env ruby',
2105
- '# encoding: utf-8',
2106
- 'x = 0'])
2107
- create_file('dir/thing.rb', ['# encoding: utf-8',
2108
- 'x = 0'])
2109
- create_file('.rubocop.yml', ['Lint/UselessAssignment:',
2110
- ' Exclude:',
2111
- ' - example.rb',
2112
- ' - !ruby/regexp /regexp.rb\z/',
2113
- ' - "exclude_*"',
2114
- ' - "dir/*"'])
2115
- expect(cli.run(%w(--format simple))).to eq(0)
2116
- expect($stdout.string)
2117
- .to eq(['', '4 files inspected, no offenses detected',
2118
- ''].join("\n"))
2119
- end
2120
- end
2121
-
2122
- describe 'configuration from file' do
2123
- it 'allows the default configuration file as the -c argument' do
2124
- create_file('example.rb', ['# encoding: utf-8',
2125
- 'x = 0',
2126
- 'puts x'
2127
- ])
2128
- create_file('.rubocop.yml', [])
2129
-
2130
- expect(cli.run(%w(--format simple -c .rubocop.yml))).to eq(0)
2131
- expect($stdout.string)
2132
- .to eq(['', '1 file inspected, no offenses detected',
2133
- ''].join("\n"))
2134
- end
2135
-
2136
- it 'displays cop names if DisplayCopNames is true' do
2137
- source = ['# encoding: utf-8',
2138
- 'x = 0 ',
2139
- 'puts x']
2140
- create_file('example1.rb', source)
2141
-
2142
- # DisplayCopNames: false inherited from config/default.yml
2143
- create_file('.rubocop.yml', [])
2144
-
2145
- create_file('dir/example2.rb', source)
2146
- create_file('dir/.rubocop.yml', ['AllCops:',
2147
- ' DisplayCopNames: true'])
2148
-
2149
- expect(cli.run(%w(--format simple))).to eq(1)
2150
- expect($stdout.string)
2151
- .to eq(['== example1.rb ==',
2152
- 'C: 2: 6: Trailing whitespace detected.',
2153
- '== dir/example2.rb ==',
2154
- 'C: 2: 6: Style/TrailingWhitespace: Trailing whitespace' \
2155
- ' detected.',
2156
- '',
2157
- '2 files inspected, 2 offenses detected',
2158
- ''].join("\n"))
2159
- end
2160
-
2161
- it 'finds included files' do
2162
- create_file('file.rb', 'x=0') # Included by default
2163
- create_file('example', 'x=0')
2164
- create_file('regexp', 'x=0')
2165
- create_file('.dot1/file.rb', 'x=0') # Hidden but explicitly included
2166
- create_file('.dot2/file.rb', 'x=0') # Hidden, excluded by default
2167
- create_file('.rubocop.yml', ['AllCops:',
2168
- ' Include:',
2169
- ' - example',
2170
- ' - !ruby/regexp /regexp$/',
2171
- ' - .dot1/**/*'
2172
- ])
2173
- expect(cli.run(%w(--format files))).to eq(1)
2174
- expect($stderr.string).to eq('')
2175
- expect($stdout.string.split($RS).sort).to eq([abs('.dot1/file.rb'),
2176
- abs('example'),
2177
- abs('file.rb'),
2178
- abs('regexp')])
2179
- end
2180
-
2181
- it 'ignores excluded files' do
2182
- create_file('example.rb', ['# encoding: utf-8',
2183
- 'x = 0',
2184
- 'puts x'
2185
- ])
2186
- create_file('regexp.rb', ['# encoding: utf-8',
2187
- 'x = 0',
2188
- 'puts x'
2189
- ])
2190
- create_file('exclude_glob.rb', ['#!/usr/bin/env ruby',
2191
- '# encoding: utf-8',
2192
- 'x = 0',
2193
- 'puts x'
2194
- ])
2195
- create_file('.rubocop.yml', ['AllCops:',
2196
- ' Exclude:',
2197
- ' - example.rb',
2198
- ' - !ruby/regexp /regexp.rb$/',
2199
- ' - "exclude_*"'
2200
- ])
2201
- expect(cli.run(%w(--format simple))).to eq(0)
2202
- expect($stdout.string)
2203
- .to eq(['', '0 files inspected, no offenses detected',
2204
- ''].join("\n"))
2205
- end
2206
-
2207
- it 'only reads configuration in explicitly included hidden directories' do
2208
- create_file('.hidden/example.rb', ['# encoding: utf-8',
2209
- 'x=0'])
2210
- # This file contains configuration for an unknown cop. This would cause a
2211
- # warning to be printed on stderr if the file was read. But it's in a
2212
- # hidden directory, so it's not read.
2213
- create_file('.hidden/.rubocop.yml', ['SymbolName:',
2214
- ' Enabled: false'])
2215
-
2216
- create_file('.other/example.rb', ['# encoding: utf-8',
2217
- 'x=0'])
2218
- # The .other directory is explicitly included, so the configuration file
2219
- # is read, and modifies the behavior.
2220
- create_file('.other/.rubocop.yml', ['Style/SpaceAroundOperators:',
2221
- ' Enabled: false'])
2222
- create_file('.rubocop.yml', ['AllCops:',
2223
- ' Include:',
2224
- ' - .other/**/*'])
2225
- expect(cli.run(%w(--format simple))).to eq(1)
2226
- expect($stderr.string).to eq('')
2227
- expect($stdout.string)
2228
- .to eq(['== .other/example.rb ==',
2229
- 'W: 2: 1: Useless assignment to variable - x.',
2230
- '',
2231
- '1 file inspected, 1 offense detected',
2232
- ''].join("\n"))
2233
- end
2234
-
2235
- it 'does not consider Include parameters in subdirectories' do
2236
- create_file('dir/example.ruby', ['# encoding: utf-8',
2237
- 'x=0'])
2238
- create_file('dir/.rubocop.yml', ['AllCops:',
2239
- ' Include:',
2240
- ' - "*.ruby"'])
2241
- expect(cli.run(%w(--format simple))).to eq(0)
2242
- expect($stderr.string).to eq('')
2243
- expect($stdout.string)
2244
- .to eq(['',
2245
- '0 files inspected, no offenses detected',
2246
- ''].join("\n"))
2247
- end
2248
-
2249
- it 'matches included/excluded files correctly when . argument is given' do
2250
- create_file('example.rb', 'x = 0')
2251
- create_file('special.dsl', ['# encoding: utf-8',
2252
- 'setup { "stuff" }'
2253
- ])
2254
- create_file('.rubocop.yml', ['AllCops:',
2255
- ' Include:',
2256
- ' - "*.dsl"',
2257
- ' Exclude:',
2258
- ' - example.rb'
2259
- ])
2260
- expect(cli.run(%w(--format simple .))).to eq(1)
2261
- expect($stdout.string)
2262
- .to eq(['== special.dsl ==',
2263
- "C: 2: 9: Prefer single-quoted strings when you don't " \
2264
- 'need string interpolation or special symbols.',
2265
- '',
2266
- '1 file inspected, 1 offense detected',
2267
- ''].join("\n"))
2268
- end
2269
-
2270
- # With rubinius 2.0.0.rc1 + rspec 2.13.1,
2271
- # File.stub(:open).and_call_original causes SystemStackError.
2272
- it 'does not read files in excluded list', broken: :rbx do
2273
- %w(rb.rb non-rb.ext without-ext).each do |filename|
2274
- create_file("example/ignored/#{filename}", ['# encoding: utf-8',
2275
- '#' * 90
2276
- ])
2277
- end
2278
-
2279
- create_file('example/.rubocop.yml', ['AllCops:',
2280
- ' Exclude:',
2281
- ' - ignored/**'])
2282
- expect(File).not_to receive(:open).with(%r{/ignored/})
2283
- allow(File).to receive(:open).and_call_original
2284
- expect(cli.run(%w(--format simple example))).to eq(0)
2285
- expect($stdout.string)
2286
- .to eq(['', '0 files inspected, no offenses detected',
2287
- ''].join("\n"))
2288
- end
2289
-
2290
- it 'can be configured with option to disable a certain error' do
2291
- create_file('example1.rb', 'puts 0 ')
2292
- create_file('rubocop.yml', ['Style/Encoding:',
2293
- ' Enabled: false',
2294
- '',
2295
- 'Style/CaseIndentation:',
2296
- ' Enabled: false'])
2297
- expect(cli.run(['--format', 'simple',
2298
- '-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
2299
- expect($stdout.string)
2300
- .to eq(['== example1.rb ==',
2301
- 'C: 1: 7: Trailing whitespace detected.',
2302
- '',
2303
- '1 file inspected, 1 offense detected',
2304
- ''].join("\n"))
2305
- end
2306
-
2307
- context 'without using namespace' do
2308
- it 'can be configured with option to disable a certain error' do
2309
- create_file('example1.rb', 'puts 0 ')
2310
- create_file('rubocop.yml', ['Encoding:',
2311
- ' Enabled: false',
2312
- '',
2313
- 'CaseIndentation:',
2314
- ' Enabled: false'])
2315
- expect(cli.run(['--format', 'simple',
2316
- '-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
2317
- expect($stdout.string)
2318
- .to eq(['== example1.rb ==',
2319
- 'C: 1: 7: Trailing whitespace detected.',
2320
- '',
2321
- '1 file inspected, 1 offense detected',
2322
- ''].join("\n"))
2323
- end
2324
- end
2325
-
2326
- it 'can disable parser-derived offenses with warning severity' do
2327
- # `-' interpreted as argument prefix
2328
- create_file('example.rb', 'puts -1')
2329
- create_file('.rubocop.yml', ['Style/Encoding:',
2330
- ' Enabled: false',
2331
- '',
2332
- 'Lint/AmbiguousOperator:',
2333
- ' Enabled: false'
2334
- ])
2335
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
2336
- end
2337
-
2338
- it 'cannot disable Syntax offenses with fatal/error severity' do
2339
- create_file('example.rb', 'class Test')
2340
- create_file('.rubocop.yml', ['Style/Encoding:',
2341
- ' Enabled: false',
2342
- '',
2343
- 'Syntax:',
2344
- ' Enabled: false'
2345
- ])
2346
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
2347
- expect($stdout.string).to include('unexpected token $end')
2348
- end
2349
-
2350
- it 'can be configured to merge a parameter that is a hash' do
2351
- create_file('example1.rb',
2352
- ['# encoding: utf-8',
2353
- 'puts %w(a b c)',
2354
- 'puts %q|hi|'])
2355
- # We want to change the preferred delimiters for word arrays. The other
2356
- # settings from default.yml are unchanged.
2357
- create_file('rubocop.yml',
2358
- ['Style/PercentLiteralDelimiters:',
2359
- ' PreferredDelimiters:',
2360
- " '%w': '[]'",
2361
- " '%W': '[]'"])
2362
- cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
2363
- expect($stdout.string)
2364
- .to eq(['== example1.rb ==',
2365
- 'C: 2: 6: %w-literals should be delimited by [ and ]',
2366
- 'C: 3: 6: %q-literals should be delimited by ( and )',
2367
- 'C: 3: 6: Use %q only for strings that contain both single ' \
2368
- 'quotes and double quotes.',
2369
- '',
2370
- '1 file inspected, 3 offenses detected',
2371
- ''].join("\n"))
2372
- end
2373
-
2374
- it 'can be configured to override a parameter that is a hash in a ' \
2375
- 'special case' do
2376
- create_file('example1.rb',
2377
- ['# encoding: utf-8',
2378
- 'arr.select { |e| e > 0 }.collect { |e| e * 2 }',
2379
- 'a2.find_all { |e| e > 0 }'])
2380
- # We prefer find_all over select. This setting overrides the default
2381
- # select over find_all. Other preferred methods appearing in the default
2382
- # config (e.g., map over collect) are kept.
2383
- create_file('rubocop.yml',
2384
- ['Style/CollectionMethods:',
2385
- ' PreferredMethods:',
2386
- ' select: find_all'])
2387
- cli.run(['--format',
2388
- 'simple',
2389
- '-c',
2390
- 'rubocop.yml',
2391
- '--only',
2392
- 'CollectionMethods',
2393
- 'example1.rb'])
2394
- expect($stdout.string)
2395
- .to eq(['== example1.rb ==',
2396
- 'C: 2: 5: Prefer find_all over select.',
2397
- 'C: 2: 26: Prefer map over collect.',
2398
- '',
2399
- '1 file inspected, 2 offenses detected',
2400
- ''].join("\n"))
2401
- end
2402
-
2403
- it 'works when a cop that others depend on is disabled' do
2404
- create_file('example1.rb', ['if a',
2405
- ' b',
2406
- 'end'])
2407
- create_file('rubocop.yml', ['Style/Encoding:',
2408
- ' Enabled: false',
2409
- '',
2410
- 'Metrics/LineLength:',
2411
- ' Enabled: false'
2412
- ])
2413
- result = cli.run(['--format', 'simple',
2414
- '-c', 'rubocop.yml', 'example1.rb'])
2415
- expect($stdout.string)
2416
- .to eq(['== example1.rb ==',
2417
- 'C: 1: 1: Favor modifier if usage when having ' \
2418
- 'a single-line body. Another good alternative is the ' \
2419
- 'usage of control flow &&/||.',
2420
- '',
2421
- '1 file inspected, 1 offense detected',
2422
- ''].join("\n"))
2423
- expect(result).to eq(1)
2424
- end
2425
-
2426
- it 'can be configured with project config to disable a certain error' do
2427
- create_file('example_src/example1.rb', 'puts 0 ')
2428
- create_file('example_src/.rubocop.yml', ['Style/Encoding:',
2429
- ' Enabled: false',
2430
- '',
2431
- 'Style/CaseIndentation:',
2432
- ' Enabled: false'
2433
- ])
2434
- expect(cli.run(['--format', 'simple',
2435
- 'example_src/example1.rb'])).to eq(1)
2436
- expect($stdout.string)
2437
- .to eq(['== example_src/example1.rb ==',
2438
- 'C: 1: 7: Trailing whitespace detected.',
2439
- '',
2440
- '1 file inspected, 1 offense detected',
2441
- ''].join("\n"))
2442
- end
2443
-
2444
- it 'can use an alternative max line length from a config file' do
2445
- create_file('example_src/example1.rb', ['# encoding: utf-8',
2446
- '#' * 90
2447
- ])
2448
- create_file('example_src/.rubocop.yml', ['Metrics/LineLength:',
2449
- ' Enabled: true',
2450
- ' Max: 100'
2451
- ])
2452
- expect(cli.run(['--format', 'simple',
2453
- 'example_src/example1.rb'])).to eq(0)
2454
- expect($stdout.string)
2455
- .to eq(['', '1 file inspected, no offenses detected', ''].join("\n"))
2456
- end
2457
-
2458
- it 'can have different config files in different directories' do
2459
- %w(src lib).each do |dir|
2460
- create_file("example/#{dir}/example1.rb", ['# encoding: utf-8',
2461
- '#' * 90
2462
- ])
2463
- end
2464
- create_file('example/src/.rubocop.yml', ['Metrics/LineLength:',
2465
- ' Enabled: true',
2466
- ' Max: 100'
2467
- ])
2468
- expect(cli.run(%w(--format simple example))).to eq(1)
2469
- expect($stdout.string).to eq(['== example/lib/example1.rb ==',
2470
- 'C: 2: 81: Line is too long. [90/80]',
2471
- '',
2472
- '2 files inspected, 1 offense detected',
2473
- ''].join("\n"))
2474
- end
2475
-
2476
- it 'prefers a config file in ancestor directory to another in home' do
2477
- create_file('example_src/example1.rb', ['# encoding: utf-8',
2478
- '#' * 90
2479
- ])
2480
- create_file('example_src/.rubocop.yml', ['Metrics/LineLength:',
2481
- ' Enabled: true',
2482
- ' Max: 100'
2483
- ])
2484
- create_file("#{Dir.home}/.rubocop.yml", ['Metrics/LineLength:',
2485
- ' Enabled: true',
2486
- ' Max: 80'
2487
- ])
2488
- expect(cli.run(['--format', 'simple',
2489
- 'example_src/example1.rb'])).to eq(0)
2490
- expect($stdout.string)
2491
- .to eq(['', '1 file inspected, no offenses detected', ''].join("\n"))
2492
- end
2493
-
2494
- it 'can exclude directories relative to .rubocop.yml' do
2495
- %w(src etc/test etc/spec tmp/test tmp/spec).each do |dir|
2496
- create_file("example/#{dir}/example1.rb", ['# encoding: utf-8',
2497
- '#' * 90])
2498
- end
2499
-
2500
- # Hidden subdirectories should also be excluded.
2501
- create_file('example/etc/.dot/example1.rb', ['# encoding: utf-8',
2502
- '#' * 90])
2503
-
2504
- create_file('example/.rubocop.yml', ['AllCops:',
2505
- ' Exclude:',
2506
- ' - src/**',
2507
- ' - etc/**/*',
2508
- ' - tmp/spec/**'])
2509
-
2510
- expect(cli.run(%w(--format simple example))).to eq(1)
2511
- expect($stderr.string).to eq('')
2512
- expect($stdout.string).to eq(['== example/tmp/test/example1.rb ==',
2513
- 'C: 2: 81: Line is too long. [90/80]',
2514
- '',
2515
- '1 file inspected, 1 offense detected',
2516
- ''].join("\n"))
2517
- end
2518
-
2519
- it 'can exclude a typical vendor directory' do
2520
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
2521
- ['AllCops:',
2522
- ' Exclude:',
2523
- ' - lib/parser/lexer.rb'])
2524
-
2525
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
2526
- ['# encoding: utf-8',
2527
- '#' * 90])
2528
-
2529
- create_file('.rubocop.yml',
2530
- ['AllCops:',
2531
- ' Exclude:',
2532
- ' - vendor/**/*'])
2533
-
2534
- cli.run(%w(--format simple))
2535
- expect($stdout.string)
2536
- .to eq(['', '0 files inspected, no offenses detected',
2537
- ''].join("\n"))
2538
- end
2539
-
2540
- it 'excludes the vendor directory by default' do
2541
- create_file('vendor/ex.rb',
2542
- ['# encoding: utf-8',
2543
- '#' * 90])
2544
-
2545
- cli.run(%w(--format simple))
2546
- expect($stdout.string)
2547
- .to eq(['', '0 files inspected, no offenses detected',
2548
- ''].join("\n"))
2549
- end
2550
-
2551
- # Being immune to bad configuration files in excluded directories has
2552
- # become important due to a bug in rubygems
2553
- # (https://github.com/rubygems/rubygems/issues/680) that makes
2554
- # installations of, for example, rubocop lack their .rubocop.yml in the
2555
- # root directory.
2556
- it 'can exclude a vendor directory with an erroneous config file' do
2557
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
2558
- ['inherit_from: non_existent.yml'])
2559
-
2560
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
2561
- ['# encoding: utf-8',
2562
- '#' * 90])
2563
-
2564
- create_file('.rubocop.yml',
2565
- ['AllCops:',
2566
- ' Exclude:',
2567
- ' - vendor/**/*'])
2568
-
2569
- cli.run(%w(--format simple))
2570
- expect($stderr.string).to eq('')
2571
- expect($stdout.string)
2572
- .to eq(['', '0 files inspected, no offenses detected',
2573
- ''].join("\n"))
2574
- end
2575
-
2576
- # Relative exclude paths in .rubocop.yml files are relative to that file,
2577
- # but in configuration files with other names they will be relative to
2578
- # whatever file inherits from them.
2579
- it 'can exclude a vendor directory indirectly' do
2580
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
2581
- ['AllCops:',
2582
- ' Exclude:',
2583
- ' - lib/parser/lexer.rb'])
2584
-
2585
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
2586
- ['# encoding: utf-8',
2587
- '#' * 90])
2588
-
2589
- create_file('.rubocop.yml',
2590
- ['inherit_from: config/default.yml'])
2591
-
2592
- create_file('config/default.yml',
2593
- ['AllCops:',
2594
- ' Exclude:',
2595
- ' - vendor/**/*'])
2596
-
2597
- cli.run(%w(--format simple))
2598
- expect($stdout.string)
2599
- .to eq(['', '0 files inspected, no offenses detected',
2600
- ''].join("\n"))
2601
- end
2602
-
2603
- it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
2604
- create_file('example/example1.rb', ['# encoding: utf-8',
2605
- '#' * 90])
2606
-
2607
- create_file('example/.rubocop.yml', ['Style/LyneLenth:',
2608
- ' Enabled: true',
2609
- ' Max: 100'])
2610
-
2611
- expect(cli.run(%w(--format simple example))).to eq(1)
2612
- expect($stderr.string)
2613
- .to eq(['Warning: unrecognized cop Style/LyneLenth found in ' +
2614
- abs('example/.rubocop.yml'),
2615
- ''].join("\n"))
2616
- end
2617
-
2618
- it 'prints a warning for an unrecognized configuration parameter' do
2619
- create_file('example/example1.rb', ['# encoding: utf-8',
2620
- '#' * 90])
2621
-
2622
- create_file('example/.rubocop.yml', ['Metrics/LineLength:',
2623
- ' Enabled: true',
2624
- ' Min: 10'])
2625
-
2626
- expect(cli.run(%w(--format simple example))).to eq(1)
2627
- expect($stderr.string)
2628
- .to eq(['Warning: unrecognized parameter Metrics/LineLength:Min ' \
2629
- 'found in ' + abs('example/.rubocop.yml'),
2630
- ''].join("\n"))
2631
- end
2632
-
2633
- it 'works when a configuration file passed by -c specifies Exclude ' \
2634
- 'with regexp' do
2635
- create_file('example/example1.rb', ['# encoding: utf-8',
2636
- '#' * 90])
2637
-
2638
- create_file('rubocop.yml', ['AllCops:',
2639
- ' Exclude:',
2640
- ' - !ruby/regexp /example1\.rb$/'])
2641
-
2642
- cli.run(%w(--format simple -c rubocop.yml))
2643
- expect($stdout.string)
2644
- .to eq(['', '0 files inspected, no offenses detected',
2645
- ''].join("\n"))
2646
- end
2647
-
2648
- it 'works when a configuration file passed by -c specifies Exclude ' \
2649
- 'with strings' do
2650
- create_file('example/example1.rb', ['# encoding: utf-8',
2651
- '#' * 90])
2652
-
2653
- create_file('rubocop.yml', ['AllCops:',
2654
- ' Exclude:',
2655
- ' - example/**'])
2656
-
2657
- cli.run(%w(--format simple -c rubocop.yml))
2658
- expect($stdout.string)
2659
- .to eq(['', '0 files inspected, no offenses detected',
2660
- ''].join("\n"))
2661
- end
2662
-
2663
- it 'works when a configuration file specifies a Severity' do
2664
- create_file('example/example1.rb', ['# encoding: utf-8',
2665
- '#' * 90])
2666
-
2667
- create_file('rubocop.yml', ['Metrics/LineLength:',
2668
- ' Severity: error'])
2669
-
2670
- cli.run(%w(--format simple -c rubocop.yml))
2671
- expect($stdout.string)
2672
- .to eq(['== example/example1.rb ==',
2673
- 'E: 2: 81: Line is too long. [90/80]',
2674
- '',
2675
- '1 file inspected, 1 offense detected',
2676
- ''].join("\n"))
2677
- expect($stderr.string).to eq('')
2678
- end
2679
-
2680
- it 'fails when a configuration file specifies an invalid Severity' do
2681
- create_file('example/example1.rb', ['# encoding: utf-8',
2682
- '#' * 90])
2683
-
2684
- create_file('rubocop.yml', ['Metrics/LineLength:',
2685
- ' Severity: superbad'])
2686
-
2687
- cli.run(%w(--format simple -c rubocop.yml))
2688
- expect($stderr.string)
2689
- .to eq(["Warning: Invalid severity 'superbad'. " \
2690
- 'Valid severities are refactor, convention, ' \
2691
- 'warning, error, fatal.',
2692
- ''].join("\n"))
2693
- end
2694
-
2695
- it 'fails when a configuration file has invalid YAML syntax' do
2696
- create_file('example/.rubocop.yml', ['AllCops:',
2697
- ' Exclude:',
2698
- ' - **/*_old.rb'])
2699
-
2700
- cli.run(['example'])
2701
- expect($stderr.string)
2702
- .to start_with('(<unknown>): did not find expected alphabetic or ' \
2703
- 'numeric character while scanning an alias at line 3 column 7')
2704
- end
2705
-
2706
- context 'when a file inherits from the old auto generated file' do
2707
- before do
2708
- create_file('rubocop-todo.yml', '')
2709
- create_file('.rubocop.yml', ['inherit_from: rubocop-todo.yml'])
2710
- end
2711
-
2712
- it 'prints no warning when --auto-gen-config is not set' do
2713
- expect { cli.run(%w(-c .rubocop.yml)) }.not_to exit_with_code(1)
2714
- end
2715
-
2716
- it 'prints a warning when --auto-gen-config is set' do
2717
- expect { cli.run(%w(-c .rubocop.yml --auto-gen-config)) }
2718
- .to exit_with_code(1)
2719
- expect($stderr.string)
2720
- .to eq(['Attention: rubocop-todo.yml has been renamed to ' \
2721
- '.rubocop_todo.yml',
2722
- ''].join("\n"))
2723
- end
2724
- end
2725
-
2726
- context 'when a file inherits from a higher level' do
2727
- before do
2728
- create_file('.rubocop.yml', ['Metrics/LineLength:',
2729
- ' Exclude:',
2730
- ' - dir/example.rb'])
2731
- create_file('dir/.rubocop.yml', 'inherit_from: ../.rubocop.yml')
2732
- create_file('dir/example.rb', '#' * 90)
2733
- end
2734
-
2735
- it 'inherits relative excludes correctly' do
2736
- expect(cli.run([])).to eq(0)
2737
- end
2738
- end
2739
- end
2740
- end