Skip to content

Commit

Permalink
test_in_tail: Simplify cleanup_directory and cleanup_file
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ashie committed Jun 1, 2022
1 parent a36b740 commit 944b16d
Showing 1 changed file with 2 additions and 52 deletions.
54 changes: 2 additions & 52 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 944b16d

Please sign in to comment.