Skip to content

Commit 64702a2

Browse files
committed
Add -R option to chmod, chown and chgrp with tests
1 parent 142e90b commit 64702a2

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/specinfra/command/base/file.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ def get_size(file)
137137
"stat -c %s #{escape(file)}"
138138
end
139139

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

144-
def change_owner(file, owner, group=nil)
145+
def change_owner(file, owner, group=nil, options = {})
146+
option = '-R' if options[:recursive]
145147
owner = "#{owner}:#{group}" if group
146-
"chown #{owner} #{escape(file)}"
148+
"chown #{option} #{owner} #{escape(file)}".squeeze(' ')
147149
end
148150

149-
def change_group(file, group)
150-
"chgrp #{group} #{escape(file)}"
151+
def change_group(file, group, options = {})
152+
option = '-R' if options[:recursive]
153+
"chgrp #{option} #{group} #{escape(file)}".squeeze(' ')
151154
end
152155

153156
def create_as_directory(file)

spec/command/base/file_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
it { should eq 'chmod 0644 /tmp' }
1515
end
1616

17+
describe get_command(:change_file_mode, '/tmp', '0644', :recursive => true) do
18+
it { should eq 'chmod -R 0644 /tmp' }
19+
end
20+
1721
describe get_command(:change_file_owner, '/tmp', 'root') do
1822
it { should eq 'chown root /tmp' }
1923
end
@@ -22,10 +26,18 @@
2226
it { should eq 'chown root:root /tmp' }
2327
end
2428

29+
describe get_command(:change_file_owner, '/tmp', 'root', 'root', :recursive => true) do
30+
it { should eq 'chown -R root:root /tmp' }
31+
end
32+
2533
describe get_command(:change_file_group, '/tmp', 'root') do
2634
it { should eq 'chgrp root /tmp' }
2735
end
2836

37+
describe get_command(:change_file_group, '/tmp', 'root', :recursive => true) do
38+
it { should eq 'chgrp -R root /tmp' }
39+
end
40+
2941
describe get_command(:create_file_as_directory, '/tmp') do
3042
it { should eq 'mkdir -p /tmp' }
3143
end

0 commit comments

Comments
 (0)