Skip to content

Commit bc17996

Browse files
committed
Improve builds badge specs, remove legacy methods
1 parent 8ae2220 commit bc17996

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

lib/gitlab/badge/build.rb

+6-15
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,26 @@ module Badge
44
# Build badge
55
#
66
class Build
7+
delegate :key_text, :value_text, to: :template
8+
79
def initialize(project, ref)
810
@project = project
911
@ref = ref
12+
@sha = @project.commit(@ref).try(:sha)
1013
end
1114

1215
def status
13-
sha = @project.commit(@ref).try(:sha)
14-
1516
@project.pipelines
16-
.where(sha: sha, ref: @ref)
17+
.where(sha: @sha, ref: @ref)
1718
.status || 'unknown'
1819
end
1920

2021
def metadata
21-
Build::Metadata.new(@project, @ref)
22+
@metadata ||= Build::Metadata.new(@project, @ref)
2223
end
2324

2425
def template
25-
Build::Template.new(status)
26-
end
27-
28-
def type
29-
'image/svg+xml'
30-
end
31-
32-
def data
33-
File.read(
34-
Rails.root.join('public/ci', 'build-' + status + '.svg')
35-
)
26+
@template ||= Build::Template.new(status)
3627
end
3728
end
3829
end

spec/lib/gitlab/badge/build_spec.rb

+15-25
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
let(:branch) { 'master' }
77
let(:badge) { described_class.new(project, branch) }
88

9-
describe '#type' do
10-
subject { badge.type }
11-
it { is_expected.to eq 'image/svg+xml' }
12-
end
13-
149
describe '#metadata' do
1510
it 'returns badge metadata' do
1611
expect(badge.metadata.image_url)
1712
.to include 'badges/master/build.svg'
1813
end
1914
end
2015

16+
describe '#key_text' do
17+
it 'always says build' do
18+
expect(badge.key_text).to eq 'build'
19+
end
20+
end
21+
2122
context 'build exists' do
2223
let!(:build) { create_build(project, sha, branch) }
2324

@@ -30,11 +31,9 @@
3031
end
3132
end
3233

33-
describe '#data' do
34-
let(:data) { badge.data }
35-
36-
it 'contains information about success' do
37-
expect(status_node(data, 'success')).to be_truthy
34+
describe '#value_text' do
35+
it 'returns correct value text' do
36+
expect(badge.value_text).to eq 'success'
3837
end
3938
end
4039
end
@@ -48,11 +47,9 @@
4847
end
4948
end
5049

51-
describe '#data' do
52-
let(:data) { badge.data }
53-
54-
it 'contains information about failure' do
55-
expect(status_node(data, 'failed')).to be_truthy
50+
describe '#value_text' do
51+
it 'has correct value text' do
52+
expect(badge.value_text).to eq 'failed'
5653
end
5754
end
5855
end
@@ -65,11 +62,9 @@
6562
end
6663
end
6764

68-
describe '#data' do
69-
let(:data) { badge.data }
70-
71-
it 'contains infromation about unknown build' do
72-
expect(status_node(data, 'unknown')).to be_truthy
65+
describe '#value_text' do
66+
it 'has correct value text' do
67+
expect(badge.value_text).to eq 'unknown'
7368
end
7469
end
7570
end
@@ -95,9 +90,4 @@ def create_build(project, sha, branch)
9590

9691
create(:ci_build, pipeline: pipeline, stage: 'notify')
9792
end
98-
99-
def status_node(data, status)
100-
xml = Nokogiri::XML.parse(data)
101-
xml.at(%Q{text:contains("#{status}")})
102-
end
10393
end

0 commit comments

Comments
 (0)