forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offense_count_formatter_spec.rb
130 lines (104 loc) · 3.56 KB
/
offense_count_formatter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true
RSpec.describe RuboCop::Formatter::OffenseCountFormatter do
subject(:formatter) { described_class.new(output, options) }
let(:output) { StringIO.new }
let(:options) { { display_style_guide: false } }
let(:file_count) { files.size }
let(:files) do
%w[lib/rubocop.rb spec/spec_helper.rb exe/rubocop].map do |path|
File.expand_path(path)
end
end
let(:finish) { files.each { |file| formatter.file_finished(file, offenses) } }
describe '#file_finished' do
before { formatter.started(files) }
context 'when no offenses are detected' do
let(:offenses) { [] }
it 'does not add to offense_counts' do
expect { finish }.not_to change(formatter, :offense_counts)
end
end
context 'when any offenses are detected' do
let(:offenses) { [instance_double(RuboCop::Cop::Offense, cop_name: 'OffendedCop')] }
before { offenses.each { |o| allow(o).to receive(:message).and_return('') } }
it 'increments the count for the cop in offense_counts' do
expect { finish }.to change(formatter, :offense_counts)
end
end
end
describe '#report_summary' do
context 'when an offense is detected' do
let(:cop_counts) { { 'OffendedCop' => 3 } }
before { formatter.started(files) }
it 'shows the cop and the offense count' do
formatter.report_summary(cop_counts, 2)
expect(output.string).to include(<<~OUTPUT)
3 OffendedCop
--
3 Total in 2 files
OUTPUT
end
end
end
describe '#finished' do
context 'when there are many offenses' do
let(:files) { super().take(1) }
let(:offenses) do
%w[CopB CopA CopC CopC].map do |cop|
instance_double(RuboCop::Cop::Offense, cop_name: cop)
end
end
before do
allow(output).to receive(:tty?).and_return(false)
formatter.started(files)
offenses.each do |o|
allow(o).to receive(:message)
.and_return(format('Unwanted. (https://rubystyle.guide#no-good-%s)', o.cop_name))
end
finish
end
context 'when --display-style-guide was not given' do
it 'sorts by offense count first and then by cop name' do
formatter.finished(files)
expect(output.string).to eq(<<~OUTPUT)
2 CopC
1 CopA
1 CopB
--
4 Total in 1 files
OUTPUT
end
end
context 'when --display-style-guide was given' do
let(:options) { { display_style_guide: true } }
it 'shows links and sorts by offense count first and then by cop name' do
formatter.finished(files)
expect(output.string).to eq(<<~OUTPUT)
2 CopC (https://rubystyle.guide#no-good-CopC)
1 CopA (https://rubystyle.guide#no-good-CopA)
1 CopB (https://rubystyle.guide#no-good-CopB)
--
4 Total in 1 files
OUTPUT
end
end
end
context 'when output tty is true' do
let(:offenses) do
%w[CopB CopA CopC CopC].map do |cop|
instance_double(RuboCop::Cop::Offense, cop_name: cop)
end
end
before do
allow(output).to receive(:tty?).and_return(true)
offenses.each { |o| allow(o).to receive(:message).and_return('') }
formatter.started(files)
finish
end
it 'has a progress bar' do
formatter.finished(files)
expect(formatter.instance_variable_get(:@progressbar).progress).to eq 3
end
end
end
end