summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-22 12:18:04 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-22 12:18:04 +0200
commit90bf514ad7e1d56864d7aac9f5b7fb2a18fd0c5f (patch)
treeb1f405abd1fbaaa27368d2589a4bfda5da60a483
parent69dad9677c76564831204772b6fdbb079865cdee (diff)
downloadgitlab-ce-refactor/ci-config-logical-validation.tar.gz
Add IoC container which is CI config entry locatorrefactor/ci-config-logical-validation
-rw-r--r--lib/gitlab/ci/config/dependencies.rb26
-rw-r--r--spec/lib/gitlab/ci/config/dependencies_spec.rb42
2 files changed, 68 insertions, 0 deletions
diff --git a/lib/gitlab/ci/config/dependencies.rb b/lib/gitlab/ci/config/dependencies.rb
new file mode 100644
index 00000000000..9029e5f5155
--- /dev/null
+++ b/lib/gitlab/ci/config/dependencies.rb
@@ -0,0 +1,26 @@
+module Gitlab
+ module Ci
+ class Config
+ class Dependencies
+ class UnmetDependencyError < StandardError; end
+
+ def initialize
+ @entries = {}
+ end
+
+ def visit(entry)
+ @entries[entry.location] = entry
+ entry.dependencies = self
+ end
+
+ def entry(location)
+ unless @entries.has_key?(location)
+ raise UnmetDependencyError, "Unmet dependency #{location}"
+ end
+
+ @entries[location]
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/ci/config/dependencies_spec.rb b/spec/lib/gitlab/ci/config/dependencies_spec.rb
new file mode 100644
index 00000000000..4ed2e6ab300
--- /dev/null
+++ b/spec/lib/gitlab/ci/config/dependencies_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe Gitlab::Ci::Config::Dependencies do
+ let(:entry) { double('Entry') }
+
+ before do
+ allow(entry).to receive(:location).and_return('test:entry')
+ allow(entry).to receive(:dependencies=)
+ end
+
+ describe '#visit' do
+ it 'adds entry to registry' do
+ subject.visit(entry)
+
+ expect(subject.entry('test:entry')).to be entry
+ end
+
+ it 'sets itself as a service locator' do
+ expect(entry).to receive(:dependencies=).with(subject)
+
+ subject.visit(entry)
+ end
+ end
+
+ describe '#entry' do
+ context 'when entry location exists' do
+ it 'return entry object' do
+ subject.visit(entry)
+
+ expect(subject.entry('test:entry')).to be entry
+ end
+ end
+
+ context 'when entry location does not exist' do
+ it 'raises error' do
+ expect { subject.entry('no:entry') }.to raise_error(
+ Gitlab::Ci::Config::Dependencies::UnmetDependencyError
+ )
+ end
+ end
+ end
+end