Skip to content

Commit 9f0de68

Browse files
committed
Ignore non-empty directories in cleanup file
- Before ruby 2.5, FileUtils.rmdir silently fails when the target directory is not empty, so the non-empty directories in cleanup file are not removed. But the implementation of rmdir is changed in ruby 2.5 so that it doesn’t rescue Errno::ENOTEMPTY any more.
1 parent dc22129 commit 9f0de68

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/instance_agent/plugins/codedeploy/install_instruction.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ def execute
209209
FileUtils.rm(@file_path)
210210
elsif File.exist?(@file_path)
211211
if File.directory?(@file_path)
212-
# TODO (AWSGLUE-713): handle the exception if the directory is non-empty;
213-
# this might mean the customer has put files in this directory and we should
214-
# probably ignore the error and move on
215-
FileUtils.rmdir(@file_path)
212+
begin
213+
FileUtils.rmdir(@file_path)
214+
rescue Errno::ENOTEMPTY
215+
end
216216
else
217217
FileUtils.rm(@file_path)
218218
end

test/instance_agent/plugins/codedeploy/install_instruction_test.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,34 @@ class InstallInstructionTest < InstanceAgentTestCase
249249
File.stubs(:exist?).with("test_delete_path").returns(true)
250250
end
251251

252-
should "produce a command that deletes test_delete_path" do
252+
should "use rm for a regular file" do
253253
commands = InstallInstruction.parse_remove_commands(@parse_string)
254254
FileUtils.expects(:rm).with("test_delete_path")
255-
assert_not_equal nil, commands
256255
commands.each do |command|
257256
command.execute
258257
end
259258
end
259+
260+
should "use rmdir for a directory" do
261+
File.stubs(:directory?).with("test_delete_path").returns(true)
262+
commands = InstallInstruction.parse_remove_commands(@parse_string)
263+
FileUtils.expects(:rmdir).with("test_delete_path")
264+
commands.each do |command|
265+
command.execute
266+
end
267+
end
268+
269+
should "ignore a non-empty directory by rescuing Errno::ENOTEMPTY" do
270+
File.stubs(:directory?).with("test_delete_path").returns(true)
271+
commands = InstallInstruction.parse_remove_commands(@parse_string)
272+
FileUtils.stubs(:rmdir).raises(Errno::ENOTEMPTY)
273+
274+
assert_nothing_raised do
275+
commands.each do |command|
276+
command.execute
277+
end
278+
end
279+
end
260280
end
261281

262282
context "multiple files to delete" do

0 commit comments

Comments
 (0)