Skip to content

Commit a11dcf7

Browse files
committed
tests
1 parent 6a19ed1 commit a11dcf7

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io
22
import itertools
33
import contextlib
4+
import os
45
import pathlib
56
import pickle
67
import stat
@@ -665,7 +666,9 @@ def for_name(cls, name, archive):
665666
666667
TODO: extract this functionality and re-use
667668
"""
668-
self = cls(filename=name, date_time=time.localtime(time.time())[:6])
669+
epoch = os.environ.get('SOURCE_DATE_EPOCH')
670+
get_time = int(epoch) if epoch is not None else time.time()
671+
self = cls(filename=name, date_time=time.gmtime(get_time)[:6])
669672
self.compress_type = archive.compression
670673
self.compress_level = archive.compresslevel
671674
if self.filename.endswith('/'): # pragma: no cover

Lib/test/test_zipfile/test_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,27 @@ def test_writestr_extended_local_header_issue1202(self):
17801780
zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header.
17811781
orig_zip.writestr(zinfo, data)
17821782

1783+
def test_write_with_source_date_epoch(self):
1784+
os.environ['SOURCE_DATE_EPOCH'] = "1727351057"
1785+
1786+
with zipfile.ZipFile(TESTFN, "w") as zf:
1787+
zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH")
1788+
1789+
with zipfile.ZipFile(TESTFN, "r") as zf:
1790+
zip_info = zf.getinfo("test_source_date_epoch.txt")
1791+
self.assertEqual(zip_info.date_time, time.gmtime(int(os.environ['SOURCE_DATE_EPOCH']))[:6])
1792+
1793+
def test_write_without_source_date_epoch(self):
1794+
if 'SOURCE_DATE_EPOCH' in os.environ:
1795+
del os.environ['SOURCE_DATE_EPOCH']
1796+
1797+
with zipfile.ZipFile(TESTFN, "w") as zf:
1798+
zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH")
1799+
1800+
with zipfile.ZipFile(TESTFN, "r") as zf:
1801+
zip_info = zf.getinfo("test_no_source_date_epoch.txt")
1802+
self.assertNotEqual(zip_info.date_time, time.gmtime()[:6])
1803+
17831804
def test_close(self):
17841805
"""Check that the zipfile is closed after the 'with' block."""
17851806
with zipfile.ZipFile(TESTFN2, "w") as zipfp:

Lib/zipfile/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,10 +1904,10 @@ def writestr(self, zinfo_or_arcname, data,
19041904
if isinstance(data, str):
19051905
data = data.encode("utf-8")
19061906
if not isinstance(zinfo_or_arcname, ZipInfo):
1907-
time_ = os.environ.get('SOURCE_DATE_EPOCH') or time.time()
1908-
date_time = time.gmtime(time_)[:6]
1907+
epoch = os.environ.get('SOURCE_DATE_EPOCH')
1908+
get_time = int(epoch) if epoch else time.time()
19091909
zinfo = ZipInfo(filename=zinfo_or_arcname,
1910-
date_time=date_time)
1910+
date_time=time.gmtime(get_time)[:6])
19111911
zinfo.compress_type = self.compression
19121912
zinfo.compress_level = self.compresslevel
19131913
if zinfo.filename.endswith('/'):
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
:func:`zipfile.writestr` now respect ``SOURCE_DATE_EPOCH``, that distributions can
2-
set centrally and have build tools consume this in order to produce
3-
reproducible output.
1+
:meth:`zipfile.ZipFile.writestr` now respect ``SOURCE_DATE_EPOCH`` that
2+
distributions can set centrally and have build tools consume this in order to
3+
produce reproducible output.

0 commit comments

Comments
 (0)