Skip to content

Commit

Permalink
Add -R option to chmod, chown and chgrp with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hico-horiuchi committed Mar 20, 2016
1 parent 142e90b commit 64702a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/specinfra/command/base/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ def get_size(file)
"stat -c %s #{escape(file)}"
end

def change_mode(file, mode)
"chmod #{mode} #{escape(file)}"
def change_mode(file, mode, options = {})
option = '-R' if options[:recursive]
"chmod #{option} #{mode} #{escape(file)}".squeeze(' ')
end

def change_owner(file, owner, group=nil)
def change_owner(file, owner, group=nil, options = {})
option = '-R' if options[:recursive]
owner = "#{owner}:#{group}" if group
"chown #{owner} #{escape(file)}"
"chown #{option} #{owner} #{escape(file)}".squeeze(' ')
end

def change_group(file, group)
"chgrp #{group} #{escape(file)}"
def change_group(file, group, options = {})
option = '-R' if options[:recursive]
"chgrp #{option} #{group} #{escape(file)}".squeeze(' ')
end

def create_as_directory(file)
Expand Down
12 changes: 12 additions & 0 deletions spec/command/base/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
it { should eq 'chmod 0644 /tmp' }
end

describe get_command(:change_file_mode, '/tmp', '0644', :recursive => true) do
it { should eq 'chmod -R 0644 /tmp' }
end

describe get_command(:change_file_owner, '/tmp', 'root') do
it { should eq 'chown root /tmp' }
end
Expand All @@ -22,10 +26,18 @@
it { should eq 'chown root:root /tmp' }
end

describe get_command(:change_file_owner, '/tmp', 'root', 'root', :recursive => true) do
it { should eq 'chown -R root:root /tmp' }
end

describe get_command(:change_file_group, '/tmp', 'root') do
it { should eq 'chgrp root /tmp' }
end

describe get_command(:change_file_group, '/tmp', 'root', :recursive => true) do
it { should eq 'chgrp -R root /tmp' }
end

describe get_command(:create_file_as_directory, '/tmp') do
it { should eq 'mkdir -p /tmp' }
end
Expand Down

0 comments on commit 64702a2

Please sign in to comment.