summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/product/variables_spec.rb
blob: 230b001d620431377ddd3759cbd86cc4729ad7f6 (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
# frozen_string_literal: true

require 'fast_spec_helper'
require_dependency 'active_model'

RSpec.describe Gitlab::Ci::Config::Entry::Product::Variables do
  let(:entry) { described_class.new(config) }

  describe 'validations' do
    context 'when entry config value is correct' do
      let(:config) do
        {
          'VARIABLE_1' => 1,
          'VARIABLE_2' => 'value 2',
          'VARIABLE_3' => :value_3,
          :VARIABLE_4 => 'value 4',
          5 => ['value 5'],
          'VARIABLE_6' => ['value 6']
        }
      end

      describe '#value' do
        it 'returns hash with key value strings' do
          expect(entry.value).to match({
            'VARIABLE_1' => ['1'],
            'VARIABLE_2' => ['value 2'],
            'VARIABLE_3' => ['value_3'],
            'VARIABLE_4' => ['value 4'],
            '5' => ['value 5'],
            'VARIABLE_6' => ['value 6']
          })
        end
      end

      describe '#errors' do
        it 'does not append errors' do
          expect(entry.errors).to be_empty
        end
      end

      describe '#valid?' do
        it 'is valid' do
          expect(entry).to be_valid
        end
      end
    end

    context 'when entry value is not correct' do
      shared_examples 'invalid variables' do |message|
        describe '#errors' do
          it 'saves errors' do
            expect(entry.errors).to include(message)
          end
        end

        describe '#valid?' do
          it 'is not valid' do
            expect(entry).not_to be_valid
          end
        end
      end

      context 'with array' do
        let(:config) { [:VAR, 'test'] }

        it_behaves_like 'invalid variables', /should be a hash of key value pairs/
      end

      context 'with empty array' do
        let(:config) { { VAR: 'test', VAR2: [] } }

        it_behaves_like 'invalid variables', /should be a hash of key value pairs/
      end

      context 'with nested array' do
        let(:config) { { VAR: 'test', VAR2: [1, [2]] } }

        it_behaves_like 'invalid variables', /should be a hash of key value pairs/
      end

      context 'with only one variable' do
        let(:config) { { VAR: 'test' } }

        it_behaves_like 'invalid variables', /variables config requires at least 2 items/
      end
    end
  end
end