summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/variables/helpers_spec.rb
blob: f13b334c10e95f5348700983bd76d611416a7faa (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
# 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', public: true },
       { key: 'key2', value: 'value22', public: true },
       { key: 'key3', value: 'value3', public: true }]
    end

    subject { described_class.merge_variables(current_variables, new_variables) }

    it { is_expected.to eq(result) }

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

      it { is_expected.to eq(result) }
    end

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

      it { is_expected.to eq(result) }
    end

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

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

  describe '.transform_to_yaml_variables' do
    let(:variables) do
      { 'key1' => 'value1', 'key2' => 'value2' }
    end

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

    subject { described_class.transform_to_yaml_variables(variables) }

    it { is_expected.to eq(result) }

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

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

  describe '.transform_from_yaml_variables' do
    let(:variables) do
      [{ key: 'key1', value: 'value1', public: true },
       { key: 'key2', value: 'value2', public: true }]
    end

    let(:result) do
      { 'key1' => 'value1', 'key2' => 'value2' }
    end

    subject { described_class.transform_from_yaml_variables(variables) }

    it { is_expected.to eq(result) }

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

      it { is_expected.to eq({}) }
    end

    context 'when variables is a hash' do
      let(:variables) do
        { key1: 'value1', 'key2' => 'value2' }
      end

      it { is_expected.to eq(result) }
    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', public: true },
       { key: 'key2', value: 'value22', public: true },
       { key: 'key3', value: 'value3', public: true }]
    end

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

    it { is_expected.to eq(result) }

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

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

      it { is_expected.to eq(result) }
    end

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

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

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