Skip to content

Commit 7fdda95

Browse files
authored
Merge pull request #201 from what-is-quality/fix-same-directory-deployment
Ignore non-empty directories when cleanup
2 parents 93602fa + 3dedbc9 commit 7fdda95

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

lib/instance_agent/plugins/codedeploy/install_instruction.rb

+4-4
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

+34-24
Original file line numberDiff line numberDiff line change
@@ -231,40 +231,54 @@ class InstallInstructionTest < InstanceAgentTestCase
231231
context "Parsing a delete file" do
232232
context "an empty delete file" do
233233
setup do
234-
@parse_string = <<-END
235-
END
236-
end
234+
@parse_string = ""
235+
end
237236

238-
should "return an empty command collection" do
239-
commands = InstallInstruction.parse_remove_commands(@parse_string)
240-
assert_equal 0, commands.length
237+
should "return an empty command collection" do
238+
commands = InstallInstruction.parse_remove_commands(@parse_string)
239+
assert_equal 0, commands.length
240+
end
241241
end
242-
end
243242

244-
context "a single file to delete" do
245-
setup do
246-
@parse_string = <<-END
247-
test_delete_path
248-
END
243+
context "a single file to delete" do
244+
setup do
245+
@parse_string = "test_delete_path\n"
249246
File.stubs(:exist?).with("test_delete_path").returns(true)
250247
end
251248

252-
should "produce a command that deletes test_delete_path" do
249+
should "use rm for a regular file" do
253250
commands = InstallInstruction.parse_remove_commands(@parse_string)
254251
FileUtils.expects(:rm).with("test_delete_path")
255-
assert_not_equal nil, commands
256252
commands.each do |command|
257253
command.execute
258254
end
259255
end
256+
257+
should "use rmdir for a directory" do
258+
File.stubs(:directory?).with("test_delete_path").returns(true)
259+
commands = InstallInstruction.parse_remove_commands(@parse_string)
260+
FileUtils.expects(:rmdir).with("test_delete_path")
261+
commands.each do |command|
262+
command.execute
263+
end
264+
end
265+
266+
should "ignore a non-empty directory by rescuing Errno::ENOTEMPTY" do
267+
File.stubs(:directory?).with("test_delete_path").returns(true)
268+
commands = InstallInstruction.parse_remove_commands(@parse_string)
269+
FileUtils.stubs(:rmdir).raises(Errno::ENOTEMPTY)
270+
271+
assert_nothing_raised do
272+
commands.each do |command|
273+
command.execute
274+
end
275+
end
276+
end
260277
end
261278

262279
context "multiple files to delete" do
263280
setup do
264-
@parse_string = <<-END
265-
test_delete_path
266-
another_delete_path
267-
END
281+
@parse_string = "test_delete_path\nanother_delete_path\n"
268282
File.stubs(:directory?).returns(false)
269283
File.stubs(:exist?).with("test_delete_path").returns(true)
270284
File.stubs(:exist?).with("another_delete_path").returns(true)
@@ -295,11 +309,7 @@ class InstallInstructionTest < InstanceAgentTestCase
295309

296310
context "removes mangled line at the end" do
297311
setup do
298-
@parse_string = <<-END
299-
test_delete_path
300-
another_delete_path
301-
END
302-
@parse_string << "mangled"
312+
@parse_string = "test_delete_path\nanother_delete_path\nmangled"
303313
File.stubs(:exist?).with("test_delete_path").returns(true)
304314
File.stubs(:exist?).with("another_delete_path").returns(true)
305315
end
@@ -318,7 +328,7 @@ class InstallInstructionTest < InstanceAgentTestCase
318328

319329
context "correctly determines method from file type" do
320330
setup do
321-
@parse_string = 'foo'
331+
@parse_string = "foo\n"
322332
@instruction_file = mock
323333
@instruction_file.stubs(:path).returns("test/123-cleanup")
324334
File.stubs(:open).with("test/123-cleanup", 'r').returns(@instruction_file)

0 commit comments

Comments
 (0)