summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/external/file/template_spec.rb
blob: 8583f676b4654fc9964be497e36d2d06e591546a (plain)
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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Config::External::File::Template do
  let(:context) { described_class::Context.new(nil, '12345') }
  let(:template) { 'Auto-DevOps.gitlab-ci.yml' }
  let(:params) { { template: template } }

  subject { described_class.new(params, context) }

  describe '#matching?' do
    context 'when a template is specified' do
      let(:params) { { template: 'some-template' } }

      it 'should return true' do
        expect(subject).to be_matching
      end
    end

    context 'with a missing template' do
      let(:params) { { template: nil } }

      it 'should return false' do
        expect(subject).not_to be_matching
      end
    end

    context 'with a missing template key' do
      let(:params) { {} }

      it 'should return false' do
        expect(subject).not_to be_matching
      end
    end
  end

  describe "#valid?" do
    context 'when is a valid template name' do
      let(:template) { 'Auto-DevOps.gitlab-ci.yml' }

      it 'should return true' do
        expect(subject).to be_valid
      end
    end

    context 'with invalid template name' do
      let(:template) { 'Template.yml' }

      it 'should return false' do
        expect(subject).not_to be_valid
        expect(subject.error_message).to include(_('Template file `Template.yml` is not a valid location!'))
      end
    end

    context 'with a non-existing template' do
      let(:template) { 'I-Do-Not-Have-This-Template.gitlab-ci.yml' }

      it 'should return false' do
        expect(subject).not_to be_valid
        expect(subject.error_message).to include(_('Included file `I-Do-Not-Have-This-Template.gitlab-ci.yml` is empty or does not exist!'))
      end
    end
  end

  describe '#template_name' do
    let(:template_name) { subject.send(:template_name) }

    context 'when template does end with .gitlab-ci.yml' do
      let(:template) { 'my-template.gitlab-ci.yml' }

      it 'returns template name' do
        expect(template_name).to eq('my-template')
      end
    end

    context 'when template is nil' do
      let(:template) { nil }

      it 'returns nil' do
        expect(template_name).to be_nil
      end
    end

    context 'when template does not end with .gitlab-ci.yml' do
      let(:template) { 'my-template' }

      it 'returns nil' do
        expect(template_name).to be_nil
      end
    end
  end
end