summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/variables/helpers_spec.rb
blob: 2a1cdaeb3a70186e9cb5246d10bc9b5a14fcecff (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Ci::Variables::Helpers do
  describe '.merge_variables' do
    let(:current_variables) do
      [{ key: 'key1', value: 'value1' },
       { key: 'key2', value: 'value2' }]
    end

    let(:new_variables) do
      [{ key: 'key2', value: 'value22' },
       { key: 'key3', value: 'value3' }]
    end

    let(:result) do
      [{ key: 'key1', value: 'value1' },
       { key: 'key2', value: 'value22' },
       { key: 'key3', value: 'value3' }]
    end

    subject { described_class.merge_variables(current_variables, new_variables) }

    it { is_expected.to match_array(result) }

    context 'when new variables is a hash' do
      let(:new_variables) do
        { 'key2' => 'value22', 'key3' => 'value3' }
      end

      let(:result) do
        [{ key: 'key1', value: 'value1' },
         { key: 'key2', value: 'value22' },
         { key: 'key3', value: 'value3' }]
      end

      it { is_expected.to match_array(result) }
    end

    context 'when new variables is a hash with symbol keys' do
      let(:new_variables) do
        { key2: 'value22', key3: 'value3' }
      end

      let(:result) do
        [{ key: 'key1', value: 'value1' },
         { key: 'key2', value: 'value22' },
         { key: 'key3', value: 'value3' }]
      end

      it { is_expected.to match_array(result) }
    end

    context 'when new variables is nil' do
      let(:new_variables) {}
      let(:result) do
        [{ key: 'key1', value: 'value1' },
         { key: 'key2', value: 'value2' }]
      end

      it { is_expected.to match_array(result) }
    end
  end

  describe '.transform_to_array' do
    subject { described_class.transform_to_array(variables) }

    context 'when values are strings' do
      let(:variables) do
        { 'key1' => 'value1', 'key2' => 'value2' }
      end

      let(:result) do
        [{ key: 'key1', value: 'value1' },
         { key: 'key2', value: 'value2' }]
      end

      it { is_expected.to match_array(result) }
    end

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

      it { is_expected.to match_array([]) }
    end

    context 'when values are hashes' do
      let(:variables) do
        { 'key1' => { value: 'value1', description: 'var 1' }, 'key2' => { value: 'value2' } }
      end

      let(:result) do
        [{ key: 'key1', value: 'value1', description: 'var 1' },
         { key: 'key2', value: 'value2' }]
      end

      it { is_expected.to match_array(result) }

      context 'when a value data has `key` as a key' do
        let(:variables) do
          { 'key1' => { value: 'value1', key: 'new_key1' }, 'key2' => { value: 'value2' } }
        end

        let(:result) do
          [{ key: 'key1', value: 'value1' },
           { key: 'key2', value: 'value2' }]
        end

        it { is_expected.to match_array(result) }
      end
    end
  end

  describe '.inherit_yaml_variables' do
    let(:from) do
      [{ key: 'key1', value: 'value1' },
       { key: 'key2', value: 'value2' }]
    end

    let(:to) do
      [{ key: 'key2', value: 'value22' },
       { key: 'key3', value: 'value3' }]
    end

    let(:inheritance) { true }

    let(:result) do
      [{ key: 'key1', value: 'value1' },
       { key: 'key2', value: 'value22' },
       { key: 'key3', value: 'value3' }]
    end

    subject { described_class.inherit_yaml_variables(from: from, to: to, inheritance: inheritance) }

    it { is_expected.to match_array(result) }

    context 'when inheritance is false' do
      let(:inheritance) { false }

      let(:result) do
        [{ key: 'key2', value: 'value22' },
         { key: 'key3', value: 'value3' }]
      end

      it { is_expected.to match_array(result) }
    end

    context 'when inheritance is array' do
      let(:inheritance) { ['key2'] }

      let(:result) do
        [{ key: 'key2', value: 'value22' },
         { key: 'key3', value: 'value3' }]
      end

      it { is_expected.to match_array(result) }
    end
  end
end