bpo-34022: Stop forcing of hash-based invalidation with SOURCE_DATE_E… · python/cpython@24b51b1 (original) (raw)
`@@ -22,7 +22,11 @@
`
22
22
`from test import support
`
23
23
`from test.support import script_helper
`
24
24
``
25
``
`-
class CompileallTests(unittest.TestCase):
`
``
25
`+
from .test_py_compile import without_source_date_epoch
`
``
26
`+
from .test_py_compile import SourceDateEpochTestMeta
`
``
27
+
``
28
+
``
29
`+
class CompileallTestsBase:
`
26
30
``
27
31
`def setUp(self):
`
28
32
`self.directory = tempfile.mkdtemp()
`
`@@ -46,7 +50,7 @@ def add_bad_source_file(self):
`
46
50
`with open(self.bad_source_path, 'w') as file:
`
47
51
`file.write('x (\n')
`
48
52
``
49
``
`-
def data(self):
`
``
53
`+
def timestamp_metadata(self):
`
50
54
`with open(self.bc_path, 'rb') as file:
`
51
55
`data = file.read(12)
`
52
56
`mtime = int(os.stat(self.source_path).st_mtime)
`
`@@ -57,16 +61,18 @@ def data(self):
`
57
61
`def recreation_check(self, metadata):
`
58
62
`"""Check that compileall recreates bytecode when the new metadata is
`
59
63
` used."""
`
``
64
`+
if os.environ.get('SOURCE_DATE_EPOCH'):
`
``
65
`+
raise unittest.SkipTest('SOURCE_DATE_EPOCH is set')
`
60
66
`py_compile.compile(self.source_path)
`
61
``
`-
self.assertEqual(*self.data())
`
``
67
`+
self.assertEqual(*self.timestamp_metadata())
`
62
68
`with open(self.bc_path, 'rb') as file:
`
63
69
`bc = file.read()[len(metadata):]
`
64
70
`with open(self.bc_path, 'wb') as file:
`
65
71
`file.write(metadata)
`
66
72
`file.write(bc)
`
67
``
`-
self.assertNotEqual(*self.data())
`
``
73
`+
self.assertNotEqual(*self.timestamp_metadata())
`
68
74
`compileall.compile_dir(self.directory, force=False, quiet=True)
`
69
``
`-
self.assertTrue(*self.data())
`
``
75
`+
self.assertTrue(*self.timestamp_metadata())
`
70
76
``
71
77
`def test_mtime(self):
`
72
78
`# Test a change in mtime leads to a new .pyc.
`
`@@ -189,6 +195,21 @@ def test_compile_missing_multiprocessing(self, compile_file_mock):
`
189
195
`compileall.compile_dir(self.directory, quiet=True, workers=5)
`
190
196
`self.assertTrue(compile_file_mock.called)
`
191
197
``
``
198
+
``
199
`+
class CompileallTestsWithSourceEpoch(CompileallTestsBase,
`
``
200
`+
unittest.TestCase,
`
``
201
`+
metaclass=SourceDateEpochTestMeta,
`
``
202
`+
source_date_epoch=True):
`
``
203
`+
pass
`
``
204
+
``
205
+
``
206
`+
class CompileallTestsWithoutSourceEpoch(CompileallTestsBase,
`
``
207
`+
unittest.TestCase,
`
``
208
`+
metaclass=SourceDateEpochTestMeta,
`
``
209
`+
source_date_epoch=False):
`
``
210
`+
pass
`
``
211
+
``
212
+
192
213
`class EncodingTest(unittest.TestCase):
`
193
214
`"""Issue 6716: compileall should escape source code when printing errors
`
194
215
` to stdout."""
`
`@@ -212,7 +233,7 @@ def test_error(self):
`
212
233
`sys.stdout = orig_stdout
`
213
234
``
214
235
``
215
``
`-
class CommandLineTests(unittest.TestCase):
`
``
236
`+
class CommandLineTestsBase:
`
216
237
`"""Test compileall's CLI."""
`
217
238
``
218
239
`@classmethod
`
`@@ -285,6 +306,7 @@ def test_no_args_compiles_path(self):
`
285
306
`self.assertNotCompiled(self.initfn)
`
286
307
`self.assertNotCompiled(self.barfn)
`
287
308
``
``
309
`+
@without_source_date_epoch # timestamp invalidation test
`
288
310
`def test_no_args_respects_force_flag(self):
`
289
311
`self._skip_if_sys_path_not_writable()
`
290
312
`bazfn = script_helper.make_script(self.directory, 'baz', '')
`
`@@ -353,6 +375,7 @@ def test_multiple_runs(self):
`
353
375
`self.assertTrue(os.path.exists(self.pkgdir_cachedir))
`
354
376
`self.assertFalse(os.path.exists(cachecachedir))
`
355
377
``
``
378
`+
@without_source_date_epoch # timestamp invalidation test
`
356
379
`def test_force(self):
`
357
380
`self.assertRunOK('-q', self.pkgdir)
`
358
381
`pycpath = importlib.util.cache_from_source(self.barfn)
`
`@@ -556,5 +579,20 @@ def test_workers_available_cores(self, compile_dir):
`
556
579
`self.assertEqual(compile_dir.call_args[-1]['workers'], None)
`
557
580
``
558
581
``
``
582
`+
class CommmandLineTestsWithSourceEpoch(CommandLineTestsBase,
`
``
583
`+
unittest.TestCase,
`
``
584
`+
metaclass=SourceDateEpochTestMeta,
`
``
585
`+
source_date_epoch=True):
`
``
586
`+
pass
`
``
587
+
``
588
+
``
589
`+
class CommmandLineTestsNoSourceEpoch(CommandLineTestsBase,
`
``
590
`+
unittest.TestCase,
`
``
591
`+
metaclass=SourceDateEpochTestMeta,
`
``
592
`+
source_date_epoch=False):
`
``
593
`+
pass
`
``
594
+
``
595
+
``
596
+
559
597
`if name == "main":
`
560
598
`unittest.main()
`