sabat-rubocop 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (239) hide show
  1. data/.gitignore +50 -0
  2. data/.rspec +1 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +7 -0
  5. data/.yardopts +2 -0
  6. data/CHANGELOG.md +268 -0
  7. data/CONTRIBUTING.md +16 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +324 -0
  11. data/Rakefile +29 -0
  12. data/bin/rubocop +22 -0
  13. data/config/default.yml +58 -0
  14. data/config/disabled.yml +5 -0
  15. data/config/enabled.yml +403 -0
  16. data/lib/rubocop.rb +116 -0
  17. data/lib/rubocop/cli.rb +407 -0
  18. data/lib/rubocop/config.rb +250 -0
  19. data/lib/rubocop/config_store.rb +39 -0
  20. data/lib/rubocop/cop/cop.rb +138 -0
  21. data/lib/rubocop/cop/lint/assignment_in_condition.rb +54 -0
  22. data/lib/rubocop/cop/lint/end_alignment.rb +189 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +30 -0
  24. data/lib/rubocop/cop/lint/ensure_return.rb +22 -0
  25. data/lib/rubocop/cop/lint/eval.rb +22 -0
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +20 -0
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +81 -0
  28. data/lib/rubocop/cop/lint/loop.rb +29 -0
  29. data/lib/rubocop/cop/lint/rescue_exception.rb +29 -0
  30. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +34 -0
  31. data/lib/rubocop/cop/lint/unreachable_code.rb +35 -0
  32. data/lib/rubocop/cop/lint/unused_local_variable.rb +32 -0
  33. data/lib/rubocop/cop/lint/void.rb +58 -0
  34. data/lib/rubocop/cop/offence.rb +136 -0
  35. data/lib/rubocop/cop/rails/validation.rb +30 -0
  36. data/lib/rubocop/cop/style/access_control.rb +58 -0
  37. data/lib/rubocop/cop/style/alias.rb +28 -0
  38. data/lib/rubocop/cop/style/align_parameters.rb +39 -0
  39. data/lib/rubocop/cop/style/and_or.rb +45 -0
  40. data/lib/rubocop/cop/style/ascii_comments.rb +21 -0
  41. data/lib/rubocop/cop/style/ascii_identifiers.rb +22 -0
  42. data/lib/rubocop/cop/style/attr.rb +20 -0
  43. data/lib/rubocop/cop/style/avoid_class_vars.rb +20 -0
  44. data/lib/rubocop/cop/style/avoid_for.rb +18 -0
  45. data/lib/rubocop/cop/style/avoid_global_vars.rb +65 -0
  46. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +21 -0
  47. data/lib/rubocop/cop/style/avoid_perlisms.rb +50 -0
  48. data/lib/rubocop/cop/style/begin_block.rb +18 -0
  49. data/lib/rubocop/cop/style/block_comments.rb +20 -0
  50. data/lib/rubocop/cop/style/block_nesting.rb +47 -0
  51. data/lib/rubocop/cop/style/blocks.rb +27 -0
  52. data/lib/rubocop/cop/style/case_equality.rb +22 -0
  53. data/lib/rubocop/cop/style/case_indentation.rb +28 -0
  54. data/lib/rubocop/cop/style/character_literal.rb +37 -0
  55. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +33 -0
  56. data/lib/rubocop/cop/style/class_methods.rb +22 -0
  57. data/lib/rubocop/cop/style/collection_methods.rb +56 -0
  58. data/lib/rubocop/cop/style/colon_method_call.rb +29 -0
  59. data/lib/rubocop/cop/style/constant_name.rb +31 -0
  60. data/lib/rubocop/cop/style/def_parentheses.rb +70 -0
  61. data/lib/rubocop/cop/style/documentation.rb +58 -0
  62. data/lib/rubocop/cop/style/dot_position.rb +25 -0
  63. data/lib/rubocop/cop/style/empty_line_between_defs.rb +26 -0
  64. data/lib/rubocop/cop/style/empty_lines.rb +40 -0
  65. data/lib/rubocop/cop/style/empty_literal.rb +53 -0
  66. data/lib/rubocop/cop/style/encoding.rb +29 -0
  67. data/lib/rubocop/cop/style/end_block.rb +18 -0
  68. data/lib/rubocop/cop/style/end_of_line.rb +23 -0
  69. data/lib/rubocop/cop/style/favor_join.rb +29 -0
  70. data/lib/rubocop/cop/style/favor_modifier.rb +118 -0
  71. data/lib/rubocop/cop/style/favor_sprintf.rb +28 -0
  72. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +54 -0
  73. data/lib/rubocop/cop/style/hash_syntax.rb +47 -0
  74. data/lib/rubocop/cop/style/if_then_else.rb +29 -0
  75. data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -0
  76. data/lib/rubocop/cop/style/lambda.rb +47 -0
  77. data/lib/rubocop/cop/style/leading_comment_space.rb +25 -0
  78. data/lib/rubocop/cop/style/line_continuation.rb +26 -0
  79. data/lib/rubocop/cop/style/line_length.rb +30 -0
  80. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +61 -0
  81. data/lib/rubocop/cop/style/method_call_parentheses.rb +22 -0
  82. data/lib/rubocop/cop/style/method_length.rb +57 -0
  83. data/lib/rubocop/cop/style/multiline_if_then.rb +47 -0
  84. data/lib/rubocop/cop/style/not.rb +24 -0
  85. data/lib/rubocop/cop/style/numeric_literals.rb +25 -0
  86. data/lib/rubocop/cop/style/one_line_conditional.rb +20 -0
  87. data/lib/rubocop/cop/style/op_method.rb +29 -0
  88. data/lib/rubocop/cop/style/parameter_lists.rb +42 -0
  89. data/lib/rubocop/cop/style/parentheses_around_condition.rb +42 -0
  90. data/lib/rubocop/cop/style/proc.rb +30 -0
  91. data/lib/rubocop/cop/style/reduce_arguments.rb +34 -0
  92. data/lib/rubocop/cop/style/regexp_literal.rb +39 -0
  93. data/lib/rubocop/cop/style/rescue_modifier.rb +55 -0
  94. data/lib/rubocop/cop/style/semicolon.rb +51 -0
  95. data/lib/rubocop/cop/style/single_line_methods.rb +48 -0
  96. data/lib/rubocop/cop/style/space_after_comma_etc.rb +69 -0
  97. data/lib/rubocop/cop/style/space_after_control_keyword.rb +32 -0
  98. data/lib/rubocop/cop/style/string_literals.rb +36 -0
  99. data/lib/rubocop/cop/style/surrounding_space.rb +314 -0
  100. data/lib/rubocop/cop/style/symbol_array.rb +31 -0
  101. data/lib/rubocop/cop/style/symbol_name.rb +27 -0
  102. data/lib/rubocop/cop/style/tab.rb +25 -0
  103. data/lib/rubocop/cop/style/ternary_operator.rb +49 -0
  104. data/lib/rubocop/cop/style/trailing_whitespace.rb +24 -0
  105. data/lib/rubocop/cop/style/trivial_accessors.rb +32 -0
  106. data/lib/rubocop/cop/style/unless_else.rb +26 -0
  107. data/lib/rubocop/cop/style/variable_interpolation.rb +32 -0
  108. data/lib/rubocop/cop/style/when_then.rb +25 -0
  109. data/lib/rubocop/cop/style/while_until_do.rb +45 -0
  110. data/lib/rubocop/cop/style/word_array.rb +44 -0
  111. data/lib/rubocop/cop/util.rb +27 -0
  112. data/lib/rubocop/cop/variable_inspector.rb +280 -0
  113. data/lib/rubocop/formatter/base_formatter.rb +119 -0
  114. data/lib/rubocop/formatter/clang_style_formatter.rb +21 -0
  115. data/lib/rubocop/formatter/emacs_style_formatter.rb +17 -0
  116. data/lib/rubocop/formatter/formatter_set.rb +77 -0
  117. data/lib/rubocop/formatter/json_formatter.rb +76 -0
  118. data/lib/rubocop/formatter/progress_formatter.rb +63 -0
  119. data/lib/rubocop/formatter/simple_text_formatter.rb +62 -0
  120. data/lib/rubocop/version.rb +21 -0
  121. data/rubocop.gemspec +36 -0
  122. data/spec/.rubocop.yml +5 -0
  123. data/spec/project_spec.rb +24 -0
  124. data/spec/rubocop/cli_spec.rb +906 -0
  125. data/spec/rubocop/config_spec.rb +470 -0
  126. data/spec/rubocop/config_store_spec.rb +66 -0
  127. data/spec/rubocop/cops/cop_spec.rb +38 -0
  128. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +111 -0
  129. data/spec/rubocop/cops/lint/end_alignment_spec.rb +333 -0
  130. data/spec/rubocop/cops/lint/end_in_method_spec.rb +35 -0
  131. data/spec/rubocop/cops/lint/ensure_return_spec.rb +37 -0
  132. data/spec/rubocop/cops/lint/eval_spec.rb +41 -0
  133. data/spec/rubocop/cops/lint/handle_exceptions_spec.rb +36 -0
  134. data/spec/rubocop/cops/lint/literal_in_condition_spec.rb +42 -0
  135. data/spec/rubocop/cops/lint/loop_spec.rb +33 -0
  136. data/spec/rubocop/cops/lint/rescue_exception_spec.rb +127 -0
  137. data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +243 -0
  138. data/spec/rubocop/cops/lint/unreachable_code_spec.rb +69 -0
  139. data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +497 -0
  140. data/spec/rubocop/cops/lint/void_spec.rb +63 -0
  141. data/spec/rubocop/cops/offence_spec.rb +133 -0
  142. data/spec/rubocop/cops/rails/validation_spec.rb +27 -0
  143. data/spec/rubocop/cops/style/access_control_spec.rb +142 -0
  144. data/spec/rubocop/cops/style/alias_spec.rb +47 -0
  145. data/spec/rubocop/cops/style/align_parameters_spec.rb +199 -0
  146. data/spec/rubocop/cops/style/and_or_spec.rb +39 -0
  147. data/spec/rubocop/cops/style/ascii_comments_spec.rb +28 -0
  148. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +28 -0
  149. data/spec/rubocop/cops/style/attr_spec.rb +20 -0
  150. data/spec/rubocop/cops/style/avoid_class_vars_spec.rb +27 -0
  151. data/spec/rubocop/cops/style/avoid_for_spec.rb +37 -0
  152. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +34 -0
  153. data/spec/rubocop/cops/style/avoid_perl_backrefs_spec.rb +20 -0
  154. data/spec/rubocop/cops/style/avoid_perlisms_spec.rb +47 -0
  155. data/spec/rubocop/cops/style/begin_block_spec.rb +19 -0
  156. data/spec/rubocop/cops/style/block_comments_spec.rb +27 -0
  157. data/spec/rubocop/cops/style/block_nesting_spec.rb +159 -0
  158. data/spec/rubocop/cops/style/blocks_spec.rb +35 -0
  159. data/spec/rubocop/cops/style/case_equality_spec.rb +18 -0
  160. data/spec/rubocop/cops/style/case_indentation_spec.rb +88 -0
  161. data/spec/rubocop/cops/style/character_literal_spec.rb +28 -0
  162. data/spec/rubocop/cops/style/class_and_module_camel_case_spec.rb +46 -0
  163. data/spec/rubocop/cops/style/class_methods_spec.rb +51 -0
  164. data/spec/rubocop/cops/style/collection_methods_spec.rb +41 -0
  165. data/spec/rubocop/cops/style/colon_method_call_spec.rb +55 -0
  166. data/spec/rubocop/cops/style/constant_name_spec.rb +56 -0
  167. data/spec/rubocop/cops/style/def_with_parentheses_spec.rb +40 -0
  168. data/spec/rubocop/cops/style/def_without_parentheses_spec.rb +34 -0
  169. data/spec/rubocop/cops/style/documentation_spec.rb +79 -0
  170. data/spec/rubocop/cops/style/dot_position_spec.rb +30 -0
  171. data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +85 -0
  172. data/spec/rubocop/cops/style/empty_lines_spec.rb +40 -0
  173. data/spec/rubocop/cops/style/empty_literal_spec.rb +91 -0
  174. data/spec/rubocop/cops/style/encoding_spec.rb +49 -0
  175. data/spec/rubocop/cops/style/end_block_spec.rb +19 -0
  176. data/spec/rubocop/cops/style/end_of_line_spec.rb +25 -0
  177. data/spec/rubocop/cops/style/favor_join_spec.rb +37 -0
  178. data/spec/rubocop/cops/style/favor_modifier_spec.rb +160 -0
  179. data/spec/rubocop/cops/style/favor_sprintf_spec.rb +53 -0
  180. data/spec/rubocop/cops/style/favor_unless_over_negated_if_spec.rb +64 -0
  181. data/spec/rubocop/cops/style/favor_until_over_negated_while_spec.rb +47 -0
  182. data/spec/rubocop/cops/style/hash_syntax_spec.rb +51 -0
  183. data/spec/rubocop/cops/style/if_with_semicolon_spec.rb +25 -0
  184. data/spec/rubocop/cops/style/lambda_spec.rb +45 -0
  185. data/spec/rubocop/cops/style/leading_comment_space_spec.rb +65 -0
  186. data/spec/rubocop/cops/style/line_continuation_spec.rb +26 -0
  187. data/spec/rubocop/cops/style/line_length_spec.rb +25 -0
  188. data/spec/rubocop/cops/style/method_and_variable_snake_case_spec.rb +95 -0
  189. data/spec/rubocop/cops/style/method_call_parentheses_spec.rb +25 -0
  190. data/spec/rubocop/cops/style/method_length_spec.rb +151 -0
  191. data/spec/rubocop/cops/style/multiline_if_then_spec.rb +97 -0
  192. data/spec/rubocop/cops/style/not_spec.rb +28 -0
  193. data/spec/rubocop/cops/style/numeric_literals_spec.rb +51 -0
  194. data/spec/rubocop/cops/style/one_line_conditional_spec.rb +18 -0
  195. data/spec/rubocop/cops/style/op_method_spec.rb +80 -0
  196. data/spec/rubocop/cops/style/parameter_lists_spec.rb +49 -0
  197. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +59 -0
  198. data/spec/rubocop/cops/style/proc_spec.rb +28 -0
  199. data/spec/rubocop/cops/style/reduce_arguments_spec.rb +59 -0
  200. data/spec/rubocop/cops/style/regexp_literal_spec.rb +83 -0
  201. data/spec/rubocop/cops/style/rescue_modifier_spec.rb +122 -0
  202. data/spec/rubocop/cops/style/semicolon_spec.rb +95 -0
  203. data/spec/rubocop/cops/style/single_line_methods_spec.rb +54 -0
  204. data/spec/rubocop/cops/style/space_after_colon_spec.rb +29 -0
  205. data/spec/rubocop/cops/style/space_after_comma_spec.rb +31 -0
  206. data/spec/rubocop/cops/style/space_after_control_keyword_spec.rb +69 -0
  207. data/spec/rubocop/cops/style/space_after_semicolon_spec.rb +24 -0
  208. data/spec/rubocop/cops/style/space_around_braces_spec.rb +49 -0
  209. data/spec/rubocop/cops/style/space_around_equals_in_default_parameter_spec.rb +34 -0
  210. data/spec/rubocop/cops/style/space_around_operators_spec.rb +216 -0
  211. data/spec/rubocop/cops/style/space_inside_brackets_spec.rb +51 -0
  212. data/spec/rubocop/cops/style/space_inside_hash_literal_braces_spec.rb +99 -0
  213. data/spec/rubocop/cops/style/space_inside_parens_spec.rb +33 -0
  214. data/spec/rubocop/cops/style/string_literals_spec.rb +62 -0
  215. data/spec/rubocop/cops/style/symbol_array_spec.rb +45 -0
  216. data/spec/rubocop/cops/style/symbol_name_spec.rb +122 -0
  217. data/spec/rubocop/cops/style/tab_spec.rb +23 -0
  218. data/spec/rubocop/cops/style/ternary_operator_spec.rb +42 -0
  219. data/spec/rubocop/cops/style/trailing_whitespace_spec.rb +29 -0
  220. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +338 -0
  221. data/spec/rubocop/cops/style/unless_else_spec.rb +31 -0
  222. data/spec/rubocop/cops/style/variable_interpolation_spec.rb +53 -0
  223. data/spec/rubocop/cops/style/when_then_spec.rb +40 -0
  224. data/spec/rubocop/cops/style/while_until_do_spec.rb +47 -0
  225. data/spec/rubocop/cops/style/word_array_spec.rb +61 -0
  226. data/spec/rubocop/cops/variable_inspector_spec.rb +374 -0
  227. data/spec/rubocop/formatter/base_formatter_spec.rb +190 -0
  228. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +70 -0
  229. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +32 -0
  230. data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
  231. data/spec/rubocop/formatter/json_formatter_spec.rb +142 -0
  232. data/spec/rubocop/formatter/progress_formatter_spec.rb +196 -0
  233. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +74 -0
  234. data/spec/spec_helper.rb +92 -0
  235. data/spec/support/file_helper.rb +21 -0
  236. data/spec/support/isolated_environment.rb +27 -0
  237. data/spec/support/mri_syntax_checker.rb +69 -0
  238. data/spec/support/shared_examples.rb +33 -0
  239. metadata +517 -0
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
4
+ require 'rubocop/version'
5
+ require 'English'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'sabat-rubocop'
9
+ s.version = Rubocop::Version::STRING
10
+ s.platform = Gem::Platform::RUBY
11
+ s.required_ruby_version = '>= 1.9.2'
12
+ s.authors = ['Bozhidar Batsov']
13
+ s.description = <<-EOF
14
+ Automatic Ruby code style checking tool.
15
+ Aims to enforce the community-driven Ruby Style Guide.
16
+ EOF
17
+
18
+ s.email = 'bozhidar@batsov.com'
19
+ s.files = `git ls-files`.split($RS)
20
+ s.test_files = s.files.grep(/^spec\//)
21
+ s.executables = s.files.grep(/^bin\//) { |f| File.basename(f) }
22
+ s.extra_rdoc_files = ['LICENSE.txt', 'README.md']
23
+ s.homepage = 'http://github.com/sabat/rubocop'
24
+ s.licenses = ['MIT']
25
+ s.require_paths = ['lib']
26
+ s.rubygems_version = '1.8.23'
27
+ s.summary = 'Automatic Ruby code style checking tool.'
28
+
29
+ s.add_runtime_dependency('rainbow', '>= 1.1.4')
30
+ s.add_runtime_dependency('parser', '2.0.0.beta9')
31
+ s.add_development_dependency('rake', '~> 10.0')
32
+ s.add_development_dependency('rspec', '~> 2.13')
33
+ s.add_development_dependency('yard', '~> 0.8')
34
+ s.add_development_dependency('bundler', '~> 1.3')
35
+ s.add_development_dependency('simplecov', '~> 0.7')
36
+ end
@@ -0,0 +1,5 @@
1
+ inherit_from: ../.rubocop.yml
2
+
3
+ # The documentation check doesn't make sense for test code
4
+ Documentation:
5
+ Enabled: false
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'RuboCop Project' do
6
+ describe 'default configuration file' do
7
+ it 'has configuration for all cops' do
8
+ cop_names = Rubocop::Cop::Cop.all.map(&:cop_name)
9
+ expect(Rubocop::Config.load_file('config/default.yml').keys.sort)
10
+ .to eq((['AllCops'] + cop_names).sort)
11
+ end
12
+ end
13
+
14
+ describe 'source codes' do
15
+ before { $stdout = StringIO.new }
16
+ after { $stdout = STDOUT }
17
+
18
+ it 'has no violations' do
19
+ # Need to pass an empty array explicitly
20
+ # so that the CLI does not refer arguments of `rspec`
21
+ expect(Rubocop::CLI.new.run([])).to eq(0)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,906 @@
1
+ # encoding: utf-8
2
+
3
+ require 'fileutils'
4
+ require 'tmpdir'
5
+ require 'spec_helper'
6
+
7
+ module Rubocop
8
+ describe CLI, :isolated_environment do
9
+ include FileHelper
10
+
11
+ let(:cli) { CLI.new }
12
+
13
+ before(:each) do
14
+ $stdout = StringIO.new
15
+ $stderr = StringIO.new
16
+ end
17
+
18
+ after(:each) do
19
+ $stdout = STDOUT
20
+ $stderr = STDERR
21
+ end
22
+
23
+ def abs(path)
24
+ File.expand_path(path)
25
+ end
26
+
27
+ it 'exits cleanly when -h is used' do
28
+ expect { cli.run ['-h'] }.to exit_with_code(0)
29
+ expect { cli.run ['--help'] }.to exit_with_code(0)
30
+ message = <<-END
31
+ Usage: rubocop [options] [file1, file2, ...]
32
+ -d, --debug Display debug info.
33
+ -L, --list-cops List class name of cops.
34
+ -c, --config FILE Specify configuration file.
35
+ --only COP Run just one cop.
36
+ -f, --format FORMATTER Choose an output formatter. This option
37
+ can be specified multiple times to enable
38
+ multiple formatters at the same time.
39
+ [p]rogress (default)
40
+ [s]imple
41
+ [c]lang
42
+ [e]macs
43
+ [j]son
44
+ custom formatter class name
45
+ -o, --out FILE Write output to a file instead of STDOUT.
46
+ This option applies to the previously
47
+ specified --format, or the default format
48
+ if no format is specified.
49
+ -r, --require FILE Require Ruby file.
50
+ -R, --rails Run extra Rails cops.
51
+ -l, --lint Run only lint cops.
52
+ -a, --auto-correct Auto-correct offences.
53
+ -s, --silent Silence summary.
54
+ -n, --no-color Disable color output.
55
+ -v, --version Display version.
56
+ -V, --verbose-version Display verbose version.
57
+ END
58
+ expect($stdout.string).to eq(message * 2)
59
+ end
60
+
61
+ it 'exits cleanly when -v is used' do
62
+ expect { cli.run ['-v'] }.to exit_with_code(0)
63
+ expect { cli.run ['--version'] }.to exit_with_code(0)
64
+ expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
65
+ end
66
+
67
+ describe '#wants_to_quit?' do
68
+ it 'is initially false' do
69
+ expect(cli.wants_to_quit?).to be_false
70
+ end
71
+ end
72
+
73
+ describe '#trap_interrupt' do
74
+ before do
75
+ @interrupt_handlers = []
76
+ Signal.stub(:trap).with('INT') do |&block|
77
+ @interrupt_handlers << block
78
+ end
79
+ end
80
+
81
+ def interrupt
82
+ @interrupt_handlers.each(&:call)
83
+ end
84
+
85
+ it 'adds a handler for SIGINT' do
86
+ expect(@interrupt_handlers).to be_empty
87
+ cli.trap_interrupt
88
+ expect(@interrupt_handlers).to have(1).item
89
+ end
90
+
91
+ context 'with SIGINT once' do
92
+ it 'sets #wants_to_quit? to true' do
93
+ cli.trap_interrupt
94
+ expect(cli.wants_to_quit?).to be_false
95
+ interrupt
96
+ expect(cli.wants_to_quit?).to be_true
97
+ end
98
+
99
+ it 'does not exit immediately' do
100
+ Object.any_instance.should_not_receive(:exit)
101
+ Object.any_instance.should_not_receive(:exit!)
102
+ cli.trap_interrupt
103
+ interrupt
104
+ end
105
+ end
106
+
107
+ context 'with SIGINT twice' do
108
+ it 'exits immediately' do
109
+ Object.any_instance.should_receive(:exit!).with(1)
110
+ cli.trap_interrupt
111
+ interrupt
112
+ interrupt
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'when #wants_to_quit? is true' do
118
+ it 'returns 1' do
119
+ create_file('example.rb', '# encoding: utf-8')
120
+ cli.wants_to_quit = true
121
+ expect(cli.run(['example.rb'])).to eq(1)
122
+ end
123
+ end
124
+
125
+ it 'checks a given correct file and returns 0' do
126
+ create_file('example.rb', [
127
+ '# encoding: utf-8',
128
+ 'x = 0',
129
+ 'puts x'
130
+ ])
131
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
132
+ expect($stdout.string)
133
+ .to eq("\n1 file inspected, no offences detected\n")
134
+ end
135
+
136
+ it 'checks a given file with faults and returns 1' do
137
+ create_file('example.rb', [
138
+ '# encoding: utf-8',
139
+ 'x = 0 ',
140
+ 'puts x'
141
+ ])
142
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
143
+ expect($stdout.string)
144
+ .to eq ['== example.rb ==',
145
+ 'C: 2: 6: Trailing whitespace detected.',
146
+ '',
147
+ '1 file inspected, 1 offence detected',
148
+ ''].join("\n")
149
+ end
150
+
151
+ it 'can report in emacs style', ruby: 1.9 do
152
+ create_file('example1.rb', [
153
+ 'x= 0 ',
154
+ 'y ',
155
+ 'puts x'
156
+ ])
157
+ create_file('example2.rb', [
158
+ "\tx = 0",
159
+ 'puts x'
160
+ ])
161
+ expect(cli.run(['--format', 'emacs', 'example1.rb', 'example2.rb']))
162
+ .to eq(1)
163
+ expect($stdout.string)
164
+ .to eq(
165
+ ["#{abs('example1.rb')}:1:1: C: Missing utf-8 encoding comment.",
166
+ "#{abs('example1.rb')}:1:2: C: Surrounding space missing" +
167
+ " for operator '='.",
168
+ "#{abs('example1.rb')}:1:5: C: Trailing whitespace detected.",
169
+ "#{abs('example1.rb')}:2:2: C: Trailing whitespace detected.",
170
+ "#{abs('example2.rb')}:1:1: C: Missing utf-8 encoding comment.",
171
+ "#{abs('example2.rb')}:1:1: C: Tab detected.",
172
+ '',
173
+ '2 files inspected, 6 offences detected',
174
+ ''].join("\n"))
175
+ end
176
+
177
+ it 'can report in emacs style', ruby: 2.0 do
178
+ create_file('example1.rb', [
179
+ 'x= 0 ',
180
+ 'y ',
181
+ 'puts x'
182
+ ])
183
+ create_file('example2.rb', [
184
+ "\tx = 0",
185
+ 'puts x'
186
+ ])
187
+ expect(cli.run(['--format', 'emacs', 'example1.rb', 'example2.rb']))
188
+ .to eq(1)
189
+ expect($stdout.string)
190
+ .to eq(
191
+ ["#{abs('example1.rb')}:1:2: C: Surrounding space missing" +
192
+ " for operator '='.",
193
+ "#{abs('example1.rb')}:1:5: C: Trailing whitespace detected.",
194
+ "#{abs('example1.rb')}:2:2: C: Trailing whitespace detected.",
195
+ "#{abs('example2.rb')}:1:1: C: Tab detected.",
196
+ '',
197
+ '2 files inspected, 4 offences detected',
198
+ ''].join("\n"))
199
+ end
200
+
201
+ it 'can report in clang style' do
202
+ create_file('example1.rb', ['# encoding: utf-8',
203
+ 'x= 0 ',
204
+ '#' * 85,
205
+ 'y ',
206
+ 'puts x'])
207
+ create_file('example2.rb', ['# encoding: utf-8',
208
+ "\tx = 0",
209
+ 'puts x'])
210
+ expect(cli.run(['--format', 'clang', 'example1.rb', 'example2.rb']))
211
+ .to eq(1)
212
+ expect($stdout.string)
213
+ .to eq(['example1.rb:2:2: C: Surrounding space missing for operator ' +
214
+ "'='.",
215
+ 'x= 0 ',
216
+ ' ^',
217
+ 'example1.rb:2:5: C: Trailing whitespace detected.',
218
+ 'x= 0 ',
219
+ ' ^',
220
+ 'example1.rb:3:80: C: Line is too long. [85/79]',
221
+ '###########################################################' +
222
+ '##########################',
223
+ ' ' +
224
+ ' ^^^^^^',
225
+ 'example1.rb:4:2: C: Trailing whitespace detected.',
226
+ 'y ',
227
+ ' ^',
228
+ 'example2.rb:2:1: C: Tab detected.',
229
+ "\tx = 0",
230
+ '^',
231
+ '',
232
+ '2 files inspected, 5 offences detected',
233
+ ''].join("\n"))
234
+ end
235
+
236
+ it 'runs just one cop if --only is passed' do
237
+ create_file('example.rb', ['if x== 0 ',
238
+ "\ty",
239
+ 'end'])
240
+ # IfUnlessModifier depends on the configuration of LineLength.
241
+ # That configuration might have been set by other spec examples
242
+ # so we reset it to emulate a start from scratch.
243
+ Cop::Style::LineLength.config = nil
244
+
245
+ expect(cli.run(['--format', 'simple',
246
+ '--only', 'IfUnlessModifier', 'example.rb'])).to eq(1)
247
+ expect($stdout.string)
248
+ .to eq(['== example.rb ==',
249
+ 'C: 1: 1: Favor modifier if/unless usage when you have a ' +
250
+ 'single-line body. Another good alternative is the usage of ' +
251
+ 'control flow &&/||.',
252
+ '',
253
+ '1 file inspected, 1 offence detected',
254
+ ''].join("\n"))
255
+ end
256
+
257
+ it 'runs only lint cops if --lint is passed' do
258
+ # FIXME state seems to be leaking here
259
+ pending
260
+ create_file('example.rb', ['if 0 ',
261
+ "\ty",
262
+ 'end'])
263
+ # IfUnlessModifier depends on the configuration of LineLength.
264
+ # That configuration might have been set by other spec examples
265
+ # so we reset it to emulate a start from scratch.
266
+ Cop::Style::LineLength.config = nil
267
+
268
+ expect(cli.run(['--format', 'simple', '--lint', 'example.rb'])).to eq(1)
269
+ expect($stdout.string)
270
+ .to eq(['== example.rb ==',
271
+ 'W: 1: 4: Literal 0 appeared in a condition.',
272
+ '',
273
+ '1 file inspected, 1 offence detected',
274
+ ''].join("\n"))
275
+
276
+ end
277
+
278
+ it 'exits with error if an incorrect cop name is passed to --only' do
279
+ expect(cli.run(%w(--only 123))).to eq(1)
280
+
281
+ expect($stderr.string).to eq("Unrecognized cop name: 123.\n")
282
+ end
283
+
284
+ it 'ommits summary when --silent passed', ruby: 1.9 do
285
+ create_file('example1.rb', 'puts 0 ')
286
+ create_file('example2.rb', "\tputs 0")
287
+ expect(cli.run(['--format',
288
+ 'emacs',
289
+ '--silent',
290
+ 'example1.rb',
291
+ 'example2.rb'])).to eq(1)
292
+ expect($stdout.string).to eq(
293
+ ["#{abs('example1.rb')}:1:1: C: Missing utf-8 encoding comment.",
294
+ "#{abs('example1.rb')}:1:7: C: Trailing whitespace detected.",
295
+ "#{abs('example2.rb')}:1:1: C: Missing utf-8 encoding comment.",
296
+ "#{abs('example2.rb')}:1:1: C: Tab detected.",
297
+ ''].join("\n"))
298
+ end
299
+
300
+ it 'ommits summary when --silent passed', ruby: 2.0 do
301
+ create_file('example1.rb', 'puts 0 ')
302
+ create_file('example2.rb', "\tputs 0")
303
+ expect(cli.run(['--format',
304
+ 'emacs',
305
+ '--silent',
306
+ 'example1.rb',
307
+ 'example2.rb'])).to eq(1)
308
+ expect($stdout.string).to eq(
309
+ ["#{abs('example1.rb')}:1:7: C: Trailing whitespace detected.",
310
+ "#{abs('example2.rb')}:1:1: C: Tab detected.",
311
+ ''].join("\n"))
312
+ end
313
+
314
+ it 'shows cop names when --debug is passed', ruby: 2.0 do
315
+ create_file('example1.rb', "\tputs 0")
316
+ expect(cli.run(['--format',
317
+ 'emacs',
318
+ '--silent',
319
+ '--debug',
320
+ 'example1.rb'])).to eq(1)
321
+ expect($stdout.string.lines[-1]).to eq(
322
+ ["#{abs('example1.rb')}:1:1: C: Tab: Tab detected.",
323
+ ''].join("\n"))
324
+ end
325
+
326
+ it 'shows cop names when --list-cops is passed', ruby: 2.0 do
327
+ create_file('example1.rb', "\tputs 0")
328
+ expect(cli.run(['--format',
329
+ 'emacs',
330
+ '--silent',
331
+ '--list-cops',
332
+ 'example1.rb'])).to eq(1)
333
+ expect($stdout.string.lines[-1]).to eq(
334
+ ["#{abs('example1.rb')}:1:1: C: Tab: Tab detected.",
335
+ ''].join("\n"))
336
+ end
337
+
338
+ it 'can be configured with option to disable a certain error' do
339
+ create_file('example1.rb', 'puts 0 ')
340
+ create_file('rubocop.yml', [
341
+ 'Encoding:',
342
+ ' Enabled: false',
343
+ '',
344
+ 'CaseIndentation:',
345
+ ' Enabled: false'
346
+ ])
347
+ expect(cli.run(['--format', 'simple',
348
+ '-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
349
+ expect($stdout.string).to eq(
350
+ ['== example1.rb ==',
351
+ 'C: 1: 7: Trailing whitespace detected.',
352
+ '',
353
+ '1 file inspected, 1 offence detected',
354
+ ''].join("\n"))
355
+ end
356
+
357
+ it 'works when a cop that others depend on is disabled' do
358
+ create_file('example1.rb', ['if a',
359
+ ' b',
360
+ 'end'])
361
+ create_file('rubocop.yml', [
362
+ 'Encoding:',
363
+ ' Enabled: false',
364
+ '',
365
+ 'LineLength:',
366
+ ' Enabled: false'
367
+ ])
368
+ result = cli.run(['--format', 'simple',
369
+ '-c', 'rubocop.yml', 'example1.rb'])
370
+ expect($stdout.string).to eq(
371
+ ['== example1.rb ==',
372
+ 'C: 1: 1: Favor modifier if/unless usage when you have a ' +
373
+ 'single-line body. Another good alternative is the usage of ' +
374
+ 'control flow &&/||.',
375
+ '',
376
+ '1 file inspected, 1 offence detected',
377
+ ''].join("\n"))
378
+ expect(result).to eq(1)
379
+ end
380
+
381
+ it 'can be configured with project config to disable a certain error' do
382
+ create_file('example_src/example1.rb', 'puts 0 ')
383
+ create_file('example_src/.rubocop.yml', [
384
+ 'Encoding:',
385
+ ' Enabled: false',
386
+ '',
387
+ 'CaseIndentation:',
388
+ ' Enabled: false'
389
+ ])
390
+ expect(cli.run(['--format', 'simple',
391
+ 'example_src/example1.rb'])).to eq(1)
392
+ expect($stdout.string).to eq(
393
+ ['== example_src/example1.rb ==',
394
+ 'C: 1: 7: Trailing whitespace detected.',
395
+ '',
396
+ '1 file inspected, 1 offence detected',
397
+ ''].join("\n"))
398
+ end
399
+
400
+ it 'can use an alternative max line length from a config file' do
401
+ create_file('example_src/example1.rb', [
402
+ '# encoding: utf-8',
403
+ '#' * 90
404
+ ])
405
+ create_file('example_src/.rubocop.yml', [
406
+ 'LineLength:',
407
+ ' Enabled: true',
408
+ ' Max: 100'
409
+ ])
410
+ expect(cli.run(['--format', 'simple',
411
+ 'example_src/example1.rb'])).to eq(0)
412
+ expect($stdout.string).to eq(
413
+ ['', '1 file inspected, no offences detected',
414
+ ''].join("\n"))
415
+ end
416
+
417
+ it 'can have different config files in different directories' do
418
+ %w(src lib).each do |dir|
419
+ create_file("example/#{dir}/example1.rb", [
420
+ '# encoding: utf-8',
421
+ '#' * 90
422
+ ])
423
+ end
424
+ create_file('example/src/.rubocop.yml', [
425
+ 'LineLength:',
426
+ ' Enabled: true',
427
+ ' Max: 100'
428
+ ])
429
+ expect(cli.run(%w(--format simple example))).to eq(1)
430
+ expect($stdout.string).to eq(
431
+ ['== example/lib/example1.rb ==',
432
+ 'C: 2: 80: Line is too long. [90/79]',
433
+ '',
434
+ '2 files inspected, 1 offence detected',
435
+ ''].join("\n"))
436
+ end
437
+
438
+ it 'prefers a config file in ancestor directory to another in home' do
439
+ create_file('example_src/example1.rb', [
440
+ '# encoding: utf-8',
441
+ '#' * 90
442
+ ])
443
+ create_file('example_src/.rubocop.yml', [
444
+ 'LineLength:',
445
+ ' Enabled: true',
446
+ ' Max: 100'
447
+ ])
448
+ create_file("#{Dir.home}/.rubocop.yml", [
449
+ 'LineLength:',
450
+ ' Enabled: true',
451
+ ' Max: 80'
452
+ ])
453
+ expect(cli.run(['--format', 'simple',
454
+ 'example_src/example1.rb'])).to eq(0)
455
+ expect($stdout.string).to eq(
456
+ ['', '1 file inspected, no offences detected',
457
+ ''].join("\n"))
458
+ end
459
+
460
+ it 'can exclude directories relative to .rubocop.yml' do
461
+ %w(src etc/test etc/spec tmp/test tmp/spec).each do |dir|
462
+ create_file("example/#{dir}/example1.rb", [
463
+ '# encoding: utf-8',
464
+ '#' * 90
465
+ ])
466
+ end
467
+
468
+ create_file('example/.rubocop.yml', [
469
+ 'AllCops:',
470
+ ' Excludes:',
471
+ ' - src/**',
472
+ ' - etc/**',
473
+ ' - tmp/spec/**'
474
+ ])
475
+
476
+ expect(cli.run(%w(--format simple example))).to eq(1)
477
+ expect($stdout.string).to eq(
478
+ ['== example/tmp/test/example1.rb ==',
479
+ 'C: 2: 80: Line is too long. [90/79]',
480
+ '',
481
+ '1 file inspected, 1 offence detected',
482
+ ''].join("\n"))
483
+ end
484
+
485
+ it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
486
+ create_file('example/example1.rb', [
487
+ '# encoding: utf-8',
488
+ '#' * 90
489
+ ])
490
+
491
+ create_file('example/.rubocop.yml', [
492
+ 'LyneLenth:',
493
+ ' Enabled: true',
494
+ ' Max: 100'
495
+ ])
496
+
497
+ expect(cli.run(%w(--format simple example))).to eq(1)
498
+ expect($stdout.string).to eq(
499
+ ['Warning: unrecognized cop LyneLenth found in ' +
500
+ File.expand_path('example/.rubocop.yml'),
501
+ '== example/example1.rb ==',
502
+ 'C: 2: 80: Line is too long. [90/79]',
503
+ '',
504
+ '1 file inspected, 1 offence detected',
505
+ ''].join("\n"))
506
+ end
507
+
508
+ it 'prints a warning for an unrecognized configuration parameter' do
509
+ create_file('example/example1.rb', [
510
+ '# encoding: utf-8',
511
+ '#' * 90
512
+ ])
513
+
514
+ create_file('example/.rubocop.yml', [
515
+ 'LineLength:',
516
+ ' Enabled: true',
517
+ ' Min: 10'
518
+ ])
519
+
520
+ expect(cli.run(%w(--format simple example))).to eq(1)
521
+ expect($stdout.string).to eq(
522
+ ['Warning: unrecognized parameter LineLength:Min found in ' +
523
+ File.expand_path('example/.rubocop.yml'),
524
+ '== example/example1.rb ==',
525
+ 'C: 2: 80: Line is too long. [90/79]',
526
+ '',
527
+ '1 file inspected, 1 offence detected',
528
+ ''].join("\n"))
529
+ end
530
+
531
+ it 'registers an offence for a syntax error' do
532
+ create_file('example.rb', [
533
+ '# encoding: utf-8',
534
+ 'class Test',
535
+ 'en'
536
+ ])
537
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
538
+ expect($stdout.string)
539
+ .to eq(["#{abs('example.rb')}:3:3: E: unexpected " +
540
+ 'token $end',
541
+ '',
542
+ '1 file inspected, 1 offence detected',
543
+ ''].join("\n"))
544
+ end
545
+
546
+ it 'registers an offence for Parser warnings' do
547
+ create_file('example.rb', [
548
+ '# encoding: utf-8',
549
+ 'puts *test'
550
+ ])
551
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
552
+ expect($stdout.string)
553
+ .to eq(["#{abs('example.rb')}:2:6: W: " +
554
+ "`*' interpreted as argument prefix",
555
+ '',
556
+ '1 file inspected, 1 offence detected',
557
+ ''].join("\n"))
558
+ end
559
+
560
+ it 'can process a file with an invalid UTF-8 byte sequence' do
561
+ create_file('example.rb', [
562
+ '# encoding: utf-8',
563
+ "# #{'f9'.hex.chr}#{'29'.hex.chr}"
564
+ ])
565
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
566
+ end
567
+
568
+ it 'can have all cops disabled in a code section' do
569
+ create_file('example.rb', [
570
+ '# encoding: utf-8',
571
+ '# rubocop:disable all',
572
+ '#' * 90,
573
+ 'x(123456)',
574
+ 'y("123")',
575
+ 'def func',
576
+ ' # rubocop: enable LineLength, StringLiterals',
577
+ ' ' + '#' * 93,
578
+ ' x(123456)',
579
+ ' y("123")',
580
+ 'end'
581
+ ])
582
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
583
+ # all cops were disabled, then 2 were enabled again, so we
584
+ # should get 2 offences reported.
585
+ expect($stdout.string).to eq(
586
+ ["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
587
+ "#{abs('example.rb')}:10:5: C: Prefer single-quoted strings when " +
588
+ "you don't need string interpolation or special symbols.",
589
+ '',
590
+ '1 file inspected, 2 offences detected',
591
+ ''].join("\n"))
592
+ end
593
+
594
+ it 'can have selected cops disabled in a code section' do
595
+ create_file('example.rb', [
596
+ '# encoding: utf-8',
597
+ '# rubocop:disable LineLength,NumericLiterals,StringLiterals',
598
+ '#' * 90,
599
+ 'x(123456)',
600
+ 'y("123")',
601
+ 'def func',
602
+ ' # rubocop: enable LineLength, StringLiterals',
603
+ ' ' + '#' * 93,
604
+ ' x(123456)',
605
+ ' y("123")',
606
+ 'end'
607
+ ])
608
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
609
+ # 3 cops were disabled, then 2 were enabled again, so we
610
+ # should get 2 offences reported.
611
+ expect($stdout.string).to eq(
612
+ ["#{abs('example.rb')}:8:80: C: Line is too long. [95/79]",
613
+ "#{abs('example.rb')}:10:5: C: Prefer single-quoted strings when " +
614
+ "you don't need string interpolation or special symbols.",
615
+ '',
616
+ '1 file inspected, 2 offences detected',
617
+ ''].join("\n"))
618
+ end
619
+
620
+ it 'can have all cops disabled on a single line' do
621
+ create_file('example.rb', [
622
+ '# encoding: utf-8',
623
+ 'y("123", 123456) # rubocop:disable all'
624
+ ])
625
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(0)
626
+ expect($stdout.string).to eq(
627
+ ['',
628
+ '1 file inspected, no offences detected',
629
+ ''].join("\n"))
630
+ end
631
+
632
+ it 'can have selected cops disabled on a single line' do
633
+ create_file('example.rb', [
634
+ '# encoding: utf-8',
635
+ '#' * 90 + ' # rubocop:disable LineLength',
636
+ '#' * 95,
637
+ 'y("123") # rubocop:disable LineLength,StringLiterals'
638
+ ])
639
+ expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
640
+ expect($stdout.string).to eq(
641
+ ["#{abs('example.rb')}:3:80: C: Line is too long. [95/79]",
642
+ '',
643
+ '1 file inspected, 1 offence detected',
644
+ ''].join("\n"))
645
+ end
646
+
647
+ it 'finds a file with no .rb extension but has a shebang line' do
648
+ create_file('example', [
649
+ '#!/usr/bin/env ruby',
650
+ '# encoding: utf-8',
651
+ 'x = 0',
652
+ 'puts x'
653
+ ])
654
+ expect(cli.run(%w(--format simple))).to eq(0)
655
+ expect($stdout.string).to eq(
656
+ ['', '1 file inspected, no offences detected',
657
+ ''].join("\n"))
658
+ end
659
+
660
+ it 'finds included files' do
661
+ create_file('example', [
662
+ '# encoding: utf-8',
663
+ 'x = 0',
664
+ 'puts x'
665
+ ])
666
+ create_file('regexp', [
667
+ '# encoding: utf-8',
668
+ 'x = 0',
669
+ 'puts x'
670
+ ])
671
+ create_file('.rubocop.yml', [
672
+ 'AllCops:',
673
+ ' Includes:',
674
+ ' - example',
675
+ ' - !ruby/regexp /regexp$/'
676
+ ])
677
+ expect(cli.run(%w(--format simple))).to eq(0)
678
+ expect($stdout.string).to eq(
679
+ ['', '2 files inspected, no offences detected',
680
+ ''].join("\n"))
681
+ end
682
+
683
+ it 'ignores excluded files' do
684
+ create_file('example.rb', [
685
+ '# encoding: utf-8',
686
+ 'x = 0',
687
+ 'puts x'
688
+ ])
689
+ create_file('regexp.rb', [
690
+ '# encoding: utf-8',
691
+ 'x = 0',
692
+ 'puts x'
693
+ ])
694
+ create_file('exclude_glob.rb', [
695
+ '#!/usr/bin/env ruby',
696
+ '# encoding: utf-8',
697
+ 'x = 0',
698
+ 'puts x'
699
+ ])
700
+ create_file('.rubocop.yml', [
701
+ 'AllCops:',
702
+ ' Excludes:',
703
+ ' - example.rb',
704
+ ' - !ruby/regexp /regexp.rb$/',
705
+ ' - "exclude_*"'
706
+ ])
707
+ expect(cli.run(%w(--format simple))).to eq(0)
708
+ expect($stdout.string).to eq(
709
+ ['', '0 files inspected, no offences detected',
710
+ ''].join("\n"))
711
+ end
712
+
713
+ # With rubinius 2.0.0.rc1 + rspec 2.13.1,
714
+ # File.stub(:open).and_call_original causes SystemStackError.
715
+ it 'does not read files in excluded list', broken: :rbx do
716
+ %w(rb.rb non-rb.ext without-ext).each do |filename|
717
+ create_file("example/ignored/#{filename}", [
718
+ '# encoding: utf-8',
719
+ '#' * 90
720
+ ])
721
+ end
722
+
723
+ create_file('example/.rubocop.yml', [
724
+ 'AllCops:',
725
+ ' Excludes:',
726
+ ' - ignored/**',
727
+ ])
728
+ File.should_not_receive(:open).with(%r(/ignored/))
729
+ File.stub(:open).and_call_original
730
+ expect(cli.run(%w(--format simple example))).to eq(0)
731
+ expect($stdout.string).to eq(
732
+ ['', '0 files inspected, no offences detected',
733
+ ''].join("\n"))
734
+ end
735
+
736
+ describe '--require option' do
737
+ let(:required_file_path) { './path/to/required_file.rb' }
738
+
739
+ before do
740
+ create_file('example.rb', '# encoding: utf-8')
741
+
742
+ create_file(required_file_path, [
743
+ '# encoding: utf-8',
744
+ "puts 'Hello from required file!'"
745
+ ])
746
+ end
747
+
748
+ it 'requires the passed path' do
749
+ cli.run(['--require', required_file_path, 'example.rb'])
750
+ expect($stdout.string).to start_with('Hello from required file!')
751
+ end
752
+ end
753
+
754
+ describe '-f/--format option' do
755
+ let(:target_file) { 'example.rb' }
756
+
757
+ before do
758
+ create_file(target_file, [
759
+ '# encoding: utf-8',
760
+ '#' * 90
761
+ ])
762
+ end
763
+
764
+ describe 'builtin formatters' do
765
+ context 'when simple format is specified' do
766
+ it 'outputs with simple format' do
767
+ cli.run(['--format', 'simple', 'example.rb'])
768
+ expect($stdout.string).to include([
769
+ "== #{target_file} ==",
770
+ 'C: 2: 80: Line is too long. [90/79]'
771
+ ].join("\n"))
772
+ end
773
+ end
774
+
775
+ context 'when emacs format is specified' do
776
+ it 'outputs with emacs format' do
777
+ cli.run(['--format', 'emacs', 'example.rb'])
778
+ expect($stdout.string).to include(
779
+ "#{abs(target_file)}:2:80: C: Line is too long. [90/79]")
780
+ end
781
+ end
782
+
783
+ context 'when unknown format name is specified' do
784
+ it 'aborts with error message' do
785
+ expect { cli.run(['--format', 'unknown', 'example.rb']) }
786
+ .to exit_with_code(1)
787
+ expect($stderr.string)
788
+ .to include('No formatter for "unknown"')
789
+ end
790
+ end
791
+ end
792
+
793
+ describe 'custom formatter' do
794
+ let(:target_file) { abs('example.rb') }
795
+
796
+ context 'when a class name is specified' do
797
+ it 'uses the class as a formatter' do
798
+ module ::MyTool
799
+ class RubocopFormatter < Rubocop::Formatter::BaseFormatter
800
+ def started(all_files)
801
+ output.puts "started: #{all_files.join(',')}"
802
+ end
803
+
804
+ def file_started(file, options)
805
+ output.puts "file_started: #{file}"
806
+ end
807
+
808
+ def file_finished(file, offences)
809
+ output.puts "file_finished: #{file}"
810
+ end
811
+
812
+ def finished(processed_files)
813
+ output.puts "finished: #{processed_files.join(',')}"
814
+ end
815
+ end
816
+ end
817
+
818
+ cli.run(['--format', 'MyTool::RubocopFormatter', 'example.rb'])
819
+ expect($stdout.string).to eq([
820
+ "started: #{target_file}",
821
+ "file_started: #{target_file}",
822
+ "file_finished: #{target_file}",
823
+ "finished: #{target_file}",
824
+ ''
825
+ ].join("\n"))
826
+ end
827
+ end
828
+
829
+ context 'when unknown class name is specified' do
830
+ it 'aborts with error message' do
831
+ expect { cli.run(['--format', 'UnknownFormatter', 'example.rb']) }
832
+ .to exit_with_code(1)
833
+ expect($stderr.string).to include('UnknownFormatter')
834
+ end
835
+ end
836
+ end
837
+
838
+ it 'can be used multiple times' do
839
+ cli.run(['--format', 'simple', '--format', 'emacs', 'example.rb'])
840
+ expect($stdout.string).to include([
841
+ "== #{target_file} ==",
842
+ 'C: 2: 80: Line is too long. [90/79]',
843
+ "#{abs(target_file)}:2:80: C: Line is too long. [90/79]"
844
+ ].join("\n"))
845
+ end
846
+ end
847
+
848
+ unless Rubocop::Version::STRING.start_with?('0')
849
+ describe '-e/--emacs option' do
850
+ it 'is dropped in RuboCop 1.0.0' do
851
+ # This spec can be removed once the option is dropped.
852
+ expect(cli.run(['--emacs'])).to eq(1)
853
+ expect($stderr.string).to include('invalid option: --emacs')
854
+ end
855
+ end
856
+ end
857
+
858
+ describe '-o/--out option' do
859
+ let(:target_file) { 'example.rb' }
860
+
861
+ before do
862
+ create_file(target_file, [
863
+ '# encoding: utf-8',
864
+ '#' * 90
865
+ ])
866
+ end
867
+
868
+ it 'redirects output to the specified file' do
869
+ cli.run(['--out', 'output.txt', target_file])
870
+ expect(File.read('output.txt')).to include('Line is too long.')
871
+ end
872
+
873
+ it 'is applied to the previously specified formatter' do
874
+ cli.run([
875
+ '--format', 'simple',
876
+ '--format', 'emacs', '--out', 'emacs_output.txt',
877
+ target_file
878
+ ])
879
+
880
+ expect($stdout.string).to eq([
881
+ "== #{target_file} ==",
882
+ 'C: 2: 80: Line is too long. [90/79]',
883
+ '',
884
+ '1 file inspected, 1 offence detected',
885
+ ''
886
+ ].join("\n"))
887
+
888
+ expect(File.read('emacs_output.txt')).to eq([
889
+ "#{abs(target_file)}:2:80: C: Line is too long. [90/79]",
890
+ '',
891
+ '1 file inspected, 1 offence detected',
892
+ ''
893
+ ].join("\n"))
894
+ end
895
+ end
896
+
897
+ describe '#display_error_summary' do
898
+ it 'displays an error message when errors are present' do
899
+ msg = 'An error occurred while Encoding cop was inspecting file.rb.'
900
+ cli.display_error_summary([msg])
901
+ expect($stdout.string.lines.to_a[-6..-5])
902
+ .to eq(["1 error occurred:\n", "#{msg}\n"])
903
+ end
904
+ end
905
+ end
906
+ end