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 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 <[email protected]>
  • Loading branch information
ashie committed Jun 1, 2022
1 parent 90ad37d commit a065d02
Showing 1 changed file with 2 additions and 51 deletions.
53 changes: 2 additions & 51 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a065d02

Please sign in to comment.