|
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 |
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 |
|
@@ -1960,6 +1961,7 @@ def test_repack_file_entry_before_first_file(self): |
1960 | 1961 | with zipfile.ZipFile(TESTFN) as zh: |
1961 | 1962 | self.assertIsNone(zh.testzip()) |
1962 | 1963 |
|
| 1964 | + @without_source_date_epoch # it would override the time mock below |
1963 | 1965 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
1964 | 1966 | def test_repack_bytes_before_removed_files(self): |
1965 | 1967 | """Should preserve if there are bytes before stale local file entries.""" |
@@ -2004,6 +2006,7 @@ def test_repack_bytes_before_removed_files(self): |
2004 | 2006 | with zipfile.ZipFile(TESTFN) as zh: |
2005 | 2007 | self.assertIsNone(zh.testzip()) |
2006 | 2008 |
|
| 2009 | + @without_source_date_epoch # it would override the time mock below |
2007 | 2010 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2008 | 2011 | def test_repack_bytes_after_removed_files(self): |
2009 | 2012 | """Should keep extra bytes if there are bytes after stale local file entries.""" |
@@ -2047,6 +2050,7 @@ def test_repack_bytes_after_removed_files(self): |
2047 | 2050 | with zipfile.ZipFile(TESTFN) as zh: |
2048 | 2051 | self.assertIsNone(zh.testzip()) |
2049 | 2052 |
|
| 2053 | + @without_source_date_epoch # it would override the time mock below |
2050 | 2054 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2051 | 2055 | def test_repack_bytes_between_removed_files(self): |
2052 | 2056 | """Should strip only local file entries before random bytes.""" |
@@ -2251,6 +2255,7 @@ def test_repack_removed_partial(self): |
2251 | 2255 | with zipfile.ZipFile(TESTFN) as zh: |
2252 | 2256 | self.assertIsNone(zh.testzip()) |
2253 | 2257 |
|
| 2258 | + @without_source_date_epoch # it would override the time mock below |
2254 | 2259 | @mock.patch.object(time, 'time', new=lambda: 315590400) # fix time for ZipFile.writestr() |
2255 | 2260 | def test_repack_removed_bytes_between_files(self): |
2256 | 2261 | """Should not remove bytes between local file entries.""" |
@@ -4003,29 +4008,24 @@ def test_writestr_extended_local_header_issue1202(self): |
4003 | 4008 | zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header. |
4004 | 4009 | orig_zip.writestr(zinfo, data) |
4005 | 4010 |
|
| 4011 | + @with_source_date_epoch(epoch=1735715999) |
4006 | 4012 | def test_write_with_source_date_epoch(self): |
4007 | | - with os_helper.EnvironmentVarGuard() as env: |
4008 | | - # Set the SOURCE_DATE_EPOCH environment variable to a specific timestamp |
4009 | | - env['SOURCE_DATE_EPOCH'] = "1735715999" |
4010 | | - |
4011 | | - with zipfile.ZipFile(TESTFN, "w") as zf: |
4012 | | - zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH") |
| 4013 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 4014 | + zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH") |
4013 | 4015 |
|
4014 | | - with zipfile.ZipFile(TESTFN, "r") as zf: |
4015 | | - zip_info = zf.getinfo("test_source_date_epoch.txt") |
4016 | | - expected_utc = (2025, 1, 1, 7, 19, 58) |
4017 | | - self.assertEqual(zip_info.date_time, expected_utc) |
| 4016 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 4017 | + zip_info = zf.getinfo("test_source_date_epoch.txt") |
| 4018 | + expected_utc = (2025, 1, 1, 7, 19, 58) |
| 4019 | + self.assertEqual(zip_info.date_time, expected_utc) |
4018 | 4020 |
|
| 4021 | + @without_source_date_epoch |
4019 | 4022 | def test_write_without_source_date_epoch(self): |
4020 | | - with os_helper.EnvironmentVarGuard() as env: |
4021 | | - del env['SOURCE_DATE_EPOCH'] |
4022 | | - |
4023 | | - with zipfile.ZipFile(TESTFN, "w") as zf: |
4024 | | - zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH") |
| 4023 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 4024 | + zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH") |
4025 | 4025 |
|
4026 | | - with zipfile.ZipFile(TESTFN, "r") as zf: |
4027 | | - zip_info = zf.getinfo("test_no_source_date_epoch.txt") |
4028 | | - self.assertTimestampAlmostEqual(time.localtime(), zip_info.date_time, tolerance=2) |
| 4026 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 4027 | + zip_info = zf.getinfo("test_no_source_date_epoch.txt") |
| 4028 | + self.assertTimestampAlmostEqual(time.localtime(), zip_info.date_time, tolerance=2) |
4029 | 4029 |
|
4030 | 4030 | def assertTimestampAlmostEqual(self, time1, time2, tolerance): |
4031 | 4031 | import datetime |
|
0 commit comments