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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +42 -0
- data/README.md +59 -54
- data/config/default.yml +124 -95
- data/config/disabled.yml +4 -0
- data/config/enabled.yml +286 -264
- data/lib/rubocop.rb +10 -3
- data/lib/rubocop/cli.rb +10 -171
- data/lib/rubocop/cop/cop.rb +14 -11
- data/lib/rubocop/cop/lint/debugger.rb +35 -0
- data/lib/rubocop/cop/lint/syntax.rb +34 -0
- data/lib/rubocop/cop/offence.rb +15 -23
- data/lib/rubocop/cop/style/align_array.rb +1 -10
- data/lib/rubocop/cop/style/align_hash.rb +14 -2
- data/lib/rubocop/cop/style/align_parameters.rb +1 -11
- data/lib/rubocop/cop/style/and_or.rb +13 -11
- data/lib/rubocop/cop/style/attr.rb +1 -1
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +31 -6
- data/lib/rubocop/cop/style/blocks.rb +14 -6
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/class_length.rb +38 -0
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
- data/lib/rubocop/cop/style/configurable_naming.rb +47 -0
- data/lib/rubocop/cop/style/def_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +7 -3
- data/lib/rubocop/cop/style/hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +36 -7
- data/lib/rubocop/cop/style/lambda_call.rb +36 -11
- data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +41 -0
- data/lib/rubocop/cop/style/method_length.rb +2 -13
- data/lib/rubocop/cop/style/method_name.rb +42 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +4 -0
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +60 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +32 -0
- data/lib/rubocop/cop/style/redundant_return.rb +14 -5
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +13 -4
- data/lib/rubocop/cop/style/signal_exception.rb +63 -14
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/string_help.rb +4 -1
- data/lib/rubocop/cop/style/string_literals.rb +34 -11
- data/lib/rubocop/cop/style/surrounding_space.rb +64 -17
- data/lib/rubocop/cop/style/tab.rb +1 -1
- data/lib/rubocop/cop/style/variable_name.rb +44 -0
- data/lib/rubocop/cop/style/when_then.rb +1 -1
- data/lib/rubocop/cop/style/while_until_do.rb +1 -1
- data/lib/rubocop/cop/team.rb +4 -7
- data/lib/rubocop/cop/util.rb +12 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/emacs_style_formatter.rb +5 -2
- data/lib/rubocop/formatter/json_formatter.rb +1 -0
- data/lib/rubocop/formatter/progress_formatter.rb +9 -13
- data/lib/rubocop/formatter/simple_text_formatter.rb +63 -15
- data/lib/rubocop/options.rb +184 -0
- data/lib/rubocop/processed_source.rb +4 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +1 -1
- data/spec/rubocop/cli_spec.rb +870 -1001
- data/spec/rubocop/config_spec.rb +13 -13
- data/spec/rubocop/config_store_spec.rb +38 -37
- data/spec/rubocop/cop/commissioner_spec.rb +42 -46
- data/spec/rubocop/cop/cop_spec.rb +72 -77
- data/spec/rubocop/cop/corrector_spec.rb +51 -55
- data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +100 -106
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +403 -409
- data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
- data/spec/rubocop/cop/lint/empty_ensure_spec.rb +19 -25
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +50 -56
- data/spec/rubocop/cop/lint/end_in_method_spec.rb +20 -26
- data/spec/rubocop/cop/lint/ensure_return_spec.rb +30 -36
- data/spec/rubocop/cop/lint/eval_spec.rb +25 -31
- data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +22 -28
- data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +51 -57
- data/spec/rubocop/cop/lint/loop_spec.rb +17 -23
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +51 -57
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +105 -111
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +230 -236
- data/spec/rubocop/cop/lint/syntax_spec.rb +33 -0
- data/spec/rubocop/cop/lint/unreachable_code_spec.rb +51 -57
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1544 -1550
- data/spec/rubocop/cop/lint/useless_comparison_spec.rb +21 -27
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +119 -125
- data/spec/rubocop/cop/lint/void_spec.rb +50 -56
- data/spec/rubocop/cop/offence_spec.rb +101 -131
- data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +6 -12
- data/spec/rubocop/cop/rails/read_attribute_spec.rb +6 -12
- data/spec/rubocop/cop/rails/validation_spec.rb +13 -19
- data/spec/rubocop/cop/style/access_control_spec.rb +146 -152
- data/spec/rubocop/cop/style/alias_spec.rb +30 -36
- data/spec/rubocop/cop/style/align_array_spec.rb +57 -56
- data/spec/rubocop/cop/style/align_hash_spec.rb +256 -237
- data/spec/rubocop/cop/style/align_parameters_spec.rb +216 -202
- data/spec/rubocop/cop/style/and_or_spec.rb +51 -55
- data/spec/rubocop/cop/style/ascii_comments_spec.rb +14 -20
- data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +26 -32
- data/spec/rubocop/cop/style/attr_spec.rb +11 -17
- data/spec/rubocop/cop/style/begin_block_spec.rb +6 -12
- data/spec/rubocop/cop/style/block_comments_spec.rb +13 -19
- data/spec/rubocop/cop/style/block_nesting_spec.rb +134 -140
- data/spec/rubocop/cop/style/blocks_spec.rb +81 -84
- data/spec/rubocop/cop/style/case_equality_spec.rb +5 -11
- data/spec/rubocop/cop/style/case_indentation_spec.rb +71 -77
- data/spec/rubocop/cop/style/character_literal_spec.rb +31 -37
- data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +31 -37
- data/spec/rubocop/cop/style/class_length_spec.rb +65 -0
- data/spec/rubocop/cop/style/class_methods_spec.rb +35 -41
- data/spec/rubocop/cop/style/class_vars_spec.rb +11 -17
- data/spec/rubocop/cop/style/collection_methods_spec.rb +41 -47
- data/spec/rubocop/cop/style/colon_method_call_spec.rb +45 -51
- data/spec/rubocop/cop/style/comment_annotation_spec.rb +79 -85
- data/spec/rubocop/cop/style/constant_name_spec.rb +45 -51
- data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +28 -34
- data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +23 -29
- data/spec/rubocop/cop/style/documentation_spec.rb +60 -66
- data/spec/rubocop/cop/style/dot_position_spec.rb +50 -56
- data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +109 -110
- data/spec/rubocop/cop/style/empty_lines_spec.rb +28 -34
- data/spec/rubocop/cop/style/empty_literal_spec.rb +80 -86
- data/spec/rubocop/cop/style/encoding_spec.rb +50 -56
- data/spec/rubocop/cop/style/end_block_spec.rb +6 -12
- data/spec/rubocop/cop/style/end_of_line_spec.rb +13 -19
- data/spec/rubocop/cop/style/even_odd_spec.rb +69 -41
- data/spec/rubocop/cop/style/favor_join_spec.rb +21 -27
- data/spec/rubocop/cop/style/favor_modifier_spec.rb +180 -176
- data/spec/rubocop/cop/style/favor_sprintf_spec.rb +41 -47
- data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +63 -69
- data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +32 -38
- data/spec/rubocop/cop/style/final_newline_spec.rb +16 -22
- data/spec/rubocop/cop/style/for_spec.rb +18 -24
- data/spec/rubocop/cop/style/global_vars_spec.rb +23 -29
- data/spec/rubocop/cop/style/hash_methods_spec.rb +39 -45
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +93 -65
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +11 -17
- data/spec/rubocop/cop/style/indentation_width_spec.rb +377 -383
- data/spec/rubocop/cop/style/lambda_call_spec.rb +40 -22
- data/spec/rubocop/cop/style/lambda_spec.rb +30 -34
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +44 -50
- data/spec/rubocop/cop/style/line_length_spec.rb +11 -17
- data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +14 -20
- data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
- data/spec/rubocop/cop/style/method_length_spec.rb +127 -133
- data/spec/rubocop/cop/style/method_name_spec.rb +103 -0
- data/spec/rubocop/cop/style/module_function_spec.rb +16 -22
- data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +64 -70
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +74 -80
- data/spec/rubocop/cop/style/nil_comparison_spec.rb +20 -26
- data/spec/rubocop/cop/style/not_spec.rb +13 -19
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +36 -35
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +5 -11
- data/spec/rubocop/cop/style/op_method_spec.rb +61 -67
- data/spec/rubocop/cop/style/parameter_lists_spec.rb +31 -37
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +62 -68
- data/spec/rubocop/cop/style/perl_backrefs_spec.rb +9 -15
- data/spec/rubocop/cop/style/proc_spec.rb +13 -19
- data/spec/rubocop/cop/style/raise_args_spec.rb +55 -0
- data/spec/rubocop/cop/style/reduce_arguments_spec.rb +49 -55
- data/spec/rubocop/cop/style/redundant_begin_spec.rb +47 -53
- data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
- data/spec/rubocop/cop/style/redundant_return_spec.rb +164 -72
- data/spec/rubocop/cop/style/redundant_self_spec.rb +109 -115
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +57 -63
- data/spec/rubocop/cop/style/rescue_modifier_spec.rb +99 -105
- data/spec/rubocop/cop/style/semicolon_spec.rb +95 -90
- data/spec/rubocop/cop/style/signal_exception_spec.rb +248 -95
- data/spec/rubocop/cop/style/single_line_methods_spec.rb +45 -51
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +14 -20
- data/spec/rubocop/cop/style/space_after_comma_spec.rb +16 -22
- data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +48 -54
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +49 -55
- data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +10 -16
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +118 -50
- data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +18 -24
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +204 -210
- data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +36 -42
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +39 -45
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +62 -68
- data/spec/rubocop/cop/style/space_inside_parens_spec.rb +18 -24
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +40 -46
- data/spec/rubocop/cop/style/string_literals_spec.rb +148 -70
- data/spec/rubocop/cop/style/symbol_array_spec.rb +28 -34
- data/spec/rubocop/cop/style/symbol_name_spec.rb +132 -138
- data/spec/rubocop/cop/style/tab_spec.rb +9 -15
- data/spec/rubocop/cop/style/ternary_operator_spec.rb +25 -31
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +10 -16
- data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +14 -20
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +374 -380
- data/spec/rubocop/cop/style/unless_else_spec.rb +17 -23
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +36 -42
- data/spec/rubocop/cop/style/variable_name_spec.rb +83 -0
- data/spec/rubocop/cop/style/when_then_spec.rb +31 -37
- data/spec/rubocop/cop/style/while_until_do_spec.rb +47 -53
- data/spec/rubocop/cop/style/word_array_spec.rb +49 -55
- data/spec/rubocop/cop/team_spec.rb +125 -127
- data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +176 -180
- data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +580 -586
- data/spec/rubocop/cop/variable_inspector/scope_spec.rb +140 -146
- data/spec/rubocop/cop/variable_inspector/variable_spec.rb +52 -58
- data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +217 -223
- data/spec/rubocop/cop/variable_inspector_spec.rb +15 -19
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +35 -36
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -2
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +24 -1
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/formatter_set_spec.rb +3 -3
- data/spec/rubocop/formatter/json_formatter_spec.rb +7 -2
- data/spec/rubocop/formatter/offence_count_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/simple_text_formatter_spec.rb +55 -6
- data/spec/rubocop/options_spec.rb +233 -0
- data/spec/rubocop/processed_source_spec.rb +85 -42
- data/spec/rubocop/source_parser_spec.rb +108 -110
- data/spec/rubocop/target_finder_spec.rb +125 -127
- data/spec/rubocop/token_spec.rb +15 -17
- metadata +32 -8
- data/lib/rubocop/backports/bsearch.rb +0 -39
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +0 -77
- 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
|
data/lib/rubocop/version.rb
CHANGED
data/rubocop.gemspec
CHANGED
@@ -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
|
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')
|
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -4,1142 +4,1011 @@ require 'fileutils'
|
|
4
4
|
require 'tmpdir'
|
5
5
|
require 'spec_helper'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
include FileHelper
|
7
|
+
describe Rubocop::CLI, :isolated_environment do
|
8
|
+
include FileHelper
|
10
9
|
|
11
|
-
|
10
|
+
subject(:cli) { described_class.new }
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
96
|
-
|
18
|
+
after(:each) do
|
19
|
+
$stdout = STDOUT
|
20
|
+
$stderr = STDERR
|
21
|
+
end
|
97
22
|
|
98
|
-
|
99
|
-
|
100
|
-
|
23
|
+
def abs(path)
|
24
|
+
File.expand_path(path)
|
25
|
+
end
|
101
26
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
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
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
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
|
-
|
232
|
-
|
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
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
177
|
+
before do
|
178
|
+
create_file(target_file, [
|
179
|
+
'# encoding: utf-8',
|
180
|
+
'#' * 90
|
181
|
+
])
|
182
|
+
end
|
256
183
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
265
|
-
|
266
|
-
.
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
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
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
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
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
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
|
-
|
478
|
-
|
290
|
+
describe 'custom formatter' do
|
291
|
+
let(:target_file) { abs('example.rb') }
|
479
292
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
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
|
-
|
489
|
-
|
490
|
-
|
301
|
+
def file_started(file, options)
|
302
|
+
output.puts "file_started: #{file}"
|
303
|
+
end
|
491
304
|
|
492
|
-
|
493
|
-
|
494
|
-
|
305
|
+
def file_finished(file, offences)
|
306
|
+
output.puts "file_finished: #{file}"
|
307
|
+
end
|
495
308
|
|
496
|
-
|
497
|
-
|
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
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
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
|
-
|
523
|
-
|
524
|
-
|
525
|
-
.to
|
526
|
-
|
527
|
-
|
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
|
-
|
535
|
-
|
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
|
-
|
538
|
-
|
539
|
-
'# encoding: utf-8',
|
540
|
-
'#' * 90
|
541
|
-
])
|
542
|
-
end
|
347
|
+
describe '-o/--out option' do
|
348
|
+
let(:target_file) { 'example.rb' }
|
543
349
|
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
350
|
+
before do
|
351
|
+
create_file(target_file, [
|
352
|
+
'# encoding: utf-8',
|
353
|
+
'#' * 90
|
354
|
+
])
|
355
|
+
end
|
548
356
|
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
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
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
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
|
-
|
565
|
-
|
566
|
-
|
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
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
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
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
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
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
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
|
-
|
593
|
-
|
594
|
-
|
405
|
+
def interrupt
|
406
|
+
@interrupt_handlers.each(&:call)
|
407
|
+
end
|
595
408
|
|
596
|
-
|
597
|
-
|
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(
|
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
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
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
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
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
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
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
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
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
|
648
|
-
'
|
649
|
-
'
|
650
|
-
'
|
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 '
|
655
|
-
create_file('example.rb',
|
656
|
-
|
657
|
-
|
658
|
-
|
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')}:
|
663
|
-
'
|
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 '
|
554
|
+
it 'can disable all cops on a single line' do
|
668
555
|
create_file('example.rb', [
|
669
556
|
'# encoding: utf-8',
|
670
|
-
'
|
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(
|
676
|
-
|
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
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
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
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
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
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
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
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
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
|
-
|
747
|
-
|
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
|
-
|
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(['', '
|
660
|
+
.to eq(['', '2 files inspected, no offences detected',
|
661
|
+
''].join("\n"))
|
776
662
|
end
|
777
663
|
|
778
|
-
|
779
|
-
|
780
|
-
|
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
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
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
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
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
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
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
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
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
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
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
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
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
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
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
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
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
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
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
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
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
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
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
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
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
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
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
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
892
|
+
create_file('example/.rubocop.yml', [
|
893
|
+
'AllCops:',
|
894
|
+
' Excludes:',
|
895
|
+
' - src/**',
|
896
|
+
' - etc/**',
|
897
|
+
' - tmp/spec/**'
|
898
|
+
])
|
1025
899
|
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
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
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
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
|
-
|
1036
|
-
|
1037
|
-
|
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
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
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
|
-
|
1051
|
-
|
1052
|
-
|
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
|
-
|
1055
|
-
|
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
|
-
|
1058
|
-
|
1059
|
-
|
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
|
-
|
1063
|
-
|
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
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
])
|
946
|
+
create_file('config/default.yml',
|
947
|
+
['AllCops:',
|
948
|
+
' Excludes:',
|
949
|
+
' - vendor/**'])
|
1079
950
|
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
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
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
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
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
963
|
+
create_file('example/.rubocop.yml', [
|
964
|
+
'LyneLenth:',
|
965
|
+
' Enabled: true',
|
966
|
+
' Max: 100'
|
967
|
+
])
|
1103
968
|
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
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
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
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
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
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
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
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
|