Skip to content

Commit a8373c6

Browse files
duckinatordeivid-rodriguez
authored andcommitted
Merge pull request #8673 from unasuke/accepts-mtime-to-tar-writer-add-file
Add mtime to Gem::Package::TarWriter#add_file argument (cherry picked from commit 3ad1ed8)
1 parent 8951093 commit a8373c6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/rubygems/package/tar_writer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ def initialize(io)
9595
end
9696

9797
##
98-
# Adds file +name+ with permissions +mode+, and yields an IO for writing the
99-
# file to
98+
# Adds file +name+ with permissions +mode+ and mtime +mtime+ (sets
99+
# Gem.source_date_epoch if not specified), and yields an IO for
100+
# writing the file to
100101

101-
def add_file(name, mode) # :yields: io
102+
def add_file(name, mode, mtime=nil) # :yields: io
102103
check_closed
103104

104105
name, prefix = split_name name
@@ -118,7 +119,7 @@ def add_file(name, mode) # :yields: io
118119

119120
header = Gem::Package::TarHeader.new name: name, mode: mode,
120121
size: size, prefix: prefix,
121-
mtime: Gem.source_date_epoch
122+
mtime: mtime || Gem.source_date_epoch
122123

123124
@io.write header
124125
@io.pos = final_pos

test/rubygems/test_gem_package_tar_writer.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ def test_add_file_source_date_epoch
5050
end
5151
end
5252

53+
def test_add_file_with_mtime
54+
Time.stub :now, Time.at(1_458_518_157) do
55+
mtime = Time.now
56+
57+
@tar_writer.add_file "x", 0o644, mtime do |f|
58+
f.write "a" * 10
59+
end
60+
61+
assert_headers_equal(tar_file_header("x", "", 0o644, 10, mtime),
62+
@io.string[0, 512])
63+
end
64+
end
65+
5366
def test_add_symlink
5467
Time.stub :now, Time.at(1_458_518_157) do
5568
@tar_writer.add_symlink "x", "y", 0o644

0 commit comments

Comments
 (0)