|
19 | 19 | from random import randint, random, randbytes |
20 | 20 |
|
21 | 21 | from test import archiver_tests |
22 | | -from test.support import script_helper |
| 22 | +from test.support import script_helper, os_helper |
23 | 23 | from test.support import ( |
24 | 24 | findfile, requires_zlib, requires_bz2, requires_lzma, |
25 | 25 | captured_stdout, captured_stderr, requires_subprocess |
@@ -1781,6 +1781,35 @@ def test_writestr_extended_local_header_issue1202(self): |
1781 | 1781 | zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header. |
1782 | 1782 | orig_zip.writestr(zinfo, data) |
1783 | 1783 |
|
| 1784 | + def test_write_with_source_date_epoch(self): |
| 1785 | + with os_helper.EnvironmentVarGuard() as env: |
| 1786 | + # Set the SOURCE_DATE_EPOCH environment variable to a specific timestamp |
| 1787 | + env['SOURCE_DATE_EPOCH'] = "1727440508" |
| 1788 | + |
| 1789 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 1790 | + zf.writestr("test_source_date_epoch.txt", "Testing SOURCE_DATE_EPOCH") |
| 1791 | + |
| 1792 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 1793 | + zip_info = zf.getinfo("test_source_date_epoch.txt") |
| 1794 | + get_time = time.gmtime(int(os.environ['SOURCE_DATE_EPOCH']))[:6] |
| 1795 | + # Compare each element of the date_time tuple |
| 1796 | + # Allow for a 1-second difference |
| 1797 | + for z_time, g_time in zip(zip_info.date_time, get_time): |
| 1798 | + self.assertAlmostEqual(z_time, g_time, delta=1) |
| 1799 | + |
| 1800 | + def test_write_without_source_date_epoch(self): |
| 1801 | + if 'SOURCE_DATE_EPOCH' in os.environ: |
| 1802 | + del os.environ['SOURCE_DATE_EPOCH'] |
| 1803 | + |
| 1804 | + with zipfile.ZipFile(TESTFN, "w") as zf: |
| 1805 | + zf.writestr("test_no_source_date_epoch.txt", "Testing without SOURCE_DATE_EPOCH") |
| 1806 | + |
| 1807 | + with zipfile.ZipFile(TESTFN, "r") as zf: |
| 1808 | + zip_info = zf.getinfo("test_no_source_date_epoch.txt") |
| 1809 | + current_time = time.gmtime()[:6] |
| 1810 | + for z_time, c_time in zip(zip_info.date_time, current_time): |
| 1811 | + self.assertAlmostEqual(z_time, c_time, delta=1) |
| 1812 | + |
1784 | 1813 | def test_close(self): |
1785 | 1814 | """Check that the zipfile is closed after the 'with' block.""" |
1786 | 1815 | with zipfile.ZipFile(TESTFN2, "w") as zipfp: |
|
0 commit comments