Merge lp:~gz/brz/test_lazy_import_flakey into lp:brz

Proposed by Martin Packman
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~gz/brz/test_lazy_import_flakey
Merge into: lp:brz
Diff against target: 87 lines (+19/-11)
3 files modified
breezy/lazy_import.py (+6/-3)
breezy/tests/test_lazy_import.py (+5/-8)
python3.passing (+8/-0)
To merge this branch: bzr merge lp:~gz/brz/test_lazy_import_flakey
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+348792@code.launchpad.net

Commit message

Make lazy import tests that replace __import__ work

Description of the change

Should fix tests that fail under CI but seem to pass locally by not doing an unsafe replacement of the __import__ builtin. Instead alias it at the module level for replacement in testing.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

\o/

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/lazy_import.py'
2--- breezy/lazy_import.py 2017-05-22 23:02:15 +0000
3+++ breezy/lazy_import.py 2018-06-30 23:53:50 +0000
4@@ -129,6 +129,9 @@
5 ScopeReplacer._should_proxy = False
6
7
8+_builtin_import = __import__
9+
10+
11 class ImportReplacer(ScopeReplacer):
12 """This is designed to replace only a portion of an import list.
13
14@@ -195,12 +198,12 @@
15 children = object.__getattribute__(self, '_import_replacer_children')
16 member = object.__getattribute__(self, '_member')
17 module_path = object.__getattribute__(self, '_module_path')
18- module_python_path = '.'.join(module_path)
19+ name = '.'.join(module_path)
20 if member is not None:
21- module = __import__(module_python_path, scope, scope, [member], level=0)
22+ module = _builtin_import(name, scope, scope, [member], level=0)
23 return getattr(module, member)
24 else:
25- module = __import__(module_python_path, scope, scope, [], level=0)
26+ module = _builtin_import(name, scope, scope, [], level=0)
27 for path in module_path[1:]:
28 module = getattr(module, path)
29
30
31=== modified file 'breezy/tests/test_lazy_import.py'
32--- breezy/tests/test_lazy_import.py 2017-11-12 13:53:51 +0000
33+++ breezy/tests/test_lazy_import.py 2018-06-30 23:53:50 +0000
34@@ -466,14 +466,11 @@
35 sys.path.append(base_path)
36 self.addCleanup(sys.path.remove, base_path)
37
38- original_import = __import__
39 def instrumented_import(mod, scope1, scope2, fromlist, level):
40 self.actions.append(('import', mod, fromlist, level))
41- return original_import(mod, scope1, scope2, fromlist, level)
42- def cleanup():
43- __builtins__['__import__'] = original_import
44- self.addCleanup(cleanup)
45- __builtins__['__import__'] = instrumented_import
46+ return __import__(mod, scope1, scope2, fromlist, level=level)
47+ self.addCleanup(setattr, lazy_import, '_builtin_import', __import__)
48+ lazy_import._builtin_import = instrumented_import
49
50 def create_modules(self):
51 """Create some random modules to be imported.
52@@ -530,14 +527,14 @@
53 """Test that a real import of these modules works"""
54 sub_mod_path = '.'.join([self.root_name, self.sub_name,
55 self.submoda_name])
56- root = __import__(sub_mod_path, globals(), locals(), [], 0)
57+ root = lazy_import._builtin_import(sub_mod_path, {}, {}, [], 0)
58 self.assertEqual(1, root.var1)
59 self.assertEqual(3, getattr(root, self.sub_name).var3)
60 self.assertEqual(4, getattr(getattr(root, self.sub_name),
61 self.submoda_name).var4)
62
63 mod_path = '.'.join([self.root_name, self.mod_name])
64- root = __import__(mod_path, globals(), locals(), [], 0)
65+ root = lazy_import._builtin_import(mod_path, {}, {}, [], 0)
66 self.assertEqual(2, getattr(root, self.mod_name).var2)
67
68 self.assertEqual([('import', sub_mod_path, [], 0),
69
70=== modified file 'python3.passing'
71--- python3.passing 2018-06-30 23:46:05 +0000
72+++ python3.passing 2018-06-30 23:53:50 +0000
73@@ -17301,6 +17301,14 @@
74 breezy.tests.test_lazy_import.TestImportProcessor.test_mixed
75 breezy.tests.test_lazy_import.TestImportProcessor.test_name_collision
76 breezy.tests.test_lazy_import.TestImportProcessor.test_relative_imports
77+breezy.tests.test_lazy_import.TestImportReplacerHelper.test_basic_import
78+breezy.tests.test_lazy_import.TestImportReplacer.test_import_mod
79+breezy.tests.test_lazy_import.TestImportReplacer.test_import_mod_from_root
80+breezy.tests.test_lazy_import.TestImportReplacer.test_import_root
81+breezy.tests.test_lazy_import.TestImportReplacer.test_import_root_and_mod
82+breezy.tests.test_lazy_import.TestImportReplacer.test_import_root_and_root_mod
83+breezy.tests.test_lazy_import.TestImportReplacer.test_import_root_sub_submod
84+breezy.tests.test_lazy_import.TestLazyImportProcessor.test_import_deep
85 breezy.tests.test_lazy_import.TestLazyImportProcessor.test_lazy_import
86 breezy.tests.test_lazy_import.TestLazyImportProcessor.test_root
87 breezy.tests.test_lazy_import.TestScopeReplacerReentrance.test_call

Subscribers

People subscribed via source and target branches