summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-18 12:48:10 +0200
committerPaul Buetow <paul@buetow.org>2025-02-18 12:48:10 +0200
commit2ea74a7ce93bc4759243cb6054b8d70a73e5716a (patch)
tree1af01270a9317221e076870dcffee6cc65901d35
parent2327c02594db5b50b2b59bcc5e731c5723f5423b (diff)
testing backup
-rw-r--r--lib/dslkeywords/file.rb48
-rw-r--r--test/lib/dslkeywords/file_test.rb24
2 files changed, 50 insertions, 22 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 51e241e..3476079 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -7,26 +7,24 @@ require_relative 'resource'
module RCM
# Backup the file on change
module FileBackup
- def backup!(path, checksum)
- backup_dir = "#{::File.dirname(path)}/.rcm"
+ def backup!(file_path, checksum)
+ backup_dir = "#{::File.dirname(file_path)}/.rcm"
Dir.mkdir(backup_dir) unless ::File.directory?(backup_dir)
- backup_path = "#{backup_dir}/#{::File.basename(path)}.#{checksum}"
+ backup_path = "#{backup_dir}/#{::File.basename(file_path)}.#{checksum}"
return if ::File.exist?(backup_path)
- info("Backing up #{path} -> #{backup_path}")
- ::File.rename(path, backup_path)
+ info("Backing up #{file_path} -> #{backup_path}")
+ ::File.rename(file_path, backup_path)
end
end
# Managing files
class File < Resource
- attr_reader :path
-
include FileBackup
- def initialize(path)
- super(path)
- @path = path
+ def initialize(file_path)
+ super(file_path)
+ @file_path = file_path
end
def content(text = nil)
@@ -40,6 +38,12 @@ module RCM
def from_template = @from_template = true
def ensure_line(line) = @ensure_line = line
+ def path(file_path = nil)
+ return @file_path if file_path.nil?
+
+ @file_path = file_path
+ end
+
def evaluate!
return unless super
return evaluate_ensure_line! unless @ensure_line.nil?
@@ -50,39 +54,39 @@ module RCM
private
def evaluate_ensure_line!
- return write_content!(@ensure_line) unless ::File.file?(@path)
- return if ::File.readlines(@path, chomp: true).include?(@ensure_line)
+ return write_content!(@ensure_line) unless ::File.file?(@file_path)
+ return if ::File.readlines(@file_path, chomp: true).include?(@ensure_line)
- ::File.open(@path, 'a') do |fd|
+ ::File.open(@file_path, 'a') do |fd|
fd.puts(@ensure_line)
end
end
def write_content!(text)
- info "Managing file #{@path}"
+ info "Managing file #{@file_path}"
create_parent_directory!
debug text if option :debug
- tmp_path = "#{@path}.tmp"
+ tmp_path = "#{@file_path}.tmp"
::File.write(tmp_path, text)
- if ::File.file?(@path)
- checksum = Digest::SHA256.file(@path).hexdigest
+ if ::File.file?(@file_path)
+ checksum = Digest::SHA256.file(@file_path).hexdigest
tmp_checksum = Digest::SHA256.file(tmp_path).hexdigest
if tmp_checksum == checksum
::File.delete(tmp_path) # File has not changed, not doing anything
return
end
- backup!(@path, checksum) # File changed, backup!
+ backup!(@file_path, checksum) # File changed, backup!
end
- ::File.rename(tmp_path, @path)
+ ::File.rename(tmp_path, @file_path)
end
def create_parent_directory!
- dirname = ::File.dirname(@path)
+ dirname = ::File.dirname(@file_path)
return unless !::File.directory?(dirname) && @create_parent
info "Creating parent directory #{dirname}"
@@ -97,10 +101,10 @@ module RCM
# Add file keyword to the DSL
class DSL
- def file(path, &block)
+ def file(file_path, &block)
return unless @conds_met
- f = File.new(path)
+ f = File.new(file_path)
f.content(f.instance_eval(&block))
self << f
f
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
index d0afd92..1a636b8 100644
--- a/test/lib/dslkeywords/file_test.rb
+++ b/test/lib/dslkeywords/file_test.rb
@@ -68,4 +68,28 @@ class RCMFileTest < Minitest::Test
assert File.exist?(file_path)
assert_equal :content, File.read(file_path).to_sym
end
+
+ def test_backup
+ file_path = "#{DIR_PATH}/foo/backup-me.txt"
+ original_content = 'original_content'
+ backup_path = "#{DIR_PATH}/foo/.rcm/backup-me.txt.d4c3af73588ce06c32ed04d1b79801286109ea265712a2bd3fdc3ed01c82bb86"
+
+ configure_from_scratch do
+ file :original do
+ path file_path
+ create_parent_directory
+ original_content
+ end
+
+ file :new do
+ path file_path
+ create_parent_directory
+ depends_on file(:original)
+ :new_content
+ end
+ end
+
+ assert File.file?(backup_path)
+ assert_equal original_content, File.read(backup_path)
+ end
end