From 944b16d719661d26a411624952020a21484b3c59 Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Wed, 1 Jun 2022 16:15:03 +0900 Subject: [PATCH] test_in_tail: Simplify cleanup_directory and cleanup_file Since we always create a new test ditectory on each tests, ensuring to remove old directory is no longer needed. In addition, we alreay dropped Ruby 2.7 support, and the previous implementation has some bugs. For example `FileUtils.rm_f` doesn't have `secure` option even though the latest Ruby (v3.1). Signed-off-by: Takuro Ashie --- test/plugin/test_in_tail.rb | 54 ++----------------------------------- 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index f88ecbefbd..cf7610eb5b 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -31,61 +31,11 @@ def cleanup_directory(path) return end - if Fluent.windows? - Dir.glob("*", base: path).each do |name| - begin - cleanup_file(File.join(path, name)) - rescue => e - # expect test driver block release already owned file handle. - puts "Failed to clean up #{File.join(path, name)}: #{e}" - end - end - else - begin - FileUtils.rm_f(path, secure:true) - rescue ArgumentError - FileUtils.rm_f(path) # For Ruby 2.6 or before. - end - if File.exist?(path) - FileUtils.remove_entry_secure(path, true) - end - end - FileUtils.mkdir_p(path) + FileUtils.remove_entry_secure(path, true) end def cleanup_file(path) - if Fluent.windows? - # On Windows, when the file or directory is removed and created - # frequently, there is a case that creating file or directory will - # fail. This situation is caused by pending file or directory - # deletion which is mentioned on win32 API document [1] - # As a workaround, execute rename and remove method. - # - # [1] https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#files - # - file = File.join(Dir.tmpdir, SecureRandom.hex(10)) - begin - FileUtils.mv(path, file) - FileUtils.rm_rf(file, secure: true) - rescue ArgumentError - FileUtils.rm_rf(file) # For Ruby 2.6 or before. - end - if File.exist?(file) - # ensure files are closed for Windows, on which deleted files - # are still visible from filesystem - GC.start(full_mark: true, immediate_mark: true, immediate_sweep: true) - FileUtils.remove_entry_secure(file, true) - end - else - begin - FileUtils.rm_f(path, secure: true) - rescue ArgumentError - FileUtils.rm_f(path) # For Ruby 2.6 or before. - end - if File.exist?(path) - FileUtils.remove_entry_secure(path, true) - end - end + FileUtils.remove_entry_secure(path, true) end def create_target_info(path)