diff options
| -rw-r--r-- | lib/dslkeywords/file.rb | 26 | ||||
| -rw-r--r-- | lib/dslkeywords/keyword.rb | 10 | ||||
| -rw-r--r-- | lib/dslkeywords/resource.rb | 4 | ||||
| -rw-r--r-- | test/lib/dslkeywords/dependency_test.rb | 2 | ||||
| -rw-r--r-- | test/lib/dslkeywords/file_test.rb | 14 |
5 files changed, 38 insertions, 18 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 06f66aa..b3f5be4 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -38,10 +38,18 @@ module RCM @content = text.instance_of?(Array) ? text.join("\n") : text end - def create_parent_directory = @create_parent = true + def manage(what) + what = what.to_sym + raise UnsupportedOperation, "Unsupported 'manage' operation #{what}" unless %i[directory].include?(what) + + @manage_directory = true + end + + def directory = :directory + def from_sourcefile = @from_sourcefile = true def from_template = @from_template = true - def without_backup = @without_backup = true + def without_backup = @without_backup = true # TODO: Rename to 'without backup' def line(line) = @ensure_line = line def path(file_path = nil) @@ -52,13 +60,17 @@ module RCM def is(what) @is = what.to_sym - raise UnsupportedOperation, "Unsupported operation #{@is}" unless %i[present absent clean].include?(@is) + raise UnsupportedOperation, "Unsupported 'is' operation #{@is}" unless %i[present absent].include?(@is) end + # To allow 'is present' + def present = :present + def absent = :absent + def evaluate! return unless super return evaluate_ensure_line! unless @ensure_line.nil? - return evaluate_absent! if %i[absent clean].include?(@is) + return evaluate_absent! if %i[absent].include?(@is) write!(real_content) end @@ -66,7 +78,7 @@ module RCM private def evaluate_ensure_line! - return evaluate_ensure_line_absent! if %i[absent clean].include?(@is) + return evaluate_ensure_line_absent! if %i[absent].include?(@is) return write!(@ensure_line) unless ::File.file?(@file_path) return if ::File.readlines(@file_path, chomp: true).include?(@ensure_line) @@ -88,7 +100,7 @@ module RCM info("Deleting #{@file_path}") ::File.delete(@file_path) end - return unless @is == :clean + return unless @manage_directory parent_dir = ::File.dirname(@file_path) while Dir.empty?(parent_dir) @@ -123,7 +135,7 @@ module RCM def create_parent_directory! dirname = ::File.dirname(@file_path) - return unless !::File.directory?(dirname) && @create_parent + return if ::File.directory?(dirname) || !@manage_directory info "Creating parent directory #{dirname}" FileUtils.mkdir_p(dirname) diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb index 2dfff91..ee3cfba 100644 --- a/lib/dslkeywords/keyword.rb +++ b/lib/dslkeywords/keyword.rb @@ -1,3 +1,5 @@ +require 'set' + require_relative '../options' require_relative '../log' @@ -11,5 +13,13 @@ module RCM def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '').downcase}('#{name}')" def to_s = @id + + class KeywordError < StandardError; end + + def method_missing(method_name, *args) + raise KeywordError, "No such method: #{method_name}" + end + + def respond_to_missing? = true end end diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index f665a50..be67b0a 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -14,10 +14,8 @@ module RCM end end - class NoSuchResourceType < StandardError; end - def method_missing(method_name, *args) - raise NoSuchResourceType, "No such resource type: #{method_name}" unless @valid_resources.include?(method_name) + super(method_name, *args) unless @valid_resources.include?(method_name) args.map { |arg| "#{method_name}('#{arg}')" } end diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb index aeb2790..48a3e1a 100644 --- a/test/lib/dslkeywords/dependency_test.rb +++ b/test/lib/dslkeywords/dependency_test.rb @@ -34,7 +34,7 @@ class RCMDependencyTest < Minitest::Test end def test_depends_on_invalid_resource - assert_raises(RCM::ResourceDependencies::NoSuchResourceType) do + assert_raises(RCM::Keyword::KeywordError) do configure_from_scratch do notify { depends_on invalid('baz') } end diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb index eaece2d..4c38dc9 100644 --- a/test/lib/dslkeywords/file_test.rb +++ b/test/lib/dslkeywords/file_test.rb @@ -47,15 +47,15 @@ class RCMFileTest < Minitest::Test configure_from_scratch do file :create_file_empty_directory_test do path file_path - create_parent_directory - is :present + manage directory :text end file :delete_file_empty_directory_test do path file_path + is absent + manage directory depends_on file :create_file_empty_directory_test - is :clean end end @@ -116,11 +116,11 @@ class RCMFileTest < Minitest::Test assert_equal 'Whats up?', File.read(FILE_PATH) end - def test_create_parent_directory + def test_manage_directory file_path = "#{DIR_PATH}/foo/bar/baz/foo.txt" configure_from_scratch do file file_path do - create_parent_directory + manage directory :content end end @@ -137,13 +137,13 @@ class RCMFileTest < Minitest::Test configure_from_scratch do file :original do path file_path - create_parent_directory + manage directory original_content end file :new do path file_path - create_parent_directory + manage directory depends_on file(:original) :new_content end |
