diff options
Diffstat (limited to 'spec/lib/gitlab/ci/badge/pipeline')
-rw-r--r-- | spec/lib/gitlab/ci/badge/pipeline/metadata_spec.rb | 29 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/badge/pipeline/status_spec.rb | 127 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/badge/pipeline/template_spec.rb | 140 |
3 files changed, 296 insertions, 0 deletions
diff --git a/spec/lib/gitlab/ci/badge/pipeline/metadata_spec.rb b/spec/lib/gitlab/ci/badge/pipeline/metadata_spec.rb new file mode 100644 index 00000000000..2f677237fad --- /dev/null +++ b/spec/lib/gitlab/ci/badge/pipeline/metadata_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'lib/gitlab/ci/badge/shared/metadata' + +RSpec.describe Gitlab::Ci::Badge::Pipeline::Metadata do + let(:badge) { double(project: create(:project), ref: 'feature') } + let(:metadata) { described_class.new(badge) } + + it_behaves_like 'badge metadata' + + describe '#title' do + it 'returns build status title' do + expect(metadata.title).to eq 'pipeline status' + end + end + + describe '#image_url' do + it 'returns valid url' do + expect(metadata.image_url).to include 'badges/feature/pipeline.svg' + end + end + + describe '#link_url' do + it 'returns valid link' do + expect(metadata.link_url).to include 'commits/feature' + end + end +end diff --git a/spec/lib/gitlab/ci/badge/pipeline/status_spec.rb b/spec/lib/gitlab/ci/badge/pipeline/status_spec.rb new file mode 100644 index 00000000000..45d0d781090 --- /dev/null +++ b/spec/lib/gitlab/ci/badge/pipeline/status_spec.rb @@ -0,0 +1,127 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Badge::Pipeline::Status do + let(:project) { create(:project, :repository) } + let(:sha) { project.commit.sha } + let(:branch) { 'master' } + let(:badge) { described_class.new(project, branch) } + + describe '#entity' do + it 'always says pipeline' do + expect(badge.entity).to eq 'pipeline' + end + end + + describe '#template' do + it 'returns badge template' do + expect(badge.template.key_text).to eq 'pipeline' + end + end + + describe '#metadata' do + it 'returns badge metadata' do + expect(badge.metadata.image_url).to include 'badges/master/pipeline.svg' + end + end + + context 'pipeline exists', :sidekiq_might_not_need_inline do + let!(:pipeline) { create_pipeline(project, sha, branch) } + + context 'pipeline success' do + before do + pipeline.success! + end + + describe '#status' do + it 'is successful' do + expect(badge.status).to eq 'success' + end + end + end + + context 'pipeline failed' do + before do + pipeline.drop! + end + + describe '#status' do + it 'failed' do + expect(badge.status).to eq 'failed' + end + end + end + + context 'when outdated pipeline for given ref exists' do + before do + pipeline.success! + + old_pipeline = create_pipeline(project, '11eeffdd', branch) + old_pipeline.drop! + end + + it 'does not take outdated pipeline into account' do + expect(badge.status).to eq 'success' + end + end + + context 'when multiple pipelines exist for given sha' do + before do + pipeline.drop! + + new_pipeline = create_pipeline(project, sha, branch) + new_pipeline.success! + end + + it 'does not take outdated pipeline into account' do + expect(badge.status).to eq 'success' + end + end + + context 'when ignored_skipped is set to true' do + let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: true }) } + + before do + pipeline.skip! + end + + describe '#status' do + it 'uses latest non-skipped status' do + expect(new_badge.status).not_to eq 'skipped' + end + end + end + + context 'when ignored_skipped is set to false' do + let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: false }) } + + before do + pipeline.skip! + end + + describe '#status' do + it 'uses latest status' do + expect(new_badge.status).to eq 'skipped' + end + end + end + end + + context 'build does not exist' do + describe '#status' do + it 'is unknown' do + expect(badge.status).to eq 'unknown' + end + end + end + + def create_pipeline(project, sha, branch) + pipeline = create(:ci_empty_pipeline, + project: project, + sha: sha, + ref: branch) + + create(:ci_build, pipeline: pipeline, stage: 'notify') + end +end diff --git a/spec/lib/gitlab/ci/badge/pipeline/template_spec.rb b/spec/lib/gitlab/ci/badge/pipeline/template_spec.rb new file mode 100644 index 00000000000..696bb62b4d6 --- /dev/null +++ b/spec/lib/gitlab/ci/badge/pipeline/template_spec.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do + let(:badge) { double(entity: 'pipeline', status: 'success', customization: {}) } + let(:template) { described_class.new(badge) } + + describe '#key_text' do + it 'says pipeline by default' do + expect(template.key_text).to eq 'pipeline' + end + + context 'when custom key_text is defined' do + before do + allow(badge).to receive(:customization).and_return({ key_text: 'custom text' }) + end + + it 'returns custom value' do + expect(template.key_text).to eq 'custom text' + end + + context 'when its size is larger than the max allowed value' do + before do + allow(badge).to receive(:customization).and_return({ key_text: 't' * 65 }) + end + + it 'returns default value' do + expect(template.key_text).to eq 'pipeline' + end + end + end + end + + describe '#value_text' do + it 'is status value' do + expect(template.value_text).to eq 'passed' + end + end + + describe '#key_width' do + it 'is fixed by default' do + expect(template.key_width).to eq 62 + end + + context 'when custom key_width is defined' do + before do + allow(badge).to receive(:customization).and_return({ key_width: 101 }) + end + + it 'returns custom value' do + expect(template.key_width).to eq 101 + end + + context 'when it is larger than the max allowed value' do + before do + allow(badge).to receive(:customization).and_return({ key_width: 513 }) + end + + it 'returns default value' do + expect(template.key_width).to eq 62 + end + end + end + end + + describe 'widths and text anchors' do + it 'has fixed width and text anchors' do + expect(template.width).to eq 116 + expect(template.key_width).to eq 62 + expect(template.value_width).to eq 54 + expect(template.key_text_anchor).to eq 31 + expect(template.value_text_anchor).to eq 89 + end + end + + describe '#key_color' do + it 'is always the same' do + expect(template.key_color).to eq '#555' + end + end + + describe '#value_color' do + context 'when status is success' do + it 'has expected color' do + expect(template.value_color).to eq '#4c1' + end + end + + context 'when status is failed' do + before do + allow(badge).to receive(:status).and_return('failed') + end + + it 'has expected color' do + expect(template.value_color).to eq '#e05d44' + end + end + + context 'when status is running' do + before do + allow(badge).to receive(:status).and_return('running') + end + + it 'has expected color' do + expect(template.value_color).to eq '#dfb317' + end + end + + context 'when status is preparing' do + before do + allow(badge).to receive(:status).and_return('preparing') + end + + it 'has expected color' do + expect(template.value_color).to eq '#a7a7a7' + end + end + + context 'when status is unknown' do + before do + allow(badge).to receive(:status).and_return('unknown') + end + + it 'has expected color' do + expect(template.value_color).to eq '#9f9f9f' + end + end + + context 'when status does not match any known statuses' do + before do + allow(badge).to receive(:status).and_return('invalid') + end + + it 'has expected color' do + expect(template.value_color).to eq '#9f9f9f' + end + end + end +end |