summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock2
-rw-r--r--Rakefile53
-rw-r--r--lib/dslkeywords/file.rb18
-rw-r--r--lib/options.rb12
-rw-r--r--playground/Rakefile51
-rw-r--r--test/lib/dslkeywords/file_test.rb18
7 files changed, 93 insertions, 62 deletions
diff --git a/Gemfile b/Gemfile
index 728602d..c9e5323 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,5 +1,6 @@
source 'https://rubygems.org'
gem 'erb'
+gem 'minitest'
gem 'rbs'
gem 'toml'
diff --git a/Gemfile.lock b/Gemfile.lock
index 0fcb5fb..72f8426 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -5,6 +5,7 @@ GEM
erb (4.0.4)
cgi (>= 0.3.3)
logger (1.6.4)
+ minitest (5.25.4)
parslet (2.0.0)
rbs (3.7.0)
logger
@@ -17,6 +18,7 @@ PLATFORMS
DEPENDENCIES
erb
+ minitest
rbs
toml
diff --git a/Rakefile b/Rakefile
index 99a599f..bca9c85 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,51 +1,8 @@
-require_relative 'lib/dsl'
+require 'rake/testtask'
-desc 'Set up wireguard mesh'
-task :wireguard do
- configure do
- # p option :verbose
- # dump_config
- only_when { hostname is :earth }
-
- file '/tmp/test/wg/wg0.conf' do
- create_parent_directory and from_template
-
- 'the content is here and the result is <%= 1 + 2 %>'
- end
-
- file '/tmp/test/wg/wg1.conf' do
- create_parent_directory and from_sourcefile
-
- './Rakefile'
- end
- end
-end
-
-desc 'foo task'
-task :foo do
- configure do
- file '/tmp/test.txt' do
- %w[foo bar baz].sort
- end
- end
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+ t.test_files = FileList['test/**/*_test.rb']
end
-desc 'Set up the /etc/hosts file'
-task :hosts do
- configure do
- only_when { hostname is :earth }
-
- file '/etc/hosts.test' do
- ensure_line '192.168.1.101 foo'
- end
- end
-end
-
-desc 'foo'
-task :foo do
- configure do
- file '/tmp/foo.txt' do
- ensure_line 'foo bar baz'
- end
- end
-end
+task default: :test
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 403f01e..58d7a47 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -19,10 +19,10 @@ module RCM
def to_s = id
- def content(content = nil)
- return @content if content.nil?
+ def content(text = nil)
+ return @content if text.nil?
- @content = content.instance_of?(Array) ? content.join("\n") : content
+ @content = text.instance_of?(Array) ? text.join("\n") : text
end
def create_parent_directory = @create_parent = true
@@ -49,12 +49,12 @@ module RCM
end
end
- def write_content!(content)
+ def write_content!(text)
create_parent_directory!
- debug content if option :debug
+ debug text if option :debug
info "Creating file #{@path}"
tmp_path = "#{@path}.tmp"
- ::File.write(tmp_path, content)
+ ::File.write(tmp_path, text)
::File.rename(tmp_path, @path)
end
@@ -67,8 +67,8 @@ module RCM
end
def real_content
- content = @from_sourcefile ? ::File.read(@content) : @content
- @from_template ? ERB.new(content).result : content
+ text = @from_sourcefile ? ::File.read(@content) : @content
+ @from_template ? ERB.new(text).result : text
end
end
@@ -77,6 +77,8 @@ module RCM
def file(path, &block)
return unless @conds_met
+ p :FOO, path
+
f = File.new(path)
f.content(f.instance_eval(&block))
self << f
diff --git a/lib/options.rb b/lib/options.rb
index 79e3c51..ee30295 100644
--- a/lib/options.rb
+++ b/lib/options.rb
@@ -5,12 +5,12 @@ module RCM
module Options
@@options = { debug: false }
- after_double_dash = ARGV.slice_before('--').to_a.last.drop(1)
-
- OptionParser.new do |opts|
- opts.banner = 'Usage: rake [task] -- [options]'
- opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v }
- end.parse!(after_double_dash)
+ if (after_double_dash = ARGV.slice_before('--').to_a.last&.drop(1))
+ OptionParser.new do |opts|
+ opts.banner = 'Usage: rake [task] -- [options]'
+ opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v }
+ end.parse!(after_double_dash)
+ end
def option(key)
raise "No such option: #{key}" unless @@options.key?(key)
diff --git a/playground/Rakefile b/playground/Rakefile
new file mode 100644
index 0000000..99a599f
--- /dev/null
+++ b/playground/Rakefile
@@ -0,0 +1,51 @@
+require_relative 'lib/dsl'
+
+desc 'Set up wireguard mesh'
+task :wireguard do
+ configure do
+ # p option :verbose
+ # dump_config
+ only_when { hostname is :earth }
+
+ file '/tmp/test/wg/wg0.conf' do
+ create_parent_directory and from_template
+
+ 'the content is here and the result is <%= 1 + 2 %>'
+ end
+
+ file '/tmp/test/wg/wg1.conf' do
+ create_parent_directory and from_sourcefile
+
+ './Rakefile'
+ end
+ end
+end
+
+desc 'foo task'
+task :foo do
+ configure do
+ file '/tmp/test.txt' do
+ %w[foo bar baz].sort
+ end
+ end
+end
+
+desc 'Set up the /etc/hosts file'
+task :hosts do
+ configure do
+ only_when { hostname is :earth }
+
+ file '/etc/hosts.test' do
+ ensure_line '192.168.1.101 foo'
+ end
+ end
+end
+
+desc 'foo'
+task :foo do
+ configure do
+ file '/tmp/foo.txt' do
+ ensure_line 'foo bar baz'
+ end
+ end
+end
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
new file mode 100644
index 0000000..b4efa33
--- /dev/null
+++ b/test/lib/dslkeywords/file_test.rb
@@ -0,0 +1,18 @@
+require 'minitest/autorun'
+require_relative '../../../lib/dsl'
+
+class RCMFileTest < Minitest::Test
+ def test_create_file
+ text = 'Hello World!'
+ path = './.foo.txt.tmp'
+
+ configure do
+ file path do
+ text
+ end
+ end
+ assert_equal text, File.read(path)
+ ensure
+ File.unlink(path)
+ end
+end