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
|
# frozen_string_literal: true
require 'fast_spec_helper'
require 'tempfile'
require 'json'
require_relative '../../../scripts/pipeline/average_reports'
RSpec.describe AverageReports, feature_category: :tooling do
let(:initial_report) do
{
'spec/frontend/fixtures/analytics.rb' => 1,
'spec/frontend/fixtures/runner_instructions.rb' => 0.8074841039997409,
'ee/spec/frontend/fixtures/analytics/value_streams_test_stage.rb' => 50.35115972699987,
'ee/spec/frontend/fixtures/merge_requests.rb' => 19.16644390500005,
'old' => 123
}
end
let(:new_report) do
{
'spec/frontend/fixtures/analytics.rb' => 2,
'spec/frontend/fixtures/runner_instructions.rb' => 0,
'ee/spec/frontend/fixtures/analytics/value_streams_test_stage.rb' => 0,
'ee/spec/frontend/fixtures/merge_requests.rb' => 0,
'new' => 234
}
end
let(:new_report_2) do
{
'spec/frontend/fixtures/analytics.rb' => 3,
'new' => 468
}
end
let(:initial_report_file) do
Tempfile.new('temp_initial_report.json').tap do |f|
# rubocop:disable Gitlab/Json
f.write(JSON.dump(initial_report))
# rubocop:enable Gitlab/Json
f.close
end
end
let(:new_report_file_1) do |_f|
Tempfile.new('temp_new_report1.json').tap do |f|
# rubocop:disable Gitlab/Json
f.write(JSON.dump(new_report))
# rubocop:enable Gitlab/Json
f.close
end
end
let(:new_report_file_2) do |_f|
Tempfile.new('temp_new_report2.json').tap do |f|
# rubocop:disable Gitlab/Json
f.write(JSON.dump(new_report_2))
# rubocop:enable Gitlab/Json
f.close
end
end
before do
allow(subject).to receive(:puts)
end
after do
initial_report_file.unlink
new_report_file_1.unlink
new_report_file_2.unlink
end
describe 'execute' do
context 'with 1 new report' do
subject do
described_class.new(
initial_report_file: initial_report_file.path,
new_report_files: [new_report_file_1.path]
)
end
it 'returns average durations' do
results = subject.execute
expect(results['spec/frontend/fixtures/analytics.rb']).to be_within(0.01).of(1.5)
expect(results['spec/frontend/fixtures/runner_instructions.rb']).to be_within(0.01).of(0.4)
expect(results['ee/spec/frontend/fixtures/analytics/value_streams_test_stage.rb']).to be_within(0.01).of(25.17)
expect(results['ee/spec/frontend/fixtures/merge_requests.rb']).to be_within(0.01).of(9.58)
expect(results['new']).to be_within(0.01).of(234)
# excludes entry missing from the new report
expect(results['old']).to be_nil
end
end
context 'with 2 new reports' do
subject do
described_class.new(
initial_report_file: initial_report_file.path,
new_report_files: [new_report_file_1.path, new_report_file_2.path]
)
end
it 'returns average durations' do
results = subject.execute
expect(subject).to have_received(:puts).with("Updating #{initial_report_file.path} with 2 new reports...")
expect(subject).to have_received(:puts).with("Updated 5 data points from #{new_report_file_1.path}")
expect(subject).to have_received(:puts).with("Updated 2 data points from #{new_report_file_2.path}")
expect(results['spec/frontend/fixtures/analytics.rb']).to be_within(0.01).of(2)
expect(results['new']).to be_within(0.01).of(351)
# retains entry present in one of the new reports
expect(results['spec/frontend/fixtures/runner_instructions.rb']).to be_within(0.01).of(0.4)
expect(results['ee/spec/frontend/fixtures/analytics/value_streams_test_stage.rb']).to be_within(0.01).of(25.17)
expect(results['ee/spec/frontend/fixtures/merge_requests.rb']).to be_within(0.01).of(9.58)
# excludes entry missing from both of the new reports
expect(results['old']).to be_nil
end
end
context 'when some of the new report files do not exist' do
subject do
described_class.new(
initial_report_file: initial_report_file.path,
new_report_files: [new_report_file_1.path, 'file_does_not_exist.json']
)
end
it 'ignores the nil file and only process 1 new report' do
subject.execute
expect(subject).to have_received(:puts).with("Updating #{initial_report_file.path} with 1 new reports...")
expect(subject).to have_received(:puts).with("Updated 5 data points from #{new_report_file_1.path}")
end
end
end
end
|