Welcome to mirror list, hosted at ThFree Co, Russian Federation.

yaml_spec.rb « config « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3576dd481c65f8a2c5459d8762d0db1c51a8fb9d (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Config::Yaml, feature_category: :pipeline_composition do
  describe '.load!' do
    it 'loads a YAML file' do
      yaml = <<~YAML
      image: 'image:1.0'
      texts:
        nested_key: 'value1'
        more_text:
          more_nested_key: 'value2'
      YAML

      config = described_class.load!(yaml)

      expect(config).to eq({
        image: 'image:1.0',
        texts: {
          nested_key: 'value1',
          more_text: {
            more_nested_key: 'value2'
          }
        }
      })
    end

    context 'when YAML is invalid' do
      let(:yaml) { 'some: invalid: syntax' }

      it 'raises an error' do
        expect { described_class.load!(yaml) }
          .to raise_error ::Gitlab::Config::Loader::FormatError, /mapping values are not allowed in this context/
      end
    end
  end

  describe '.load_result!' do
    let_it_be(:project) { create(:project) }

    subject(:result) { described_class.load_result!(yaml, project: project) }

    context 'when syntax is invalid' do
      let(:yaml) { 'some: invalid: syntax' }

      it 'returns an invalid result object' do
        expect(result).not_to be_valid
        expect(result.error).to be_a ::Gitlab::Config::Loader::FormatError
      end
    end

    context 'when the first document is a header' do
      context 'with explicit document start marker' do
        let(:yaml) do
          <<~YAML
            ---
            spec:
            ---
            b: 2
          YAML
        end

        it 'considers the first document as header and the second as content' do
          expect(result).to be_valid
          expect(result.error).to be_nil
          expect(result.header).to eq({ spec: nil })
          expect(result.content).to eq({ b: 2 })
        end
      end
    end

    context 'when first document is empty' do
      let(:yaml) do
        <<~YAML
          ---
          ---
          b: 2
        YAML
      end

      it 'considers the first document as header and the second as content' do
        expect(result).not_to have_header
      end
    end

    context 'when first document is an empty hash' do
      let(:yaml) do
        <<~YAML
          {}
          ---
          b: 2
        YAML
      end

      it 'returns second document as a content' do
        expect(result).not_to have_header
        expect(result.content).to eq({ b: 2 })
      end
    end

    context 'when first an array' do
      let(:yaml) do
        <<~YAML
          ---
           - a
           - b
          ---
          b: 2
        YAML
      end

      it 'considers the first document as header and the second as content' do
        expect(result).not_to have_header
      end
    end

    context 'when the first document is not a header' do
      let(:yaml) do
        <<~YAML
          a: 1
          ---
          b: 2
        YAML
      end

      it 'considers the first document as content for backwards compatibility' do
        expect(result).to be_valid
        expect(result.error).to be_nil
        expect(result).not_to have_header
        expect(result.content).to eq({ a: 1 })
      end

      context 'with explicit document start marker' do
        let(:yaml) do
          <<~YAML
            ---
            a: 1
            ---
            b: 2
          YAML
        end

        it 'considers the first document as content for backwards compatibility' do
          expect(result).to be_valid
          expect(result.error).to be_nil
          expect(result).not_to have_header
          expect(result.content).to eq({ a: 1 })
        end
      end
    end

    context 'when the first document is not a header and second document is empty' do
      let(:yaml) do
        <<~YAML
          a: 1
          ---
        YAML
      end

      it 'considers the first document as content' do
        expect(result).to be_valid
        expect(result.error).to be_nil
        expect(result).not_to have_header
        expect(result.content).to eq({ a: 1 })
      end

      context 'with explicit document start marker' do
        let(:yaml) do
          <<~YAML
            ---
            a: 1
            ---
          YAML
        end

        it 'considers the first document as content' do
          expect(result).to be_valid
          expect(result.error).to be_nil
          expect(result).not_to have_header
          expect(result.content).to eq({ a: 1 })
        end
      end
    end
  end
end