rubocop 0.6.0 → 0.6.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59160d74a2b79a8d6402bca657745ea9aadaf27c
4
+ data.tar.gz: 074abc5e163cfff8ad8c3f11fc61bbf309be6515
5
+ SHA512:
6
+ metadata.gz: 84391e7848783db7aeaf55918c3ef7c93819f7329a1d22cbaf1cc072d62b02494d1e9a87b71d0fa79f620cf3368be3f96b314d69f5f6633962714be57126c58a
7
+ data.tar.gz: aedd8920e0a1ab20d364c149283f9954c4f15b33054de3e53edfd3364817a1af23c852167256fd1e3c95f38da49398c63a780b270581df077cff12026f3b892e
data/.rubocop.yml CHANGED
@@ -232,8 +232,12 @@ EnsureReturn:
232
232
  HandleExceptions:
233
233
  Enabled: true
234
234
 
235
- # Use only ascii symbols in identifiers and comments.
236
- AsciiIdentifiersAndComments:
235
+ # Use only ascii symbols in identifiers.
236
+ AsciiIdentifiers:
237
+ Enabled: true
238
+
239
+ # Use only ascii symbols in comments.
240
+ AsciiComments:
237
241
  Enabled: true
238
242
 
239
243
  # Avoid rescuing the Exception class.
data/CHANGELOG.md CHANGED
@@ -6,6 +6,28 @@
6
6
 
7
7
  ### Bugs fixed
8
8
 
9
+ ## 0.6.1 (04/28/2013)
10
+
11
+ ### New features
12
+
13
+ * Split AsciiIdentifiersAndComments cop in two separate cops
14
+
15
+ ### Bugs fixed
16
+
17
+ * [#90](https://github.com/bbatsov/rubocop/issues/90) Two cops crash when scanning code using super
18
+ * [#93](https://github.com/bbatsov/rubocop/issues/93) Issue with whitespace?': undefined method
19
+ * [#97](https://github.com/bbatsov/rubocop/issues/97) Build fails
20
+ * [#100](https://github.com/bbatsov/rubocop/issues/100) OpMethod cop doesn't work if method arg is not in braces
21
+ * SymbolSnakeCase now tracks Ruby 1.9 hash labels as well as regular symbols
22
+
23
+ ### Misc
24
+
25
+ * [#88](https://github.com/bbatsov/rubocop/issues/88) Abort gracefully when interrupted with Ctrl-C
26
+ * No longer crashes on bugs within cops. Now problematic checks are skipped and a message is displayed.
27
+ * Replaced Term::ANSIColor with Rainbow.
28
+ * Add an option to disable colors in the output.
29
+ * Cop names are now displayed alongside messages when `-d/--debug` is passed.
30
+
9
31
  ## 0.6.0 (04/23/2013)
10
32
 
11
33
  ### New features
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test do
6
+ gem 'coveralls', require: false
7
+ end
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/rubocop.png)](http://badge.fury.io/rb/rubocop)
2
2
  [![Build Status](https://travis-ci.org/bbatsov/rubocop.png?branch=master)](https://travis-ci.org/bbatsov/rubocop)
3
+ [![Coverage Status](https://coveralls.io/repos/bbatsov/rubocop/badge.png?branch=master)](https://coveralls.io/r/bbatsov/rubocop)
3
4
 
4
5
  # RuboCop
5
6
 
@@ -114,11 +115,24 @@ and quickly jump between problems in your code.
114
115
  [flycheck](https://github.com/lunaryorn/flycheck) > 0.9 also supports
115
116
  RuboCop and uses it by default when available.
116
117
 
118
+ ### Vim
119
+
120
+ The [vim-rubocop](https://github.com/ngmy/vim-rubocop) plugin runs
121
+ RuboCop and displays the results in Vim.
122
+
117
123
  ### Other Editors
118
124
 
119
125
  Here's one great opportunity to contribute to RuboCop - implement
120
126
  RuboCop integration for your favorite editor.
121
127
 
128
+ ## Guard integration
129
+
130
+ If you're fond of [Guard](https://github.com/guard/guard) you might
131
+ like
132
+ [guard-rubocop](https://github.com/yujinakayama/guard-rubocop). It
133
+ allows you to automatically check Ruby code style with RuboCop when
134
+ files are modified.
135
+
122
136
  ## Contributors
123
137
 
124
138
  Here's a [list](https://github.com/bbatsov/rubocop/contributors) of
data/lib/rubocop.rb CHANGED
@@ -1,11 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'ripper'
4
- require 'term/ansicolor'
5
-
6
- class String
7
- include Term::ANSIColor
8
- end
4
+ require 'rainbow'
9
5
 
10
6
  require 'rubocop/cop/offence'
11
7
  require 'rubocop/cop/cop'
@@ -53,7 +49,8 @@ require 'rubocop/cop/brace_after_percent'
53
49
  require 'rubocop/cop/ensure_return'
54
50
  require 'rubocop/cop/handle_exceptions'
55
51
  require 'rubocop/cop/rescue_exception'
56
- require 'rubocop/cop/ascii_identifiers_and_comments'
52
+ require 'rubocop/cop/ascii_identifiers'
53
+ require 'rubocop/cop/ascii_comments'
57
54
  require 'rubocop/cop/hash_literal'
58
55
  require 'rubocop/cop/array_literal'
59
56
  require 'rubocop/cop/method_length'
data/lib/rubocop/cli.rb CHANGED
@@ -8,40 +8,32 @@ module Rubocop
8
8
  # The CLI is a class responsible of handling all the command line interface
9
9
  # logic.
10
10
  class CLI
11
+ # If set true while running,
12
+ # RuboCop will abort processing and exit gracefully.
13
+ attr_accessor :wants_to_quit
14
+ alias_method :wants_to_quit?, :wants_to_quit
15
+
11
16
  # Entry point for the application logic. Here we
12
17
  # do the command line arguments processing and inspect
13
18
  # the target files
14
19
  # @return [Fixnum] UNIX exit code
15
20
  def run(args = ARGV)
21
+ trap_interrupt
22
+
16
23
  $options = { mode: :default }
17
24
 
18
- OptionParser.new do |opts|
19
- opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
20
-
21
- opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
22
- $options[:debug] = d
23
- end
24
- opts.on('-e', '--emacs', 'Emacs style output') do
25
- $options[:mode] = :emacs_style
26
- end
27
- opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
28
- $options[:config] = YAML.load_file(f)
29
- end
30
- opts.on('-s', '--silent', 'Silence summary') do |s|
31
- $options[:silent] = s
32
- end
33
- opts.on('-v', '--version', 'Display version') do
34
- puts Rubocop::Version::STRING
35
- exit(0)
36
- end
37
- end.parse!(args)
25
+ parse_options(args)
38
26
 
39
27
  cops = Cop::Cop.all
40
28
  show_cops_on_duty(cops) if $options[:debug]
29
+ processed_file_count = 0
41
30
  total_offences = 0
31
+ errors_count = 0
42
32
  @configs = {}
43
33
 
44
34
  target_files(args).each do |file|
35
+ break if wants_to_quit?
36
+
45
37
  puts "Scanning #{file}" if $options[:debug]
46
38
 
47
39
  report = Report.create(file, $options[:mode])
@@ -71,24 +63,68 @@ module Rubocop
71
63
  cop = cop_klass.new
72
64
  cop.correlations = correlations
73
65
  cop.disabled_lines = disabled_lines[cop_name]
74
- cop.inspect(file, source, tokens, sexp)
66
+ begin
67
+ cop.inspect(file, source, tokens, sexp)
68
+ rescue => e
69
+ errors_count += 1
70
+ warn "An error occurred while #{cop.name} cop" +
71
+ " was inspecting #{file}."
72
+ warn 'To see the complete backtrace run rubocop -d.'
73
+ puts e.backtrace if $options[:debug]
74
+ end
75
75
  total_offences += cop.offences.count
76
76
  report << cop if cop.has_report?
77
77
  end
78
78
  end
79
79
  end
80
80
 
81
+ processed_file_count += 1
81
82
  report.display unless report.empty?
82
83
  end
83
84
 
84
85
  unless $options[:silent]
85
- display_summary(target_files(args).count, total_offences)
86
+ display_summary(processed_file_count, total_offences, errors_count)
86
87
  end
87
88
 
88
- return total_offences == 0 ? 0 : 1
89
+ (total_offences == 0) && !wants_to_quit ? 0 : 1
90
+ end
91
+
92
+ def parse_options(args)
93
+ OptionParser.new do |opts|
94
+ opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
95
+
96
+ opts.on('-d', '--debug', 'Display debug info') do |d|
97
+ $options[:debug] = d
98
+ end
99
+ opts.on('-e', '--emacs', 'Emacs style output') do
100
+ $options[:mode] = :emacs_style
101
+ end
102
+ opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
103
+ $options[:config] = YAML.load_file(f)
104
+ end
105
+ opts.on('-s', '--silent', 'Silence summary') do |s|
106
+ $options[:silent] = s
107
+ end
108
+ opts.on('-n', '--no-color', 'Disable color output') do |s|
109
+ Sickill::Rainbow.enabled = false
110
+ end
111
+ opts.on('-v', '--version', 'Display version') do
112
+ puts Rubocop::Version::STRING
113
+ exit(0)
114
+ end
115
+ end.parse!(args)
89
116
  end
90
117
 
91
- def display_summary(num_files, total_offences)
118
+ def trap_interrupt
119
+ Signal.trap('INT') do
120
+ exit!(1) if wants_to_quit?
121
+ self.wants_to_quit = true
122
+ STDERR.puts
123
+ STDERR.puts 'Exiting... Interrupt again to exit immediately.'
124
+ end
125
+ end
126
+
127
+ def display_summary(num_files, total_offences, errors_count)
92
128
  print "\n#{num_files} file#{num_files > 1 ? 's' : ''} inspected, "
93
129
  offences_string = if total_offences.zero?
94
130
  'no offences'
@@ -98,7 +134,14 @@ module Rubocop
98
134
  "#{total_offences} offences"
99
135
  end
100
136
  puts "#{offences_string} detected"
101
- .send(total_offences.zero? ? :green : :red)
137
+ .color(total_offences.zero? ? :green : :red)
138
+
139
+ if errors_count > 0
140
+ plural = errors_count > 1 ? 's' : ''
141
+ puts "\n#{errors_count} error#{plural} occurred.".color(:red)
142
+ puts 'Errors are usually caused by RuboCop bugs.'
143
+ puts 'Please, report your problems to RuboCop\'s issue tracker.'
144
+ end
102
145
  end
103
146
 
104
147
  def disabled_lines_in(source)
@@ -188,7 +231,9 @@ module Rubocop
188
231
 
189
232
  def show_cops_on_duty(cops)
190
233
  puts '== Reporting for duty =='
191
- cops.each { |c| puts ' * '.yellow + c.to_s.green }
234
+ cops.each do |c|
235
+ puts ' * '.color(:yellow) + c.to_s.color(:green)
236
+ end
192
237
  puts '========================'
193
238
  end
194
239
 
@@ -9,8 +9,8 @@ module Rubocop
9
9
  each(:method_add_arg, sexp) do |s|
10
10
  potential_class = s[1][1][1]
11
11
 
12
- if potential_class[1] == 'Array' && s[1][3][1] == 'new' &&
13
- s[2] == [:arg_paren, nil]
12
+ if potential_class && potential_class[1] == 'Array' &&
13
+ s[1][3][1] == 'new' && s[2] == [:arg_paren, nil]
14
14
  add_offence(:convention,
15
15
  potential_class[2].lineno,
16
16
  ERROR_MESSAGE)
@@ -2,12 +2,12 @@
2
2
 
3
3
  module Rubocop
4
4
  module Cop
5
- class AsciiIdentifiersAndComments < Cop
6
- ERROR_MESSAGE = 'Use only ascii symbols in identifiers and comments.'
5
+ class AsciiComments < Cop
6
+ ERROR_MESSAGE = 'Use only ascii symbols in comments.'
7
7
 
8
8
  def inspect(file, source, tokens, sexp)
9
9
  tokens.each do |t|
10
- if [:on_ident, :on_comment].include?(t.type) &&
10
+ if t.type == :on_comment &&
11
11
  t.text =~ /[^\x00-\x7f]/
12
12
  add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
13
13
  end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AsciiIdentifiers < Cop
6
+ ERROR_MESSAGE = 'Use only ascii symbols in identifiers.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ tokens.each do |t|
10
+ if t.type == :on_ident &&
11
+ t.text =~ /[^\x00-\x7f]/
12
+ add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -56,10 +56,15 @@ module Rubocop
56
56
 
57
57
  def add_offence(severity, line_number, message)
58
58
  unless @disabled_lines && @disabled_lines.include?(line_number)
59
+ message = $options[:debug] ? "#{name}: #{message}" : message
59
60
  @offences << Offence.new(severity, line_number, message)
60
61
  end
61
62
  end
62
63
 
64
+ def name
65
+ self.class.to_s.split('::')[-1]
66
+ end
67
+
63
68
  private
64
69
 
65
70
  def each_parent_of(sym, sexp)
@@ -9,8 +9,8 @@ module Rubocop
9
9
  each(:method_add_arg, sexp) do |s|
10
10
  potential_class = s[1][1][1]
11
11
 
12
- if potential_class[1] == 'Hash' && s[1][3][1] == 'new' &&
13
- s[2] == [:arg_paren, nil]
12
+ if potential_class && potential_class[1] == 'Hash' &&
13
+ s[1][3][1] == 'new' && s[2] == [:arg_paren, nil]
14
14
  add_offence(:convention,
15
15
  potential_class[2].lineno,
16
16
  ERROR_MESSAGE)
@@ -8,7 +8,12 @@ module Rubocop
8
8
  def inspect(file, source, tokens, sexp)
9
9
  each(:def, sexp) do |s|
10
10
  if s[1][0] == :@op && !%w([] []= <<).include?(s[1][1])
11
- param = s[2][1][1][0]
11
+ if s[2][0] == :paren
12
+ # param is surrounded by braces
13
+ param = s[2][1][1][0]
14
+ else
15
+ param = s[2][1][0]
16
+ end
12
17
 
13
18
  unless param[1] == 'other'
14
19
  add_offence(:convention,
@@ -6,11 +6,10 @@ module Rubocop
6
6
  ERROR_MESSAGE = 'Space missing after %s.'
7
7
 
8
8
  def inspect(file, source, tokens, sexp)
9
- tokens.each_index do |ix|
10
- t = tokens[ix]
11
- if kind(t) && !whitespace?(tokens[ix + 1])
12
- add_offence(:convention, t.pos.lineno,
13
- sprintf(ERROR_MESSAGE, kind(t)))
9
+ tokens.each_cons(2) do |t1, t2|
10
+ if kind(t1) && !whitespace?(t2)
11
+ add_offence(:convention, t1.pos.lineno,
12
+ sprintf(ERROR_MESSAGE, kind(t1)))
14
13
  end
15
14
  end
16
15
  end
@@ -6,6 +6,13 @@ module Rubocop
6
6
  ERROR_MESSAGE = 'Use snake_case for symbols.'
7
7
  SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
8
8
  def inspect(file, source, tokens, sexp)
9
+ check_for_symbols(sexp)
10
+ check_for_hash_labels(sexp)
11
+ end
12
+
13
+ private
14
+
15
+ def check_for_symbols(sexp)
9
16
  each(:symbol_literal, sexp) do |s|
10
17
  symbol_type = s[1][1][0]
11
18
 
@@ -22,6 +29,19 @@ module Rubocop
22
29
  end
23
30
  end
24
31
  end
32
+
33
+ def check_for_hash_labels(sexp)
34
+ each(:@label, sexp) do |s|
35
+ label_ident = s[1].chop
36
+
37
+ unless label_ident =~ SNAKE_CASE
38
+ line_no = s[2].lineno
39
+ add_offence(:convention,
40
+ line_no,
41
+ ERROR_MESSAGE)
42
+ end
43
+ end
44
+ end
25
45
  end
26
46
  end
27
47
  end
@@ -6,7 +6,7 @@ module Rubocop
6
6
  class PlainText < Report
7
7
  # Generates a string representation of the report
8
8
  def generate
9
- report = "== #{filename} ==\n".yellow
9
+ report = "== #{filename} ==\n".color(:yellow)
10
10
  report << entries.join("\n")
11
11
  end
12
12
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Version
5
- STRING = '0.6.0'
5
+ STRING = '0.6.1'
6
6
  end
7
7
  end
data/rubocop.gemspec CHANGED
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
33
33
  s.rubygems_version = '1.8.23'
34
34
  s.summary = 'Automatic Ruby code style checking tool.'
35
35
 
36
- s.add_runtime_dependency(%q<term-ansicolor>, '~> 1.1')
36
+ s.add_runtime_dependency(%q<rainbow>, '>= 1.1.4')
37
37
  s.add_development_dependency(%q<rake>, '~> 10.0')
38
38
  s.add_development_dependency(%q<rspec>, '~> 2.13')
39
39
  s.add_development_dependency(%q<yard>, '~> 0.8')
@@ -15,10 +15,11 @@ module Rubocop
15
15
  expect { cli.run ['-h'] }.to exit_with_code(0)
16
16
  expect { cli.run ['--help'] }.to exit_with_code(0)
17
17
  message = ['Usage: rubocop [options] [file1, file2, ...]',
18
- ' -d, --[no-]debug Display debug info',
18
+ ' -d, --debug Display debug info',
19
19
  ' -e, --emacs Emacs style output',
20
20
  ' -c, --config FILE Configuration file',
21
21
  ' -s, --silent Silence summary',
22
+ ' -n, --no-color Disable color output',
22
23
  ' -v, --version Display version']
23
24
  expect($stdout.string).to eq((message * 2).join("\n") + "\n")
24
25
  end
@@ -29,6 +30,74 @@ module Rubocop
29
30
  expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
30
31
  end
31
32
 
33
+ describe '#wants_to_quit?' do
34
+ it 'is initially false' do
35
+ expect(cli.wants_to_quit?).to be_false
36
+ end
37
+ end
38
+
39
+ context 'when interrupted with Ctrl-C' do
40
+ def execute_rubocop
41
+ project_root = File.expand_path('../..', File.dirname(__FILE__))
42
+ rubocop_command = File.join(project_root, 'bin', 'rubocop')
43
+
44
+ _, stdout, stderr, thread = Open3.popen3(rubocop_command, '--debug')
45
+
46
+ unless IO.select([stdout], nil, nil, 10)
47
+ fail 'rubocop took too long to start running'
48
+ end
49
+
50
+ yield stdout, stderr, thread.pid
51
+
52
+ thread.value.exitstatus
53
+ ensure
54
+ thread.terminate
55
+ end
56
+
57
+ def wait_for_output(output)
58
+ IO.select([output], nil, nil, 10)
59
+ end
60
+
61
+ it 'exits with status 1' do
62
+ exit_status = execute_rubocop do |stdout, stderr, pid|
63
+ Process.kill('INT', pid)
64
+ end
65
+ expect(exit_status).to eq(1)
66
+ end
67
+
68
+ it 'exits gracefully without dumping backtraces' do
69
+ execute_rubocop do |stdout, stderr, pid|
70
+ Process.kill('INT', pid)
71
+ wait_for_output(stderr)
72
+ expect(stderr.read).not_to match(/from .+:\d+:in /)
73
+ end
74
+ end
75
+
76
+ context 'with Ctrl-C once' do
77
+ it 'reports summary' do
78
+ execute_rubocop do |stdout, stderr, pid|
79
+ Process.kill('INT', pid)
80
+ wait_for_output(stdout)
81
+ output = stdout.read
82
+ expect(output).to match(/files? inspected/)
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'with Ctrl-C twice' do
88
+ it 'exits immediately without reporting summary' do
89
+ execute_rubocop do |stdout, stderr, pid|
90
+ Process.kill('INT', pid)
91
+ wait_for_output(stderr) # Wait for "Exiting...".
92
+ Process.kill('INT', pid)
93
+ wait_for_output(stdout)
94
+ output = stdout.read
95
+ expect(output).not_to match(/files? inspected/)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
32
101
  it 'checks a given correct file and returns 0' do
33
102
  File.open('example.rb', 'w') do |f|
34
103
  f.puts '# encoding: utf-8'
@@ -37,7 +106,7 @@ module Rubocop
37
106
  end
38
107
  begin
39
108
  expect(cli.run(['example.rb'])).to eq(0)
40
- expect($stdout.string.uncolored)
109
+ expect($stdout.string)
41
110
  .to eq("\n1 file inspected, no offences detected\n")
42
111
  ensure
43
112
  File.delete 'example.rb'
@@ -52,7 +121,7 @@ module Rubocop
52
121
  end
53
122
  begin
54
123
  expect(cli.run(['example.rb'])).to eq(1)
55
- expect($stdout.string.uncolored)
124
+ expect($stdout.string)
56
125
  .to eq ['== example.rb ==',
57
126
  'C: 2: Trailing whitespace detected.',
58
127
  '',
@@ -68,7 +137,7 @@ module Rubocop
68
137
  File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
69
138
  begin
70
139
  expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
71
- expect($stdout.string.uncolored)
140
+ expect($stdout.string)
72
141
  .to eq(
73
142
  ['example1.rb:1: C: Missing utf-8 encoding comment.',
74
143
  'example1.rb:1: C: Trailing whitespace detected.',
@@ -90,7 +159,7 @@ module Rubocop
90
159
  File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
91
160
  begin
92
161
  expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
93
- expect($stdout.string.uncolored)
162
+ expect($stdout.string)
94
163
  .to eq(
95
164
  ['example1.rb:1: C: Trailing whitespace detected.',
96
165
  "example1.rb:1: C: Surrounding space missing for operator '='.",
@@ -143,6 +212,21 @@ module Rubocop
143
212
  end
144
213
  end
145
214
 
215
+ it 'shows cop names when --debug is passed', ruby: 2.0 do
216
+ File.open('example1.rb', 'w') { |f| f.puts "\tputs 0" }
217
+ begin
218
+ expect(cli.run(['--emacs',
219
+ '--silent',
220
+ '--debug',
221
+ 'example1.rb'])).to eq(1)
222
+ expect($stdout.string.lines[-1]).to eq(
223
+ ['example1.rb:1: C: Tab: Tab detected.',
224
+ ''].join("\n"))
225
+ ensure
226
+ File.delete 'example1.rb'
227
+ end
228
+ end
229
+
146
230
  it 'can be configured with option to disable a certain error' do
147
231
  File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
148
232
  File.open('rubocop.yml', 'w') do |f|
@@ -154,7 +238,7 @@ module Rubocop
154
238
  end
155
239
  begin
156
240
  expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
157
- expect($stdout.string.uncolored).to eq(
241
+ expect($stdout.string).to eq(
158
242
  ['== example1.rb ==',
159
243
  'C: 1: Trailing whitespace detected.',
160
244
  '',
@@ -178,7 +262,7 @@ module Rubocop
178
262
  end
179
263
  begin
180
264
  expect(cli.run(['example_src/example1.rb'])).to eq(1)
181
- expect($stdout.string.uncolored).to eq(
265
+ expect($stdout.string).to eq(
182
266
  ['== example_src/example1.rb ==',
183
267
  'C: 1: Trailing whitespace detected.',
184
268
  '',
@@ -202,7 +286,7 @@ module Rubocop
202
286
  end
203
287
  begin
204
288
  expect(cli.run(['example_src/example1.rb'])).to eq(0)
205
- expect($stdout.string.uncolored).to eq(
289
+ expect($stdout.string).to eq(
206
290
  ['', '1 file inspected, no offences detected',
207
291
  ''].join("\n"))
208
292
  ensure
@@ -225,7 +309,7 @@ module Rubocop
225
309
  end
226
310
  begin
227
311
  expect(cli.run(['example'])).to eq(1)
228
- expect($stdout.string.uncolored).to eq(
312
+ expect($stdout.string).to eq(
229
313
  ['== example/lib/example1.rb ==',
230
314
  'C: 2: Line is too long. [90/79]',
231
315
  '',
@@ -257,7 +341,7 @@ module Rubocop
257
341
  end
258
342
  begin
259
343
  expect(cli.run(['example_src/example1.rb'])).to eq(0)
260
- expect($stdout.string.uncolored).to eq(
344
+ expect($stdout.string).to eq(
261
345
  ['', '1 file inspected, no offences detected',
262
346
  ''].join("\n"))
263
347
  ensure
@@ -271,7 +355,7 @@ module Rubocop
271
355
  # Need to pass an empty array explicitly
272
356
  # so that the CLI does not refer arguments of `rspec`
273
357
  cli.run([])
274
- expect($stdout.string.uncolored).to match(
358
+ expect($stdout.string).to match(
275
359
  /files inspected, no offences detected\n/
276
360
  )
277
361
  end
@@ -285,7 +369,7 @@ module Rubocop
285
369
  begin
286
370
  expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
287
371
  unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
288
- expect($stdout.string.uncolored).to eq(
372
+ expect($stdout.string).to eq(
289
373
  ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
290
374
  'expecting keyword_end',
291
375
  '',
@@ -333,7 +417,7 @@ module Rubocop
333
417
  expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
334
418
  # all cops were disabled, then 2 were enabled again, so we
335
419
  # should get 2 offences reported.
336
- expect($stdout.string.uncolored).to eq(
420
+ expect($stdout.string).to eq(
337
421
  ['example.rb:8: C: Line is too long. [95/79]',
338
422
  "example.rb:10: C: Prefer single-quoted strings when you don't " +
339
423
  'need string interpolation or special symbols.',
@@ -363,7 +447,7 @@ module Rubocop
363
447
  expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
364
448
  # 3 cops were disabled, then 2 were enabled again, so we
365
449
  # should get 2 offences reported.
366
- expect($stdout.string.uncolored).to eq(
450
+ expect($stdout.string).to eq(
367
451
  ['example.rb:8: C: Line is too long. [95/79]',
368
452
  "example.rb:10: C: Prefer single-quoted strings when you don't " +
369
453
  'need string interpolation or special symbols.',
@@ -382,7 +466,7 @@ module Rubocop
382
466
  end
383
467
  begin
384
468
  expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
385
- expect($stdout.string.uncolored).to eq(
469
+ expect($stdout.string).to eq(
386
470
  ['',
387
471
  '1 file inspected, no offences detected',
388
472
  ''].join("\n"))
@@ -400,7 +484,7 @@ module Rubocop
400
484
  end
401
485
  begin
402
486
  expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
403
- expect($stdout.string.uncolored).to eq(
487
+ expect($stdout.string).to eq(
404
488
  ['example.rb:3: C: Line is too long. [95/79]',
405
489
  '',
406
490
  '1 file inspected, 1 offence detected',
@@ -423,7 +507,7 @@ module Rubocop
423
507
  # Need to pass an empty array explicitly
424
508
  # so that the CLI does not refer arguments of `rspec`
425
509
  expect(cli.run([])).to eq(0)
426
- expect($stdout.string.uncolored).to eq(
510
+ expect($stdout.string).to eq(
427
511
  ['', '1 file inspected, no offences detected',
428
512
  ''].join("\n"))
429
513
  end
@@ -431,6 +515,28 @@ module Rubocop
431
515
  FileUtils::rm_rf 'test'
432
516
  end
433
517
  end
518
+
519
+ describe '#display_summary' do
520
+ it 'handles pluralization correctly' do
521
+ cli.display_summary(1, 0, 0)
522
+ expect($stdout.string).to eq(
523
+ "\n1 file inspected, no offences detected\n")
524
+ $stdout = StringIO.new
525
+ cli.display_summary(1, 1, 0)
526
+ expect($stdout.string).to eq(
527
+ "\n1 file inspected, 1 offence detected\n")
528
+ $stdout = StringIO.new
529
+ cli.display_summary(2, 2, 0)
530
+ expect($stdout.string).to eq(
531
+ "\n2 files inspected, 2 offences detected\n")
532
+ end
533
+
534
+ it 'displays an error message when errors are present' do
535
+ cli.display_summary(1, 1, 1)
536
+ expect($stdout.string.lines.to_a[-3])
537
+ .to eq("1 error occurred.\n")
538
+ end
539
+ end
434
540
  end
435
541
  end
436
542
  end
@@ -24,6 +24,16 @@ module Rubocop
24
24
  ['test = Array.new(3)'])
25
25
  expect(a.offences).to be_empty
26
26
  end
27
+
28
+ it 'does not crash when a method is called on super' do
29
+ inspect_source(a,
30
+ 'file.rb',
31
+ ['class Derived < Base',
32
+ ' def func',
33
+ ' super.slice(1..2)',
34
+ ' end',
35
+ 'end'])
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AsciiComments do
8
+ let(:ascii) { AsciiComments.new }
9
+
10
+ it 'registers an offence for a comment with non-ascii chars' do
11
+ inspect_source(ascii,
12
+ 'file.rb',
13
+ ['# 这是什么?'])
14
+ expect(ascii.offences.size).to eq(1)
15
+ expect(ascii.offences.map(&:message))
16
+ .to eq([AsciiComments::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'accepts comments with only ascii chars' do
20
+ inspect_source(ascii,
21
+ 'file.rb',
22
+ ['# AZaz1@$%~,;*_`|'])
23
+ expect(ascii.offences).to be_empty
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe AsciiIdentifiers do
8
+ let(:ascii) { AsciiIdentifiers.new }
9
+
10
+ it 'registers an offence for a variable name with non-ascii chars' do
11
+ inspect_source(ascii,
12
+ 'file.rb',
13
+ ['älg = 1'])
14
+ expect(ascii.offences.size).to eq(1)
15
+ expect(ascii.offences.map(&:message))
16
+ .to eq([AsciiIdentifiers::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'accepts identifiers with only ascii chars' do
20
+ inspect_source(ascii,
21
+ 'file.rb',
22
+ ['x.empty?'])
23
+ expect(ascii.offences).to be_empty
24
+ end
25
+ end
26
+ end
27
+ end
@@ -24,6 +24,16 @@ module Rubocop
24
24
  ['test = Hash.new(3)'])
25
25
  expect(a.offences).to be_empty
26
26
  end
27
+
28
+ it 'does not crash when a method is called on super' do
29
+ inspect_source(a,
30
+ 'file.rb',
31
+ ['class Derived < Base',
32
+ ' def func',
33
+ ' super.slice(1..2)',
34
+ ' end',
35
+ 'end'])
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -18,6 +18,17 @@ module Rubocop
18
18
  .to eq([sprintf(OpMethod::ERROR_MESSAGE, '+')])
19
19
  end
20
20
 
21
+ it 'works properly even if the argument is not surrounded with braces' do
22
+ inspect_source(om,
23
+ 'file.rb',
24
+ ['def + another',
25
+ ' another',
26
+ 'end'])
27
+ expect(om.offences.size).to eq(1)
28
+ expect(om.offences.map(&:message))
29
+ .to eq([sprintf(OpMethod::ERROR_MESSAGE, '+')])
30
+ end
31
+
21
32
  it 'does not register an offence for arg named other' do
22
33
  inspect_source(om,
23
34
  'file.rb',
@@ -12,6 +12,11 @@ module Rubocop
12
12
  expect(space.offences.map(&:message)).to eq(
13
13
  ['Space missing after semicolon.'])
14
14
  end
15
+
16
+ it 'does not crash if semicolon is the last character of the file' do
17
+ inspect_source(space, 'file.rb', ['x = 1;'])
18
+ expect(space.offences.map(&:message)).to be_empty
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -14,12 +14,25 @@ module Rubocop
14
14
  ['Use snake_case for symbols.'])
15
15
  end
16
16
 
17
+ it 'registers an offence for symbol used as hash label' do
18
+ inspect_source(snake_case, 'file.rb',
19
+ ['{ ONE: 1, TWO: 2 }'])
20
+ expect(snake_case.offences.map(&:message)).to eq(
21
+ ['Use snake_case for symbols.'] * 2)
22
+ end
23
+
17
24
  it 'accepts snake case in names' do
18
25
  inspect_source(snake_case, 'file.rb',
19
26
  ['test = :good_idea'])
20
27
  expect(snake_case.offences).to be_empty
21
28
  end
22
29
 
30
+ it 'accepts snake case in hash label names' do
31
+ inspect_source(snake_case, 'file.rb',
32
+ ['{ one: 1, one_more_3: 2 }'])
33
+ expect(snake_case.offences).to be_empty
34
+ end
35
+
23
36
  it 'accepts snake case with a prefix @ in names' do
24
37
  inspect_source(snake_case, 'file.rb',
25
38
  ['test = :@good_idea'])
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
3
6
  if ENV['COVERAGE']
4
7
  require 'simplecov'
5
8
  SimpleCov.start
@@ -11,6 +14,9 @@ require 'rspec'
11
14
  require 'rubocop'
12
15
  require 'rubocop/cli'
13
16
 
17
+ # disable colors in specs
18
+ Sickill::Rainbow.enabled = false
19
+
14
20
  # Requires supporting files with custom matchers and macros, etc,
15
21
  # in ./support/ and its subdirectories.
16
22
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
5
- prerelease:
4
+ version: 0.6.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Bozhidar Batsov
@@ -12,25 +11,22 @@ cert_chain: []
12
11
  date: 2013-04-17 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: term-ansicolor
14
+ name: rainbow
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
- version: '1.1'
19
+ version: 1.1.4
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
- version: '1.1'
26
+ version: 1.1.4
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: yard
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: bundler
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: simplecov
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ~>
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ~>
108
95
  - !ruby/object:Gem::Version
@@ -134,7 +121,8 @@ files:
134
121
  - lib/rubocop/cop/align_parameters.rb
135
122
  - lib/rubocop/cop/ampersands_pipes_vs_and_or.rb
136
123
  - lib/rubocop/cop/array_literal.rb
137
- - lib/rubocop/cop/ascii_identifiers_and_comments.rb
124
+ - lib/rubocop/cop/ascii_comments.rb
125
+ - lib/rubocop/cop/ascii_identifiers.rb
138
126
  - lib/rubocop/cop/avoid_class_vars.rb
139
127
  - lib/rubocop/cop/avoid_for.rb
140
128
  - lib/rubocop/cop/avoid_perl_backrefs.rb
@@ -195,7 +183,8 @@ files:
195
183
  - spec/rubocop/cops/align_parameters_spec.rb
196
184
  - spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
197
185
  - spec/rubocop/cops/array_literal_spec.rb
198
- - spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb
186
+ - spec/rubocop/cops/ascii_comments_spec.rb
187
+ - spec/rubocop/cops/ascii_identifiers_spec.rb
199
188
  - spec/rubocop/cops/avoid_class_vars_spec.rb
200
189
  - spec/rubocop/cops/avoid_for_spec.rb
201
190
  - spec/rubocop/cops/avoid_perl_backrefs_spec.rb
@@ -263,30 +252,26 @@ files:
263
252
  homepage: http://github.com/bbatsov/rubocop
264
253
  licenses:
265
254
  - MIT
255
+ metadata: {}
266
256
  post_install_message:
267
257
  rdoc_options: []
268
258
  require_paths:
269
259
  - lib
270
260
  required_ruby_version: !ruby/object:Gem::Requirement
271
- none: false
272
261
  requirements:
273
- - - ! '>='
262
+ - - '>='
274
263
  - !ruby/object:Gem::Version
275
264
  version: 1.9.2
276
265
  required_rubygems_version: !ruby/object:Gem::Requirement
277
- none: false
278
266
  requirements:
279
- - - ! '>='
267
+ - - '>='
280
268
  - !ruby/object:Gem::Version
281
269
  version: '0'
282
- segments:
283
- - 0
284
- hash: 4221530219562804900
285
270
  requirements: []
286
271
  rubyforge_project:
287
- rubygems_version: 1.8.23
272
+ rubygems_version: 2.0.3
288
273
  signing_key:
289
- specification_version: 3
274
+ specification_version: 4
290
275
  summary: Automatic Ruby code style checking tool.
291
276
  test_files:
292
277
  - spec/rubocop/cli_spec.rb
@@ -294,7 +279,8 @@ test_files:
294
279
  - spec/rubocop/cops/align_parameters_spec.rb
295
280
  - spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb
296
281
  - spec/rubocop/cops/array_literal_spec.rb
297
- - spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb
282
+ - spec/rubocop/cops/ascii_comments_spec.rb
283
+ - spec/rubocop/cops/ascii_identifiers_spec.rb
298
284
  - spec/rubocop/cops/avoid_class_vars_spec.rb
299
285
  - spec/rubocop/cops/avoid_for_spec.rb
300
286
  - spec/rubocop/cops/avoid_perl_backrefs_spec.rb
@@ -1,38 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- module Rubocop
6
- module Cop
7
- describe AsciiIdentifiersAndComments do
8
- let(:ascii) { AsciiIdentifiersAndComments.new }
9
-
10
- it 'registers an offence for a variable name with non-ascii chars' do
11
- inspect_source(ascii,
12
- 'file.rb',
13
- ['älg = 1'])
14
- expect(ascii.offences.size).to eq(1)
15
- expect(ascii.offences.map(&:message))
16
- .to eq([AsciiIdentifiersAndComments::ERROR_MESSAGE])
17
- end
18
-
19
- it 'registers an offence for a comment with non-ascii chars' do
20
- inspect_source(ascii,
21
- 'file.rb',
22
- ['# 这是什么?'])
23
- expect(ascii.offences.size).to eq(1)
24
- expect(ascii.offences.map(&:message))
25
- .to eq([AsciiIdentifiersAndComments::ERROR_MESSAGE])
26
- end
27
-
28
- it 'accepts comments and identifiers with only ascii chars' do
29
- inspect_source(ascii,
30
- 'file.rb',
31
- ['# AZaz1@$%~,;*_`|',
32
- 'x.empty?'])
33
- expect(ascii.offences.size).to eq(0)
34
- expect(ascii.offences.map(&:message)).to be_empty
35
- end
36
- end
37
- end
38
- end