rubocop 0.3.2 → 0.4.0

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

Potentially problematic release.


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

Files changed (66) hide show
  1. data/.rubocop.yml +6 -0
  2. data/README.md +18 -6
  3. data/VERSION +1 -1
  4. data/bin/rubocop +1 -1
  5. data/lib/rubocop.rb +3 -0
  6. data/lib/rubocop/cli.rb +33 -16
  7. data/lib/rubocop/cop/collection_methods.rb +29 -0
  8. data/lib/rubocop/cop/cop.rb +1 -1
  9. data/lib/rubocop/cop/def_parentheses.rb +4 -1
  10. data/lib/rubocop/cop/encoding.rb +6 -4
  11. data/lib/rubocop/cop/favor_modifier.rb +5 -5
  12. data/lib/rubocop/cop/indentation.rb +1 -1
  13. data/lib/rubocop/cop/syntax.rb +19 -0
  14. data/lib/rubocop/version.rb +1 -1
  15. data/rubocop.gemspec +6 -5
  16. data/spec/rubocop/cli_spec.rb +98 -52
  17. data/spec/rubocop/cops/align_parameters_spec.rb +29 -29
  18. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
  19. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +4 -4
  20. data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
  21. data/spec/rubocop/cops/cop_spec.rb +5 -5
  22. data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
  23. data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
  24. data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
  25. data/spec/rubocop/cops/encoding_spec.rb +17 -11
  26. data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
  27. data/spec/rubocop/cops/favor_modifier_spec.rb +12 -12
  28. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +8 -8
  29. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +7 -7
  30. data/spec/rubocop/cops/grammar_spec.rb +14 -9
  31. data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
  32. data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
  33. data/spec/rubocop/cops/indentation_spec.rb +7 -7
  34. data/spec/rubocop/cops/line_length_spec.rb +4 -4
  35. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +7 -7
  36. data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
  37. data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
  38. data/spec/rubocop/cops/new_lambda_literal_spec.rb +4 -4
  39. data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
  40. data/spec/rubocop/cops/offence_spec.rb +4 -4
  41. data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
  42. data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
  43. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +6 -6
  44. data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
  45. data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
  46. data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
  47. data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
  48. data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
  49. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
  50. data/spec/rubocop/cops/space_around_operators_spec.rb +28 -28
  51. data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
  52. data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
  53. data/spec/rubocop/cops/string_literals_spec.rb +5 -5
  54. data/spec/rubocop/cops/syntax_spec.rb +18 -0
  55. data/spec/rubocop/cops/tab_spec.rb +3 -3
  56. data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
  57. data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
  58. data/spec/rubocop/cops/unless_else_spec.rb +4 -4
  59. data/spec/rubocop/cops/when_then_spec.rb +5 -5
  60. data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
  61. data/spec/rubocop/reports/report_spec.rb +5 -5
  62. data/spec/spec_helper.rb +6 -0
  63. metadata +7 -6
  64. data/features/rubocop.feature +0 -9
  65. data/features/step_definitions/rubocop_steps.rb +0 -1
  66. data/features/support/env.rb +0 -15
data/.rubocop.yml CHANGED
@@ -121,3 +121,9 @@ MethodAndVariableSnakeCase:
121
121
 
122
122
  ClassAndModuleCamelCase:
123
123
  Enabled: true
124
+
125
+ Syntax:
126
+ Enabled: true
127
+
128
+ CollectionMethods:
129
+ Enabled: true
data/README.md CHANGED
@@ -1,19 +1,23 @@
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
3
 
4
- # rubocop
4
+ # RuboCop
5
5
 
6
- Ruby code style checker based on the [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide).
6
+ **RuboCop** is a Ruby code style checker based on the
7
+ [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide).
7
8
 
8
9
  ## Installation
9
10
 
11
+ **RuboCop**'s installation is pretty standard:
12
+
10
13
  ```bash
11
14
  $ gem install rubocop
12
15
  ```
13
16
 
14
17
  ## Basic Usage
15
18
 
16
- Running `rubocop` with no arguments will check all Ruby source files in the current folder:
19
+ Running `rubocop` with no arguments will check all Ruby source files
20
+ in the current folder:
17
21
 
18
22
  ```bash
19
23
  $ rubocop
@@ -31,14 +35,22 @@ For more details check the available command-line options:
31
35
  $ rubocop -h
32
36
  ```
33
37
 
38
+ Command flag | Description
39
+ -------------------|------------------------------------------------------------
40
+ `-v/--version` | Displays the current version and exits
41
+ `-d/--debug` | Displays some extra debug output
42
+ `-e/--emacs` | Output the results in Emacs format
43
+ `-c/--config` | Run with specified config file
44
+ `-s/-silent` | Suppress the final summary
45
+
34
46
  ## Configuration
35
47
 
36
- The behavior of `rubocop` can be controlled via the
48
+ The behavior of RuboCop can be controlled via the
37
49
  [.rubocop.yml](https://github.com/bbatsov/rubocop/blob/master/.rubocop.yml)
38
50
  configuration file. The file can be placed either in your home folder
39
51
  or in some project folder.
40
52
 
41
- `rubocop` will start looking for the configuration file in the directory
53
+ RuboCop will start looking for the configuration file in the directory
42
54
  it was started in and continue its way up to the home folder.
43
55
 
44
56
  The file has the following format:
@@ -61,7 +73,7 @@ Unfortunately every major Ruby implementation has its own code
61
73
  analysis tooling, which makes the development of a portable code
62
74
  analyzer a daunting task.
63
75
 
64
- `rubocop` currently supports MRI 1.9 and MRI 2.0. Support for JRuby and
76
+ RuboCop currently supports MRI 1.9 and MRI 2.0. Support for JRuby and
65
77
  Rubinius is not planned at this point.
66
78
 
67
79
  ## Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
data/bin/rubocop CHANGED
@@ -12,5 +12,5 @@ time = Benchmark.realtime do
12
12
  result = cli.run
13
13
  end
14
14
 
15
- puts "Finished in #{time} seconds" if $options[:verbose]
15
+ puts "Finished in #{time} seconds" if $options[:debug]
16
16
  exit result
data/lib/rubocop.rb CHANGED
@@ -11,6 +11,7 @@ require 'rubocop/cop/offence'
11
11
  require 'rubocop/cop/cop'
12
12
  require 'rubocop/cop/encoding'
13
13
  require 'rubocop/cop/line_length'
14
+ require 'rubocop/cop/syntax'
14
15
  require 'rubocop/cop/tab'
15
16
  require 'rubocop/cop/trailing_whitespace'
16
17
  require 'rubocop/cop/indentation'
@@ -36,9 +37,11 @@ require 'rubocop/cop/new_lambda_literal'
36
37
  require 'rubocop/cop/parentheses_around_condition'
37
38
  require 'rubocop/cop/method_and_variable_snake_case'
38
39
  require 'rubocop/cop/class_and_module_camel_case'
40
+ require 'rubocop/cop/collection_methods'
39
41
 
40
42
  require 'rubocop/report/report'
41
43
  require 'rubocop/report/plain_text'
42
44
  require 'rubocop/report/emacs_style'
43
45
 
44
46
  require 'rubocop/cli'
47
+ require 'rubocop/version'
data/lib/rubocop/cli.rb CHANGED
@@ -18,8 +18,8 @@ module Rubocop
18
18
  OptionParser.new do |opts|
19
19
  opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
20
20
 
21
- opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
22
- $options[:verbose] = v
21
+ opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
22
+ $options[:debug] = d
23
23
  end
24
24
  opts.on('-e', '--emacs', 'Emacs style output') do
25
25
  $options[:mode] = :emacs_style
@@ -30,13 +30,21 @@ module Rubocop
30
30
  opts.on('-s', '--silent', 'Silence summary') do |s|
31
31
  $options[:silent] = s
32
32
  end
33
+ opts.on('-v', '--version', 'Display version') do
34
+ puts Rubocop::VERSION
35
+ exit(0)
36
+ end
33
37
  end.parse!(args)
34
38
 
35
- cops = Cop::Cop.all
36
- show_cops_on_duty(cops) if $options[:verbose]
37
39
  total_offences = 0
38
40
  @configs = {}
39
41
 
42
+ target_files = target_files(args)
43
+ config = $options[:config] || config_from_dotfile(target_files[0])
44
+
45
+ cops = cops_on_duty(config)
46
+ show_cops_on_duty(cops) if $options[:debug]
47
+
40
48
  target_files(args).each do |file|
41
49
  report = Report.create(file, $options[:mode])
42
50
  source = File.readlines(file).map do |line|
@@ -45,18 +53,15 @@ module Rubocop
45
53
  end
46
54
 
47
55
  tokens, sexp, correlations = CLI.rip_source(source)
48
- config = $options[:config] || config_from_dotfile(File.dirname(file))
49
56
 
50
57
  cops.each do |cop_klass|
51
58
  cop_config = config[cop_klass.name.split('::').last] if config
52
- if cop_config.nil? || cop_config['Enabled']
53
- cop_klass.config = cop_config
54
- cop = cop_klass.new
55
- cop.correlations = correlations
56
- cop.inspect(file, source, tokens, sexp)
57
- total_offences += cop.offences.count
58
- report << cop if cop.has_report?
59
- end
59
+ cop_klass.config = cop_config
60
+ cop = cop_klass.new
61
+ cop.correlations = correlations
62
+ cop.inspect(file, source, tokens, sexp)
63
+ total_offences += cop.offences.count
64
+ report << cop if cop.has_report?
60
65
  end
61
66
 
62
67
  report.display unless report.empty?
@@ -92,6 +97,7 @@ module Rubocop
92
97
  # directory where the inspected file is. If no .rubocop.yml is
93
98
  # found there, the user's home directory is checked.
94
99
  def config_from_dotfile(target_file_dir)
100
+ return unless target_file_dir
95
101
  # @configs is a cache that maps directories to
96
102
  # configurations. We search for .rubocop.yml only if we haven't
97
103
  # already found it for the given directory.
@@ -111,10 +117,21 @@ module Rubocop
111
117
  @configs[target_file_dir]
112
118
  end
113
119
 
120
+ def cops_on_duty(config)
121
+ cops_on_duty = []
122
+
123
+ Cop::Cop.all.each do |cop_klass|
124
+ cop_config = config[cop_klass.name.split('::').last] if config
125
+ cops_on_duty << cop_klass if cop_config.nil? || cop_config['Enabled']
126
+ end
127
+
128
+ cops_on_duty
129
+ end
130
+
114
131
  def show_cops_on_duty(cops)
115
- puts 'Reporting for duty:'
116
- cops.each { |c| puts c }
117
- puts '*******************'
132
+ puts '== Reporting for duty =='
133
+ cops.each { |c| puts ' * '.yellow + c.to_s.green }
134
+ puts '========================'
118
135
  end
119
136
 
120
137
  # Generate a list of target files by expanding globing patterns
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class CollectionMethods < Cop
6
+ PREFERRED_METHODS = {
7
+ 'collect' => 'map',
8
+ 'inject' => 'reduce',
9
+ 'detect' => 'find',
10
+ 'find_all' => 'select',
11
+ }
12
+
13
+ def inspect(file, source, tokens, sexp)
14
+ each(:call, sexp) do |s|
15
+ s.drop(2).each_slice(2) do |m|
16
+ method_name = m[1][1]
17
+ if PREFERRED_METHODS[method_name]
18
+ add_offence(
19
+ :convention,
20
+ m[1][2].lineno,
21
+ "Prefer #{PREFERRED_METHODS[method_name]} over #{method_name}."
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -89,7 +89,7 @@ module Rubocop
89
89
 
90
90
  def all_positions(sexp)
91
91
  return [sexp[2]] if sexp[0] =~ /^@/
92
- sexp.grep(Array).inject([]) { |memo, s| memo + all_positions(s) }
92
+ sexp.grep(Array).reduce([]) { |memo, s| memo + all_positions(s) }
93
93
  end
94
94
  end
95
95
  end
@@ -3,7 +3,10 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  module DefParentheses
6
- EMPTY_PARAMS = [:params, nil, nil, nil, nil, nil]
6
+ EMPTY_PARAMS = [:params, nil, nil, nil,
7
+ nil, nil, nil, nil] if RUBY_VERSION >= '2.0.0'
8
+ EMPTY_PARAMS = [:params, nil, nil, nil,
9
+ nil, nil] if RUBY_VERSION < '2.0.0'
7
10
 
8
11
  def inspect(file, source, tokens, sexp)
9
12
  each(:def, sexp) { |def_sexp| check(tokens, def_sexp) }
@@ -6,10 +6,12 @@ module Rubocop
6
6
  ERROR_MESSAGE = 'Missing encoding comment.'
7
7
 
8
8
  def inspect(file, source, tokens, sexp)
9
- expected_line = 0
10
- expected_line += 1 if source[expected_line] =~ /^#!/
11
- unless source[expected_line] =~ /#.*coding: (UTF|utf)-8/
12
- add_offence(:convention, 1, ERROR_MESSAGE)
9
+ unless RUBY_VERSION >= '2.0.0'
10
+ expected_line = 0
11
+ expected_line += 1 if source[expected_line] =~ /^#!/
12
+ unless source[expected_line] =~ /#.*coding: (UTF|utf)-8/
13
+ add_offence(:convention, 1, ERROR_MESSAGE)
14
+ end
13
15
  end
14
16
  end
15
17
  end
@@ -28,13 +28,13 @@ module Rubocop
28
28
  # The start ix is the index of the leftmost token on the
29
29
  # line of the if/unless, i.e. the index of if/unless itself,
30
30
  # or of the indentation space.
31
- start_ix = if_ix.downto(0).find do |ix|
32
- ix == 0 || tokens[ix - 1].text =~ /\n/
31
+ start_ix = if_ix.downto(0).find do |block_ix|
32
+ block_ix == 0 || tokens[block_ix - 1].text =~ /\n/
33
33
  end
34
34
  # The stop index is the index of the token just before
35
35
  # 'end', not counting whitespace tokens.
36
- stop_ix = (end_ix - 1).downto(0).find do |ix|
37
- tokens[ix].text !~ /\s/
36
+ stop_ix = (end_ix - 1).downto(0).find do |block_ix|
37
+ tokens[block_ix].text !~ /\s/
38
38
  end
39
39
  if length(tokens, start_ix, stop_ix) <= LineLength.max
40
40
  add_offence(:convention, token_positions[if_ix].lineno,
@@ -44,7 +44,7 @@ module Rubocop
44
44
  end
45
45
 
46
46
  def length(tokens, start_ix, stop_ix)
47
- (start_ix..stop_ix).inject(0) do |acc, ix|
47
+ (start_ix..stop_ix).reduce(0) do |acc, ix|
48
48
  acc + if ix > start_ix && tokens[ix - 1].text =~ /\n/
49
49
  0
50
50
  else
@@ -17,7 +17,7 @@ module Rubocop
17
17
  end
18
18
 
19
19
  def find_keywords(tokens, keyword)
20
- indexes = tokens.each_index.find_all do |ix|
20
+ indexes = tokens.each_index.select do |ix|
21
21
  keyword?(tokens, ix, keyword)
22
22
  end
23
23
  tokens.values_at(*indexes)
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'open3'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ class Syntax < Cop
8
+ def inspect(file, source, tokens, sexp)
9
+ _, stderr, _ =
10
+ Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
11
+
12
+ stderr.each_line do |line|
13
+ line_no, warning = line.match(/.+:(\d+): warning: (.+)/).captures
14
+ add_offence(:warning, line_no.to_i, warning.capitalize) if line_no
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Rubocop
4
- VERSION = '0.0.1'
4
+ VERSION = '0.3.2'
5
5
  end
data/rubocop.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rubocop"
8
- s.version = "0.3.2"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bozhidar Batsov"]
12
- s.date = "2013-04-06"
12
+ s.date = "2013-04-11"
13
13
  s.description = "Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide."
14
14
  s.email = "bozhidar@batsov.com"
15
15
  s.executables = ["rubocop"]
@@ -30,15 +30,13 @@ Gem::Specification.new do |s|
30
30
  "Rakefile",
31
31
  "VERSION",
32
32
  "bin/rubocop",
33
- "features/rubocop.feature",
34
- "features/step_definitions/rubocop_steps.rb",
35
- "features/support/env.rb",
36
33
  "lib/rubocop.rb",
37
34
  "lib/rubocop/cli.rb",
38
35
  "lib/rubocop/cop/align_parameters.rb",
39
36
  "lib/rubocop/cop/ampersands_pipes_vs_and_or.rb",
40
37
  "lib/rubocop/cop/blocks.rb",
41
38
  "lib/rubocop/cop/class_and_module_camel_case.rb",
39
+ "lib/rubocop/cop/collection_methods.rb",
42
40
  "lib/rubocop/cop/cop.rb",
43
41
  "lib/rubocop/cop/def_parentheses.rb",
44
42
  "lib/rubocop/cop/empty_lines.rb",
@@ -60,6 +58,7 @@ Gem::Specification.new do |s|
60
58
  "lib/rubocop/cop/space_after_comma_etc.rb",
61
59
  "lib/rubocop/cop/string_literals.rb",
62
60
  "lib/rubocop/cop/surrounding_space.rb",
61
+ "lib/rubocop/cop/syntax.rb",
63
62
  "lib/rubocop/cop/tab.rb",
64
63
  "lib/rubocop/cop/ternary_operator.rb",
65
64
  "lib/rubocop/cop/trailing_whitespace.rb",
@@ -74,6 +73,7 @@ Gem::Specification.new do |s|
74
73
  "spec/rubocop/cops/align_parameters_spec.rb",
75
74
  "spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb",
76
75
  "spec/rubocop/cops/class_and_module_camel_case_spec.rb",
76
+ "spec/rubocop/cops/collection_methods_spec.rb",
77
77
  "spec/rubocop/cops/cop_spec.rb",
78
78
  "spec/rubocop/cops/def_with_parentheses_spec.rb",
79
79
  "spec/rubocop/cops/def_without_parentheses_spec.rb",
@@ -107,6 +107,7 @@ Gem::Specification.new do |s|
107
107
  "spec/rubocop/cops/space_inside_brackets_spec.rb",
108
108
  "spec/rubocop/cops/space_inside_parens_spec.rb",
109
109
  "spec/rubocop/cops/string_literals_spec.rb",
110
+ "spec/rubocop/cops/syntax_spec.rb",
110
111
  "spec/rubocop/cops/tab_spec.rb",
111
112
  "spec/rubocop/cops/ternary_operator_spec.rb",
112
113
  "spec/rubocop/cops/trailing_whitespace_spec.rb",
@@ -10,25 +10,33 @@ module Rubocop
10
10
  after(:each) { $stdout = STDOUT }
11
11
 
12
12
  it 'exits cleanly when -h is used' do
13
- -> { cli.run ['-h'] }.should exit_with_code(0)
14
- -> { cli.run ['--help'] }.should exit_with_code(0)
13
+ expect { cli.run ['-h'] }.to exit_with_code(0)
14
+ expect { cli.run ['--help'] }.to exit_with_code(0)
15
15
  message = ['Usage: rubocop [options] [file1, file2, ...]',
16
- ' -v, --[no-]verbose Run verbosely',
16
+ ' -d, --[no-]debug Display debug info',
17
17
  ' -e, --emacs Emacs style output',
18
18
  ' -c, --config FILE Configuration file',
19
- ' -s, --silent Silence summary']
20
- $stdout.string.should == (message * 2).join("\n") + "\n"
19
+ ' -s, --silent Silence summary',
20
+ ' -v, --version Display version']
21
+ expect($stdout.string).to eq((message * 2).join("\n") + "\n")
22
+ end
23
+
24
+ it 'exits cleanly when -v is used' do
25
+ expect { cli.run ['-v'] }.to exit_with_code(0)
26
+ expect { cli.run ['--version'] }.to exit_with_code(0)
27
+ expect($stdout.string).to eq((Rubocop::VERSION + "\n") * 2)
21
28
  end
22
29
 
23
30
  it 'checks a given correct file and returns 0' do
24
31
  File.open('example.rb', 'w') do |f|
25
32
  f.puts '# encoding: utf-8'
26
33
  f.puts 'x = 0'
34
+ f.puts 'puts x'
27
35
  end
28
36
  begin
29
- cli.run(['example.rb']).should == 0
30
- $stdout.string.uncolored.should ==
31
- "\n1 files inspected, 0 offences detected\n"
37
+ expect(cli.run(['example.rb'])).to eq(0)
38
+ expect($stdout.string.uncolored)
39
+ .to eq("\n1 files inspected, 0 offences detected\n")
32
40
  ensure
33
41
  File.delete 'example.rb'
34
42
  end
@@ -38,26 +46,28 @@ module Rubocop
38
46
  File.open('example.rb', 'w') do |f|
39
47
  f.puts '# encoding: utf-8'
40
48
  f.puts 'x = 0 '
49
+ f.puts 'puts x'
41
50
  end
42
51
  begin
43
- cli.run(['example.rb']).should == 1
44
- $stdout.string.uncolored.should ==
45
- ['== example.rb ==',
46
- 'C: 2: Trailing whitespace detected.',
47
- '',
48
- '1 files inspected, 1 offences detected',
49
- ''].join("\n")
52
+ expect(cli.run(['example.rb'])).to eq(1)
53
+ expect($stdout.string.uncolored)
54
+ .to eq ['== example.rb ==',
55
+ 'C: 2: Trailing whitespace detected.',
56
+ '',
57
+ '1 files inspected, 1 offences detected',
58
+ ''].join("\n")
50
59
  ensure
51
60
  File.delete 'example.rb'
52
61
  end
53
62
  end
54
63
 
55
- it 'can report in emacs style' do
56
- File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ' }
57
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
64
+ it 'can report in emacs style', ruby: 1.9 do
65
+ File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
66
+ File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
58
67
  begin
59
- cli.run(['--emacs', 'example1.rb', 'example2.rb']).should == 1
60
- $stdout.string.uncolored.should ==
68
+ expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
69
+ expect($stdout.string.uncolored)
70
+ .to eq(
61
71
  ['example1.rb:1: C: Missing encoding comment.',
62
72
  'example1.rb:1: C: Trailing whitespace detected.',
63
73
  "example1.rb:1: C: Surrounding space missing for operator '='.",
@@ -66,27 +76,65 @@ module Rubocop
66
76
  'example2.rb:1: C: Tab detected.',
67
77
  '',
68
78
  '2 files inspected, 6 offences detected',
69
- ''].join("\n")
79
+ ''].join("\n"))
80
+ ensure
81
+ File.delete 'example1.rb'
82
+ File.delete 'example2.rb'
83
+ end
84
+ end
85
+
86
+ it 'can report in emacs style', ruby: 2.0 do
87
+ File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
88
+ File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
89
+ begin
90
+ expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
91
+ expect($stdout.string.uncolored)
92
+ .to eq(
93
+ ['example1.rb:1: C: Trailing whitespace detected.',
94
+ "example1.rb:1: C: Surrounding space missing for operator '='.",
95
+ 'example1.rb:2: C: Trailing whitespace detected.',
96
+ 'example2.rb:1: C: Tab detected.',
97
+ '',
98
+ '2 files inspected, 4 offences detected',
99
+ ''].join("\n"))
70
100
  ensure
71
101
  File.delete 'example1.rb'
72
102
  File.delete 'example2.rb'
73
103
  end
74
104
  end
75
105
 
76
- it 'ommits summary when --silent passed' do
77
- File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
78
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
106
+ it 'ommits summary when --silent passed', ruby: 1.9 do
107
+ File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
108
+ File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
79
109
  begin
80
- cli.run(['--emacs',
81
- '--silent',
82
- 'example1.rb',
83
- 'example2.rb']).should == 1
84
- $stdout.string.should ==
110
+ expect(cli.run(['--emacs',
111
+ '--silent',
112
+ 'example1.rb',
113
+ 'example2.rb'])).to eq(1)
114
+ expect($stdout.string).to eq(
85
115
  ['example1.rb:1: C: Missing encoding comment.',
86
116
  'example1.rb:1: C: Trailing whitespace detected.',
87
117
  'example2.rb:1: C: Missing encoding comment.',
88
118
  'example2.rb:1: C: Tab detected.',
89
- ''].join("\n")
119
+ ''].join("\n"))
120
+ ensure
121
+ File.delete 'example1.rb'
122
+ File.delete 'example2.rb'
123
+ end
124
+ end
125
+
126
+ it 'ommits summary when --silent passed', ruby: 2.0 do
127
+ File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
128
+ File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
129
+ begin
130
+ expect(cli.run(['--emacs',
131
+ '--silent',
132
+ 'example1.rb',
133
+ 'example2.rb'])).to eq(1)
134
+ expect($stdout.string).to eq(
135
+ ['example1.rb:1: C: Trailing whitespace detected.',
136
+ 'example2.rb:1: C: Tab detected.',
137
+ ''].join("\n"))
90
138
  ensure
91
139
  File.delete 'example1.rb'
92
140
  File.delete 'example2.rb'
@@ -94,7 +142,7 @@ module Rubocop
94
142
  end
95
143
 
96
144
  it 'can be configured with option to disable a certain error' do
97
- File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
145
+ File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
98
146
  File.open('rubocop.yml', 'w') do |f|
99
147
  f.puts('Encoding:',
100
148
  ' Enabled: false',
@@ -103,14 +151,13 @@ module Rubocop
103
151
  ' Enabled: false')
104
152
  end
105
153
  begin
106
- return_code = cli.run(['-c', 'rubocop.yml', 'example1.rb'])
107
- $stdout.string.uncolored.should ==
154
+ expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
155
+ expect($stdout.string.uncolored).to eq(
108
156
  ['== example1.rb ==',
109
157
  'C: 1: Trailing whitespace detected.',
110
158
  '',
111
159
  '1 files inspected, 1 offences detected',
112
- ''].join("\n")
113
- return_code.should == 1
160
+ ''].join("\n"))
114
161
  ensure
115
162
  File.delete 'example1.rb'
116
163
  File.delete 'rubocop.yml'
@@ -119,7 +166,7 @@ module Rubocop
119
166
 
120
167
  it 'can be configured with project config to disable a certain error' do
121
168
  FileUtils.mkdir 'example_src'
122
- File.open('example_src/example1.rb', 'w') { |f| f.puts 'x = 0 ' }
169
+ File.open('example_src/example1.rb', 'w') { |f| f.puts 'puts 0 ' }
123
170
  File.open('example_src/.rubocop.yml', 'w') do |f|
124
171
  f.puts('Encoding:',
125
172
  ' Enabled: false',
@@ -128,14 +175,13 @@ module Rubocop
128
175
  ' Enabled: false')
129
176
  end
130
177
  begin
131
- return_code = cli.run(['example_src/example1.rb'])
132
- $stdout.string.uncolored.should ==
178
+ expect(cli.run(['example_src/example1.rb'])).to eq(1)
179
+ expect($stdout.string.uncolored).to eq(
133
180
  ['== example_src/example1.rb ==',
134
181
  'C: 1: Trailing whitespace detected.',
135
182
  '',
136
183
  '1 files inspected, 1 offences detected',
137
- ''].join("\n")
138
- return_code.should == 1
184
+ ''].join("\n"))
139
185
  ensure
140
186
  FileUtils.rm_rf 'example_src'
141
187
  end
@@ -143,21 +189,20 @@ module Rubocop
143
189
 
144
190
  it 'can use an alternative max line length from a config file' do
145
191
  FileUtils.mkdir 'example_src'
146
- File.open('example_src/example1.rb', 'w') { |f| f.puts '#' * 90 }
192
+ File.open('example_src/example1.rb', 'w') do |f|
193
+ f.puts '# encoding: utf-8'
194
+ f.puts '#' * 90
195
+ end
147
196
  File.open('example_src/.rubocop.yml', 'w') do |f|
148
197
  f.puts('LineLength:',
149
198
  ' Enabled: true',
150
199
  ' Max: 100')
151
200
  end
152
201
  begin
153
- return_code = cli.run(['example_src/example1.rb'])
154
- $stdout.string.uncolored.should ==
155
- ['== example_src/example1.rb ==',
156
- 'C: 1: Missing encoding comment.',
157
- '',
158
- '1 files inspected, 1 offences detected',
159
- ''].join("\n")
160
- return_code.should == 1
202
+ expect(cli.run(['example_src/example1.rb'])).to eq(0)
203
+ expect($stdout.string.uncolored).to eq(
204
+ ['', '1 files inspected, 0 offences detected',
205
+ ''].join("\n"))
161
206
  ensure
162
207
  FileUtils.rm_rf 'example_src'
163
208
  end
@@ -165,8 +210,9 @@ module Rubocop
165
210
 
166
211
  it 'finds no violations when checking the rubocop source code' do
167
212
  cli.run
168
- $stdout.string.uncolored.should =~
213
+ expect($stdout.string.uncolored).to match(
169
214
  /files inspected, 0 offences detected\n/
215
+ )
170
216
  end
171
217
 
172
218
  it 'can process a file with an invalide UTF-8 byte sequence' do
@@ -175,7 +221,7 @@ module Rubocop
175
221
  f.puts "# \xf9\x29"
176
222
  end
177
223
  begin
178
- cli.run(['--emacs', 'example.rb']).should == 0
224
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
179
225
  ensure
180
226
  File.delete 'example.rb'
181
227
  end
@@ -185,7 +231,7 @@ module Rubocop
185
231
  cop_names = Cop::Cop.all.map do |cop_class|
186
232
  cop_class.name.split('::').last
187
233
  end
188
- YAML.load_file('.rubocop.yml').keys.sort.should == cop_names.sort
234
+ expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
189
235
  end
190
236
  end
191
237
  end