/
simplecov_spec.rb
196 lines (162 loc) · 6.03 KB
/
simplecov_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true
require "helper"
if SimpleCov.usable?
describe SimpleCov do
describe ".result" do
before do
SimpleCov.clear_result
allow(Coverage).to receive(:result).once.and_return({})
end
context "with merging disabled" do
before do
allow(SimpleCov).to receive(:use_merging).once.and_return(false)
expect(SimpleCov).to_not receive(:wait_for_other_processes)
end
context "when not running" do
before do
allow(SimpleCov).to receive(:running).and_return(false)
end
it "returns nil" do
expect(SimpleCov.result).to be_nil
end
end
context "when running" do
before do
allow(SimpleCov).to receive(:running).and_return(true, false)
end
it "uses the result from Coverage" do
expect(Coverage).to receive(:result).once.and_return(__FILE__ => [0, 1])
expect(SimpleCov.result.filenames).to eq [__FILE__]
end
it "adds not-loaded-files" do
expect(SimpleCov).to receive(:add_not_loaded_files).once.and_return({})
SimpleCov.result
end
it "doesn't store the current coverage" do
expect(SimpleCov::ResultMerger).not_to receive(:store_result)
SimpleCov.result
end
it "doesn't merge the result" do
expect(SimpleCov::ResultMerger).not_to receive(:merged_result)
SimpleCov.result
end
it "caches its result" do
result = SimpleCov.result
expect(SimpleCov.result).to be(result)
end
end
end
context "with merging enabled" do
let(:the_merged_result) { double }
before do
allow(SimpleCov).to receive(:use_merging).once.and_return(true)
allow(SimpleCov::ResultMerger).to receive(:store_result).once
allow(SimpleCov::ResultMerger).to receive(:merged_result).once.and_return(the_merged_result)
expect(SimpleCov).to receive(:wait_for_other_processes)
end
context "when not running" do
before do
allow(SimpleCov).to receive(:running).and_return(false)
end
it "merges the result" do
expect(SimpleCov.result).to be(the_merged_result)
end
end
context "when running" do
before do
allow(SimpleCov).to receive(:running).and_return(true, false)
end
it "uses the result from Coverage" do
expect(Coverage).to receive(:result).once.and_return({})
SimpleCov.result
end
it "adds not-loaded-files" do
expect(SimpleCov).to receive(:add_not_loaded_files).once.and_return({})
SimpleCov.result
end
it "stores the current coverage" do
expect(SimpleCov::ResultMerger).to receive(:store_result).once
SimpleCov.result
end
it "merges the result" do
expect(SimpleCov.result).to be(the_merged_result)
end
it "caches its result" do
result = SimpleCov.result
expect(SimpleCov.result).to be(result)
end
end
end
end
describe ".set_exit_exception" do
context "when an exception has occurred" do
let(:error) { StandardError.new "SomeError" }
after do
# Clear the exit_exception
SimpleCov.set_exit_exception
end
it "captures the current exception" do
begin
raise error
rescue
SimpleCov.set_exit_exception
expect(SimpleCov.exit_exception).to be(error)
end
end
end
context "when an exception has not occurred" do
it "has no exit_exception" do
SimpleCov.set_exit_exception
expect(SimpleCov.exit_exception).to eq(nil)
end
end
end
describe ".exit_status_from_exception" do
context "when no exception has occurred" do
before do
allow(SimpleCov).to receive(:exit_exception).and_return(nil)
end
it "returns SimpleCov::ExitCodes::SUCCESS" do
expect(SimpleCov.exit_status_from_exception).to eq(SimpleCov::ExitCodes::SUCCESS)
end
end
context "when a SystemExit has occurred" do
let(:system_exit) { SystemExit.new(1) }
before do
allow(SimpleCov).to receive(:exit_exception).and_return(system_exit)
end
it "returns the SystemExit status" do
expect(SimpleCov.exit_status_from_exception).to eq(system_exit.status)
end
end
context "when a non SystemExit occurrs" do
let(:error) { StandardError.new "NonSystemExit" }
before do
allow(SimpleCov).to receive(:exit_exception).and_return(error)
end
it "return SimpleCov::ExitCodes::EXCEPTION" do
expect(SimpleCov.exit_status_from_exception).to eq(SimpleCov::ExitCodes::EXCEPTION)
end
end
end
describe ".process_result" do
before do
expect(SimpleCov).to receive(:result_exit_status).and_return SimpleCov::ExitCodes::MINIMUM_COVERAGE
end
context "when the final result process" do
let(:result) { double(SimpleCov::Result, :covered_percent => 0.0) }
before { expect(SimpleCov).to receive(:final_result_process?).and_return true }
it "returns the exit code from .result_exit_status" do
expect(SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)).to eq SimpleCov::ExitCodes::MINIMUM_COVERAGE
end
end
context "when not the final result process" do
let(:result) { double(SimpleCov::Result, :covered_percent => 0.0) }
before { expect(SimpleCov).to receive(:final_result_process?).and_return false }
it "returns the success exit code" do
expect(SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)).to eq SimpleCov::ExitCodes::SUCCESS
end
end
end
end
end