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

multi_doc_yaml_spec.rb « loader « config « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f63aacecce6a5faef38ade28f0d960c1a237a50d (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
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Config::Loader::MultiDocYaml, feature_category: :pipeline_composition do
  let(:loader) { described_class.new(yml, max_documents: 2) }

  describe '#load!' do
    context 'when a simple single delimiter is being used' do
      let(:yml) do
        <<~YAML
        spec:
          inputs:
            env:
        ---
        test:
          script: echo "$[[ inputs.env ]]"
        YAML
      end

      it 'returns the loaded YAML with all keys as symbols' do
        expect(loader.load!).to contain_exactly(
          { spec: { inputs: { env: nil } } },
          { test: { script: 'echo "$[[ inputs.env ]]"' } }
        )
      end
    end

    context 'when the delimiter has a trailing configuration' do
      let(:yml) do
        <<~YAML
        spec:
          inputs:
            test_input:
        --- !test/content
        test_job:
          script: echo "$[[ inputs.test_input ]]"
        YAML
      end

      it 'returns the loaded YAML with all keys as symbols' do
        expect(loader.load!).to contain_exactly(
          { spec: { inputs: { test_input: nil } } },
          { test_job: { script: 'echo "$[[ inputs.test_input ]]"' } }
        )
      end
    end

    context 'when the YAML file has a leading delimiter' do
      let(:yml) do
        <<~YAML
        ---
        spec:
          inputs:
            test_input:
        --- !test/content
        test_job:
          script: echo "$[[ inputs.test_input ]]"
        YAML
      end

      it 'returns the loaded YAML with all keys as symbols' do
        expect(loader.load!).to contain_exactly(
          { spec: { inputs: { test_input: nil } } },
          { test_job: { script: 'echo "$[[ inputs.test_input ]]"' } }
        )
      end
    end

    context 'when the delimiter is followed by content on the same line' do
      let(:yml) do
        <<~YAML
        --- a: 1
        --- b: 2
        YAML
      end

      it 'loads the content as part of the document' do
        expect(loader.load!).to contain_exactly({ a: 1 }, { b: 2 })
      end
    end

    context 'when the delimiter does not have trailing whitespace' do
      let(:yml) do
        <<~YAML
        --- a: 1
        ---b: 2
        YAML
      end

      it 'is not a valid delimiter' do
        expect(loader.load!).to contain_exactly({ :'---b' => 2, a: 1 }) # rubocop:disable Style/HashSyntax
      end
    end

    context 'when the YAML file has whitespace preceding the content' do
      let(:yml) do
        <<-EOYML
          variables:
            SUPPORTED: "parsed"

          workflow:
            rules:
              - if: $VAR == "value"

          hello:
            script: echo world
        EOYML
      end

      it 'loads everything correctly' do
        expect(loader.load!).to contain_exactly(
          {
            variables: { SUPPORTED: 'parsed' },
            workflow: { rules: [{ if: '$VAR == "value"' }] },
            hello: { script: 'echo world' }
          }
        )
      end
    end

    context 'when the YAML file is empty' do
      let(:yml) { '' }

      it 'returns an empty array' do
        expect(loader.load!).to be_empty
      end
    end

    context 'when there are more than the maximum number of documents' do
      let(:yml) do
        <<~YAML
        --- a: 1
        --- b: 2
        --- c: 3
        --- d: 4
        YAML
      end

      it 'stops splitting documents after the maximum number' do
        expect(loader.load!).to contain_exactly({ a: 1 }, { b: 2 })
      end
    end
  end

  describe '#load_raw!' do
    let(:yml) do
      <<~YAML
      spec:
        inputs:
          test_input:
      --- !test/content
      test_job:
        script: echo "$[[ inputs.test_input ]]"
      YAML
    end

    it 'returns the loaded YAML with all keys as strings' do
      expect(loader.load_raw!).to contain_exactly(
        { 'spec' => { 'inputs' => { 'test_input' => nil } } },
        { 'test_job' => { 'script' => 'echo "$[[ inputs.test_input ]]"' } }
      )
    end
  end

  describe '#valid?' do
    context 'when a document is invalid' do
      let(:yml) do
        <<~YAML
        a: b
        ---
        c
        YAML
      end

      it 'returns false' do
        expect(loader).not_to be_valid
      end
    end

    context 'when the number of documents is below the maximum and all documents are valid' do
      let(:yml) do
        <<~YAML
        a: b
        ---
        c: d
        YAML
      end

      it 'returns true' do
        expect(loader).to be_valid
      end
    end
  end
end