[rubygems/rubygems] Add mtime to Gem::Package::TarWriter#add_file argument

Since 9e21dd9, Gem::Package::TarWriter#add_file adds the file to
the tar with Gem.source_date_epoch for its mtime.
This behavior breaks the code depending on the previous add_file
behavior.
Therefore, add_file accepts mtime as an argument, and uses
Gem.source_date_epoch if not specified.

https://github.com/rubygems/rubygems/commit/7020ea98a0
This commit is contained in:
Yusuke Nakamura 2025-05-03 23:59:41 +09:00 committed by Hiroshi SHIBATA
parent 8685a81e6a
commit 819ecd115d
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2
2 changed files with 16 additions and 4 deletions

View File

@ -95,10 +95,11 @@ class Gem::Package::TarWriter
end
##
# Adds file +name+ with permissions +mode+, and yields an IO for writing the
# file to
# Adds file +name+ with permissions +mode+ and mtime +mtime+ (sets
# Gem.source_date_epoch if not specified), and yields an IO for
# writing the file to
def add_file(name, mode) # :yields: io
def add_file(name, mode, mtime=nil) # :yields: io
check_closed
name, prefix = split_name name
@ -118,7 +119,7 @@ class Gem::Package::TarWriter
header = Gem::Package::TarHeader.new name: name, mode: mode,
size: size, prefix: prefix,
mtime: Gem.source_date_epoch
mtime: mtime || Gem.source_date_epoch
@io.write header
@io.pos = final_pos

View File

@ -50,6 +50,17 @@ class TestGemPackageTarWriter < Gem::Package::TarTestCase
end
end
def test_add_file_with_mtime
Time.stub :now, Time.at(1_458_518_157) do
@tar_writer.add_file "x", 0o644, Time.now do |f|
f.write "a" * 10
end
assert_headers_equal(tar_file_header("x", "", 0o644, 10, Time.now),
@io.string[0, 512])
end
end
def test_add_symlink
Time.stub :now, Time.at(1_458_518_157) do
@tar_writer.add_symlink "x", "y", 0o644