/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_lazy_import.py

  • Committer: Martin
  • Date: 2018-06-30 23:39:17 UTC
  • mto: This revision was merged to the branch mainline in revision 7013.
  • Revision ID: gzlist@googlemail.com-20180630233917-4lh8na2wist64hab
Make replacement for lazy import tests less dodgy

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.

Show diffs side-by-side

added added

removed removed

Lines of Context:
466
466
        sys.path.append(base_path)
467
467
        self.addCleanup(sys.path.remove, base_path)
468
468
 
469
 
        original_import = __import__
470
469
        def instrumented_import(mod, scope1, scope2, fromlist, level):
471
470
            self.actions.append(('import', mod, fromlist, level))
472
 
            return original_import(mod, scope1, scope2, fromlist, level)
473
 
        def cleanup():
474
 
            __builtins__['__import__'] = original_import
475
 
        self.addCleanup(cleanup)
476
 
        __builtins__['__import__'] = instrumented_import
 
471
            return __import__(mod, scope1, scope2, fromlist, level=level)
 
472
        self.addCleanup(setattr, lazy_import, '_builtin_import', __import__)
 
473
        lazy_import._builtin_import = instrumented_import
477
474
 
478
475
    def create_modules(self):
479
476
        """Create some random modules to be imported.
530
527
        """Test that a real import of these modules works"""
531
528
        sub_mod_path = '.'.join([self.root_name, self.sub_name,
532
529
                                  self.submoda_name])
533
 
        root = __import__(sub_mod_path, globals(), locals(), [], 0)
 
530
        root = lazy_import._builtin_import(sub_mod_path, {}, {}, [], 0)
534
531
        self.assertEqual(1, root.var1)
535
532
        self.assertEqual(3, getattr(root, self.sub_name).var3)
536
533
        self.assertEqual(4, getattr(getattr(root, self.sub_name),
537
534
                                    self.submoda_name).var4)
538
535
 
539
536
        mod_path = '.'.join([self.root_name, self.mod_name])
540
 
        root = __import__(mod_path, globals(), locals(), [], 0)
 
537
        root = lazy_import._builtin_import(mod_path, {}, {}, [], 0)
541
538
        self.assertEqual(2, getattr(root, self.mod_name).var2)
542
539
 
543
540
        self.assertEqual([('import', sub_mod_path, [], 0),