|
22 | 22 | from random import randint, random, randbytes |
23 | 23 |
|
24 | 24 | from test import archiver_tests |
25 | | -from test.support import script_helper, os_helper |
| 25 | +from test.support import script_helper |
26 | 26 | from test.support import ( |
27 | 27 | findfile, requires_zlib, requires_bz2, requires_lzma, |
28 | 28 | requires_zstd, captured_stdout, captured_stderr, requires_subprocess, |
29 | 29 | cpython_only, gc_collect |
30 | 30 | ) |
31 | 31 | from test.support.os_helper import ( |
32 | | - TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count, FakePath |
| 32 | + TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count, FakePath, |
| 33 | + with_source_date_epoch, without_source_date_epoch, |
33 | 34 | ) |
34 | 35 | from test.support.import_helper import ensure_lazy_imports |
35 | 36 | from test.support.warnings_helper import check_no_resource_warning |
@@ -1961,6 +1962,7 @@ def test_repack_file_entry_before_first_file(self): |
1961 | 1962 | with zipfile.ZipFile(TESTFN) as zh: |
1962 | 1963 | self.assertIsNone(zh.testzip()) |
1963 | 1964 |
|
| 1965 | + @without_source_date_epoch # SOURCE_DATE_EPOCH would bypass the time mock below |
1964 | 1966 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
1965 | 1967 | def test_repack_bytes_before_removed_files(self): |
1966 | 1968 | """Should preserve if there are bytes before stale local file entries.""" |
@@ -2005,6 +2007,7 @@ def test_repack_bytes_before_removed_files(self): |
2005 | 2007 | with zipfile.ZipFile(TESTFN) as zh: |
2006 | 2008 | self.assertIsNone(zh.testzip()) |
2007 | 2009 |
|
| 2010 | + @without_source_date_epoch # SOURCE_DATE_EPOCH would bypass the time mock below |
2008 | 2011 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2009 | 2012 | def test_repack_bytes_after_removed_files(self): |
2010 | 2013 | """Should keep extra bytes if there are bytes after stale local file entries.""" |
@@ -2048,6 +2051,7 @@ def test_repack_bytes_after_removed_files(self): |
2048 | 2051 | with zipfile.ZipFile(TESTFN) as zh: |
2049 | 2052 | self.assertIsNone(zh.testzip()) |
2050 | 2053 |
|
| 2054 | + @without_source_date_epoch # SOURCE_DATE_EPOCH would bypass the time mock below |
2051 | 2055 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2052 | 2056 | def test_repack_bytes_between_removed_files(self): |
2053 | 2057 | """Should strip only local file entries before random bytes.""" |
@@ -2252,6 +2256,7 @@ def test_repack_removed_partial(self): |
2252 | 2256 | with zipfile.ZipFile(TESTFN) as zh: |
2253 | 2257 | self.assertIsNone(zh.testzip()) |
2254 | 2258 |
|
| 2259 | + @without_source_date_epoch # SOURCE_DATE_EPOCH would bypass the time mock below |
2255 | 2260 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2256 | 2261 | def test_repack_removed_bytes_between_files(self): |
2257 | 2262 | """Should not remove bytes between local file entries.""" |
@@ -4004,29 +4009,24 @@ def test_writestr_extended_local_header_issue1202(self): |
4004 | 4009 | zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header. |
4005 | 4010 | orig_zip.writestr(zinfo, data) |
4006 | 4011 |
|
| 4012 | + @with_source_date_epoch(epoch=1735715999) |
4007 | 4013 | def test_write_with_source_date_epoch(self): |
4008 | | - with os_helper.EnvironmentVarGuard() as env: |
4009 | | - # Set the SOURCE_DATE_EPOCH environment variable to a specific timestamp |
4010 | | - env['SOURCE_DATE_EPOCH'] = "1735715999" |
4011 | | - |
4012 | | - with zipfile.ZipFile(TESTFN, "w") as zf: |
4013 | | - zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH") |
| 4014 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 4015 | + zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH") |
4014 | 4016 |
|
4015 | | - with zipfile.ZipFile(TESTFN, "r") as zf: |
4016 | | - zip_info = zf.getinfo("test_source_date_epoch.txt") |
4017 | | - expected_utc = (2025, 1, 1, 7, 19, 58) |
4018 | | - self.assertEqual(zip_info.date_time, expected_utc) |
| 4017 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 4018 | + zip_info = zf.getinfo("test_source_date_epoch.txt") |
| 4019 | + expected_utc = (2025, 1, 1, 7, 19, 58) |
| 4020 | + self.assertEqual(zip_info.date_time, expected_utc) |
4019 | 4021 |
|
| 4022 | + @without_source_date_epoch |
4020 | 4023 | def test_write_without_source_date_epoch(self): |
4021 | | - with os_helper.EnvironmentVarGuard() as env: |
4022 | | - del env['SOURCE_DATE_EPOCH'] |
4023 | | - |
4024 | | - with zipfile.ZipFile(TESTFN, "w") as zf: |
4025 | | - zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH") |
| 4024 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 4025 | + zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH") |
4026 | 4026 |
|
4027 | | - with zipfile.ZipFile(TESTFN, "r") as zf: |
4028 | | - zip_info = zf.getinfo("test_no_source_date_epoch.txt") |
4029 | | - self.assertTimestampAlmostEqual(time.localtime(), zip_info.date_time, tolerance=2) |
| 4027 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 4028 | + zip_info = zf.getinfo("test_no_source_date_epoch.txt") |
| 4029 | + self.assertTimestampAlmostEqual(time.localtime(), zip_info.date_time, tolerance=2) |
4030 | 4030 |
|
4031 | 4031 | def assertTimestampAlmostEqual(self, time1, time2, tolerance): |
4032 | 4032 | import datetime |
|
0 commit comments