rubocop 0.26.1 → 0.27.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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +10 -6
  4. data/.travis.yml +2 -0
  5. data/CHANGELOG.md +30 -0
  6. data/README.md +9 -2
  7. data/assets/logo.png +0 -0
  8. data/assets/output.html.erb +68 -65
  9. data/config/default.yml +42 -7
  10. data/config/disabled.yml +5 -0
  11. data/config/enabled.yml +32 -7
  12. data/lib/rubocop.rb +10 -2
  13. data/lib/rubocop/comment_config.rb +11 -17
  14. data/lib/rubocop/config.rb +20 -16
  15. data/lib/rubocop/config_loader.rb +8 -12
  16. data/lib/rubocop/cop/cop.rb +13 -12
  17. data/lib/rubocop/cop/lint/block_alignment.rb +4 -6
  18. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
  19. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  20. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -3
  21. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  22. data/lib/rubocop/cop/metrics/abc_size.rb +27 -0
  23. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -4
  24. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  25. data/lib/rubocop/cop/metrics/line_length.rb +2 -5
  26. data/lib/rubocop/cop/metrics/method_length.rb +2 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +24 -15
  28. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +15 -2
  29. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +63 -0
  30. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  31. data/lib/rubocop/cop/mixin/if_node.rb +3 -1
  32. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -3
  33. data/lib/rubocop/cop/mixin/{on_method.rb → on_method_def.rb} +3 -3
  34. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  35. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  36. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_literals_help.rb +28 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  39. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -2
  40. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  41. data/lib/rubocop/cop/style/align_hash.rb +16 -12
  42. data/lib/rubocop/cop/style/align_parameters.rb +1 -1
  43. data/lib/rubocop/cop/style/and_or.rb +14 -6
  44. data/lib/rubocop/cop/style/array_join.rb +1 -1
  45. data/lib/rubocop/cop/style/block_comments.rb +16 -8
  46. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -30
  47. data/lib/rubocop/cop/style/case_indentation.rb +20 -12
  48. data/lib/rubocop/cop/style/collection_methods.rb +4 -4
  49. data/lib/rubocop/cop/style/colon_method_call.rb +9 -0
  50. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  51. data/lib/rubocop/cop/style/comment_indentation.rb +22 -22
  52. data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
  53. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
  54. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  55. data/lib/rubocop/cop/style/else_alignment.rb +93 -0
  56. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  57. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  58. data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +34 -0
  59. data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +37 -0
  60. data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +30 -0
  61. data/lib/rubocop/cop/style/encoding.rb +1 -1
  62. data/lib/rubocop/cop/style/format_string.rb +4 -4
  63. data/lib/rubocop/cop/style/indent_array.rb +2 -2
  64. data/lib/rubocop/cop/style/indent_hash.rb +17 -12
  65. data/lib/rubocop/cop/style/indentation_width.rb +27 -19
  66. data/lib/rubocop/cop/style/method_call_parentheses.rb +3 -3
  67. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  68. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -11
  69. data/lib/rubocop/cop/style/method_name.rb +1 -1
  70. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +174 -0
  71. data/lib/rubocop/cop/style/non_nil_check.rb +12 -15
  72. data/lib/rubocop/cop/style/not.rb +1 -1
  73. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +12 -17
  74. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  75. data/lib/rubocop/cop/style/predicate_name.rb +2 -2
  76. data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
  77. data/lib/rubocop/cop/style/redundant_return.rb +3 -3
  78. data/lib/rubocop/cop/style/redundant_self.rb +3 -3
  79. data/lib/rubocop/cop/style/regexp_literal.rb +17 -13
  80. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
  81. data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
  82. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
  83. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +17 -11
  84. data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
  85. data/lib/rubocop/cop/style/space_inside_block_braces.rb +17 -14
  86. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +10 -6
  87. data/lib/rubocop/cop/style/string_literals.rb +13 -16
  88. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +41 -0
  89. data/lib/rubocop/cop/style/trailing_comma.rb +1 -3
  90. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -3
  91. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  92. data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -2
  93. data/lib/rubocop/cop/style/word_array.rb +23 -19
  94. data/lib/rubocop/cop/team.rb +13 -26
  95. data/lib/rubocop/cop/util.rb +5 -0
  96. data/lib/rubocop/cop/variable_force/locatable.rb +7 -13
  97. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  98. data/lib/rubocop/formatter/formatter_set.rb +9 -1
  99. data/lib/rubocop/formatter/html_formatter.rb +83 -55
  100. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
  101. data/lib/rubocop/formatter/text_util.rb +25 -0
  102. data/lib/rubocop/options.rb +14 -7
  103. data/lib/rubocop/path_util.rb +11 -7
  104. data/lib/rubocop/runner.rb +7 -2
  105. data/lib/rubocop/version.rb +1 -1
  106. data/relnotes/v0.27.0.md +77 -0
  107. data/rubocop.gemspec +1 -1
  108. data/spec/fixtures/html_formatter/expected.html +495 -0
  109. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +5 -0
  110. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +74 -0
  111. data/spec/fixtures/html_formatter/project/app/models/book.rb +5 -0
  112. data/spec/rubocop/cli_spec.rb +56 -13
  113. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +1 -1
  114. data/spec/rubocop/cop/metrics/abc_size_spec.rb +99 -0
  115. data/spec/rubocop/cop/rails/action_filter_spec.rb +1 -0
  116. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +23 -1
  117. data/spec/rubocop/cop/style/align_hash_spec.rb +13 -0
  118. data/spec/rubocop/cop/style/align_parameters_spec.rb +44 -33
  119. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  120. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +9 -9
  121. data/spec/rubocop/cop/style/case_indentation_spec.rb +3 -2
  122. data/spec/rubocop/cop/style/colon_method_call_spec.rb +5 -0
  123. data/spec/rubocop/cop/style/comment_indentation_spec.rb +6 -1
  124. data/spec/rubocop/cop/style/else_alignment_spec.rb +437 -0
  125. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +75 -0
  126. data/spec/rubocop/cop/style/{empty_lines_around_body_spec.rb → empty_lines_around_method_body_spec.rb} +9 -50
  127. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +79 -0
  128. data/spec/rubocop/cop/style/file_name_spec.rb +1 -1
  129. data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
  130. data/spec/rubocop/cop/style/indent_array_spec.rb +6 -1
  131. data/spec/rubocop/cop/style/indent_hash_spec.rb +2 -1
  132. data/spec/rubocop/cop/style/indentation_width_spec.rb +765 -722
  133. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +414 -0
  134. data/spec/rubocop/cop/style/non_nil_check_spec.rb +86 -55
  135. data/spec/rubocop/cop/style/single_line_methods_spec.rb +5 -1
  136. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +2 -1
  137. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +2 -1
  138. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +63 -0
  139. data/spec/rubocop/cop/style/string_literals_spec.rb +2 -2
  140. data/spec/rubocop/cop/style/word_array_spec.rb +15 -1
  141. data/spec/rubocop/formatter/base_formatter_spec.rb +1 -1
  142. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -1
  143. data/spec/rubocop/formatter/formatter_set_spec.rb +9 -0
  144. data/spec/rubocop/formatter/html_formatter_spec.rb +25 -122
  145. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  146. data/spec/rubocop/runner_spec.rb +1 -1
  147. data/spec/spec_helper.rb +12 -130
  148. data/spec/support/cop_helper.rb +72 -0
  149. data/spec/support/coverage.rb +15 -0
  150. data/spec/support/{offenses_matcher.rb → custom_matchers.rb} +28 -0
  151. data/spec/support/jruby_workaround.rb +15 -0
  152. data/spec/support/{isolated_environment.rb → shared_contexts.rb} +19 -0
  153. metadata +49 -14
  154. data/lib/rubocop/cop/style/empty_lines_around_body.rb +0 -75
  155. data/spec/support/shared_context.rb +0 -20
@@ -2,8 +2,12 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe RuboCop::Cop::Style::SingleLineMethods, :config do
5
+ describe RuboCop::Cop::Style::SingleLineMethods do
6
6
  subject(:cop) { described_class.new(config) }
7
+ let(:config) do
8
+ RuboCop::Config.new('Style/SingleLineMethods' => cop_config,
9
+ 'Style/IndentationWidth' => { 'Width' => 2 })
10
+ end
7
11
  let(:cop_config) { { 'AllowIfMethodIsEmpty' => true } }
8
12
 
9
13
  it 'registers an offense for a single-line method' do
@@ -6,7 +6,8 @@ describe RuboCop::Cop::Style::SpaceBeforeBlockBraces do
6
6
  subject(:cop) { described_class.new(config) }
7
7
  let(:config) do
8
8
  merged = RuboCop::ConfigLoader
9
- .default_configuration['Style/SpaceBeforeBlockBraces'].merge(cop_config)
9
+ .default_configuration['Style/SpaceBeforeBlockBraces']
10
+ .merge(cop_config)
10
11
  RuboCop::Config.new('Style/Blocks' => { 'Enabled' => false },
11
12
  'Style/SpaceBeforeBlockBraces' => merged)
12
13
  end
@@ -8,7 +8,8 @@ describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
8
8
  subject(:cop) { described_class.new(config) }
9
9
  let(:config) do
10
10
  merged = RuboCop::ConfigLoader
11
- .default_configuration['Style/SpaceInsideBlockBraces'].merge(cop_config)
11
+ .default_configuration['Style/SpaceInsideBlockBraces']
12
+ .merge(cop_config)
12
13
  RuboCop::Config.new('Style/Blocks' => { 'Enabled' => false },
13
14
  'Style/SpaceInsideBlockBraces' => merged)
14
15
  end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RuboCop::Cop::Style::StringLiteralsInInterpolation, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ context 'configured with single quotes preferred' do
9
+ let(:cop_config) { { 'EnforcedStyle' => 'single_quotes' } }
10
+
11
+ it 'detects unneeded double quotes within embedded expression' do
12
+ src = ['"#{"A"}"']
13
+ inspect_source(cop, src)
14
+ expect(cop.messages)
15
+ .to eq(['Prefer single-quoted strings inside interpolations.'])
16
+ end
17
+
18
+ it 'accepts double quotes on a static string' do
19
+ src = ['"A"']
20
+ inspect_source(cop, src)
21
+ expect(cop.offenses).to be_empty
22
+ end
23
+
24
+ it 'can handle a built-in constant parsed as string' do
25
+ # Parser will produce str nodes for constants such as __FILE__.
26
+ src = ['if __FILE__ == $PROGRAM_NAME',
27
+ 'end']
28
+ inspect_source(cop, src)
29
+ expect(cop.offenses).to be_empty
30
+ end
31
+
32
+ it 'can handle character literals' do
33
+ src = 'a = ?/'
34
+ inspect_source(cop, src)
35
+ expect(cop.offenses).to be_empty
36
+ end
37
+
38
+ it 'auto-corrects " with \'' do
39
+ new_source = autocorrect_source(cop, 's = "#{"abc"}"')
40
+ expect(new_source).to eq(%q(s = "#{'abc'}"))
41
+ end
42
+ end
43
+
44
+ context 'configured with double quotes preferred' do
45
+ let(:cop_config) { { 'EnforcedStyle' => 'double_quotes' } }
46
+
47
+ it 'registers an offense for single quotes within embedded expression' do
48
+ src = %q("#{'A'}")
49
+ inspect_source(cop, src)
50
+ expect(cop.messages)
51
+ .to eq(['Prefer double-quoted strings inside interpolations.'])
52
+ end
53
+ end
54
+
55
+ context 'when configured with a bad value' do
56
+ let(:cop_config) { { 'EnforcedStyle' => 'other' } }
57
+
58
+ it 'fails' do
59
+ expect { inspect_source(cop, ['a = "#{"b"}"']) }
60
+ .to raise_error(RuntimeError)
61
+ end
62
+ end
63
+ end
@@ -95,10 +95,10 @@ describe RuboCop::Cop::Style::StringLiterals, :config do
95
95
  expect(cop.offenses).to be_empty
96
96
  end
97
97
 
98
- it 'detects unneeded double quotes within embedded expression' do
98
+ it 'accepts double quotes within embedded expression' do
99
99
  src = ['"#{"A"}"']
100
100
  inspect_source(cop, src)
101
- expect(cop.offenses.size).to eq(1)
101
+ expect(cop.offenses).to be_empty
102
102
  end
103
103
 
104
104
  it 'detects unneeded double quotes within concatenated string' do
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
 
5
5
  describe RuboCop::Cop::Style::WordArray, :config do
6
6
  subject(:cop) { described_class.new(config) }
7
- let(:cop_config) { { 'MinSize' => 0 } }
7
+ let(:cop_config) { { 'MinSize' => 0, 'WordRegex' => /\A[\p{Word}]+\z/ } }
8
8
 
9
9
  it 'registers an offense for arrays of single quoted strings' do
10
10
  inspect_source(cop,
@@ -100,4 +100,18 @@ describe RuboCop::Cop::Style::WordArray, :config do
100
100
  new_source = autocorrect_source(cop, '[%{one}, %Q(two), ?\n, ?\t]')
101
101
  expect(new_source).to eq('%W(one two \n \t)')
102
102
  end
103
+
104
+ context 'with a custom WordRegex configuration' do
105
+ let(:cop_config) { { 'MinSize' => 0, 'WordRegex' => /\A[\w@.]+\z/ } }
106
+
107
+ it 'registers an offense for arrays of email addresses' do
108
+ inspect_source(cop, ["['a@example.com', 'b@example.com']"])
109
+ expect(cop.offenses.size).to eq(1)
110
+ end
111
+
112
+ it 'auto-corrects an array of email addresses' do
113
+ new_source = autocorrect_source(cop, "['a@example.com', 'b@example.com']")
114
+ expect(new_source).to eq('%w(a@example.com b@example.com)')
115
+ end
116
+ end
103
117
  end
@@ -168,7 +168,7 @@ module RuboCop
168
168
 
169
169
  it 'receives an array of detected offenses for the file' do
170
170
  expect(formatter).to receive(:file_finished)
171
- .exactly(3).times do |file, offenses|
171
+ .exactly(3).times do |file, offenses|
172
172
  case File.basename(file)
173
173
  when '1_offense.rb'
174
174
  expect(offenses.size).to eq(1)
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'stringio'
5
- require 'tempfile'
6
5
 
7
6
  module RuboCop
8
7
  module Formatter
@@ -62,6 +62,15 @@ module RuboCop
62
62
  expect(formatter_set.first.output.class).to eq(File)
63
63
  expect(formatter_set.first.output.path).to eq(output_path)
64
64
  end
65
+
66
+ it "creates parent directories if they don't exist" do
67
+ Dir.mktmpdir do |tmpdir|
68
+ output_path = File.join(tmpdir, 'path/does/not/exist')
69
+ formatter_set.add_formatter('simple', output_path)
70
+ expect(formatter_set.first.output.class).to eq(File)
71
+ expect(formatter_set.first.output.path).to eq(output_path)
72
+ end
73
+ end
65
74
  end
66
75
  end
67
76
 
@@ -1,144 +1,47 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require 'stringio'
4
+ require 'fileutils'
5
5
 
6
6
  module RuboCop
7
- describe Formatter::HTMLFormatter do
8
- subject(:formatter) { described_class.new(output) }
9
- let(:output) { StringIO.new }
10
- let(:files) { %w(/path/to/file1 /path/to/file2) }
11
- let(:location) do
12
- source_buffer = Parser::Source::Buffer.new('test', 1)
13
- source_buffer.source = %w(a b cdefghi).join("\n")
14
- Parser::Source::Range.new(source_buffer, 9, 10)
15
- end
16
- let(:offense) do
17
- Cop::Offense.new(:convention, location,
18
- 'This is message', 'CopName', true)
19
- end
20
-
21
- describe '#started' do
22
- let(:summary) { formatter.output_hash[:summary] }
7
+ module Formatter
8
+ describe HTMLFormatter, :isolated_environment do
9
+ spec_root = File.expand_path('../../..', __FILE__)
23
10
 
24
- it 'sets target file count in summary' do
25
- expect(summary[:target_file_count]).to be_nil
26
- formatter.started(%w(/path/to/file1 /path/to/file2))
27
- expect(summary[:target_file_count]).to eq(2)
28
- end
29
- end
11
+ around do |example|
12
+ project_path = File.join(spec_root, 'fixtures/html_formatter/project')
13
+ FileUtils.cp_r(project_path, '.')
30
14
 
31
- describe '#file_finished' do
32
- before do
33
- count = 0
34
- allow(formatter).to receive(:hash_for_file) do
35
- count += 1
15
+ Dir.chdir(File.basename(project_path)) do
16
+ example.run
36
17
  end
37
18
  end
38
19
 
39
- let(:summary) { formatter.output_hash[:summary] }
40
-
41
- it 'adds detected offense count in summary' do
42
- expect(summary[:offense_count]).to eq(0)
43
-
44
- formatter.file_started(files[0], {})
45
- expect(summary[:offense_count]).to eq(0)
46
- formatter.file_finished(files[0], [
47
- double('offense1'), double('offense2')
48
- ])
49
- expect(summary[:offense_count]).to eq(2)
20
+ let(:actual_html_path) do
21
+ path = File.expand_path('result.html')
22
+ CLI.new.run(['--format', 'html', '--out', path])
23
+ path
50
24
  end
51
25
 
52
- it 'adds value of #hash_for_file to #output_hash[:files]' do
53
- expect(formatter.output_hash[:files]).to be_empty
54
-
55
- formatter.file_started(files[0], {})
56
- expect(formatter.output_hash[:files]).to be_empty
57
- formatter.file_finished(files[0], [])
58
- expect(formatter.output_hash[:files]).to eq([1])
59
-
60
- formatter.file_started(files[1], {})
61
- expect(formatter.output_hash[:files]).to eq([1])
62
- formatter.file_finished(files[1], [])
63
- expect(formatter.output_hash[:files]).to eq([1, 2])
26
+ let(:actual_html) do
27
+ File.read(actual_html_path, encoding: 'UTF-8')
64
28
  end
65
- end
66
29
 
67
- describe '#finished' do
68
- let(:summary) { formatter.output_hash[:summary] }
69
-
70
- it 'sets inspected file count in summary' do
71
- expect(summary[:inspected_file_count]).to be_nil
72
- formatter.finished(%w(/path/to/file1 /path/to/file2))
73
- expect(summary[:inspected_file_count]).to eq(2)
74
- end
75
- end
76
-
77
- describe '#hash_for_file' do
78
- subject(:hash) { formatter.hash_for_file(file, offenses) }
79
- let(:file) { File.expand_path('spec/spec_helper.rb') }
80
- let(:offenses) { [double('offense1'), double('offense2')] }
81
-
82
- it 'sets relative file path for :path key' do
83
- expect(hash[:path]).to eq('spec/spec_helper.rb')
30
+ let(:expected_html_path) do
31
+ File.join(spec_root, 'fixtures/html_formatter/expected.html')
84
32
  end
85
33
 
86
- before do
87
- count = 0
88
- allow(formatter).to receive(:hash_for_offense) do
89
- count += 1
34
+ let(:expected_html) do
35
+ html = File.read(expected_html_path, encoding: 'UTF-8')
36
+ # Avoid failure on version bump
37
+ html.sub(/(class="version".{0,20})\d+(?:\.\d+){2}/i) do
38
+ Regexp.last_match(1) + RuboCop::Version::STRING
90
39
  end
91
40
  end
92
41
 
93
- it 'sets an array of #hash_for_offense values for :offenses key' do
94
- expect(hash[:offenses]).to eq([1, 2])
95
- end
96
- end
97
-
98
- describe '#hash_for_offense' do
99
- subject(:hash) { formatter.hash_for_offense(offense) }
100
-
101
- it 'sets Offense#severity value for :severity key' do
102
- expect(hash[:severity]).to eq(:convention)
103
- end
104
-
105
- it 'sets Offense#message value for :message key' do
106
- expect(hash[:message]).to eq('This is message')
107
- end
108
-
109
- it 'sets Offense#cop_name value for :cop_name key' do
110
- expect(hash[:cop_name]).to eq('CopName')
111
- end
112
-
113
- it 'sets Offense#corrected? value for :corrected key' do
114
- expect(hash[:corrected]).to be_truthy
115
- end
116
-
117
- before do
118
- allow(formatter)
119
- .to receive(:hash_for_location).and_return(location_hash)
120
- end
121
-
122
- let(:location_hash) { { line: 1, column: 2 } }
123
-
124
- it 'sets value of #hash_for_location for :location key' do
125
- expect(hash[:location]).to eq(location_hash)
126
- end
127
- end
128
-
129
- describe '#hash_for_location' do
130
- subject(:hash) { formatter.hash_for_location(offense) }
131
-
132
- it 'sets line value for :line key' do
133
- expect(hash[:line]).to eq(3)
134
- end
135
-
136
- it 'sets column value for :column key' do
137
- expect(hash[:column]).to eq(6)
138
- end
139
-
140
- it 'sets length value for :length key' do
141
- expect(hash[:length]).to eq(1)
42
+ it 'outputs the result in HTML' do
43
+ # FileUtils.copy(actual_html_path, expected_html_path)
44
+ expect(actual_html).to eq(expected_html)
142
45
  end
143
46
  end
144
47
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'stringio'
5
- require 'tempfile'
6
5
 
7
6
  module RuboCop
8
7
  module Formatter
@@ -52,7 +52,7 @@ describe RuboCop::Runner, :isolated_environment do
52
52
 
53
53
  Offenses:
54
54
 
55
- example.rb:2:5: C: Use snake_case for methods.
55
+ example.rb:2:5: C: Use snake_case for method names.
56
56
  def INVALID_CODE
57
57
  ^^^^^^^^^^^^
58
58
 
@@ -1,64 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
- if ENV['TRAVIS'] && RUBY_ENGINE == 'jruby'
4
- # Force JRuby not to select working directory
5
- # as temporary directory on Travis CI.
6
- # https://github.com/jruby/jruby/issues/405
7
- require 'fileutils'
8
- tmp_dir = ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] ||
9
- Etc.systmpdir || '/tmp'
10
- non_world_writable_tmp_dir = File.join(tmp_dir, 'rubocop')
11
- FileUtils.makedirs(non_world_writable_tmp_dir, mode: 0700)
12
- ENV['TMPDIR'] = non_world_writable_tmp_dir
13
- end
14
-
15
- if ENV['TRAVIS'] || ENV['COVERAGE']
16
- require 'simplecov'
17
-
18
- if ENV['TRAVIS']
19
- require 'coveralls'
20
- SimpleCov.formatter = Coveralls::SimpleCov::Formatter
21
- end
22
-
23
- SimpleCov.start do
24
- add_filter '/spec/'
25
- add_filter '/vendor/bundle/'
26
- end
27
- end
3
+ # Requires supporting files with custom matchers and macros, etc,
4
+ # in ./support/ and its subdirectories.
5
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
28
6
 
29
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
30
- $LOAD_PATH.unshift(File.dirname(__FILE__))
31
- require 'rspec'
32
7
  require 'rubocop'
33
- require 'rubocop/cli'
34
-
35
- # disable colors in specs
36
- Rainbow.enabled = false
37
-
38
- module ExitCodeMatchers
39
- RSpec::Matchers.define :exit_with_code do |code|
40
- supports_block_expectations
41
- actual = nil
42
- match do |block|
43
- begin
44
- block.call
45
- rescue SystemExit => e
46
- actual = e.status
47
- end
48
- actual && actual == code
49
- end
50
- failure_message do
51
- "expected block to call exit(#{code}) but exit" +
52
- (actual.nil? ? ' not called' : "(#{actual}) was called")
53
- end
54
- failure_message_when_negated do
55
- "expected block not to call exit(#{code})"
56
- end
57
- description do
58
- "expect block to call exit(#{code})"
59
- end
60
- end
61
- end
62
8
 
63
9
  RSpec.configure do |config|
64
10
  # These two settings work together to allow you to limit a spec run
@@ -67,88 +13,24 @@ RSpec.configure do |config|
67
13
  # get run.
68
14
  config.filter_run :focus
69
15
  config.run_all_when_everything_filtered = true
16
+
70
17
  config.order = :random
71
18
 
19
+ config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
20
+
72
21
  broken_filter = lambda do |v|
73
22
  v.is_a?(Symbol) ? RUBY_ENGINE == v.to_s : v
74
23
  end
75
- config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
76
24
  config.filter_run_excluding broken: broken_filter
77
25
 
78
- config.expect_with :rspec do |c|
79
- c.syntax = :expect # disables `should`
80
- end
81
-
82
- config.mock_with :rspec do |c|
83
- c.syntax = :expect # disables `should_receive` and `stub`
84
- end
85
-
86
- config.include(ExitCodeMatchers)
87
- end
88
-
89
- def inspect_source_file(cop, source)
90
- Tempfile.open('tmp') { |f| inspect_source(cop, source, f) }
91
- end
92
-
93
- def inspect_source(cop, source, file = nil)
94
- RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
95
- processed_source = parse_source(source, file)
96
- fail 'Error parsing example code' unless processed_source.valid_syntax?
97
- _investigate(cop, processed_source)
98
- end
99
-
100
- def parse_source(source, file = nil)
101
- source = source.join($RS) if source.is_a?(Array)
102
-
103
- if file && file.respond_to?(:write)
104
- file.write(source)
105
- file.rewind
106
- file = file.path
26
+ config.expect_with :rspec do |expectations|
27
+ expectations.syntax = :expect # Disable `should`
107
28
  end
108
29
 
109
- RuboCop::ProcessedSource.new(source, file)
110
- end
111
-
112
- def autocorrect_source_file(cop, source)
113
- Tempfile.open('tmp') { |f| autocorrect_source(cop, source, f) }
114
- end
115
-
116
- def autocorrect_source(cop, source, file = nil)
117
- cop.instance_variable_get(:@options)[:auto_correct] = true
118
- processed_source = parse_source(source, file)
119
- _investigate(cop, processed_source)
120
-
121
- corrector =
122
- RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections)
123
- corrector.rewrite
124
- end
125
-
126
- def _investigate(cop, processed_source)
127
- forces = RuboCop::Cop::Force.all.each_with_object([]) do |klass, instances|
128
- next unless cop.join_force?(klass)
129
- instances << klass.new([cop])
30
+ config.mock_with :rspec do |mocks|
31
+ mocks.syntax = :expect # Disable `should_receive` and `stub`
130
32
  end
131
-
132
- commissioner =
133
- RuboCop::Cop::Commissioner.new([cop], forces, raise_error: true)
134
- commissioner.investigate(processed_source)
135
- commissioner
136
33
  end
137
34
 
138
- module RuboCop
139
- module Cop
140
- class Cop
141
- def messages
142
- offenses.sort.map(&:message)
143
- end
144
-
145
- def highlights
146
- offenses.sort.map { |o| o.location.source }
147
- end
148
- end
149
- end
150
- end
151
-
152
- # Requires supporting files with custom matchers and macros, etc,
153
- # in ./support/ and its subdirectories.
154
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
35
+ # Disable colors in specs
36
+ Rainbow.enabled = false