rubocop 0.13.1 → 0.14.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 (222) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +42 -0
  4. data/README.md +59 -54
  5. data/config/default.yml +124 -95
  6. data/config/disabled.yml +4 -0
  7. data/config/enabled.yml +286 -264
  8. data/lib/rubocop.rb +10 -3
  9. data/lib/rubocop/cli.rb +10 -171
  10. data/lib/rubocop/cop/cop.rb +14 -11
  11. data/lib/rubocop/cop/lint/debugger.rb +35 -0
  12. data/lib/rubocop/cop/lint/syntax.rb +34 -0
  13. data/lib/rubocop/cop/offence.rb +15 -23
  14. data/lib/rubocop/cop/style/align_array.rb +1 -10
  15. data/lib/rubocop/cop/style/align_hash.rb +14 -2
  16. data/lib/rubocop/cop/style/align_parameters.rb +1 -11
  17. data/lib/rubocop/cop/style/and_or.rb +13 -11
  18. data/lib/rubocop/cop/style/attr.rb +1 -1
  19. data/lib/rubocop/cop/style/autocorrect_alignment.rb +31 -6
  20. data/lib/rubocop/cop/style/blocks.rb +14 -6
  21. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  22. data/lib/rubocop/cop/style/class_length.rb +38 -0
  23. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  24. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  25. data/lib/rubocop/cop/style/configurable_naming.rb +47 -0
  26. data/lib/rubocop/cop/style/def_parentheses.rb +2 -2
  27. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  28. data/lib/rubocop/cop/style/even_odd.rb +7 -3
  29. data/lib/rubocop/cop/style/hash_methods.rb +1 -1
  30. data/lib/rubocop/cop/style/hash_syntax.rb +36 -7
  31. data/lib/rubocop/cop/style/lambda_call.rb +36 -11
  32. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  33. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +41 -0
  34. data/lib/rubocop/cop/style/method_length.rb +2 -13
  35. data/lib/rubocop/cop/style/method_name.rb +42 -0
  36. data/lib/rubocop/cop/style/numeric_literals.rb +4 -0
  37. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  38. data/lib/rubocop/cop/style/raise_args.rb +60 -0
  39. data/lib/rubocop/cop/style/redundant_exception.rb +32 -0
  40. data/lib/rubocop/cop/style/redundant_return.rb +14 -5
  41. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  42. data/lib/rubocop/cop/style/semicolon.rb +13 -4
  43. data/lib/rubocop/cop/style/signal_exception.rb +63 -14
  44. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  45. data/lib/rubocop/cop/style/string_help.rb +4 -1
  46. data/lib/rubocop/cop/style/string_literals.rb +34 -11
  47. data/lib/rubocop/cop/style/surrounding_space.rb +64 -17
  48. data/lib/rubocop/cop/style/tab.rb +1 -1
  49. data/lib/rubocop/cop/style/variable_name.rb +44 -0
  50. data/lib/rubocop/cop/style/when_then.rb +1 -1
  51. data/lib/rubocop/cop/style/while_until_do.rb +1 -1
  52. data/lib/rubocop/cop/team.rb +4 -7
  53. data/lib/rubocop/cop/util.rb +12 -0
  54. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  55. data/lib/rubocop/formatter/emacs_style_formatter.rb +5 -2
  56. data/lib/rubocop/formatter/json_formatter.rb +1 -0
  57. data/lib/rubocop/formatter/progress_formatter.rb +9 -13
  58. data/lib/rubocop/formatter/simple_text_formatter.rb +63 -15
  59. data/lib/rubocop/options.rb +184 -0
  60. data/lib/rubocop/processed_source.rb +4 -0
  61. data/lib/rubocop/version.rb +1 -1
  62. data/rubocop.gemspec +1 -1
  63. data/spec/rubocop/cli_spec.rb +870 -1001
  64. data/spec/rubocop/config_spec.rb +13 -13
  65. data/spec/rubocop/config_store_spec.rb +38 -37
  66. data/spec/rubocop/cop/commissioner_spec.rb +42 -46
  67. data/spec/rubocop/cop/cop_spec.rb +72 -77
  68. data/spec/rubocop/cop/corrector_spec.rb +51 -55
  69. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +100 -106
  70. data/spec/rubocop/cop/lint/block_alignment_spec.rb +403 -409
  71. data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
  72. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +19 -25
  73. data/spec/rubocop/cop/lint/end_alignment_spec.rb +50 -56
  74. data/spec/rubocop/cop/lint/end_in_method_spec.rb +20 -26
  75. data/spec/rubocop/cop/lint/ensure_return_spec.rb +30 -36
  76. data/spec/rubocop/cop/lint/eval_spec.rb +25 -31
  77. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +22 -28
  78. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +51 -57
  79. data/spec/rubocop/cop/lint/loop_spec.rb +17 -23
  80. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +51 -57
  81. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +105 -111
  82. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +230 -236
  83. data/spec/rubocop/cop/lint/syntax_spec.rb +33 -0
  84. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +51 -57
  85. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1544 -1550
  86. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +21 -27
  87. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +119 -125
  88. data/spec/rubocop/cop/lint/void_spec.rb +50 -56
  89. data/spec/rubocop/cop/offence_spec.rb +101 -131
  90. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +6 -12
  91. data/spec/rubocop/cop/rails/read_attribute_spec.rb +6 -12
  92. data/spec/rubocop/cop/rails/validation_spec.rb +13 -19
  93. data/spec/rubocop/cop/style/access_control_spec.rb +146 -152
  94. data/spec/rubocop/cop/style/alias_spec.rb +30 -36
  95. data/spec/rubocop/cop/style/align_array_spec.rb +57 -56
  96. data/spec/rubocop/cop/style/align_hash_spec.rb +256 -237
  97. data/spec/rubocop/cop/style/align_parameters_spec.rb +216 -202
  98. data/spec/rubocop/cop/style/and_or_spec.rb +51 -55
  99. data/spec/rubocop/cop/style/ascii_comments_spec.rb +14 -20
  100. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +26 -32
  101. data/spec/rubocop/cop/style/attr_spec.rb +11 -17
  102. data/spec/rubocop/cop/style/begin_block_spec.rb +6 -12
  103. data/spec/rubocop/cop/style/block_comments_spec.rb +13 -19
  104. data/spec/rubocop/cop/style/block_nesting_spec.rb +134 -140
  105. data/spec/rubocop/cop/style/blocks_spec.rb +81 -84
  106. data/spec/rubocop/cop/style/case_equality_spec.rb +5 -11
  107. data/spec/rubocop/cop/style/case_indentation_spec.rb +71 -77
  108. data/spec/rubocop/cop/style/character_literal_spec.rb +31 -37
  109. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +31 -37
  110. data/spec/rubocop/cop/style/class_length_spec.rb +65 -0
  111. data/spec/rubocop/cop/style/class_methods_spec.rb +35 -41
  112. data/spec/rubocop/cop/style/class_vars_spec.rb +11 -17
  113. data/spec/rubocop/cop/style/collection_methods_spec.rb +41 -47
  114. data/spec/rubocop/cop/style/colon_method_call_spec.rb +45 -51
  115. data/spec/rubocop/cop/style/comment_annotation_spec.rb +79 -85
  116. data/spec/rubocop/cop/style/constant_name_spec.rb +45 -51
  117. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +28 -34
  118. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +23 -29
  119. data/spec/rubocop/cop/style/documentation_spec.rb +60 -66
  120. data/spec/rubocop/cop/style/dot_position_spec.rb +50 -56
  121. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +109 -110
  122. data/spec/rubocop/cop/style/empty_lines_spec.rb +28 -34
  123. data/spec/rubocop/cop/style/empty_literal_spec.rb +80 -86
  124. data/spec/rubocop/cop/style/encoding_spec.rb +50 -56
  125. data/spec/rubocop/cop/style/end_block_spec.rb +6 -12
  126. data/spec/rubocop/cop/style/end_of_line_spec.rb +13 -19
  127. data/spec/rubocop/cop/style/even_odd_spec.rb +69 -41
  128. data/spec/rubocop/cop/style/favor_join_spec.rb +21 -27
  129. data/spec/rubocop/cop/style/favor_modifier_spec.rb +180 -176
  130. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +41 -47
  131. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +63 -69
  132. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +32 -38
  133. data/spec/rubocop/cop/style/final_newline_spec.rb +16 -22
  134. data/spec/rubocop/cop/style/for_spec.rb +18 -24
  135. data/spec/rubocop/cop/style/global_vars_spec.rb +23 -29
  136. data/spec/rubocop/cop/style/hash_methods_spec.rb +39 -45
  137. data/spec/rubocop/cop/style/hash_syntax_spec.rb +93 -65
  138. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +11 -17
  139. data/spec/rubocop/cop/style/indentation_width_spec.rb +377 -383
  140. data/spec/rubocop/cop/style/lambda_call_spec.rb +40 -22
  141. data/spec/rubocop/cop/style/lambda_spec.rb +30 -34
  142. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +44 -50
  143. data/spec/rubocop/cop/style/line_length_spec.rb +11 -17
  144. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +14 -20
  145. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
  146. data/spec/rubocop/cop/style/method_length_spec.rb +127 -133
  147. data/spec/rubocop/cop/style/method_name_spec.rb +103 -0
  148. data/spec/rubocop/cop/style/module_function_spec.rb +16 -22
  149. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +64 -70
  150. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +74 -80
  151. data/spec/rubocop/cop/style/nil_comparison_spec.rb +20 -26
  152. data/spec/rubocop/cop/style/not_spec.rb +13 -19
  153. data/spec/rubocop/cop/style/numeric_literals_spec.rb +36 -35
  154. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +5 -11
  155. data/spec/rubocop/cop/style/op_method_spec.rb +61 -67
  156. data/spec/rubocop/cop/style/parameter_lists_spec.rb +31 -37
  157. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +62 -68
  158. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +9 -15
  159. data/spec/rubocop/cop/style/proc_spec.rb +13 -19
  160. data/spec/rubocop/cop/style/raise_args_spec.rb +55 -0
  161. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +49 -55
  162. data/spec/rubocop/cop/style/redundant_begin_spec.rb +47 -53
  163. data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
  164. data/spec/rubocop/cop/style/redundant_return_spec.rb +164 -72
  165. data/spec/rubocop/cop/style/redundant_self_spec.rb +109 -115
  166. data/spec/rubocop/cop/style/regexp_literal_spec.rb +57 -63
  167. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +99 -105
  168. data/spec/rubocop/cop/style/semicolon_spec.rb +95 -90
  169. data/spec/rubocop/cop/style/signal_exception_spec.rb +248 -95
  170. data/spec/rubocop/cop/style/single_line_methods_spec.rb +45 -51
  171. data/spec/rubocop/cop/style/space_after_colon_spec.rb +14 -20
  172. data/spec/rubocop/cop/style/space_after_comma_spec.rb +16 -22
  173. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +48 -54
  174. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +49 -55
  175. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +10 -16
  176. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +118 -50
  177. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +18 -24
  178. data/spec/rubocop/cop/style/space_around_operators_spec.rb +204 -210
  179. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +36 -42
  180. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +39 -45
  181. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +62 -68
  182. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +18 -24
  183. data/spec/rubocop/cop/style/special_global_vars_spec.rb +40 -46
  184. data/spec/rubocop/cop/style/string_literals_spec.rb +148 -70
  185. data/spec/rubocop/cop/style/symbol_array_spec.rb +28 -34
  186. data/spec/rubocop/cop/style/symbol_name_spec.rb +132 -138
  187. data/spec/rubocop/cop/style/tab_spec.rb +9 -15
  188. data/spec/rubocop/cop/style/ternary_operator_spec.rb +25 -31
  189. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +10 -16
  190. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +14 -20
  191. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +374 -380
  192. data/spec/rubocop/cop/style/unless_else_spec.rb +17 -23
  193. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +36 -42
  194. data/spec/rubocop/cop/style/variable_name_spec.rb +83 -0
  195. data/spec/rubocop/cop/style/when_then_spec.rb +31 -37
  196. data/spec/rubocop/cop/style/while_until_do_spec.rb +47 -53
  197. data/spec/rubocop/cop/style/word_array_spec.rb +49 -55
  198. data/spec/rubocop/cop/team_spec.rb +125 -127
  199. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +176 -180
  200. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +580 -586
  201. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +140 -146
  202. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +52 -58
  203. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +217 -223
  204. data/spec/rubocop/cop/variable_inspector_spec.rb +15 -19
  205. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +35 -36
  206. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -2
  207. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +24 -1
  208. data/spec/rubocop/formatter/file_list_formatter_spec.rb +1 -1
  209. data/spec/rubocop/formatter/formatter_set_spec.rb +3 -3
  210. data/spec/rubocop/formatter/json_formatter_spec.rb +7 -2
  211. data/spec/rubocop/formatter/offence_count_formatter_spec.rb +1 -1
  212. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  213. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +55 -6
  214. data/spec/rubocop/options_spec.rb +233 -0
  215. data/spec/rubocop/processed_source_spec.rb +85 -42
  216. data/spec/rubocop/source_parser_spec.rb +108 -110
  217. data/spec/rubocop/target_finder_spec.rb +125 -127
  218. data/spec/rubocop/token_spec.rb +15 -17
  219. metadata +32 -8
  220. data/lib/rubocop/backports/bsearch.rb +0 -39
  221. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +0 -77
  222. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +0 -102
@@ -0,0 +1,184 @@
1
+ # encoding: utf-8
2
+
3
+ require 'optparse'
4
+
5
+ module Rubocop
6
+ # This class handles command line options.
7
+ class Options
8
+ DEFAULT_FORMATTER = 'progress'
9
+
10
+ def initialize(config_store)
11
+ @config_store = config_store
12
+ @options = {}
13
+ end
14
+
15
+ # rubocop:disable MethodLength
16
+ def parse(args)
17
+ ignore_dropped_options(args)
18
+ convert_deprecated_options(args)
19
+
20
+ OptionParser.new do |opts|
21
+ opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
22
+
23
+ opts.on('-d', '--debug', 'Display debug info.') do |d|
24
+ @options[:debug] = d
25
+ end
26
+ opts.on('-c', '--config FILE', 'Specify configuration file.') do |f|
27
+ @options[:config] = f
28
+ @config_store.set_options_config(@options[:config])
29
+ end
30
+ opts.on('--only COP', 'Run just one cop.') do |s|
31
+ @options[:only] = s
32
+ validate_only_option
33
+ end
34
+ opts.on('--auto-gen-config',
35
+ 'Generate a configuration file acting as a',
36
+ 'TODO list.') do
37
+ @options[:auto_gen_config] = true
38
+ @options[:formatters] = [
39
+ [DEFAULT_FORMATTER],
40
+ [Formatter::DisabledConfigFormatter,
41
+ Config::AUTO_GENERATED_FILE]
42
+ ]
43
+ validate_auto_gen_config_option(args)
44
+ end
45
+ opts.on('--show-cops',
46
+ 'Shows cops and their config for the',
47
+ 'current directory.') do
48
+ print_available_cops
49
+ exit(0)
50
+ end
51
+ opts.on('-f', '--format FORMATTER',
52
+ 'Choose an output formatter. This option',
53
+ 'can be specified multiple times to enable',
54
+ 'multiple formatters at the same time.',
55
+ ' [p]rogress (default)',
56
+ ' [s]imple',
57
+ ' [c]lang',
58
+ ' [e]macs',
59
+ ' [j]son',
60
+ ' [f]iles',
61
+ ' [o]ffences',
62
+ ' custom formatter class name') do |key|
63
+ @options[:formatters] ||= []
64
+ @options[:formatters] << [key]
65
+ end
66
+ opts.on('-o', '--out FILE',
67
+ 'Write output to a file instead of STDOUT.',
68
+ 'This option applies to the previously',
69
+ 'specified --format, or the default format',
70
+ 'if no format is specified.') do |path|
71
+ @options[:formatters] ||= [[DEFAULT_FORMATTER]]
72
+ @options[:formatters].last << path
73
+ end
74
+ opts.on('-r', '--require FILE', 'Require Ruby file.') do |f|
75
+ require f
76
+ end
77
+ opts.on('-R', '--rails', 'Run extra Rails cops.') do |r|
78
+ @options[:rails] = r
79
+ end
80
+ opts.on('-l', '--lint', 'Run only lint cops.') do |l|
81
+ @options[:lint] = l
82
+ end
83
+ opts.on('-a', '--auto-correct', 'Auto-correct offences.') do |a|
84
+ @options[:autocorrect] = a
85
+ end
86
+ opts.on('-n', '--no-color', 'Disable color output.') do |s|
87
+ Sickill::Rainbow.enabled = false
88
+ end
89
+ opts.on('-v', '--version', 'Display version.') do
90
+ puts Rubocop::Version.version(false)
91
+ exit(0)
92
+ end
93
+ opts.on('-V', '--verbose-version', 'Display verbose version.') do
94
+ puts Rubocop::Version.version(true)
95
+ exit(0)
96
+ end
97
+ end.parse!(args)
98
+
99
+ target_files = target_finder.find(args)
100
+ [@options, target_files]
101
+ end
102
+ # rubocop:enable MethodLength
103
+
104
+ private
105
+
106
+ def ignore_dropped_options(args)
107
+ # Currently we don't make -s/--silent option raise error
108
+ # since those are mostly used by external tools.
109
+ rejected = args.reject! { |a| %w(-s --silent).include?(a) }
110
+ if rejected
111
+ warn '-s/--silent options is dropped. ' +
112
+ '`emacs` and `files` formatters no longer display summary.'
113
+ end
114
+ end
115
+
116
+ def convert_deprecated_options(args)
117
+ args.map! do |arg|
118
+ case arg
119
+ when '-e', '--emacs'
120
+ deprecate("#{arg} option", '--format emacs', '1.0.0')
121
+ %w(--format emacs)
122
+ else
123
+ arg
124
+ end
125
+ end.flatten!
126
+ end
127
+
128
+ def deprecate(subject, alternative = nil, version = nil)
129
+ message = "#{subject} is deprecated"
130
+ message << " and will be removed in RuboCop #{version}" if version
131
+ message << '.'
132
+ message << " Please use #{alternative} instead." if alternative
133
+ warn message
134
+ end
135
+
136
+ def validate_only_option
137
+ if Cop::Cop.all.none? { |c| c.cop_name == @options[:only] }
138
+ fail ArgumentError, "Unrecognized cop name: #{@options[:only]}."
139
+ end
140
+ end
141
+
142
+ def validate_auto_gen_config_option(args)
143
+ if args.any?
144
+ fail ArgumentError,
145
+ '--auto-gen-config can not be combined with any other arguments.'
146
+ end
147
+
148
+ target_finder.find(args).each do |file|
149
+ config = @config_store.for(file)
150
+ if @options[:auto_gen_config] && config.contains_auto_generated_config
151
+ fail "Remove #{Config::AUTO_GENERATED_FILE} from the current " +
152
+ 'configuration before generating it again.'
153
+ end
154
+ end
155
+ end
156
+
157
+ def target_finder
158
+ @target_finder ||= TargetFinder.new(@config_store, @options[:debug])
159
+ end
160
+
161
+ def print_available_cops
162
+ cops = Cop::Cop.all
163
+ puts "Available cops (#{cops.length}) + config for #{Dir.pwd.to_s}: "
164
+ dirconf = @config_store.for(Dir.pwd.to_s)
165
+ cops.types.sort!.each do |type|
166
+ coptypes = cops.with_type(type).sort_by!(&:cop_name)
167
+ puts "Type '#{type.to_s.capitalize}' (#{coptypes.size}):"
168
+ coptypes.each do |cop|
169
+ puts " - #{cop.cop_name}"
170
+ cnf = dirconf.for_cop(cop).dup
171
+ print_conf_option('Description',
172
+ cnf.delete('Description') { 'None' })
173
+ cnf.each { |k, v| print_conf_option(k, v) }
174
+ print_conf_option('SupportsAutoCorrection',
175
+ cop.new.support_autocorrect?.to_s)
176
+ end
177
+ end
178
+ end
179
+
180
+ def print_conf_option(option, value)
181
+ puts " - #{option}: #{value}"
182
+ end
183
+ end
184
+ end
@@ -38,6 +38,10 @@ module Rubocop
38
38
  lines[*args]
39
39
  end
40
40
 
41
+ def valid_syntax?
42
+ @diagnostics.none? { |d| [:error, :fatal].include?(d.level) }
43
+ end
44
+
41
45
  private
42
46
 
43
47
  def init_lines
@@ -3,7 +3,7 @@
3
3
  module Rubocop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.13.1'
6
+ STRING = '0.14.0'
7
7
 
8
8
  MSG = '%s (using Parser %s, running on %s %s %s)'
9
9
 
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.summary = 'Automatic Ruby code style checking tool.'
28
28
 
29
29
  s.add_runtime_dependency('rainbow', '>= 1.1.4')
30
- s.add_runtime_dependency('parser', '~> 2.0.0.pre6')
30
+ s.add_runtime_dependency('parser', '~> 2.0')
31
31
  s.add_runtime_dependency('powerpack', '~> 0.0.6')
32
32
  s.add_development_dependency('rake', '~> 10.1')
33
33
  s.add_development_dependency('rspec', '~> 2.14')
@@ -4,1142 +4,1011 @@ require 'fileutils'
4
4
  require 'tmpdir'
5
5
  require 'spec_helper'
6
6
 
7
- module Rubocop
8
- describe CLI, :isolated_environment do
9
- include FileHelper
7
+ describe Rubocop::CLI, :isolated_environment do
8
+ include FileHelper
10
9
 
11
- subject(:cli) { CLI.new }
10
+ subject(:cli) { described_class.new }
12
11
 
13
- before(:each) do
14
- $stdout = StringIO.new
15
- $stderr = StringIO.new
16
- Config.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 '-h/--help' do
30
- it 'exits cleanly' do
31
- expect { cli.run ['-h'] }.to exit_with_code(0)
32
- expect { cli.run ['--help'] }.to exit_with_code(0)
33
- end
34
-
35
- it 'shows help text' do
36
- begin
37
- cli.run(['--help'])
38
- rescue SystemExit # rubocop:disable HandleExceptions
39
- end
40
-
41
- expected_help = <<-END
42
- Usage: rubocop [options] [file1, file2, ...]
43
- -d, --debug Display debug info.
44
- -c, --config FILE Specify configuration file.
45
- --only COP Run just one cop.
46
- --auto-gen-config Generate a configuration file acting as a
47
- TODO list.
48
- --show-cops Shows cops and their config for the
49
- current directory.
50
- -f, --format FORMATTER Choose an output formatter. This option
51
- can be specified multiple times to enable
52
- multiple formatters at the same time.
53
- [p]rogress (default)
54
- [s]imple
55
- [c]lang
56
- [e]macs
57
- [j]son
58
- [f]iles
59
- [o]ffences
60
- custom formatter class name
61
- -o, --out FILE Write output to a file instead of STDOUT.
62
- This option applies to the previously
63
- specified --format, or the default format
64
- if no format is specified.
65
- -r, --require FILE Require Ruby file.
66
- -R, --rails Run extra Rails cops.
67
- -l, --lint Run only lint cops.
68
- -a, --auto-correct Auto-correct offences.
69
- -n, --no-color Disable color output.
70
- -v, --version Display version.
71
- -V, --verbose-version Display verbose version.
72
- END
73
-
74
- expect($stdout.string).to eq(expected_help)
75
- end
76
-
77
- it 'lists all builtin formatters' do
78
- begin
79
- cli.run(['--help'])
80
- rescue SystemExit # rubocop:disable HandleExceptions
81
- end
82
-
83
- option_sections = $stdout.string.lines.slice_before(/^\s*-/)
84
-
85
- format_section = option_sections.find do |lines|
86
- lines.first =~ /^\s*-f/
87
- end
88
-
89
- formatter_keys = format_section.reduce([]) do |keys, line|
90
- match = line.match(/^[ ]{39}(\[[a-z\]]+)/)
91
- next keys unless match
92
- keys << match.captures.first.gsub(/\[|\]/, '')
93
- end.sort
12
+ before(:each) do
13
+ $stdout = StringIO.new
14
+ $stderr = StringIO.new
15
+ Rubocop::Config.debug = false
16
+ end
94
17
 
95
- expected_formatter_keys =
96
- Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS.keys.sort
18
+ after(:each) do
19
+ $stdout = STDOUT
20
+ $stderr = STDERR
21
+ end
97
22
 
98
- expect(formatter_keys).to eq(expected_formatter_keys)
99
- end
100
- end
23
+ def abs(path)
24
+ File.expand_path(path)
25
+ end
101
26
 
102
- describe '--version' do
103
- it 'exits cleanly' do
104
- expect { cli.run ['-v'] }.to exit_with_code(0)
105
- expect { cli.run ['--version'] }.to exit_with_code(0)
106
- expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
107
- end
27
+ describe 'option' do
28
+ describe '--auto-gen-config' do
29
+ it 'exits with error if asked to re-generate a todo list that is in ' +
30
+ 'use' do
31
+ create_file('example1.rb', ['# encoding: utf-8',
32
+ 'x= 0 ',
33
+ '#' * 85,
34
+ 'y ',
35
+ 'puts x'])
36
+ todo_contents = ['# This configuration was generated with `rubocop' +
37
+ ' --auto-gen-config`',
38
+ '',
39
+ 'LineLength:',
40
+ ' Enabled: false']
41
+ create_file('rubocop-todo.yml', todo_contents)
42
+ expect(IO.read('rubocop-todo.yml'))
43
+ .to eq(todo_contents.join("\n") + "\n")
44
+ create_file('.rubocop.yml', ['inherit_from: rubocop-todo.yml'])
45
+ expect(cli.run(['--auto-gen-config'])).to eq(1)
46
+ expect($stderr.string).to eq('Remove rubocop-todo.yml from the ' +
47
+ 'current configuration before ' +
48
+ "generating it again.\n")
49
+ end
50
+
51
+ it 'exits with error if file arguments are given' do
52
+ create_file('example1.rb', ['# encoding: utf-8',
53
+ 'x= 0 ',
54
+ '#' * 85,
55
+ 'y ',
56
+ 'puts x'])
57
+ expect(cli.run(['--auto-gen-config', 'example1.rb'])).to eq(1)
58
+ expect($stderr.string)
59
+ .to eq('--auto-gen-config can not be combined with any other ' +
60
+ "arguments.\n")
61
+ expect($stdout.string).to eq('')
62
+ end
63
+
64
+ it 'can generate a todo list' do
65
+ create_file('example1.rb', ['# encoding: utf-8',
66
+ 'x= 0 ',
67
+ '#' * 85,
68
+ 'y ',
69
+ 'puts x'])
70
+ create_file('example2.rb', ['# encoding: utf-8',
71
+ "\tx = 0",
72
+ 'puts x'])
73
+ expect(cli.run(['--auto-gen-config'])).to eq(1)
74
+ expect($stderr.string).to eq('')
75
+ expect($stdout.string)
76
+ .to include([
77
+ 'Created rubocop-todo.yml.',
78
+ 'Run rubocop with --config rubocop-todo.yml, or',
79
+ 'add inherit_from: rubocop-todo.yml in a ' +
80
+ '.rubocop.yml file.',
81
+ ''].join("\n"))
82
+ expect(IO.read('rubocop-todo.yml'))
83
+ .to eq(['# This configuration was generated by `rubocop' +
84
+ ' --auto-gen-config`.',
85
+ '# The point is for the user to remove these' +
86
+ ' configuration records',
87
+ '# one by one as the offences are removed from the code ' +
88
+ 'base.',
89
+ '',
90
+ 'LineLength:',
91
+ ' Enabled: false',
92
+ '',
93
+ 'SpaceAroundOperators:',
94
+ ' Enabled: false',
95
+ '',
96
+ 'Tab:',
97
+ ' Enabled: false',
98
+ '',
99
+ 'TrailingWhitespace:',
100
+ ' Enabled: false',
101
+ ''].join("\n"))
108
102
  end
103
+ end
109
104
 
110
- describe '--auto-gen-config' do
111
- it 'exits with error if asked to re-generate a todo list that is in ' +
112
- 'use' do
113
- create_file('example1.rb', ['# encoding: utf-8',
114
- 'x= 0 ',
115
- '#' * 85,
116
- 'y ',
117
- 'puts x'])
118
- todo_contents = ['# This configuration was generated with `rubocop' +
119
- ' --auto-gen-config`',
120
- '',
121
- 'LineLength:',
122
- ' Enabled: false']
123
- create_file('rubocop-todo.yml', todo_contents)
124
- expect(IO.read('rubocop-todo.yml'))
125
- .to eq(todo_contents.join("\n") + "\n")
126
- create_file('.rubocop.yml', ['inherit_from: rubocop-todo.yml'])
127
- expect(cli.run(['--auto-gen-config'])).to eq(1)
128
- expect($stderr.string).to eq('Remove rubocop-todo.yml from the ' +
129
- 'current configuration before ' +
130
- "generating it again.\n")
131
- end
132
-
133
- it 'exits with error if file arguments are given' do
134
- create_file('example1.rb', ['# encoding: utf-8',
135
- 'x= 0 ',
136
- '#' * 85,
137
- 'y ',
138
- 'puts x'])
139
- expect(cli.run(['--auto-gen-config', 'example1.rb'])).to eq(1)
140
- expect($stderr.string)
141
- .to eq('--auto-gen-config can not be combined with any other ' +
142
- "arguments.\n")
143
- expect($stdout.string).to eq('')
144
- end
105
+ describe '--only' do
106
+ it 'runs just one cop' do
107
+ create_file('example.rb', ['if x== 0 ',
108
+ "\ty",
109
+ 'end'])
110
+ # IfUnlessModifier depends on the configuration of LineLength.
145
111
 
146
- it 'can generate a todo list' do
147
- create_file('example1.rb', ['# encoding: utf-8',
148
- 'x= 0 ',
149
- '#' * 85,
150
- 'y ',
151
- 'puts x'])
152
- create_file('example2.rb', ['# encoding: utf-8',
153
- "\tx = 0",
154
- 'puts x'])
155
- expect(cli.run(['--auto-gen-config'])).to eq(1)
156
- expect($stderr.string).to eq('')
157
- expect($stdout.string)
158
- .to include([
159
- 'Created rubocop-todo.yml.',
160
- 'Run rubocop with --config rubocop-todo.yml, or',
161
- 'add inherit_from: rubocop-todo.yml in a ' +
162
- '.rubocop.yml file.',
163
- ''].join("\n"))
164
- expect(IO.read('rubocop-todo.yml'))
165
- .to eq(['# This configuration was generated by `rubocop' +
166
- ' --auto-gen-config`.',
167
- '# The point is for the user to remove these' +
168
- ' configuration records',
169
- '# one by one as the offences are removed from the code ' +
170
- 'base.',
171
- '',
172
- 'LineLength:',
173
- ' Enabled: false',
174
- '',
175
- 'SpaceAroundOperators:',
176
- ' Enabled: false',
177
- '',
178
- 'Tab:',
179
- ' Enabled: false',
180
- '',
181
- 'TrailingWhitespace:',
182
- ' Enabled: false',
183
- ''].join("\n"))
184
- end
112
+ expect(cli.run(['--format', 'simple',
113
+ '--only', 'IfUnlessModifier',
114
+ 'example.rb'])).to eq(1)
115
+ expect($stdout.string)
116
+ .to eq(['== example.rb ==',
117
+ 'C: 1: 1: Favor modifier if/unless usage when you ' +
118
+ 'have a single-line body. Another good alternative is ' +
119
+ 'the usage of control flow &&/||.',
120
+ '',
121
+ '1 file inspected, 1 offence detected',
122
+ ''].join("\n"))
185
123
  end
124
+ end
186
125
 
187
- describe '--only' do
188
- it 'runs just one cop' do
189
- create_file('example.rb', ['if x== 0 ',
190
- "\ty",
191
- 'end'])
192
- # IfUnlessModifier depends on the configuration of LineLength.
193
-
194
- expect(cli.run(['--format', 'simple',
195
- '--only', 'IfUnlessModifier',
196
- 'example.rb'])).to eq(1)
197
- expect($stdout.string)
198
- .to eq(['== example.rb ==',
199
- 'C: 1: 1: Favor modifier if/unless usage when you ' +
200
- 'have a single-line body. Another good alternative is ' +
201
- 'the usage of control flow &&/||.',
202
- '',
203
- '1 file inspected, 1 offence detected',
204
- ''].join("\n"))
205
- end
126
+ describe '--lint' do
127
+ it 'runs only lint cops' do
128
+ create_file('example.rb', ['if 0 ',
129
+ "\ty",
130
+ 'end'])
131
+ # IfUnlessModifier depends on the configuration of LineLength.
206
132
 
207
- it 'exits with error if an incorrect cop name is passed' do
208
- expect(cli.run(%w(--only 123))).to eq(1)
209
- expect($stderr.string).to eq("Unrecognized cop name: 123.\n")
210
- end
133
+ expect(cli.run(['--format', 'simple', '--lint',
134
+ 'example.rb'])).to eq(1)
135
+ expect($stdout.string)
136
+ .to eq(['== example.rb ==',
137
+ 'W: 1: 4: Literal 0 appeared in a condition.',
138
+ '',
139
+ '1 file inspected, 1 offence detected',
140
+ ''].join("\n"))
211
141
  end
142
+ end
212
143
 
213
- describe '--lint' do
214
- it 'runs only lint cops' do
215
- create_file('example.rb', ['if 0 ',
216
- "\ty",
217
- 'end'])
218
- # IfUnlessModifier depends on the configuration of LineLength.
219
-
220
- expect(cli.run(['--format', 'simple', '--lint',
221
- 'example.rb'])).to eq(1)
222
- expect($stdout.string)
223
- .to eq(['== example.rb ==',
224
- 'W: 1: 4: Literal 0 appeared in a condition.',
225
- '',
226
- '1 file inspected, 1 offence detected',
227
- ''].join("\n"))
228
- end
144
+ describe '-d/--debug' do
145
+ it 'shows config files', ruby: 2.0 do
146
+ create_file('example1.rb', "\tputs 0")
147
+ expect(cli.run(['--debug', 'example1.rb'])).to eq(1)
148
+ home = File.dirname(File.dirname(File.dirname(__FILE__)))
149
+ expect($stdout.string.lines[2, 7].map(&:chomp).join("\n"))
150
+ .to eq(["For #{abs('')}:" +
151
+ " configuration from #{home}/config/default.yml",
152
+ "Inheriting configuration from #{home}/config/enabled.yml",
153
+ "Inheriting configuration from #{home}/config/" +
154
+ 'disabled.yml',
155
+ "AllCops/Excludes configuration from #{home}/.rubocop.yml",
156
+ "Inheriting configuration from #{home}/config/default.yml",
157
+ "Inheriting configuration from #{home}/config/enabled.yml",
158
+ "Inheriting configuration from #{home}/config/disabled.yml"
159
+ ].join("\n"))
160
+ end
161
+
162
+ it 'shows cop names', ruby: 2.0 do
163
+ create_file('example1.rb', "\tputs 0")
164
+ expect(cli.run(['--format',
165
+ 'emacs',
166
+ '--debug',
167
+ 'example1.rb'])).to eq(1)
168
+ expect($stdout.string.lines[-1])
169
+ .to eq(["#{abs('example1.rb')}:1:1: C: Tab: Tab detected.",
170
+ ''].join("\n"))
229
171
  end
172
+ end
230
173
 
231
- describe '--show-cops' do
232
- let(:cops) { Cop::Cop.all }
233
-
234
- let(:global_conf) do
235
- config_path = Rubocop::Config.configuration_file_for(Dir.pwd.to_s)
236
- Rubocop::Config.configuration_from_file(config_path)
237
- end
238
-
239
- let(:stdout) { $stdout.string }
240
-
241
- before do
242
- expect { cli.run ['--show-cops'] }.to exit_with_code(0)
243
- end
244
-
245
- # Extracts the first line out of the description
246
- def short_description_of_cop(cop)
247
- desc = full_description_of_cop(cop)
248
- desc ? desc.lines.first.strip : ''
249
- end
174
+ describe '-f/--format' do
175
+ let(:target_file) { 'example.rb' }
250
176
 
251
- # Gets the full description of the cop or nil if no description is set.
252
- def full_description_of_cop(cop)
253
- cop_config = global_conf.for_cop(cop)
254
- cop_config['Description']
255
- end
177
+ before do
178
+ create_file(target_file, [
179
+ '# encoding: utf-8',
180
+ '#' * 90
181
+ ])
182
+ end
256
183
 
257
- it 'prints all available cops and their description' do
258
- cops.each do |cop|
259
- expect(stdout).to include cop.cop_name
260
- expect(stdout).to include short_description_of_cop(cop)
184
+ describe 'builtin formatters' do
185
+ context 'when simple format is specified' do
186
+ it 'outputs with simple format' do
187
+ cli.run(['--format', 'simple', 'example.rb'])
188
+ expect($stdout.string)
189
+ .to include([
190
+ "== #{target_file} ==",
191
+ 'C: 2: 80: Line is too long. [90/79]'
192
+ ].join("\n"))
261
193
  end
262
194
  end
263
195
 
264
- it 'prints all types' do
265
- cops
266
- .types
267
- .map(&:to_s)
268
- .map(&:capitalize)
269
- .each { |type| expect(stdout).to include(type) }
270
- end
271
-
272
- it 'prints all cops in their right type listing' do
273
- lines = stdout.lines
274
- lines.slice_before(/Type /).each do |slice|
275
- types = cops.types.map(&:to_s).map(&:capitalize)
276
- current = types.delete(slice.shift[/Type '(?<c>[^'']+)'/, 'c'])
277
- # all cops in their type listing
278
- cops.with_type(current).each do |cop|
279
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_true
280
- end
281
-
282
- # no cop in wrong type listing
283
- types.each do |type|
284
- cops.with_type(type).each do |cop|
285
- expect(slice.any? { |l| l.include? cop.cop_name }).to be_false
286
- end
287
- end
196
+ context 'when clang format is specified' do
197
+ it 'outputs with clang format' do
198
+ create_file('example1.rb', ['# encoding: utf-8',
199
+ 'x= 0 ',
200
+ '#' * 85,
201
+ 'y ',
202
+ 'puts x'])
203
+ create_file('example2.rb', ['# encoding: utf-8',
204
+ "\tx",
205
+ 'def a',
206
+ ' puts',
207
+ 'end'])
208
+ create_file('example3.rb', ['# encoding: utf-8',
209
+ 'def badName',
210
+ ' if something',
211
+ ' test',
212
+ ' end',
213
+ 'end'])
214
+ expect(cli.run(['--format', 'clang', 'example1.rb',
215
+ 'example2.rb', 'example3.rb']))
216
+ .to eq(1)
217
+ expect($stdout.string)
218
+ .to eq(['example1.rb:2:2: C: Surrounding space missing for ' +
219
+ "operator '='.",
220
+ 'x= 0 ',
221
+ ' ^',
222
+ 'example1.rb:2:5: C: Trailing whitespace detected.',
223
+ 'x= 0 ',
224
+ ' ^',
225
+ 'example1.rb:3:80: C: Line is too long. [85/79]',
226
+ '###################################################' +
227
+ '##################################',
228
+ ' ' +
229
+ ' ^^^^^^',
230
+ 'example1.rb:4:2: C: Trailing whitespace detected.',
231
+ 'y ',
232
+ ' ^',
233
+ 'example2.rb:2:1: C: Tab detected.',
234
+ "\tx",
235
+ '^',
236
+ 'example2.rb:4:1: C: Use 2 (not 3) spaces for ' +
237
+ 'indentation.',
238
+ ' puts',
239
+ '^^^',
240
+ 'example3.rb:2:5: C: Use snake_case for methods.',
241
+ 'def badName',
242
+ ' ^^^^^^^',
243
+ 'example3.rb:3:3: C: Favor modifier if/unless usage ' +
244
+ 'when you have a single-line body. Another good ' +
245
+ 'alternative is the usage of control flow &&/||.',
246
+ ' if something',
247
+ ' ^^',
248
+ 'example3.rb:5:5: W: end at 5, 4 is not aligned ' +
249
+ 'with if at 3, 2',
250
+ ' end',
251
+ ' ^^^',
252
+ '',
253
+ '3 files inspected, 9 offences detected',
254
+ ''].join("\n"))
288
255
  end
289
256
  end
290
257
 
291
- it 'prints the current configuration' do
292
- out = stdout.lines.to_a
293
- cops.each do |cop|
294
- conf = global_conf[cop.cop_name].dup
295
- confstrt =
296
- out.find_index { |i| i.include?("- #{cop.cop_name}") } + 1
297
- c = out[confstrt, conf.keys.size].to_s
298
- conf.delete('Description')
299
- expect(c).to include(short_description_of_cop(cop))
300
- conf.each do |k, v|
301
- # ugly hack to get hash/array content tested
302
- if v.kind_of?(Hash) || v.kind_of?(Array)
303
- expect(c).to include "#{k}: #{v.to_s.dump[2, -2]}"
304
- else
305
- expect(c).to include "#{k}: #{v}"
306
- end
307
- end
258
+ context 'when emacs format is specified' do
259
+ it 'outputs with emacs format' do
260
+ create_file('example1.rb', ['# encoding: utf-8',
261
+ 'x= 0 ',
262
+ 'y ',
263
+ 'puts x'])
264
+ create_file('example2.rb', ['# encoding: utf-8',
265
+ "\tx = 0",
266
+ 'puts x'])
267
+ expect(cli.run(['--format', 'emacs', 'example1.rb',
268
+ 'example2.rb'])).to eq(1)
269
+ expected_output =
270
+ ["#{abs('example1.rb')}:2:2: C: Surrounding space missing" +
271
+ " for operator '='.",
272
+ "#{abs('example1.rb')}:2:5: C: Trailing whitespace detected.",
273
+ "#{abs('example1.rb')}:3:2: C: Trailing whitespace detected.",
274
+ "#{abs('example2.rb')}:2:1: C: Tab detected.",
275
+ ''].join("\n")
276
+ expect($stdout.string).to eq(expected_output)
308
277
  end
309
278
  end
310
- end
311
-
312
- describe '-d/--debug' do
313
- it 'shows config files', ruby: 2.0 do
314
- create_file('example1.rb', "\tputs 0")
315
- expect(cli.run(['--debug', 'example1.rb'])).to eq(1)
316
- home = File.dirname(File.dirname(File.dirname(__FILE__)))
317
- expect($stdout.string.lines[2, 7].map(&:chomp).join("\n"))
318
- .to eq(["For #{abs('')}:" +
319
- " configuration from #{home}/config/default.yml",
320
- "Inheriting configuration from #{home}/config/enabled.yml",
321
- "Inheriting configuration from #{home}/config/" +
322
- 'disabled.yml',
323
- "AllCops/Excludes configuration from #{home}/.rubocop.yml",
324
- "Inheriting configuration from #{home}/config/default.yml",
325
- "Inheriting configuration from #{home}/config/enabled.yml",
326
- "Inheriting configuration from #{home}/config/disabled.yml"
327
- ].join("\n"))
328
- end
329
-
330
- it 'shows cop names', ruby: 2.0 do
331
- create_file('example1.rb', "\tputs 0")
332
- expect(cli.run(['--format',
333
- 'emacs',
334
- '--debug',
335
- 'example1.rb'])).to eq(1)
336
- expect($stdout.string.lines[-1])
337
- .to eq(["#{abs('example1.rb')}:1:1: C: Tab: Tab detected.",
338
- ''].join("\n"))
339
- end
340
- end
341
-
342
- describe '--require' do
343
- let(:required_file_path) { './path/to/required_file.rb' }
344
-
345
- before do
346
- create_file('example.rb', '# encoding: utf-8')
347
-
348
- create_file(required_file_path, [
349
- '# encoding: utf-8',
350
- "puts 'Hello from required file!'"
351
- ])
352
- end
353
279
 
354
- it 'requires the passed path' do
355
- cli.run(['--require', required_file_path, 'example.rb'])
356
- expect($stdout.string).to start_with('Hello from required file!')
357
- end
358
- end
359
-
360
- describe '-f/--format' do
361
- let(:target_file) { 'example.rb' }
362
-
363
- before do
364
- create_file(target_file, [
365
- '# encoding: utf-8',
366
- '#' * 90
367
- ])
368
- end
369
-
370
- describe 'builtin formatters' do
371
- context 'when simple format is specified' do
372
- it 'outputs with simple format' do
373
- cli.run(['--format', 'simple', 'example.rb'])
374
- expect($stdout.string)
375
- .to include([
376
- "== #{target_file} ==",
377
- 'C: 2: 80: Line is too long. [90/79]'
378
- ].join("\n"))
379
- end
380
- end
381
-
382
- context 'when clang format is specified' do
383
- it 'outputs with clang format' do
384
- create_file('example1.rb', ['# encoding: utf-8',
385
- 'x= 0 ',
386
- '#' * 85,
387
- 'y ',
388
- 'puts x'])
389
- create_file('example2.rb', ['# encoding: utf-8',
390
- "\tx",
391
- 'def a',
392
- ' puts',
393
- 'end'])
394
- create_file('example3.rb', ['# encoding: utf-8',
395
- 'def badName',
396
- ' if something',
397
- ' test',
398
- ' end',
399
- 'end'])
400
- expect(cli.run(['--format', 'clang', 'example1.rb',
401
- 'example2.rb', 'example3.rb']))
402
- .to eq(1)
403
- expect($stdout.string)
404
- .to eq(['example1.rb:2:2: C: Surrounding space missing for ' +
405
- "operator '='.",
406
- 'x= 0 ',
407
- ' ^',
408
- 'example1.rb:2:5: C: Trailing whitespace detected.',
409
- 'x= 0 ',
410
- ' ^',
411
- 'example1.rb:3:80: C: Line is too long. [85/79]',
412
- '###################################################' +
413
- '##################################',
414
- ' ' +
415
- ' ^^^^^^',
416
- 'example1.rb:4:2: C: Trailing whitespace detected.',
417
- 'y ',
418
- ' ^',
419
- 'example2.rb:2:1: C: Tab detected.',
420
- "\tx",
421
- '^^^^^',
422
- 'example2.rb:4:1: C: Use 2 (not 3) spaces for ' +
423
- 'indentation.',
424
- ' puts',
425
- '^^^',
426
- 'example3.rb:2:5: C: Use snake_case for methods and ' +
427
- 'variables.',
428
- 'def badName',
429
- ' ^^^^^^^',
430
- 'example3.rb:3:3: C: Favor modifier if/unless usage ' +
431
- 'when you have a single-line body. Another good ' +
432
- 'alternative is the usage of control flow &&/||.',
433
- ' if something',
434
- ' ^^',
435
- 'example3.rb:5:5: W: end at 5, 4 is not aligned ' +
436
- 'with if at 3, 2',
437
- ' end',
438
- ' ^^^',
439
- '',
440
- '3 files inspected, 9 offences detected',
441
- ''].join("\n"))
442
- end
443
- end
444
-
445
- context 'when emacs format is specified' do
446
- it 'outputs with emacs format' do
447
- create_file('example1.rb', ['# encoding: utf-8',
448
- 'x= 0 ',
449
- 'y ',
450
- 'puts x'])
451
- create_file('example2.rb', ['# encoding: utf-8',
452
- "\tx = 0",
453
- 'puts x'])
454
- expect(cli.run(['--format', 'emacs', 'example1.rb',
455
- 'example2.rb'])).to eq(1)
456
- expected_output =
457
- ["#{abs('example1.rb')}:2:2: C: Surrounding space missing" +
458
- " for operator '='.",
459
- "#{abs('example1.rb')}:2:5: C: Trailing whitespace detected.",
460
- "#{abs('example1.rb')}:3:2: C: Trailing whitespace detected.",
461
- "#{abs('example2.rb')}:2:1: C: Tab detected.",
462
- ''].join("\n")
463
- expect($stdout.string).to eq(expected_output)
464
- end
465
- end
466
-
467
- context 'when unknown format name is specified' do
468
- it 'aborts with error message' do
469
- expect { cli.run(['--format', 'unknown', 'example.rb']) }
470
- .to exit_with_code(1)
471
- expect($stderr.string)
472
- .to include('No formatter for "unknown"')
473
- end
280
+ context 'when unknown format name is specified' do
281
+ it 'aborts with error message' do
282
+ expect { cli.run(['--format', 'unknown', 'example.rb']) }
283
+ .to exit_with_code(1)
284
+ expect($stderr.string)
285
+ .to include('No formatter for "unknown"')
474
286
  end
475
287
  end
288
+ end
476
289
 
477
- describe 'custom formatter' do
478
- let(:target_file) { abs('example.rb') }
290
+ describe 'custom formatter' do
291
+ let(:target_file) { abs('example.rb') }
479
292
 
480
- context 'when a class name is specified' do
481
- it 'uses the class as a formatter' do
482
- module ::MyTool
483
- class RubocopFormatter < Rubocop::Formatter::BaseFormatter
484
- def started(all_files)
485
- output.puts "started: #{all_files.join(',')}"
486
- end
293
+ context 'when a class name is specified' do
294
+ it 'uses the class as a formatter' do
295
+ module ::MyTool
296
+ class RubocopFormatter < Rubocop::Formatter::BaseFormatter
297
+ def started(all_files)
298
+ output.puts "started: #{all_files.join(',')}"
299
+ end
487
300
 
488
- def file_started(file, options)
489
- output.puts "file_started: #{file}"
490
- end
301
+ def file_started(file, options)
302
+ output.puts "file_started: #{file}"
303
+ end
491
304
 
492
- def file_finished(file, offences)
493
- output.puts "file_finished: #{file}"
494
- end
305
+ def file_finished(file, offences)
306
+ output.puts "file_finished: #{file}"
307
+ end
495
308
 
496
- def finished(processed_files)
497
- output.puts "finished: #{processed_files.join(',')}"
498
- end
309
+ def finished(processed_files)
310
+ output.puts "finished: #{processed_files.join(',')}"
499
311
  end
500
312
  end
501
-
502
- cli.run(['--format', 'MyTool::RubocopFormatter', 'example.rb'])
503
- expect($stdout.string).to eq([
504
- "started: #{target_file}",
505
- "file_started: #{target_file}",
506
- "file_finished: #{target_file}",
507
- "finished: #{target_file}",
508
- ''
509
- ].join("\n"))
510
313
  end
511
- end
512
314
 
513
- context 'when unknown class name is specified' do
514
- it 'aborts with error message' do
515
- args = '--format UnknownFormatter example.rb'
516
- expect { cli.run(args.split) }.to exit_with_code(1)
517
- expect($stderr.string).to include('UnknownFormatter')
518
- end
315
+ cli.run(['--format', 'MyTool::RubocopFormatter', 'example.rb'])
316
+ expect($stdout.string).to eq([
317
+ "started: #{target_file}",
318
+ "file_started: #{target_file}",
319
+ "file_finished: #{target_file}",
320
+ "finished: #{target_file}",
321
+ ''
322
+ ].join("\n"))
519
323
  end
520
324
  end
521
325
 
522
- it 'can be used multiple times' do
523
- cli.run(['--format', 'simple', '--format', 'emacs', 'example.rb'])
524
- expect($stdout.string)
525
- .to include([
526
- "== #{target_file} ==",
527
- 'C: 2: 80: Line is too long. [90/79]',
528
- "#{abs(target_file)}:2:80: C: Line is too long. " +
529
- '[90/79]'
530
- ].join("\n"))
326
+ context 'when unknown class name is specified' do
327
+ it 'aborts with error message' do
328
+ args = '--format UnknownFormatter example.rb'
329
+ expect { cli.run(args.split) }.to exit_with_code(1)
330
+ expect($stderr.string).to include('UnknownFormatter')
331
+ end
531
332
  end
532
333
  end
533
334
 
534
- describe '-o/--out option' do
535
- let(:target_file) { 'example.rb' }
335
+ it 'can be used multiple times' do
336
+ cli.run(['--format', 'simple', '--format', 'emacs', 'example.rb'])
337
+ expect($stdout.string)
338
+ .to include([
339
+ "== #{target_file} ==",
340
+ 'C: 2: 80: Line is too long. [90/79]',
341
+ "#{abs(target_file)}:2:80: C: Line is too long. " +
342
+ '[90/79]'
343
+ ].join("\n"))
344
+ end
345
+ end
536
346
 
537
- before do
538
- create_file(target_file, [
539
- '# encoding: utf-8',
540
- '#' * 90
541
- ])
542
- end
347
+ describe '-o/--out option' do
348
+ let(:target_file) { 'example.rb' }
543
349
 
544
- it 'redirects output to the specified file' do
545
- cli.run(['--out', 'output.txt', target_file])
546
- expect(File.read('output.txt')).to include('Line is too long.')
547
- end
350
+ before do
351
+ create_file(target_file, [
352
+ '# encoding: utf-8',
353
+ '#' * 90
354
+ ])
355
+ end
548
356
 
549
- it 'is applied to the previously specified formatter' do
550
- cli.run([
551
- '--format', 'simple',
552
- '--format', 'emacs', '--out', 'emacs_output.txt',
553
- target_file
554
- ])
357
+ it 'redirects output to the specified file' do
358
+ cli.run(['--out', 'output.txt', target_file])
359
+ expect(File.read('output.txt')).to include('Line is too long.')
360
+ end
555
361
 
556
- expect($stdout.string).to eq([
557
- "== #{target_file} ==",
558
- 'C: 2: 80: Line is too long. [90/79]',
559
- '',
560
- '1 file inspected, 1 offence detected',
561
- ''
562
- ].join("\n"))
362
+ it 'is applied to the previously specified formatter' do
363
+ cli.run([
364
+ '--format', 'simple',
365
+ '--format', 'emacs', '--out', 'emacs_output.txt',
366
+ target_file
367
+ ])
563
368
 
564
- expect(File.read('emacs_output.txt'))
565
- .to eq("#{abs(target_file)}:2:80: C: Line is too long. [90/79]\n")
566
- end
369
+ expect($stdout.string).to eq([
370
+ "== #{target_file} ==",
371
+ 'C: 2: 80: Line is too long. [90/79]',
372
+ '',
373
+ '1 file inspected, 1 offence detected',
374
+ ''
375
+ ].join("\n"))
376
+
377
+ expect(File.read('emacs_output.txt'))
378
+ .to eq("#{abs(target_file)}:2:80: C: Line is too long. [90/79]\n")
567
379
  end
568
380
  end
381
+ end
569
382
 
570
- describe '#wants_to_quit?' do
571
- it 'is initially false' do
572
- expect(cli.wants_to_quit?).to be_false
573
- end
383
+ describe '#wants_to_quit?' do
384
+ it 'is initially false' do
385
+ expect(cli.wants_to_quit?).to be_false
386
+ end
574
387
 
575
- context 'when true' do
576
- it 'returns 1' do
577
- create_file('example.rb', '# encoding: utf-8')
578
- cli.wants_to_quit = true
579
- expect(cli.run(['example.rb'])).to eq(1)
580
- end
388
+ context 'when true' do
389
+ it 'returns 1' do
390
+ create_file('example.rb', '# encoding: utf-8')
391
+ cli.wants_to_quit = true
392
+ expect(cli.run(['example.rb'])).to eq(1)
581
393
  end
582
394
  end
395
+ end
583
396
 
584
- describe '#trap_interrupt' do
585
- before do
586
- @interrupt_handlers = []
587
- Signal.stub(:trap).with('INT') do |&block|
588
- @interrupt_handlers << block
589
- end
397
+ describe '#trap_interrupt' do
398
+ before do
399
+ @interrupt_handlers = []
400
+ Signal.stub(:trap).with('INT') do |&block|
401
+ @interrupt_handlers << block
590
402
  end
403
+ end
591
404
 
592
- def interrupt
593
- @interrupt_handlers.each(&:call)
594
- end
405
+ def interrupt
406
+ @interrupt_handlers.each(&:call)
407
+ end
595
408
 
596
- it 'adds a handler for SIGINT' do
597
- expect(@interrupt_handlers).to be_empty
409
+ it 'adds a handler for SIGINT' do
410
+ expect(@interrupt_handlers).to be_empty
411
+ cli.trap_interrupt
412
+ expect(@interrupt_handlers.size).to eq(1)
413
+ end
414
+
415
+ context 'with SIGINT once' do
416
+ it 'sets #wants_to_quit? to true' do
598
417
  cli.trap_interrupt
599
- expect(@interrupt_handlers.size).to eq(1)
418
+ expect(cli.wants_to_quit?).to be_false
419
+ interrupt
420
+ expect(cli.wants_to_quit?).to be_true
600
421
  end
601
422
 
602
- context 'with SIGINT once' do
603
- it 'sets #wants_to_quit? to true' do
604
- cli.trap_interrupt
605
- expect(cli.wants_to_quit?).to be_false
606
- interrupt
607
- expect(cli.wants_to_quit?).to be_true
608
- end
609
-
610
- it 'does not exit immediately' do
611
- Object.any_instance.should_not_receive(:exit)
612
- Object.any_instance.should_not_receive(:exit!)
613
- cli.trap_interrupt
614
- interrupt
615
- end
423
+ it 'does not exit immediately' do
424
+ Object.any_instance.should_not_receive(:exit)
425
+ Object.any_instance.should_not_receive(:exit!)
426
+ cli.trap_interrupt
427
+ interrupt
616
428
  end
429
+ end
617
430
 
618
- context 'with SIGINT twice' do
619
- it 'exits immediately' do
620
- Object.any_instance.should_receive(:exit!).with(1)
621
- cli.trap_interrupt
622
- interrupt
623
- interrupt
624
- end
431
+ context 'with SIGINT twice' do
432
+ it 'exits immediately' do
433
+ Object.any_instance.should_receive(:exit!).with(1)
434
+ cli.trap_interrupt
435
+ interrupt
436
+ interrupt
625
437
  end
626
438
  end
439
+ end
627
440
 
628
- it 'checks a given correct file and returns 0' do
629
- create_file('example.rb', [
630
- '# encoding: utf-8',
631
- 'x = 0',
632
- 'puts x'
633
- ])
634
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
635
- expect($stdout.string)
636
- .to eq("\n1 file inspected, no offences detected\n")
637
- end
441
+ it 'checks a given correct file and returns 0' do
442
+ create_file('example.rb', [
443
+ '# encoding: utf-8',
444
+ 'x = 0',
445
+ 'puts x'
446
+ ])
447
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
448
+ expect($stdout.string)
449
+ .to eq("\n1 file inspected, no offences detected\n")
450
+ end
638
451
 
639
- it 'checks a given file with faults and returns 1' do
640
- create_file('example.rb', [
641
- '# encoding: utf-8',
642
- 'x = 0 ',
643
- 'puts x'
644
- ])
645
- expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
452
+ it 'checks a given file with faults and returns 1' do
453
+ create_file('example.rb', [
454
+ '# encoding: utf-8',
455
+ 'x = 0 ',
456
+ 'puts x'
457
+ ])
458
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
459
+ expect($stdout.string)
460
+ .to eq ['== example.rb ==',
461
+ 'C: 2: 6: Trailing whitespace detected.',
462
+ '',
463
+ '1 file inspected, 1 offence detected',
464
+ ''].join("\n")
465
+ end
466
+
467
+ it 'registers an offence for a syntax error' do
468
+ create_file('example.rb', [
469
+ '# encoding: utf-8',
470
+ 'class Test',
471
+ 'en'
472
+ ])
473
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
474
+ expect($stdout.string)
475
+ .to eq(["#{abs('example.rb')}:4:1: E: unexpected " +
476
+ 'token $end',
477
+ ''].join("\n"))
478
+ end
479
+
480
+ it 'registers an offence for Parser warnings' do
481
+ create_file('example.rb', [
482
+ '# encoding: utf-8',
483
+ 'puts *test',
484
+ 'if a then b else c end'
485
+ ])
486
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
487
+ expect($stdout.string)
488
+ .to eq(["#{abs('example.rb')}:2:6: W: " +
489
+ "`*' interpreted as argument prefix",
490
+ "#{abs('example.rb')}:3:1: C: " +
491
+ 'Favor the ternary operator (?:) over if/then/else/end ' +
492
+ 'constructs.',
493
+ ''].join("\n"))
494
+ end
495
+
496
+ it 'can process a file with an invalid UTF-8 byte sequence' do
497
+ create_file('example.rb', [
498
+ '# encoding: utf-8',
499
+ "# #{'f9'.hex.chr}#{'29'.hex.chr}"
500
+ ])
501
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
502
+ end
503
+
504
+ describe 'rubocop:disable comment' do
505
+ it 'can disable all cops in a code section' do
506
+ create_file('example.rb',
507
+ ['# encoding: utf-8',
508
+ '# rubocop:disable all',
509
+ '#' * 90,
510
+ 'x(123456)',
511
+ 'y("123")',
512
+ 'def func',
513
+ ' # rubocop: enable LineLength, StringLiterals',
514
+ ' ' + '#' * 93,
515
+ ' x(123456)',
516
+ ' y("123")',
517
+ 'end'])
518
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
519
+ # all cops were disabled, then 2 were enabled again, so we
520
+ # should get 2 offences reported.
646
521
  expect($stdout.string)
647
- .to eq ['== example.rb ==',
648
- 'C: 2: 6: Trailing whitespace detected.',
649
- '',
650
- '1 file inspected, 1 offence detected',
651
- ''].join("\n")
522
+ .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
523
+ "#{abs('example.rb')}:10:5: C: Prefer single-quoted " +
524
+ "strings when you don't need string interpolation or " +
525
+ 'special symbols.',
526
+ ''].join("\n"))
652
527
  end
653
528
 
654
- it 'registers an offence for a syntax error' do
655
- create_file('example.rb', [
656
- '# encoding: utf-8',
657
- 'class Test',
658
- 'en'
659
- ])
529
+ it 'can disable selected cops in a code section' do
530
+ create_file('example.rb',
531
+ ['# encoding: utf-8',
532
+ '# rubocop:disable LineLength,NumericLiterals,' +
533
+ 'StringLiterals',
534
+ '#' * 90,
535
+ 'x(123456)',
536
+ 'y("123")',
537
+ 'def func',
538
+ ' # rubocop: enable LineLength, StringLiterals',
539
+ ' ' + '#' * 93,
540
+ ' x(123456)',
541
+ ' y("123")',
542
+ 'end'])
660
543
  expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
544
+ # 3 cops were disabled, then 2 were enabled again, so we
545
+ # should get 2 offences reported.
661
546
  expect($stdout.string)
662
- .to eq(["#{abs('example.rb')}:3:3: E: unexpected " +
663
- 'token $end',
547
+ .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
548
+ "#{abs('example.rb')}:10:5: C: Prefer single-quoted " +
549
+ "strings when you don't need string interpolation or " +
550
+ 'special symbols.',
664
551
  ''].join("\n"))
665
552
  end
666
553
 
667
- it 'registers an offence for Parser warnings' do
554
+ it 'can disable all cops on a single line' do
668
555
  create_file('example.rb', [
669
556
  '# encoding: utf-8',
670
- 'puts *test',
671
- 'if a then b else c end'
557
+ 'y("123", 123456) # rubocop:disable all'
672
558
  ])
559
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
560
+ expect($stdout.string).to be_empty
561
+ end
562
+
563
+ it 'can disable selected cops on a single line' do
564
+ create_file('example.rb',
565
+ [
566
+ '# encoding: utf-8',
567
+ '#' * 90 + ' # rubocop:disable LineLength',
568
+ '#' * 95,
569
+ 'y("123") # rubocop:disable LineLength,StringLiterals'
570
+ ])
673
571
  expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
674
572
  expect($stdout.string)
675
- .to eq(["#{abs('example.rb')}:2:6: W: " +
676
- "`*' interpreted as argument prefix",
677
- "#{abs('example.rb')}:3:1: C: " +
678
- 'Favor the ternary operator (?:) over if/then/else/end ' +
679
- 'constructs.',
573
+ .to eq(
574
+ ["#{abs('example.rb')}:3:80: C: Line is too long. [95/79]",
680
575
  ''].join("\n"))
681
576
  end
577
+ end
682
578
 
683
- it 'can process a file with an invalid UTF-8 byte sequence' do
684
- create_file('example.rb', [
685
- '# encoding: utf-8',
686
- "# #{'f9'.hex.chr}#{'29'.hex.chr}"
687
- ])
688
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
579
+ it 'finds a file with no .rb extension but has a shebang line' do
580
+ create_file('example', [
581
+ '#!/usr/bin/env ruby',
582
+ '# encoding: utf-8',
583
+ 'x = 0',
584
+ 'puts x'
585
+ ])
586
+ expect(cli.run(%w(--format simple))).to eq(0)
587
+ expect($stdout.string)
588
+ .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
589
+ end
590
+
591
+ describe 'enabling/disabling rails cops' do
592
+ it 'by default does not run rails cops' do
593
+ create_file('example1.rb', ['# encoding: utf-8',
594
+ 'read_attribute(:test)'])
595
+ expect(cli.run(['--format', 'simple', 'example1.rb'])).to eq(0)
689
596
  end
690
597
 
691
- describe 'rubocop:disable comment' do
692
- it 'can disable all cops in a code section' do
693
- create_file('example.rb',
694
- ['# encoding: utf-8',
695
- '# rubocop:disable all',
696
- '#' * 90,
697
- 'x(123456)',
698
- 'y("123")',
699
- 'def func',
700
- ' # rubocop: enable LineLength, StringLiterals',
701
- ' ' + '#' * 93,
702
- ' x(123456)',
703
- ' y("123")',
704
- 'end'])
705
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
706
- # all cops were disabled, then 2 were enabled again, so we
707
- # should get 2 offences reported.
708
- expect($stdout.string)
709
- .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
710
- "#{abs('example.rb')}:10:5: C: Prefer single-quoted " +
711
- "strings when you don't need string interpolation or " +
712
- 'special symbols.',
713
- ''].join("\n"))
714
- end
598
+ it 'with -R given runs rails cops' do
599
+ create_file('example1.rb', ['# encoding: utf-8',
600
+ 'read_attribute(:test)'])
601
+ expect(cli.run(['--format', 'simple', '-R', 'example1.rb'])).to eq(1)
602
+ expect($stdout.string).to include('Prefer self[:attribute]')
603
+ end
715
604
 
716
- it 'can disable selected cops in a code section' do
717
- create_file('example.rb',
718
- ['# encoding: utf-8',
719
- '# rubocop:disable LineLength,NumericLiterals,' +
720
- 'StringLiterals',
721
- '#' * 90,
722
- 'x(123456)',
723
- 'y("123")',
724
- 'def func',
725
- ' # rubocop: enable LineLength, StringLiterals',
726
- ' ' + '#' * 93,
727
- ' x(123456)',
728
- ' y("123")',
729
- 'end'])
730
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
731
- # 3 cops were disabled, then 2 were enabled again, so we
732
- # should get 2 offences reported.
733
- expect($stdout.string)
734
- .to eq(["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
735
- "#{abs('example.rb')}:10:5: C: Prefer single-quoted " +
736
- "strings when you don't need string interpolation or " +
737
- 'special symbols.',
738
- ''].join("\n"))
739
- end
605
+ it 'with configation option true in one dir runs rails cops there' do
606
+ create_file('dir1/example1.rb', ['# encoding: utf-8',
607
+ 'read_attribute(:test)'])
608
+ create_file('dir1/.rubocop.yml', [
609
+ 'AllCops:',
610
+ ' RunRailsCops: true',
611
+ ])
612
+ create_file('dir2/example2.rb', ['# encoding: utf-8',
613
+ 'read_attribute(:test)'])
614
+ create_file('dir2/.rubocop.yml', [
615
+ 'AllCops:',
616
+ ' RunRailsCops: false',
617
+ ])
618
+ expect(cli.run(['--format', 'simple', 'dir1', 'dir2'])).to eq(1)
619
+ expect($stdout.string)
620
+ .to eq(['== dir1/example1.rb ==',
621
+ 'C: 2: 1: Prefer self[:attribute] over read_attribute' +
622
+ '(:attribute).',
623
+ '',
624
+ '2 files inspected, 1 offence detected',
625
+ ''].join("\n"))
626
+ end
740
627
 
741
- it 'can disable all cops on a single line' do
742
- create_file('example.rb', [
743
- '# encoding: utf-8',
744
- 'y("123", 123456) # rubocop:disable all'
628
+ it 'with configation option false but -R given runs rails cops' do
629
+ create_file('example1.rb', ['# encoding: utf-8',
630
+ 'read_attribute(:test)'])
631
+ create_file('.rubocop.yml', [
632
+ 'AllCops:',
633
+ ' RunRailsCops: false',
745
634
  ])
746
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
747
- expect($stdout.string).to be_empty
748
- end
749
-
750
- it 'can disable selected cops on a single line' do
751
- create_file('example.rb',
752
- [
753
- '# encoding: utf-8',
754
- '#' * 90 + ' # rubocop:disable LineLength',
755
- '#' * 95,
756
- 'y("123") # rubocop:disable LineLength,StringLiterals'
757
- ])
758
- expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
759
- expect($stdout.string)
760
- .to eq(
761
- ["#{abs('example.rb')}:3:80: C: Line is too long. [95/79]",
762
- ''].join("\n"))
763
- end
635
+ expect(cli.run(['--format', 'simple', '-R', 'example1.rb'])).to eq(1)
636
+ expect($stdout.string).to include('Prefer self[:attribute]')
764
637
  end
765
-
766
- it 'finds a file with no .rb extension but has a shebang line' do
638
+ end
639
+
640
+ describe 'configuration from file' do
641
+ it 'finds included files' do
767
642
  create_file('example', [
768
- '#!/usr/bin/env ruby',
769
643
  '# encoding: utf-8',
770
644
  'x = 0',
771
645
  'puts x'
772
646
  ])
647
+ create_file('regexp', [
648
+ '# encoding: utf-8',
649
+ 'x = 0',
650
+ 'puts x'
651
+ ])
652
+ create_file('.rubocop.yml', [
653
+ 'AllCops:',
654
+ ' Includes:',
655
+ ' - example',
656
+ ' - !ruby/regexp /regexp$/'
657
+ ])
773
658
  expect(cli.run(%w(--format simple))).to eq(0)
774
659
  expect($stdout.string)
775
- .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
660
+ .to eq(['', '2 files inspected, no offences detected',
661
+ ''].join("\n"))
776
662
  end
777
663
 
778
- describe 'configuration from file' do
779
- it 'finds included files' do
780
- create_file('example', [
664
+ it 'ignores excluded files' do
665
+ create_file('example.rb', [
666
+ '# encoding: utf-8',
667
+ 'x = 0',
668
+ 'puts x'
669
+ ])
670
+ create_file('regexp.rb', [
781
671
  '# encoding: utf-8',
782
672
  'x = 0',
783
673
  'puts x'
784
674
  ])
785
- create_file('regexp', [
786
- '# encoding: utf-8',
787
- 'x = 0',
788
- 'puts x'
789
- ])
790
- create_file('.rubocop.yml', [
791
- 'AllCops:',
792
- ' Includes:',
793
- ' - example',
794
- ' - !ruby/regexp /regexp$/'
795
- ])
796
- expect(cli.run(%w(--format simple))).to eq(0)
797
- expect($stdout.string)
798
- .to eq(['', '2 files inspected, no offences detected',
799
- ''].join("\n"))
800
- end
801
-
802
- it 'ignores excluded files' do
803
- create_file('example.rb', [
804
- '# encoding: utf-8',
805
- 'x = 0',
806
- 'puts x'
675
+ create_file('exclude_glob.rb', [
676
+ '#!/usr/bin/env ruby',
677
+ '# encoding: utf-8',
678
+ 'x = 0',
679
+ 'puts x'
680
+ ])
681
+ create_file('.rubocop.yml', [
682
+ 'AllCops:',
683
+ ' Excludes:',
684
+ ' - example.rb',
685
+ ' - !ruby/regexp /regexp.rb$/',
686
+ ' - "exclude_*"'
807
687
  ])
808
- create_file('regexp.rb', [
809
- '# encoding: utf-8',
810
- 'x = 0',
811
- 'puts x'
812
- ])
813
- create_file('exclude_glob.rb', [
814
- '#!/usr/bin/env ruby',
815
- '# encoding: utf-8',
816
- 'x = 0',
817
- 'puts x'
818
- ])
819
- create_file('.rubocop.yml', [
820
- 'AllCops:',
821
- ' Excludes:',
822
- ' - example.rb',
823
- ' - !ruby/regexp /regexp.rb$/',
824
- ' - "exclude_*"'
825
- ])
826
- expect(cli.run(%w(--format simple))).to eq(0)
827
- expect($stdout.string)
828
- .to eq(['', '0 files inspected, no offences detected',
829
- ''].join("\n"))
688
+ expect(cli.run(%w(--format simple))).to eq(0)
689
+ expect($stdout.string)
690
+ .to eq(['', '0 files inspected, no offences detected',
691
+ ''].join("\n"))
692
+ end
693
+
694
+ # With rubinius 2.0.0.rc1 + rspec 2.13.1,
695
+ # File.stub(:open).and_call_original causes SystemStackError.
696
+ it 'does not read files in excluded list', broken: :rbx do
697
+ %w(rb.rb non-rb.ext without-ext).each do |filename|
698
+ create_file("example/ignored/#{filename}", [
699
+ '# encoding: utf-8',
700
+ '#' * 90
701
+ ])
830
702
  end
831
703
 
832
- # With rubinius 2.0.0.rc1 + rspec 2.13.1,
833
- # File.stub(:open).and_call_original causes SystemStackError.
834
- it 'does not read files in excluded list', broken: :rbx do
835
- %w(rb.rb non-rb.ext without-ext).each do |filename|
836
- create_file("example/ignored/#{filename}", [
837
- '# encoding: utf-8',
838
- '#' * 90
839
- ])
840
- end
704
+ create_file('example/.rubocop.yml', [
705
+ 'AllCops:',
706
+ ' Excludes:',
707
+ ' - ignored/**',
708
+ ])
709
+ File.should_not_receive(:open).with(%r(/ignored/))
710
+ File.stub(:open).and_call_original
711
+ expect(cli.run(%w(--format simple example))).to eq(0)
712
+ expect($stdout.string)
713
+ .to eq(['', '0 files inspected, no offences detected',
714
+ ''].join("\n"))
715
+ end
841
716
 
842
- create_file('example/.rubocop.yml', [
843
- 'AllCops:',
844
- ' Excludes:',
845
- ' - ignored/**',
846
- ])
847
- File.should_not_receive(:open).with(%r(/ignored/))
848
- File.stub(:open).and_call_original
849
- expect(cli.run(%w(--format simple example))).to eq(0)
850
- expect($stdout.string)
851
- .to eq(['', '0 files inspected, no offences detected',
852
- ''].join("\n"))
853
- end
717
+ it 'can be configured with option to disable a certain error' do
718
+ create_file('example1.rb', 'puts 0 ')
719
+ create_file('rubocop.yml', [
720
+ 'Encoding:',
721
+ ' Enabled: false',
722
+ '',
723
+ 'CaseIndentation:',
724
+ ' Enabled: false'
725
+ ])
726
+ expect(cli.run(['--format', 'simple',
727
+ '-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
728
+ expect($stdout.string)
729
+ .to eq(['== example1.rb ==',
730
+ 'C: 1: 7: Trailing whitespace detected.',
731
+ '',
732
+ '1 file inspected, 1 offence detected',
733
+ ''].join("\n"))
734
+ end
854
735
 
855
- it 'can be configured with option to disable a certain error' do
856
- create_file('example1.rb', 'puts 0 ')
857
- create_file('rubocop.yml', [
858
- 'Encoding:',
859
- ' Enabled: false',
860
- '',
861
- 'CaseIndentation:',
862
- ' Enabled: false'
863
- ])
864
- expect(cli.run(['--format', 'simple',
865
- '-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
866
- expect($stdout.string)
867
- .to eq(['== example1.rb ==',
868
- 'C: 1: 7: Trailing whitespace detected.',
869
- '',
870
- '1 file inspected, 1 offence detected',
871
- ''].join("\n"))
872
- end
736
+ it 'can disable Syntax offences with warning severity' do
737
+ pending
738
+ # `-' interpreted as argument prefix
739
+ create_file('example.rb', 'puts -1')
740
+ create_file('.rubocop.yml', [
741
+ 'Encoding:',
742
+ ' Enabled: false',
743
+ '',
744
+ 'Syntax:',
745
+ ' Enabled: false'
746
+ ])
747
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
748
+ end
873
749
 
874
- it 'can be configured to override a parameter that is a hash' do
875
- create_file('example1.rb',
876
- ['# encoding: utf-8',
877
- 'arr.find_all { |e| e > 0 }.collect { |e| -e }'])
878
- # We only care about select over find_all. All other preferred methods
879
- # appearing in the default config are gone when we override
880
- # PreferredMethods. We get no report about collect.
881
- create_file('rubocop.yml',
882
- ['CollectionMethods:',
883
- ' PreferredMethods:',
884
- ' find_all: select'])
885
- cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
886
- expect($stdout.string)
887
- .to eq(['== example1.rb ==',
888
- 'C: 2: 5: Prefer select over find_all.',
889
- '',
890
- '1 file inspected, 1 offence detected',
891
- ''].join("\n"))
892
- end
750
+ it 'cannot disable Syntax offences with fatal/error severity' do
751
+ create_file('example.rb', 'class Test')
752
+ create_file('.rubocop.yml', [
753
+ 'Encoding:',
754
+ ' Enabled: false',
755
+ '',
756
+ 'Syntax:',
757
+ ' Enabled: false'
758
+ ])
759
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
760
+ expect($stdout.string).to include('unexpected token $end')
761
+ end
893
762
 
894
- it 'works when a cop that others depend on is disabled' do
895
- create_file('example1.rb', ['if a',
896
- ' b',
897
- 'end'])
898
- create_file('rubocop.yml', [
899
- 'Encoding:',
900
- ' Enabled: false',
901
- '',
902
- 'LineLength:',
903
- ' Enabled: false'
904
- ])
905
- result = cli.run(['--format', 'simple',
906
- '-c', 'rubocop.yml', 'example1.rb'])
907
- expect($stdout.string)
908
- .to eq(['== example1.rb ==',
909
- 'C: 1: 1: Favor modifier if/unless usage when you have ' +
910
- 'a single-line body. Another good alternative is the ' +
911
- 'usage of control flow &&/||.',
912
- '',
913
- '1 file inspected, 1 offence detected',
914
- ''].join("\n"))
915
- expect(result).to eq(1)
916
- end
763
+ it 'can be configured to override a parameter that is a hash' do
764
+ create_file('example1.rb',
765
+ ['# encoding: utf-8',
766
+ 'arr.find_all { |e| e > 0 }.collect { |e| -e }'])
767
+ # We only care about select over find_all. All other preferred methods
768
+ # appearing in the default config are gone when we override
769
+ # PreferredMethods. We get no report about collect.
770
+ create_file('rubocop.yml',
771
+ ['CollectionMethods:',
772
+ ' PreferredMethods:',
773
+ ' find_all: select'])
774
+ cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
775
+ expect($stdout.string)
776
+ .to eq(['== example1.rb ==',
777
+ 'C: 2: 5: Prefer select over find_all.',
778
+ '',
779
+ '1 file inspected, 1 offence detected',
780
+ ''].join("\n"))
781
+ end
917
782
 
918
- it 'can be configured with project config to disable a certain error' do
919
- create_file('example_src/example1.rb', 'puts 0 ')
920
- create_file('example_src/.rubocop.yml', [
921
- 'Encoding:',
922
- ' Enabled: false',
923
- '',
924
- 'CaseIndentation:',
925
- ' Enabled: false'
926
- ])
927
- expect(cli.run(['--format', 'simple',
928
- 'example_src/example1.rb'])).to eq(1)
929
- expect($stdout.string)
930
- .to eq(['== example_src/example1.rb ==',
931
- 'C: 1: 7: Trailing whitespace detected.',
932
- '',
933
- '1 file inspected, 1 offence detected',
934
- ''].join("\n"))
935
- end
783
+ it 'works when a cop that others depend on is disabled' do
784
+ create_file('example1.rb', ['if a',
785
+ ' b',
786
+ 'end'])
787
+ create_file('rubocop.yml', [
788
+ 'Encoding:',
789
+ ' Enabled: false',
790
+ '',
791
+ 'LineLength:',
792
+ ' Enabled: false'
793
+ ])
794
+ result = cli.run(['--format', 'simple',
795
+ '-c', 'rubocop.yml', 'example1.rb'])
796
+ expect($stdout.string)
797
+ .to eq(['== example1.rb ==',
798
+ 'C: 1: 1: Favor modifier if/unless usage when you have ' +
799
+ 'a single-line body. Another good alternative is the ' +
800
+ 'usage of control flow &&/||.',
801
+ '',
802
+ '1 file inspected, 1 offence detected',
803
+ ''].join("\n"))
804
+ expect(result).to eq(1)
805
+ end
936
806
 
937
- it 'can use an alternative max line length from a config file' do
938
- create_file('example_src/example1.rb', [
939
- '# encoding: utf-8',
940
- '#' * 90
941
- ])
942
- create_file('example_src/.rubocop.yml', [
943
- 'LineLength:',
944
- ' Enabled: true',
945
- ' Max: 100'
946
- ])
947
- expect(cli.run(['--format', 'simple',
948
- 'example_src/example1.rb'])).to eq(0)
949
- expect($stdout.string)
950
- .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
951
- end
807
+ it 'can be configured with project config to disable a certain error' do
808
+ create_file('example_src/example1.rb', 'puts 0 ')
809
+ create_file('example_src/.rubocop.yml', [
810
+ 'Encoding:',
811
+ ' Enabled: false',
812
+ '',
813
+ 'CaseIndentation:',
814
+ ' Enabled: false'
815
+ ])
816
+ expect(cli.run(['--format', 'simple',
817
+ 'example_src/example1.rb'])).to eq(1)
818
+ expect($stdout.string)
819
+ .to eq(['== example_src/example1.rb ==',
820
+ 'C: 1: 7: Trailing whitespace detected.',
821
+ '',
822
+ '1 file inspected, 1 offence detected',
823
+ ''].join("\n"))
824
+ end
952
825
 
953
- it 'can have different config files in different directories' do
954
- %w(src lib).each do |dir|
955
- create_file("example/#{dir}/example1.rb", [
956
- '# encoding: utf-8',
957
- '#' * 90
958
- ])
959
- end
960
- create_file('example/src/.rubocop.yml', [
961
- 'LineLength:',
962
- ' Enabled: true',
963
- ' Max: 100'
964
- ])
965
- expect(cli.run(%w(--format simple example))).to eq(1)
966
- expect($stdout.string).to eq(
967
- ['== example/lib/example1.rb ==',
968
- 'C: 2: 80: Line is too long. [90/79]',
969
- '',
970
- '2 files inspected, 1 offence detected',
971
- ''].join("\n"))
972
- end
826
+ it 'can use an alternative max line length from a config file' do
827
+ create_file('example_src/example1.rb', [
828
+ '# encoding: utf-8',
829
+ '#' * 90
830
+ ])
831
+ create_file('example_src/.rubocop.yml', [
832
+ 'LineLength:',
833
+ ' Enabled: true',
834
+ ' Max: 100'
835
+ ])
836
+ expect(cli.run(['--format', 'simple',
837
+ 'example_src/example1.rb'])).to eq(0)
838
+ expect($stdout.string)
839
+ .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
840
+ end
973
841
 
974
- it 'prefers a config file in ancestor directory to another in home' do
975
- create_file('example_src/example1.rb', [
976
- '# encoding: utf-8',
977
- '#' * 90
978
- ])
979
- create_file('example_src/.rubocop.yml', [
980
- 'LineLength:',
981
- ' Enabled: true',
982
- ' Max: 100'
983
- ])
984
- create_file("#{Dir.home}/.rubocop.yml", [
985
- 'LineLength:',
986
- ' Enabled: true',
987
- ' Max: 80'
988
- ])
989
- expect(cli.run(['--format', 'simple',
990
- 'example_src/example1.rb'])).to eq(0)
991
- expect($stdout.string)
992
- .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
993
- end
842
+ it 'can have different config files in different directories' do
843
+ %w(src lib).each do |dir|
844
+ create_file("example/#{dir}/example1.rb", [
845
+ '# encoding: utf-8',
846
+ '#' * 90
847
+ ])
848
+ end
849
+ create_file('example/src/.rubocop.yml', [
850
+ 'LineLength:',
851
+ ' Enabled: true',
852
+ ' Max: 100'
853
+ ])
854
+ expect(cli.run(%w(--format simple example))).to eq(1)
855
+ expect($stdout.string).to eq(
856
+ ['== example/lib/example1.rb ==',
857
+ 'C: 2: 80: Line is too long. [90/79]',
858
+ '',
859
+ '2 files inspected, 1 offence detected',
860
+ ''].join("\n"))
861
+ end
994
862
 
995
- it 'can exclude directories relative to .rubocop.yml' do
996
- %w(src etc/test etc/spec tmp/test tmp/spec).each do |dir|
997
- create_file("example/#{dir}/example1.rb", [
998
- '# encoding: utf-8',
999
- '#' * 90
1000
- ])
1001
- end
863
+ it 'prefers a config file in ancestor directory to another in home' do
864
+ create_file('example_src/example1.rb', [
865
+ '# encoding: utf-8',
866
+ '#' * 90
867
+ ])
868
+ create_file('example_src/.rubocop.yml', [
869
+ 'LineLength:',
870
+ ' Enabled: true',
871
+ ' Max: 100'
872
+ ])
873
+ create_file("#{Dir.home}/.rubocop.yml", [
874
+ 'LineLength:',
875
+ ' Enabled: true',
876
+ ' Max: 80'
877
+ ])
878
+ expect(cli.run(['--format', 'simple',
879
+ 'example_src/example1.rb'])).to eq(0)
880
+ expect($stdout.string)
881
+ .to eq(['', '1 file inspected, no offences detected', ''].join("\n"))
882
+ end
1002
883
 
1003
- create_file('example/.rubocop.yml', [
1004
- 'AllCops:',
1005
- ' Excludes:',
1006
- ' - src/**',
1007
- ' - etc/**',
1008
- ' - tmp/spec/**'
1009
- ])
1010
-
1011
- expect(cli.run(%w(--format simple example))).to eq(1)
1012
- expect($stdout.string).to eq(
1013
- ['== example/tmp/test/example1.rb ==',
1014
- 'C: 2: 80: Line is too long. [90/79]',
1015
- '',
1016
- '1 file inspected, 1 offence detected',
1017
- ''].join("\n"))
884
+ it 'can exclude directories relative to .rubocop.yml' do
885
+ %w(src etc/test etc/spec tmp/test tmp/spec).each do |dir|
886
+ create_file("example/#{dir}/example1.rb", [
887
+ '# encoding: utf-8',
888
+ '#' * 90
889
+ ])
1018
890
  end
1019
891
 
1020
- it 'can exclude a typical vendor directory' do
1021
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
1022
- ['AllCops:',
1023
- ' Excludes:',
1024
- ' - lib/parser/lexer.rb'])
892
+ create_file('example/.rubocop.yml', [
893
+ 'AllCops:',
894
+ ' Excludes:',
895
+ ' - src/**',
896
+ ' - etc/**',
897
+ ' - tmp/spec/**'
898
+ ])
1025
899
 
1026
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
1027
- ['# encoding: utf-8',
1028
- '#' * 90])
900
+ expect(cli.run(%w(--format simple example))).to eq(1)
901
+ expect($stdout.string).to eq(
902
+ ['== example/tmp/test/example1.rb ==',
903
+ 'C: 2: 80: Line is too long. [90/79]',
904
+ '',
905
+ '1 file inspected, 1 offence detected',
906
+ ''].join("\n"))
907
+ end
1029
908
 
1030
- create_file('.rubocop.yml',
1031
- ['AllCops:',
1032
- ' Excludes:',
1033
- ' - vendor/**'])
909
+ it 'can exclude a typical vendor directory' do
910
+ create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
911
+ ['AllCops:',
912
+ ' Excludes:',
913
+ ' - lib/parser/lexer.rb'])
1034
914
 
1035
- cli.run(%w(--format simple))
1036
- expect($stdout.string)
1037
- .to eq(['', '0 files inspected, no offences detected',
1038
- ''].join("\n"))
1039
- end
915
+ create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
916
+ ['# encoding: utf-8',
917
+ '#' * 90])
1040
918
 
1041
- # Relative exclude paths in .rubocop.yml files are relative to that file,
1042
- # but in configuration files with other names they will be relative to
1043
- # whatever file inherits from them.
1044
- it 'can exclude a vendor directory indirectly' do
1045
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
1046
- ['AllCops:',
1047
- ' Excludes:',
1048
- ' - lib/parser/lexer.rb'])
919
+ create_file('.rubocop.yml',
920
+ ['AllCops:',
921
+ ' Excludes:',
922
+ ' - vendor/**'])
1049
923
 
1050
- create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
1051
- ['# encoding: utf-8',
1052
- '#' * 90])
924
+ cli.run(%w(--format simple))
925
+ expect($stdout.string)
926
+ .to eq(['', '0 files inspected, no offences detected',
927
+ ''].join("\n"))
928
+ end
1053
929
 
1054
- create_file('.rubocop.yml',
1055
- ['inherit_from: config/default.yml'])
930
+ # Relative exclude paths in .rubocop.yml files are relative to that file,
931
+ # but in configuration files with other names they will be relative to
932
+ # whatever file inherits from them.
933
+ it 'can exclude a vendor directory indirectly' do
934
+ create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
935
+ ['AllCops:',
936
+ ' Excludes:',
937
+ ' - lib/parser/lexer.rb'])
1056
938
 
1057
- create_file('config/default.yml',
1058
- ['AllCops:',
1059
- ' Excludes:',
1060
- ' - vendor/**'])
939
+ create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
940
+ ['# encoding: utf-8',
941
+ '#' * 90])
1061
942
 
1062
- cli.run(%w(--format simple))
1063
- expect($stdout.string)
1064
- .to eq(['', '0 files inspected, no offences detected',
1065
- ''].join("\n"))
1066
- end
1067
-
1068
- it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
1069
- create_file('example/example1.rb', [
1070
- '# encoding: utf-8',
1071
- '#' * 90
1072
- ])
943
+ create_file('.rubocop.yml',
944
+ ['inherit_from: config/default.yml'])
1073
945
 
1074
- create_file('example/.rubocop.yml', [
1075
- 'LyneLenth:',
1076
- ' Enabled: true',
1077
- ' Max: 100'
1078
- ])
946
+ create_file('config/default.yml',
947
+ ['AllCops:',
948
+ ' Excludes:',
949
+ ' - vendor/**'])
1079
950
 
1080
- expect(cli.run(%w(--format simple example))).to eq(1)
1081
- expect($stdout.string)
1082
- .to eq(
1083
- ['Warning: unrecognized cop LyneLenth found in ' +
1084
- File.expand_path('example/.rubocop.yml'),
1085
- '== example/example1.rb ==',
1086
- 'C: 2: 80: Line is too long. [90/79]',
1087
- '',
1088
- '1 file inspected, 1 offence detected',
1089
- ''].join("\n"))
1090
- end
951
+ cli.run(%w(--format simple))
952
+ expect($stdout.string)
953
+ .to eq(['', '0 files inspected, no offences detected',
954
+ ''].join("\n"))
955
+ end
1091
956
 
1092
- it 'prints a warning for an unrecognized configuration parameter' do
1093
- create_file('example/example1.rb', [
1094
- '# encoding: utf-8',
1095
- '#' * 90
1096
- ])
957
+ it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
958
+ create_file('example/example1.rb', [
959
+ '# encoding: utf-8',
960
+ '#' * 90
961
+ ])
1097
962
 
1098
- create_file('example/.rubocop.yml', [
1099
- 'LineLength:',
1100
- ' Enabled: true',
1101
- ' Min: 10'
1102
- ])
963
+ create_file('example/.rubocop.yml', [
964
+ 'LyneLenth:',
965
+ ' Enabled: true',
966
+ ' Max: 100'
967
+ ])
1103
968
 
1104
- expect(cli.run(%w(--format simple example))).to eq(1)
1105
- expect($stdout.string)
1106
- .to eq(
1107
- ['Warning: unrecognized parameter LineLength:Min found in ' +
1108
- File.expand_path('example/.rubocop.yml'),
1109
- '== example/example1.rb ==',
1110
- 'C: 2: 80: Line is too long. [90/79]',
1111
- '',
1112
- '1 file inspected, 1 offence detected',
1113
- ''].join("\n"))
1114
- end
969
+ expect(cli.run(%w(--format simple example))).to eq(1)
970
+ expect($stdout.string)
971
+ .to eq(
972
+ ['Warning: unrecognized cop LyneLenth found in ' +
973
+ File.expand_path('example/.rubocop.yml'),
974
+ '== example/example1.rb ==',
975
+ 'C: 2: 80: Line is too long. [90/79]',
976
+ '',
977
+ '1 file inspected, 1 offence detected',
978
+ ''].join("\n"))
1115
979
  end
1116
980
 
1117
- unless Rubocop::Version::STRING.start_with?('0')
1118
- describe '-e/--emacs option' do
1119
- it 'is dropped in RuboCop 1.0.0' do
1120
- # This spec can be removed once the option is dropped.
1121
- expect(cli.run(['--emacs'])).to eq(1)
1122
- expect($stderr.string).to include('invalid option: --emacs')
1123
- end
1124
- end
981
+ it 'prints a warning for an unrecognized configuration parameter' do
982
+ create_file('example/example1.rb', [
983
+ '# encoding: utf-8',
984
+ '#' * 90
985
+ ])
1125
986
 
1126
- describe '-s/--silent option' do
1127
- it 'raises error in RuboCop 1.0.0' do
1128
- # This spec can be removed
1129
- # once CLI#ignore_dropped_options is removed.
1130
- expect(cli.run(['--silent'])).to eq(1)
1131
- expect($stderr.string).to include('invalid option: --silent')
1132
- end
1133
- end
987
+ create_file('example/.rubocop.yml', [
988
+ 'LineLength:',
989
+ ' Enabled: true',
990
+ ' Min: 10'
991
+ ])
992
+
993
+ expect(cli.run(%w(--format simple example))).to eq(1)
994
+ expect($stdout.string)
995
+ .to eq(
996
+ ['Warning: unrecognized parameter LineLength:Min found in ' +
997
+ File.expand_path('example/.rubocop.yml'),
998
+ '== example/example1.rb ==',
999
+ 'C: 2: 80: Line is too long. [90/79]',
1000
+ '',
1001
+ '1 file inspected, 1 offence detected',
1002
+ ''].join("\n"))
1134
1003
  end
1004
+ end
1135
1005
 
1136
- describe '#display_error_summary' do
1137
- it 'displays an error message to stderr when errors are present' do
1138
- msg = 'An error occurred while Encoding cop was inspecting file.rb.'
1139
- cli.display_error_summary([msg])
1140
- expect($stderr.string.lines.to_a[-6..-5])
1141
- .to eq(["1 error occurred:\n", "#{msg}\n"])
1142
- end
1006
+ describe '#display_error_summary' do
1007
+ it 'displays an error message to stderr when errors are present' do
1008
+ msg = 'An error occurred while Encoding cop was inspecting file.rb.'
1009
+ cli.display_error_summary([msg])
1010
+ expect($stderr.string.lines.to_a[-6..-5])
1011
+ .to eq(["1 error occurred:\n", "#{msg}\n"])
1143
1012
  end
1144
1013
  end
1145
1014
  end