Skip to content

Commit

Permalink
Merge pull request #532 from hico-horiuchi/add_options_to_command_bas…
Browse files Browse the repository at this point in the history
…e_file

Add options to Command::Base::File
  • Loading branch information
mizzy committed Mar 20, 2016
2 parents dc487ac + 64702a2 commit 39a82a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/specinfra/command/base/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,30 @@ 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)
"mkdir -p #{escape(file)}"
end

def copy(src, dest)
"cp #{escape(src)} #{escape(dest)}"
def copy(src, dest, options = {})
option = '-p'
option << 'R' if options[:recursive]
"cp #{option} #{escape(src)} #{escape(dest)}"
end

def move(src, dest)
Expand Down
20 changes: 20 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 All @@ -38,6 +50,14 @@
it { should eq 'stat -c %G /tmp' }
end

describe get_command(:copy_file, '/src', '/dest') do
it { should eq 'cp -p /src /dest' }
end

describe get_command(:copy_file, '/src', '/dest', :recursive => true) do
it { should eq 'cp -pR /src /dest' }
end

describe get_command(:move_file, '/src', '/dest') do
it { should eq 'mv /src /dest' }
end
Expand Down

0 comments on commit 39a82a3

Please sign in to comment.