From 94f6bbb4aad5d1ed3436a77c27e7f231545a7bb3 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 25 Apr 2014 00:28:20 +0000 Subject: [PATCH] Accept mode as argument in `mkdir' action --- features/actions/mkdir.feature | 14 ++++++++++++++ features/steps/remote_steps.rb | 13 ++++++++++--- lib/producer/core/actions/mkdir.rb | 11 ++++++++++- lib/producer/core/remote/fs.rb | 5 +++-- spec/producer/core/actions/mkdir_spec.rb | 23 +++++++++++++++++++++++ spec/producer/core/remote/fs_spec.rb | 9 ++++++++- 6 files changed, 68 insertions(+), 7 deletions(-) diff --git a/features/actions/mkdir.feature b/features/actions/mkdir.feature index 6ff0735..206bbe9 100644 --- a/features/actions/mkdir.feature +++ b/features/actions/mkdir.feature @@ -12,3 +12,17 @@ Feature: `mkdir' task action """ When I successfully execute the recipe Then the remote directory "some_directory" must exists + + Scenario: creates directory with given permissions + Given a recipe with: + """ + target 'some_host.test' + + task :create_some_dir do + mkdir '0700_directory', 0700 + mkdir '0500_directory', 0500 + end + """ + When I successfully execute the recipe + Then the remote directory "0700_directory" must have 0700 mode + And the remote directory "0500_directory" must have 0500 mode diff --git a/features/steps/remote_steps.rb b/features/steps/remote_steps.rb index 5855d21..84011ef 100644 --- a/features/steps/remote_steps.rb +++ b/features/steps/remote_steps.rb @@ -22,9 +22,16 @@ Then /^the remote file "([^"]+)" must contain exactly "([^"]+)"$/ do |path, cont check_exact_file_content path, content end -Then /^the remote file "([^"]+)" must have (\d+) mode$/ do |path, mode| +def stat_mode(path) in_current_dir do - puts path, ('%o' % [File::Stat.new(path).mode])[-4, 4], mode - expect(('%o' % [File::Stat.new(path).mode])[-4, 4]).to match mode + ('%o' % [File::Stat.new(path).mode])[-4, 4] end end + +Then /^the remote file "([^"]+)" must have (\d+) mode$/ do |path, mode| + expect(stat_mode path).to eq mode +end + +Then /^the remote directory "([^"]+)" must have (\d+) mode$/ do |path, mode| + expect(stat_mode path).to eq mode +end diff --git a/lib/producer/core/actions/mkdir.rb b/lib/producer/core/actions/mkdir.rb index 5c9e70b..fad206e 100644 --- a/lib/producer/core/actions/mkdir.rb +++ b/lib/producer/core/actions/mkdir.rb @@ -3,12 +3,21 @@ module Producer module Actions class Mkdir < Action def apply - fs.mkdir path + case arguments.size + when 1 + fs.mkdir path + when 2 + fs.mkdir path, mode + end end def path arguments.first end + + def mode + arguments[1] + end end end end diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index 69bc580..38091a5 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -20,8 +20,9 @@ module Producer false end - def mkdir(path) - sftp.mkdir! path + def mkdir(path, mode = nil) + options = mode ? { permissions: mode } : {} + sftp.mkdir! path, options end def file_read(path) diff --git a/spec/producer/core/actions/mkdir_spec.rb b/spec/producer/core/actions/mkdir_spec.rb index cc1781b..d2c94ce 100644 --- a/spec/producer/core/actions/mkdir_spec.rb +++ b/spec/producer/core/actions/mkdir_spec.rb @@ -13,6 +13,15 @@ module Producer::Core expect(remote_fs).to receive(:mkdir).with(path) mkdir.apply end + + context 'when a mode was given' do + subject(:mkdir) { Mkdir.new(env, path, 0700) } + + it 'creates the directory with given mode' do + expect(remote_fs).to receive(:mkdir).with(anything, 0700) + mkdir.apply + end + end end describe '#path' do @@ -20,6 +29,20 @@ module Producer::Core expect(mkdir.path).to eq path end end + + describe '#mode' do + it 'returns nil' do + expect(mkdir.mode).to be nil + end + + context 'when a mode was given' do + subject(:mkdir) { Mkdir.new(env, path, 0700) } + + it 'returns the mode' do + expect(mkdir.mode).to be 0700 + end + end + end end end end diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index ffa720b..911644c 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -89,9 +89,16 @@ module Producer::Core let(:path) { 'some_directory_path' } it 'creates the directory' do - expect(sftp).to receive(:mkdir!).with(path) + expect(sftp).to receive(:mkdir!).with(path, anything) fs.mkdir path end + + it 'specifies permissions from optional mode argument' do + expect(sftp).to receive(:mkdir!) do |_, options| + expect(options[:permissions]).to eq 0700 + end + fs.mkdir path, 0700 + end end describe '#file_read' do