Commit 1b4dbf8f authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'pedropombeiro/26345/3-add-lookup-to-vars-collection' into 'master'

Add lookup methods in Variables::Collection

See merge request gitlab-org/gitlab!54503
parents 9ead7c3a 85810edb
...@@ -10,13 +10,18 @@ module Gitlab ...@@ -10,13 +10,18 @@ module Gitlab
def initialize(variables = [], errors = nil) def initialize(variables = [], errors = nil)
@variables = [] @variables = []
@variables_by_key = {}
@errors = errors @errors = errors
variables.each { |variable| self.append(variable) } variables.each { |variable| self.append(variable) }
end end
def append(resource) def append(resource)
tap { @variables.append(Collection::Item.fabricate(resource)) } item = Collection::Item.fabricate(resource)
@variables.append(item)
@variables_by_key[item[:key]] = item
self
end end
def concat(resources) def concat(resources)
...@@ -36,6 +41,14 @@ module Gitlab ...@@ -36,6 +41,14 @@ module Gitlab
end end
end end
def [](key)
@variables_by_key[key]
end
def size
@variables.size
end
def to_runner_variables def to_runner_variables
self.map(&:to_runner_variable) self.map(&:to_runner_variable)
end end
......
...@@ -14,6 +14,10 @@ module Gitlab ...@@ -14,6 +14,10 @@ module Gitlab
} }
end end
def value
@variable.fetch(:value)
end
def [](key) def [](key)
@variable.fetch(key) @variable.fetch(key)
end end
......
...@@ -52,7 +52,7 @@ module Gitlab ...@@ -52,7 +52,7 @@ module Gitlab
end end
def tsort_each_child(variable, &block) def tsort_each_child(variable, &block)
each_variable_reference(variable[:value], &block) each_variable_reference(variable.value, &block)
end end
def input_vars def input_vars
......
...@@ -32,6 +32,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do ...@@ -32,6 +32,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do
it 'saves given value' do it 'saves given value' do
expect(subject[:key]).to eq variable_key expect(subject[:key]).to eq variable_key
expect(subject[:value]).to eq expected_value expect(subject[:value]).to eq expected_value
expect(subject.value).to eq expected_value
end end
end end
......
...@@ -98,6 +98,50 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ...@@ -98,6 +98,50 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
end end
end end
describe '#[]' do
variable = { key: 'VAR', value: 'value', public: true, masked: false }
collection = described_class.new([variable])
it 'returns nil for a non-existent variable name' do
expect(collection['UNKNOWN_VAR']).to be_nil
end
it 'returns Item for an existent variable name' do
expect(collection['VAR']).to be_an_instance_of(Gitlab::Ci::Variables::Collection::Item)
expect(collection['VAR'].to_runner_variable).to eq(variable)
end
end
describe '#size' do
it 'returns zero for empty collection' do
collection = described_class.new([])
expect(collection.size).to eq(0)
end
it 'returns 2 for collection with 2 variables' do
collection = described_class.new(
[
{ key: 'VAR1', value: 'value', public: true, masked: false },
{ key: 'VAR2', value: 'value', public: true, masked: false }
])
expect(collection.size).to eq(2)
end
it 'returns 3 for collection with 2 duplicate variables' do
collection = described_class.new(
[
{ key: 'VAR1', value: 'value', public: true, masked: false },
{ key: 'VAR2', value: 'value', public: true, masked: false },
{ key: 'VAR1', value: 'value', public: true, masked: false }
])
expect(collection.size).to eq(3)
end
end
describe '#to_runner_variables' do describe '#to_runner_variables' do
it 'creates an array of hashes in a runner-compatible format' do it 'creates an array of hashes in a runner-compatible format' do
collection = described_class.new([{ key: 'TEST', value: '1' }]) collection = described_class.new([{ key: 'TEST', value: '1' }])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment