From a84b34b7ca66faed595e0d3f1bf0a11841803c0b Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 8 Oct 2014 11:21:32 +0000 Subject: [PATCH] Support `mkdir' status attributes --- features/action_mkdir.feature | 8 ++++---- lib/producer/core/action.rb | 5 +++-- lib/producer/core/actions/mkdir.rb | 12 +++++++----- lib/producer/core/remote/fs.rb | 13 +++++++----- spec/producer/core/actions/mkdir_spec.rb | 25 ++++++++++++++++++------ spec/producer/core/remote/fs_spec.rb | 14 ++++--------- spec/support/shared_action.rb | 9 ++++++++- 7 files changed, 53 insertions(+), 33 deletions(-) diff --git a/features/action_mkdir.feature b/features/action_mkdir.feature index 82475c5..530d4b6 100644 --- a/features/action_mkdir.feature +++ b/features/action_mkdir.feature @@ -6,8 +6,8 @@ Feature: `mkdir' task action """ task :mkdir_action do mkdir 'some_directory' - mkdir 'some_directory_0700', 0700 - mkdir 'some_directory_0500', 0500 + mkdir 'some_directory_0700', mode: 0700 + mkdir 'some_directory_0711', mode: 0711 end """ @@ -15,10 +15,10 @@ Feature: `mkdir' task action When I successfully execute the recipe on remote target Then the remote directory "some_directory" must exists - Scenario: creates directory with given permissions + Scenario: creates directory with given attributes 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 + And the remote directory "some_directory_0711" must have 0711 mode Scenario: creates directories recursively Given a recipe with: diff --git a/lib/producer/core/action.rb b/lib/producer/core/action.rb index af2f59b..ccc31cb 100644 --- a/lib/producer/core/action.rb +++ b/lib/producer/core/action.rb @@ -7,11 +7,12 @@ module Producer def_delegators :@env, :input, :output, :error_output, :remote def_delegators :remote, :fs - attr_reader :env, :arguments + attr_reader :env, :arguments, :options - def initialize(env, *args) + def initialize(env, *args, **options) @env = env @arguments = args + @options = options end def name diff --git a/lib/producer/core/actions/mkdir.rb b/lib/producer/core/actions/mkdir.rb index 5e207e4..ae50576 100644 --- a/lib/producer/core/actions/mkdir.rb +++ b/lib/producer/core/actions/mkdir.rb @@ -2,6 +2,12 @@ module Producer module Core module Actions class Mkdir < Action + def initialize(env, *args, **options) + super + @options[:permissions] = @options.delete :mode if @options.key? :mode + @options[:owner] = @options.delete :user if @options.key? :user + end + def name 'mkdir' end @@ -10,7 +16,7 @@ module Producer path.descend do |p| next if fs.dir? p fs.mkdir p.to_s - fs.chmod p.to_s, mode if mode + fs.setstat p.to_s, @options unless @options.empty? end end @@ -20,10 +26,6 @@ module Producer def path Pathname.new(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 478aaa4..ceb6d58 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -20,13 +20,16 @@ module Producer false end - def chmod(path, mode) - sftp.setstat! path, permissions: mode + def setstat(path, attributes) + sftp.setstat! path, attributes end - def mkdir(path, mode = nil) - options = mode ? { permissions: mode } : {} - sftp.mkdir! path, options + def chmod(path, mode) + setstat path, permissions: mode + end + + def mkdir(path, attributes = {}) + ret = sftp.mkdir! path, attributes end def file_read(path) diff --git a/spec/producer/core/actions/mkdir_spec.rb b/spec/producer/core/actions/mkdir_spec.rb index 52b4c1f..9cf78c1 100644 --- a/spec/producer/core/actions/mkdir_spec.rb +++ b/spec/producer/core/actions/mkdir_spec.rb @@ -4,10 +4,23 @@ module Producer::Core module Actions describe Mkdir, :env do let(:path) { 'some_path' } - subject(:mkdir) { described_class.new(env, path) } + let(:options) { { } } + subject(:mkdir) { described_class.new(env, path, options) } it_behaves_like 'action' + describe '#initialize' do + let(:options) { { mode: 0700, user: 'root' } } + + it 'translates mode option as permissions' do + expect(mkdir.options[:permissions]).to eq 0700 + end + + it 'translates user option as owner' do + expect(mkdir.options[:owner]).to eq 'root' + end + end + describe '#apply' do before { allow(remote_fs).to receive(:dir?) { false } } @@ -16,11 +29,11 @@ module Producer::Core mkdir.apply end - context 'when a mode was given' do - subject(:mkdir) { described_class.new(env, path, 0700) } + context 'when status options are given' do + let(:options) { { group: 'wheel' } } - it 'changes the directory with given mode' do - expect(remote_fs).to receive(:chmod).with(path, 0700) + it 'changes the directory status with given options' do + expect(remote_fs).to receive(:setstat).with(path, options) mkdir.apply end end @@ -38,7 +51,7 @@ module Producer::Core context 'when directory already exists' do before { allow(remote_fs).to receive(:dir?) { true } } - it 'creates directory on remote filesystem' do + it 'does not create any directory' do expect(remote_fs).not_to receive(:mkdir) mkdir.apply end diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 22bb1f6..fd34b33 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -86,18 +86,12 @@ module Producer::Core end describe '#mkdir' do - let(:path) { 'some_directory_path' } + let(:path) { 'some_directory_path' } + let(:attributes) { { foo: :bar } } it 'creates the directory' do - 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 + expect(sftp).to receive(:mkdir!).with(path, attributes) + fs.mkdir path, attributes end end diff --git a/spec/support/shared_action.rb b/spec/support/shared_action.rb index f8d2577..ba16bfc 100644 --- a/spec/support/shared_action.rb +++ b/spec/support/shared_action.rb @@ -3,7 +3,8 @@ module Producer::Core include TestEnvHelpers let(:arguments) { [:some, :arguments] } - subject(:action) { described_class.new(env, *arguments) } + let(:options) { { foo: :bar } } + subject(:action) { described_class.new(env, *arguments, options) } describe '#env' do it 'returns the assigned env' do @@ -17,6 +18,12 @@ module Producer::Core end end + describe '#options' do + it 'returns the assigned options' do + expect(action.options).to eq options + end + end + describe '#input' do it 'returns env input' do expect(action.input).to be env.input