summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-14 22:21:00 +0200
committerPaul Buetow <paul@buetow.org>2025-02-14 22:21:00 +0200
commitc7697dbd7d26bc6c62031e0d4d530b3b5987d9ee (patch)
tree232890d0ff7913b34982b53ca41b4b5ec5fde7b0
parenta2edd9ec657dca835ce1a73807a86a9c44fdfec8 (diff)
more tests
-rw-r--r--README.md8
-rw-r--r--lib/dsl.rb16
-rw-r--r--lib/dslkeywords/file.rb2
-rw-r--r--playground/Rakefile2
-rw-r--r--test/lib/dslkeywords/file_test.rb69
5 files changed, 81 insertions, 16 deletions
diff --git a/README.md b/README.md
index d9de495..1c65f5e 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,16 @@
Ruby configuration management system (rcm) - KISS and for my personal use.
+## Run tests
+
+```
+bundle install
+rake test
+```
+
## Invokation
```sh
+cd playground
rake wireguard -- --debug
```
diff --git a/lib/dsl.rb b/lib/dsl.rb
index 1629afc..3a53762 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -10,15 +10,19 @@ module RCM
class DSL
attr_reader :id
- # TODO: Replace @@ with @ class variables
- @@rcm_counter = -1
- @@objs = {}
+ def self.reset!
+ @@rcm_counter = -1
+ @@objs = {}
+ end
+
+ reset!
include Config
include Options
include Log
- def initialize
+ def initialize(reset)
+ DSL.reset! if reset
@id = "#{self.class}(#{@@rcm_counter += 1})"
@conds_met = true
@scheduled = []
@@ -35,8 +39,8 @@ module RCM
end
end
-def configure(&block)
- RCM::DSL.new do |rcm|
+def configure(reset: false, &block)
+ RCM::DSL.new(reset) do |rcm|
rcm.info('Configuring...')
rcm.instance_eval(&block)
rcm.evaluate!
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 58d7a47..8c4deca 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -62,7 +62,7 @@ module RCM
dirname = ::File.dirname(@path)
return unless !::File.directory?(dirname) && @create_parent
- info "Creating parent directory #{parent}"
+ info "Creating parent directory #{dirname}"
FileUtils.mkdir_p(dirname)
end
diff --git a/playground/Rakefile b/playground/Rakefile
index 99a599f..e006960 100644
--- a/playground/Rakefile
+++ b/playground/Rakefile
@@ -1,4 +1,4 @@
-require_relative 'lib/dsl'
+require_relative '../lib/dsl'
desc 'Set up wireguard mesh'
task :wireguard do
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