diff --git a/features/actions/mkdir.feature b/features/actions/mkdir.feature new file mode 100644 index 0000000..6ff0735 --- /dev/null +++ b/features/actions/mkdir.feature @@ -0,0 +1,14 @@ +@sshd +Feature: `mkdir' task action + + Scenario: creates directory given as argument + Given a recipe with: + """ + target 'some_host.test' + + task :create_some_dir do + mkdir 'some_directory' + end + """ + When I successfully execute the recipe + Then the remote directory "some_directory" must exists diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index 0a43e39..ab9f4b1 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -6,6 +6,10 @@ Given /^a remote file named "([^"]+)"$/ do |file_name| write_file file_name, '' end +Then /^the remote directory "([^"]+)" should exists$/ do |path| + check_directory_presence([path], true) +end + Then /^the remote file "([^"]+)" should contain "([^"]+)"/ do |path, content| check_file_content path, content, true end diff --git a/lib/producer/core.rb b/lib/producer/core.rb index b2f9a2d..29c15fc 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -2,6 +2,7 @@ require 'producer/core/action' require 'producer/core/actions/echo' require 'producer/core/actions/shell_command' +require 'producer/core/actions/mkdir' require 'producer/core/actions/file_writer' # condition tests (need to be defined before the condition DSL) diff --git a/lib/producer/core/actions/mkdir.rb b/lib/producer/core/actions/mkdir.rb new file mode 100644 index 0000000..5c9e70b --- /dev/null +++ b/lib/producer/core/actions/mkdir.rb @@ -0,0 +1,15 @@ +module Producer + module Core + module Actions + class Mkdir < Action + def apply + fs.mkdir path + end + + def path + arguments.first + end + end + end + end +end diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index db3fbb1..114412b 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -26,6 +26,10 @@ module Producer false end + def mkdir(path) + sftp.mkdir! path + end + def file_write(path, content) sftp.file.open path, 'w' do |f| f.write content diff --git a/lib/producer/core/task/dsl.rb b/lib/producer/core/task/dsl.rb index fc02a2e..e00e2aa 100644 --- a/lib/producer/core/task/dsl.rb +++ b/lib/producer/core/task/dsl.rb @@ -13,6 +13,7 @@ module Producer define_action :echo, Actions::Echo define_action :sh, Actions::ShellCommand + define_action :mkdir, Actions::Mkdir define_action :file_write, Actions::FileWriter attr_reader :env, :block, :actions diff --git a/spec/producer/core/actions/mkdir_spec.rb b/spec/producer/core/actions/mkdir_spec.rb new file mode 100644 index 0000000..d53e15f --- /dev/null +++ b/spec/producer/core/actions/mkdir_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +module Producer::Core + describe Actions::Mkdir do + let(:env) { Env.new } + let(:path) { 'some_path' } + subject(:mkdir) { Actions::Mkdir.new(env, path) } + + describe '#apply' do + it 'creates directory on remote filesystem' do + expect(mkdir.fs).to receive(:mkdir).with(path) + mkdir.apply + end + end + + describe '#path' do + it 'returns the path' do + expect(mkdir.path).to eq path + end + end + end +end + diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 40d6c50..e8a9bde 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -112,6 +112,18 @@ module Producer::Core end end + describe '#mkdir' do + let(:sftp) { double 'sftp' } + let(:path) { 'some_directory_path' } + + before { allow(fs).to receive(:sftp) { sftp } } + + it 'creates the directory' do + expect(sftp).to receive(:mkdir!).with(path) + fs.mkdir path + end + end + describe '#file_write' do let(:sftp) { double('sftp').as_null_object } let(:file) { double('sftp').as_null_object } diff --git a/spec/producer/core/task/dsl_spec.rb b/spec/producer/core/task/dsl_spec.rb index d84f63d..b27ea21 100644 --- a/spec/producer/core/task/dsl_spec.rb +++ b/spec/producer/core/task/dsl_spec.rb @@ -6,7 +6,7 @@ module Producer::Core let(:env) { Env.new } subject(:dsl) { Task::DSL.new(env, &block) } - %w[echo sh file_write].each do |action| + %w[echo sh mkdir file_write].each do |action| it "has `#{action}' action defined" do expect(dsl).to respond_to action.to_sym end