summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-19 12:09:59 +0200
committerPaul Buetow <paul@buetow.org>2025-02-19 12:09:59 +0200
commitbbd4d006e08235c83900f206f7e4e772fbad6bd4 (patch)
treec3674605fef3a5d679dac488038946a5d6c9647b
parent967288d15793d43bafffda4bb18043262ada1a40 (diff)
more on symlinks
-rw-r--r--TODO.md1
-rw-r--r--lib/dslkeywords/file.rb19
-rw-r--r--test/lib/dslkeywords/file_test.rb51
3 files changed, 67 insertions, 4 deletions
diff --git a/TODO.md b/TODO.md
index 120fec9..1af7ab4 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,5 +1,6 @@
# TODO
+* Backup file when it gets deleted
* Recursively install a directory
* Support for file deletion
* Support for file modes (owner, chmod)
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 7de7492..7bbd587 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -51,7 +51,10 @@ module RCM
def evaluate!
return unless super
return evaluate_ensure_line! unless @ensure_line.nil?
- return evaluate_absent! if %i[absent].include?(@is)
+ return evaluate_absent! if @is == :absent
+
+ create_parent_directory! if @manage_directory
+ return evaluate_symlink! if @is == :symlink
write!(real_content)
end
@@ -99,10 +102,10 @@ module RCM
end
end
+ def evaluate_symlink! = FileUtils.ln_sf(real_content, @file_path)
+
def write!(text)
info "Managing file #{@file_path}"
-
- create_parent_directory!
debug text if option :debug
tmp_path = "#{@file_path}.rcmtmp"
@@ -124,7 +127,7 @@ module RCM
def create_parent_directory!
dirname = ::File.dirname(@file_path)
- return if ::File.directory?(dirname) || !@manage_directory
+ return if ::File.directory?(dirname)
info "Creating parent directory #{dirname}"
FileUtils.mkdir_p(dirname)
@@ -146,5 +149,13 @@ module RCM
self << f
f
end
+
+ def symlink(...)
+ return unless @conds_met
+
+ f = file(...)
+ f.is(:symlink)
+ f
+ end
end
end
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
index f52d871..d27ae77 100644
--- a/test/lib/dslkeywords/file_test.rb
+++ b/test/lib/dslkeywords/file_test.rb
@@ -160,4 +160,55 @@ class RCMFileTest < Minitest::Test
assert File.file?(backup_path)
assert_equal original_content, File.read(backup_path)
end
+
+ def test_create_symlink
+ symlink1_path = "#{DIR_PATH}/the_symlink1"
+ symlink2_path = "#{DIR_PATH}/the_symlink2"
+ symlink_target = "#{DIR_PATH}/the_symlink_target"
+
+ configure_from_scratch do
+ file symlink1_path do
+ is symlink
+ manage directory
+ symlink_target
+ end
+
+ symlink symlink2_path do
+ manage directory
+ symlink_target
+ end
+ end
+
+ assert File.symlink?(symlink1_path)
+ assert_equal symlink_target, File.readlink(symlink1_path)
+
+ assert File.symlink?(symlink2_path)
+ assert_equal symlink_target, File.readlink(symlink2_path)
+ end
+
+ def test_change_symlink
+ symlink_path = "#{DIR_PATH}/the_symlink"
+ symlink_target1 = "#{DIR_PATH}/the_symlink_target1"
+ symlink_target2 = "#{DIR_PATH}/the_symlink_target2"
+
+ configure_from_scratch do
+ symlink original do
+ path symlink_path
+ manage directory
+ symlink_target1
+ end
+
+ symlink changed do
+ path symlink_path
+ # TODO: Make it so that 'requires symlink original' works here, this
+ # requires to refactor the file class into multiple modules and two
+ # classes file and symlink
+ requires file original
+ symlink_target2
+ end
+ end
+
+ assert File.symlink?(symlink_path)
+ assert_equal symlink_target2, File.readlink(symlink_path)
+ end
end