summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-18 15:24:53 +0200
committerPaul Buetow <paul@buetow.org>2025-02-18 15:24:53 +0200
commitfdbd63c6798c6c92b482d03b1f197cbeb41a5448 (patch)
treedb78dc0d35d3a89bbff16a94f7e225d25154c490
parentcd1bdda5f963236dac849eb9257812f81c8af8ba (diff)
add ensure absent for file line
-rw-r--r--Rakefile1
-rw-r--r--TODO.md1
-rw-r--r--lib/dslkeywords/file.rb25
-rw-r--r--test/lib/dslkeywords/file_test.rb24
4 files changed, 44 insertions, 7 deletions
diff --git a/Rakefile b/Rakefile
index bca9c85..f87faac 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,5 +1,6 @@
require 'rake/testtask'
+# TODO: How to run only a specific test?
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
diff --git a/TODO.md b/TODO.md
index 9304832..120fec9 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,6 +1,5 @@
# TODO
-* Backup of files when changed
* 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 dbcc612..020a97f 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -22,9 +22,12 @@ module RCM
class File < Resource
include FileBackup
+ class UnsupportedOperation < StandardError; end
+
def initialize(file_path)
super(file_path)
@file_path = file_path
+ @is = :installed
end
def content(text = nil)
@@ -36,7 +39,7 @@ module RCM
def create_parent_directory = @create_parent = true
def from_sourcefile = @from_sourcefile = true
def from_template = @from_template = true
- def ensure_line(line) = @ensure_line = line
+ def line(line) = @ensure_line = line
def path(file_path = nil)
return @file_path if file_path.nil?
@@ -44,17 +47,23 @@ module RCM
@file_path = file_path
end
+ def is(what)
+ @is = what.to_sym
+ raise UnsupportedOperation, "Unsupported operation #{@is}" unless %i[present absent].include?(@is)
+ end
+
def evaluate!
return unless super
return evaluate_ensure_line! unless @ensure_line.nil?
- write_content!(real_content)
+ write!(real_content)
end
private
def evaluate_ensure_line!
- return write_content!(@ensure_line) unless ::File.file?(@file_path)
+ return evaluate_ensure_line_absent! if @is == :absent
+ return write!(@ensure_line) unless ::File.file?(@file_path)
return if ::File.readlines(@file_path, chomp: true).include?(@ensure_line)
::File.open(@file_path, 'a') do |fd|
@@ -62,7 +71,15 @@ module RCM
end
end
- def write_content!(text)
+ def evaluate_ensure_line_absent!
+ return unless ::File.file?(@file_path)
+
+ write!(::File.readlines(@file_path, chomp: true).reject do |line|
+ line == @ensure_line
+ end.join("\n"))
+ end
+
+ def write!(text)
info "Managing file #{@file_path}"
create_parent_directory!
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
index de1615a..60b659e 100644
--- a/test/lib/dslkeywords/file_test.rb
+++ b/test/lib/dslkeywords/file_test.rb
@@ -50,12 +50,32 @@ class RCMFileTest < Minitest::Test
assert_equal 'One plus two is 3!', File.read(FILE_PATH)
end
- def test_ensure_line
+ def test_line
File.write(FILE_PATH, "Hey there\n")
- configure_from_scratch { file(FILE_PATH) { ensure_line 'Whats up?' } }
+ configure_from_scratch { file(FILE_PATH) { line 'Whats up?' } }
assert_equal "Hey there\nWhats up?\n", File.read(FILE_PATH)
end
+ def test_line_absent
+ File.write(FILE_PATH, "Hey there\nWhats up?")
+ configure_from_scratch do
+ file(FILE_PATH) do
+ line 'Whats up?'
+ is :absent
+ end
+ end
+ assert_equal 'Hey there', File.read(FILE_PATH)
+
+ File.write(FILE_PATH, "Hey there\nWhats up?")
+ configure_from_scratch do
+ file(FILE_PATH) do
+ line 'Hey there'
+ is :absent
+ end
+ end
+ assert_equal 'Whats up?', File.read(FILE_PATH)
+ end
+
def test_create_parent_directory
file_path = "#{DIR_PATH}/foo/bar/baz/foo.txt"
configure_from_scratch do