diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py index c70c5eb34ae338..5a1238f04c03c6 100644 --- a/Lib/test/test_importlib/source/test_file_loader.py +++ b/Lib/test/test_importlib/source/test_file_loader.py @@ -236,6 +236,7 @@ def test_unloadable(self): loader.load_module('bad name') @util.writes_bytecode_files + @util.without_source_date_epoch def test_checked_hash_based_pyc(self): with util.create_modules('_temp') as mapping: source = mapping['_temp'] @@ -267,6 +268,7 @@ def test_checked_hash_based_pyc(self): ) @util.writes_bytecode_files + @util.without_source_date_epoch def test_overridden_checked_hash_based_pyc(self): with util.create_modules('_temp') as mapping, \ unittest.mock.patch('_imp.check_hash_based_pycs', 'never'): @@ -292,6 +294,7 @@ def test_overridden_checked_hash_based_pyc(self): self.assertEqual(mod.state, 'old') @util.writes_bytecode_files + @util.without_source_date_epoch def test_unchecked_hash_based_pyc(self): with util.create_modules('_temp') as mapping: source = mapping['_temp'] @@ -322,6 +325,7 @@ def test_unchecked_hash_based_pyc(self): ) @util.writes_bytecode_files + @util.without_source_date_epoch def test_overiden_unchecked_hash_based_pyc(self): with util.create_modules('_temp') as mapping, \ unittest.mock.patch('_imp.check_hash_based_pycs', 'always'): @@ -359,6 +363,25 @@ def test_overiden_unchecked_hash_based_pyc(self): abc=importlib_abc, util=importlib_util) +# Run tests with SOURCE_DATE_EPOCH set explicitly. +class SourceDateEpochTestMeta(type(Source_SimpleTest)): + def __new__(mcls, name, bases, dct): + cls = super().__new__(mcls, name, bases, dct) + + for attr in dir(cls): + if attr.startswith('test_'): + meth = getattr(cls, attr) + wrapper = util.with_source_date_epoch(meth) + setattr(cls, attr, wrapper) + + return cls + + +class SourceDateEpoch_SimpleTest(Source_SimpleTest, + metaclass=SourceDateEpochTestMeta): + pass + + class BadBytecodeTest: def import_(self, file, module_name): @@ -617,6 +640,7 @@ def test_bad_marshal(self): # [bad timestamp] @util.writes_bytecode_files + @util.without_source_date_epoch def test_old_timestamp(self): # When the timestamp is older than the source, bytecode should be # regenerated. @@ -668,6 +692,12 @@ class SourceLoaderBadBytecodeTestPEP451( util=importlib_util) +class SourceDateEpoch_SourceBadBytecodePEP451( + Source_SourceBadBytecodePEP451, + metaclass=SourceDateEpochTestMeta): + pass + + class SourceLoaderBadBytecodeTestPEP302( SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): pass diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py index 196ea1c9d4dda5..28a800bdfff74f 100644 --- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -307,6 +307,26 @@ def wrapper(*args, **kwargs): return wrapper +def without_source_date_epoch(fxn): + """Runs function with SOURCE_DATE_EPOCH unset.""" + @functools.wraps(fxn) + def wrapper(*args, **kwargs): + with support.EnvironmentVarGuard() as env: + env.unset('SOURCE_DATE_EPOCH') + return fxn(*args, **kwargs) + return wrapper + + +def with_source_date_epoch(fxn): + """Runs function with SOURCE_DATE_EPOCH set.""" + @functools.wraps(fxn) + def wrapper(*args, **kwargs): + with support.EnvironmentVarGuard() as env: + env['SOURCE_DATE_EPOCH'] = '123456789' + return fxn(*args, **kwargs) + return wrapper + + def ensure_bytecode_path(bytecode_path): """Ensure that the __pycache__ directory for PEP 3147 pyc file exists. diff --git a/Misc/NEWS.d/next/Tests/2018-09-21-18-36-29.bpo-34022.j6a0nt.rst b/Misc/NEWS.d/next/Tests/2018-09-21-18-36-29.bpo-34022.j6a0nt.rst new file mode 100644 index 00000000000000..bb20b0d8d9e427 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2018-09-21-18-36-29.bpo-34022.j6a0nt.rst @@ -0,0 +1 @@ +Unbreak importlib tests when :envvar:`SOURCE_DATE_EPOCH` is set.