From a065d02de25fffa037569b748fd628f4449ee8c1 Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Wed, 1 Jun 2022 14:07:45 +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 in hacky way is no longer needed. In addition, we already dropped Ruby 2.6 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), probably it intend `FileUtils.rm_r`. Signed-off-by: Takuro Ashie --- test/plugin/test_in_tail.rb | 53 ++----------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-) diff --git a/test/plugin/test_in_tail.rb b/test/plugin/test_in_tail.rb index 6fa6ccfc59..cf7610eb5b 100644 --- a/test/plugin/test_in_tail.rb +++ b/test/plugin/test_in_tail.rb @@ -31,60 +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 - # expect test driver block release already owned file handle. - 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)