diff options
| -rw-r--r-- | lib/dslkeywords/resource.rb | 34 | ||||
| -rw-r--r-- | test/lib/dslkeywords/dependency_test.rb | 26 |
2 files changed, 48 insertions, 12 deletions
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index 6612428..884dec9 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -1,31 +1,47 @@ +require 'set' + require_relative 'keyword' module RCM # To track recource dependencies - module Dependency - # Only to have the resourcename[id] syntax available in the DSL - class SyntaxSugar - def initialize(name) - @name = name + module ResourceDependencies + def initialize(...) + super(...) + @valid_resources = Set.new + ObjectSpace.each_object(Class).each do |klass| + @valid_resources << klass.to_s.sub('RCM::', '').downcase.to_sym if klass < Resource end + end + # Only to have the resourcename[id] syntax available in the DSL + class SyntaxSugar + def initialize(name) = @name = name def [](other) = "#{@name}['#{other}']" end - def method_missing(method_name) = SyntaxSugar.new(method_name) + class NoSuchResource < StandardError; end + + def method_missing(method_name) + raise NoSuchResource, "No such resource: #{method_name}" unless @valid_resources.include?(method_name) + + SyntaxSugar.new(method_name) + end + def respond_to_missing? = true def depends_on(*others) - @depends_on = {} if @depends_on.nil? + @dependencies = {} if @dependencies.nil? others.each do |other| info "Registered dependency on #{other}" - @depends_on[other] = {} + @dependencies[other] = {} end end + + def dependencies = @dependencies.nil ? [] : @dependencies end # A resource is something concrete to be managed, e.g. a file, or a CRON job. class Resource < Keyword - include Dependency + include ResourceDependencies end end diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb index ccbaec6..3e23282 100644 --- a/test/lib/dslkeywords/dependency_test.rb +++ b/test/lib/dslkeywords/dependency_test.rb @@ -4,14 +4,34 @@ require 'fileutils' require_relative '../../../lib/dsl' class RCMDependencyTest < Minitest::Test - def test_dependency + def test_depends_on configure_from_scratch do notify 'foo' do - depends_on notify['bar'], file['baz'] - :HELLO + depends_on notify['bar'], notify['baz'] + :foo_message end notify 'bar' + + notify 'baz' do + depends_on notify['bar'] + :baz_message + end + end + end + + def test_depends_on_invalid_resource + correct_exception_thrown = false + + configure_from_scratch do + notify 'foo' do + depends_on invalid['baz'] + :foo_message + end + rescue RCM::ResourceDependencies::NoSuchResource + correct_exception_thrown = true end + + assert correct_exception_thrown end end |
