[ruby/tempfile] Support anonymous tempfile on earlier than Ruby 3.2
https://github.com/ruby/tempfile/commit/7052805029
This commit is contained in:
parent
2e07c13049
commit
136cbf0441
@ -556,7 +556,7 @@ end
|
|||||||
# Related: Tempfile.new.
|
# Related: Tempfile.new.
|
||||||
#
|
#
|
||||||
def Tempfile.create(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block)
|
def Tempfile.create(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block)
|
||||||
if anonymous && RUBY_VERSION >= '3.2'
|
if anonymous
|
||||||
create_anonymous(basename, tmpdir, mode: mode, **options, &block)
|
create_anonymous(basename, tmpdir, mode: mode, **options, &block)
|
||||||
else
|
else
|
||||||
create_with_filename(basename, tmpdir, mode: mode, **options, &block)
|
create_with_filename(basename, tmpdir, mode: mode, **options, &block)
|
||||||
@ -593,6 +593,18 @@ private def create_with_filename(basename="", tmpdir=nil, mode: 0, **options)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
File.open(IO::NULL) do |f|
|
||||||
|
File.new(f.fileno, autoclose: false, path: "").path
|
||||||
|
rescue IOError
|
||||||
|
module PathAttr # :nodoc:
|
||||||
|
attr_reader :path
|
||||||
|
|
||||||
|
def self.set_path(file, path)
|
||||||
|
file.extend(self).instance_variable_set(:@path, path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block)
|
private def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block)
|
||||||
tmpfile = nil
|
tmpfile = nil
|
||||||
tmpdir = Dir.tmpdir() if tmpdir.nil?
|
tmpdir = Dir.tmpdir() if tmpdir.nil?
|
||||||
@ -608,12 +620,14 @@ private def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block
|
|||||||
mode |= File::SHARE_DELETE | File::BINARY # Windows needs them to unlink the opened file.
|
mode |= File::SHARE_DELETE | File::BINARY # Windows needs them to unlink the opened file.
|
||||||
tmpfile = create_with_filename(basename, tmpdir, mode: mode, **options)
|
tmpfile = create_with_filename(basename, tmpdir, mode: mode, **options)
|
||||||
File.unlink(tmpfile.path)
|
File.unlink(tmpfile.path)
|
||||||
|
tmppath = tmpfile.path
|
||||||
end
|
end
|
||||||
path = File.join(tmpdir, '')
|
path = File.join(tmpdir, '')
|
||||||
if tmpfile.path != path
|
unless tmppath == path
|
||||||
# clear path.
|
# clear path.
|
||||||
tmpfile.autoclose = false
|
tmpfile.autoclose = false
|
||||||
tmpfile = File.new(tmpfile.fileno, mode: File::RDWR, path: path)
|
tmpfile = File.new(tmpfile.fileno, mode: File::RDWR, path: path)
|
||||||
|
PathAttr.set_path(tmpfile, path) if defined?(PathAttr)
|
||||||
end
|
end
|
||||||
if block
|
if block
|
||||||
begin
|
begin
|
||||||
|
@ -488,11 +488,7 @@ puts Tempfile.new('foo').path
|
|||||||
Dir.mktmpdir {|d|
|
Dir.mktmpdir {|d|
|
||||||
t = Tempfile.create("", d, anonymous: true)
|
t = Tempfile.create("", d, anonymous: true)
|
||||||
t.close
|
t.close
|
||||||
if RUBY_VERSION >= '3.2'
|
assert_equal([], Dir.children(d))
|
||||||
assert_equal([], Dir.children(d))
|
|
||||||
else
|
|
||||||
refute_equal([], Dir.children(d))
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -500,11 +496,7 @@ puts Tempfile.new('foo').path
|
|||||||
Dir.mktmpdir {|d|
|
Dir.mktmpdir {|d|
|
||||||
begin
|
begin
|
||||||
t = Tempfile.create("", d, anonymous: true)
|
t = Tempfile.create("", d, anonymous: true)
|
||||||
if RUBY_VERSION >= '3.2'
|
assert_equal(File.join(d, ""), t.path)
|
||||||
assert_equal(File.join(d, ""), t.path)
|
|
||||||
else
|
|
||||||
refute_equal(File.join(d, ""), t.path)
|
|
||||||
end
|
|
||||||
ensure
|
ensure
|
||||||
t.close if t
|
t.close if t
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user