diff --git a/features/action_mkdir.feature b/features/action_mkdir.feature index 241edb5..82475c5 100644 --- a/features/action_mkdir.feature +++ b/features/action_mkdir.feature @@ -19,3 +19,13 @@ Feature: `mkdir' task action When I successfully execute the recipe on remote target Then the remote directory "some_directory_0700" must have 0700 mode And the remote directory "some_directory_0500" must have 0500 mode + + Scenario: creates directories recursively + Given a recipe with: + """ + task :mkdir_action do + mkdir 'some/directory' + end + """ + When I successfully execute the recipe on remote target + Then the remote directory "some/directory" must exists diff --git a/lib/producer/core.rb b/lib/producer/core.rb index 6eb8bee..5208076 100644 --- a/lib/producer/core.rb +++ b/lib/producer/core.rb @@ -1,6 +1,7 @@ -require 'forwardable' - require 'etc' +require 'forwardable' +require 'pathname' + require 'net/ssh' require 'net/sftp' diff --git a/lib/producer/core/actions/mkdir.rb b/lib/producer/core/actions/mkdir.rb index 1082e04..1648c3a 100644 --- a/lib/producer/core/actions/mkdir.rb +++ b/lib/producer/core/actions/mkdir.rb @@ -7,11 +7,12 @@ module Producer end def apply - case arguments.size - when 1 - fs.mkdir path - when 2 - fs.mkdir path, mode + Pathname.new(path).descend do |p| + next if fs.dir? p + case arguments.size + when 1 then fs.mkdir p.to_s + when 2 then fs.mkdir p.to_s, mode + end end end diff --git a/spec/producer/core/actions/mkdir_spec.rb b/spec/producer/core/actions/mkdir_spec.rb index 4e3fc3a..8f7d23a 100644 --- a/spec/producer/core/actions/mkdir_spec.rb +++ b/spec/producer/core/actions/mkdir_spec.rb @@ -9,6 +9,8 @@ module Producer::Core it_behaves_like 'action' describe '#apply' do + before { allow(remote_fs).to receive(:dir?) { false } } + it 'creates directory on remote filesystem' do expect(remote_fs).to receive(:mkdir).with(path) mkdir.apply @@ -22,6 +24,25 @@ module Producer::Core mkdir.apply end end + + context 'when parent directories does not exists' do + let(:path) { 'some/path' } + + it 'creates parent directories' do + expect(remote_fs).to receive(:mkdir).with('some').ordered + expect(remote_fs).to receive(:mkdir).with('some/path').ordered + mkdir.apply + end + end + + context 'when directory already exists' do + before { allow(remote_fs).to receive(:dir?) { true } } + + it 'creates directory on remote filesystem' do + expect(remote_fs).not_to receive(:mkdir) + mkdir.apply + end + end end end end