diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-14 22:21:00 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-14 22:21:00 +0200 |
| commit | c7697dbd7d26bc6c62031e0d4d530b3b5987d9ee (patch) | |
| tree | 232890d0ff7913b34982b53ca41b4b5ec5fde7b0 /test/lib | |
| parent | a2edd9ec657dca835ce1a73807a86a9c44fdfec8 (diff) | |
more tests
Diffstat (limited to 'test/lib')
| -rw-r--r-- | test/lib/dslkeywords/file_test.rb | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb index b4efa33..7cd0f5e 100644 --- a/test/lib/dslkeywords/file_test.rb +++ b/test/lib/dslkeywords/file_test.rb @@ -1,18 +1,71 @@ require 'minitest/autorun' +require 'fileutils' + require_relative '../../../lib/dsl' class RCMFileTest < Minitest::Test - def test_create_file + FILE_PATH = './.file_test.tmp'.freeze + DIR_PATH = './.dir_test.tmp'.freeze + + Minitest.after_run do + File.unlink(FILE_PATH) if File.file?(FILE_PATH) + FileUtils.rm_r(DIR_PATH) if File.directory?(DIR_PATH) + end + + def test_create_file_from_string + text = 'Hello World!' + configure(reset: true) { file(FILE_PATH) { text } } + assert_equal text, File.read(FILE_PATH) + end + + def test_create_file_from_array + arr = %w[Hello World and Hello Universe] + configure(reset: true) { file(FILE_PATH) { arr } } + assert_equal arr.join("\n"), File.read(FILE_PATH) + end + + def test_create_file_from_sourcefile text = 'Hello World!' - path = './.foo.txt.tmp' + source_path = "#{FILE_PATH}.source.tmp" + File.write(source_path, text) + + configure(reset: true) do + file FILE_PATH do + from_sourcefile + source_path + end + end + assert_equal File.read(source_path), File.read(FILE_PATH) + + File.unlink(source_path) + end + + def test_create_file_from_template + configure(reset: true) do + file FILE_PATH do + from_template + 'One plus two is <%= 1 + 2 %>!' + end + end + assert_equal 'One plus two is 3!', File.read(FILE_PATH) + end + + def test_ensure_line + File.write(FILE_PATH, "Hey there\n") + configure(reset: true) { file(FILE_PATH) { ensure_line 'Whats up?' } } + assert_equal "Hey there\nWhats up?\n", File.read(FILE_PATH) + end - configure do - file path do - text + def test_create_parent_directory + file_path = "#{DIR_PATH}/foo/bar/baz/foo.txt" + configure(reset: true) do + file file_path do + create_parent_directory + :content end end - assert_equal text, File.read(path) - ensure - File.unlink(path) + assert File.directory?(File.dirname(file_path)) + assert File.exist?(file_path) + assert_equal :content, File.read(file_path).to_sym end end |
