summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-25 22:42:45 +0200
committerPaul Buetow <paul@buetow.org>2025-02-25 22:42:45 +0200
commitd7254b1f4ab4ef482ac9adc1dbb0b896b1c63ebb (patch)
treef2292bb1a1fef690a725f88d70685e4efacfd27f
parent24a0c5406251db11cf12bec4f0429446080839a8 (diff)
add touch file
-rw-r--r--lib/dslkeywords/file.rb27
-rw-r--r--test/lib/dslkeywords/directory_test.rb7
-rw-r--r--test/lib/dslkeywords/touch_test.rb37
3 files changed, 67 insertions, 4 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 50728cb..39e0499 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -190,6 +190,23 @@ module RCM
end
end
+ # Emtpy file
+ class Touch < BaseFile
+ def is(what) = @is = validate(__method__, what.to_sym, :present, :absent, :purged, :updated)
+
+ def evaluate!
+ return unless super
+ return evaluate_absent! if %i[absent purged].include?(@is)
+
+ create_parent_directory! if @manage_directory
+ dry? "Touching #{@file_path}" do
+ return if @is != :updated && ::File.file?(@file_path)
+
+ FileUtils.touch(@file_path)
+ end
+ end
+ end
+
class Directory < BaseFile
def evaluate!
return unless super
@@ -249,6 +266,16 @@ module RCM
s
end
+ def touch(file_path = nil, &block)
+ return :touch if file_path.nil?
+ return unless @conds_met
+
+ t = Touch.new(file_path)
+ t.instance_eval(&block) if block
+ self << t
+ t
+ end
+
def directory(file_path = nil, &block)
return :directory if file_path.nil?
return unless @conds_met
diff --git a/test/lib/dslkeywords/directory_test.rb b/test/lib/dslkeywords/directory_test.rb
index 41229eb..9fd249f 100644
--- a/test/lib/dslkeywords/directory_test.rb
+++ b/test/lib/dslkeywords/directory_test.rb
@@ -36,16 +36,15 @@ class RCMDirectoryTest < Minitest::Test
def test_purge_directory
configure_from_scratch do
- file create do
+ touch create do
path "#{DIR_PATH}/subdir/a_file.txt"
manage directory
- 'some content'
end
directory purge do
path DIR_PATH
- without backup
+ requires touch create
is purged
- requires file create
+ without backup
end
end
refute File.directory?(DIR_PATH)
diff --git a/test/lib/dslkeywords/touch_test.rb b/test/lib/dslkeywords/touch_test.rb
new file mode 100644
index 0000000..a78b325
--- /dev/null
+++ b/test/lib/dslkeywords/touch_test.rb
@@ -0,0 +1,37 @@
+require 'minitest/autorun'
+require 'fileutils'
+
+require_relative '../../../lib/dsl'
+
+class RCMTouchTest < Minitest::Test
+ FILE_PATH = './.touch_test.rcmtmp'.freeze
+
+ Minitest.after_run do
+ File.unlink(FILE_PATH) if File.file?(FILE_PATH)
+ end
+
+ def test_touch_file
+ configure_from_scratch do
+ touch FILE_PATH
+ end
+
+ assert File.file?(FILE_PATH)
+ assert File.size(FILE_PATH).zero?
+ end
+
+ def test_touch_update_file
+ configure_from_scratch do
+ touch create do
+ path FILE_PATH
+ end
+ touch update do
+ path FILE_PATH
+ is updated
+ requires touch create
+ end
+ end
+
+ assert File.file?(FILE_PATH)
+ assert File.size(FILE_PATH).zero?
+ end
+end