summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-25 22:32:17 +0200
committerPaul Buetow <paul@buetow.org>2025-02-25 22:32:17 +0200
commit24a0c5406251db11cf12bec4f0429446080839a8 (patch)
tree6f827ea21fd580234d07905136ae1d7bdbf94545
parent274a42eff70cec7cb2c04d73780b4b57ce843b15 (diff)
can purge a dir
-rw-r--r--TODO.md1
-rw-r--r--lib/dslkeywords/file.rb26
-rw-r--r--test/lib/dslkeywords/directory_test.rb17
3 files changed, 34 insertions, 10 deletions
diff --git a/TODO.md b/TODO.md
index e56612e..8e5118a 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,6 +1,5 @@
# TODO
-* Purge a directory
* Recursively install a directory
* Support for file modes (owner, chmod)
* User and group management
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index e52e41f..50728cb 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -54,9 +54,10 @@ module RCM
@is = :present
end
- def is(what) = @is = validate(__method__, what.to_sym, :present, :absent)
+ def is(what) = @is = validate(__method__, what.to_sym, :present, :absent, :purged)
def manage(what) = @manage_directory = validate(__method__, what.to_sym, :directory) == :directory
def path(file_path = nil) = file_path.nil? ? @file_path : @file_path = file_path
+ def without(what) = @without_backup = validate(__method__, what.to_sym, :backup) == :backup
def content(text = nil)
if text.nil?
@@ -118,13 +119,12 @@ module RCM
# Managing files
class File < BaseFile
def line(line) = @ensure_line = line
- def without(what) = @without_backup = validate(__method__, what.to_sym, :backup) == :backup
def evaluate!
return unless super
return evaluate_ensure_line! unless @ensure_line.nil?
- return evaluate_absent! if @is == :absent
+ return evaluate_absent! if %i[absent purged].include?(@is)
create_parent_directory! if @manage_directory
@@ -181,7 +181,7 @@ module RCM
class Symlink < BaseFile
def evaluate!
return unless super
- return evaluate_absent! if @is == :absent
+ return evaluate_absent! if %i[absent purged].include?(@is)
create_parent_directory! if @manage_directory
dry? "Creating symlink #{@file_path}" do
@@ -194,8 +194,12 @@ module RCM
def evaluate!
return unless super
- evaluate_present! if @is == :present
- evaluate_absent! if @is == :absent
+ case @is
+ when :present
+ evaluate_present!
+ when :absent, :purged
+ evaluate_absent!
+ end
end
def evaluate_present!
@@ -212,10 +216,14 @@ module RCM
return unless ::File.directory?(@file_path)
backup!(@file_path)
- dry? "Deleting directory #{@file_path}" do
- Dir.delete(@file_path) if ::File.directory?(@file_path)
- cleanup_parent_directory! if @manage_directory
+ what = @is == :purged ? 'Purging' : 'Deleting'
+
+ dry? "#{what} directory #{@file_path}" do
+ if ::File.directory?(@file_path)
+ @is == :purged ? FileUtils.rm_r(@file_path) : Dir.delete(@file_path)
+ end
end
+ cleanup_parent_directory! if @manage_directory
end
end
diff --git a/test/lib/dslkeywords/directory_test.rb b/test/lib/dslkeywords/directory_test.rb
index 302db37..41229eb 100644
--- a/test/lib/dslkeywords/directory_test.rb
+++ b/test/lib/dslkeywords/directory_test.rb
@@ -33,4 +33,21 @@ class RCMDirectoryTest < Minitest::Test
end
refute File.directory?(DIR_PATH)
end
+
+ def test_purge_directory
+ configure_from_scratch do
+ file create do
+ path "#{DIR_PATH}/subdir/a_file.txt"
+ manage directory
+ 'some content'
+ end
+ directory purge do
+ path DIR_PATH
+ without backup
+ is purged
+ requires file create
+ end
+ end
+ refute File.directory?(DIR_PATH)
+ end
end