~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-11 20:51:55 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060911205155-e86e48e3fa709e7f
Test a nested import with multiple deep children

Show diffs side-by-side

added added

removed removed

Lines of Context:
260
260
                mod-XXX.py <= This will contain var2, func2
261
261
                sub-XXX/
262
262
                    __init__.py <= Contains var3, func3
263
 
                    submod-XXX.py <= contains var4, func4
 
263
                    submoda-XXX.py <= contains var4, func4
 
264
                    submodb-XXX.py <= containse var5, func5
264
265
        """
265
266
        rand_suffix = osutils.rand_chars(4)
266
267
        root_name = 'root_' + rand_suffix
267
268
        mod_name = 'mod_' + rand_suffix
268
269
        sub_name = 'sub_' + rand_suffix
269
 
        submod_name = 'submod_' + rand_suffix
 
270
        submoda_name = 'submoda_' + rand_suffix
 
271
        submodb_name = 'submodb_' + rand_suffix
 
272
 
270
273
        os.mkdir('base')
271
274
        root_path = osutils.pathjoin('base', root_name)
272
275
        os.mkdir(root_path)
290
293
            f.write('var3 = 3\ndef func3(a):\n  return a\n')
291
294
        finally:
292
295
            f.close()
293
 
        submod_path = osutils.pathjoin(sub_path, submod_name + '.py')
294
 
        f = open(submod_path, 'wb')
 
296
        submoda_path = osutils.pathjoin(sub_path, submoda_name + '.py')
 
297
        f = open(submoda_path, 'wb')
295
298
        try:
296
299
            f.write('var4 = 4\ndef func4(a):\n  return a\n')
297
300
        finally:
298
301
            f.close()
 
302
        submodb_path = osutils.pathjoin(sub_path, submodb_name + '.py')
 
303
        f = open(submodb_path, 'wb')
 
304
        try:
 
305
            f.write('var5 = 5\ndef func5(a):\n  return a\n')
 
306
        finally:
 
307
            f.close()
299
308
        self.root_name = root_name
300
309
        self.mod_name = mod_name
301
310
        self.sub_name = sub_name
302
 
        self.submod_name = submod_name
 
311
        self.submoda_name = submoda_name
 
312
        self.submodb_name = submodb_name
303
313
 
304
314
    def test_basic_import(self):
 
315
        """Test that a real import of these modules works"""
305
316
        sub_mod_path = '.'.join([self.root_name, self.sub_name,
306
 
                                  self.submod_name])
 
317
                                  self.submoda_name])
307
318
        root = __import__(sub_mod_path, globals(), locals(), [])
308
319
        self.assertEqual(1, root.var1)
309
320
        self.assertEqual(3, getattr(root, self.sub_name).var3)
310
321
        self.assertEqual(4, getattr(getattr(root, self.sub_name),
311
 
                                    self.submod_name).var4)
 
322
                                    self.submoda_name).var4)
312
323
 
313
324
        mod_path = '.'.join([self.root_name, self.mod_name])
314
325
        root = __import__(mod_path, globals(), locals(), [])
433
444
                         ], self.actions)
434
445
 
435
446
    def test_import_root_and_root_mod(self):
436
 
        """Test that 'import root-XXX, root-XXX.mod-XXX' can be done"""
 
447
        """Test that 'import root, root.mod' can be done.
 
448
 
 
449
        The second import should re-use the first one, and just add
 
450
        children to be imported.
 
451
        """
437
452
        try:
438
453
            root4
439
454
        except NameError:
471
486
                          ('_import', 'mod4'),
472
487
                          ('import', mod_path, []),
473
488
                         ], self.actions)
 
489
 
 
490
    def test_import_root_sub_submod(self):
 
491
        """Test import root.mod, root.sub.submoda, root.sub.submodb
 
492
        root should be a lazy import, with multiple children, who also
 
493
        have children to be imported.
 
494
        And when root is imported, the children should be lazy, and
 
495
        reuse the intermediate lazy object.
 
496
        """
 
497
        try:
 
498
            root5
 
499
        except NameError:
 
500
            # root4 shouldn't exist yet
 
501
            pass
 
502
        else:
 
503
            self.fail('root5 was not supposed to exist yet')
 
504
 
 
505
        InstrumentedImportReplacer(scope=globals(),
 
506
            name='root5', module_path=[self.root_name], member=None,
 
507
            children=[('mod5', [self.root_name, self.mod_name], []),
 
508
                      ('sub5', [self.root_name, self.sub_name],
 
509
                            [('submoda5', [self.root_name, self.sub_name,
 
510
                                         self.submoda_name], []),
 
511
                             ('submodb5', [self.root_name, self.sub_name,
 
512
                                          self.submodb_name], [])
 
513
                            ]),
 
514
                     ])
 
515
 
 
516
        # So 'root5' should be a lazy import
 
517
        self.assertEqual(InstrumentedImportReplacer,
 
518
                         object.__getattribute__(root5, '__class__'))
 
519
 
 
520
        # Accessing root5.mod5 should import root, but mod should stay lazy
 
521
        self.assertEqual(InstrumentedImportReplacer,
 
522
                         object.__getattribute__(root5.mod5, '__class__'))
 
523
        # root5.sub5 should still be lazy, but not re-import root5
 
524
        self.assertEqual(InstrumentedImportReplacer,
 
525
                         object.__getattribute__(root5.sub5, '__class__'))
 
526
        # Accessing root5.sub5.submoda5 should import sub5, but not either
 
527
        # of the sub objects (they should be available as lazy objects
 
528
        self.assertEqual(InstrumentedImportReplacer,
 
529
                     object.__getattribute__(root5.sub5.submoda5, '__class__'))
 
530
        self.assertEqual(InstrumentedImportReplacer,
 
531
                     object.__getattribute__(root5.sub5.submodb5, '__class__'))
 
532
 
 
533
        # This should import mod5
 
534
        self.assertEqual(2, root5.mod5.var2)
 
535
        # These should import submoda5 and submodb5
 
536
        self.assertEqual(4, root5.sub5.submoda5.var4)
 
537
        self.assertEqual(5, root5.sub5.submodb5.var5)
 
538
 
 
539
        mod_path = self.root_name + '.' + self.mod_name
 
540
        sub_path = self.root_name + '.' + self.sub_name
 
541
        submoda_path = sub_path + '.' + self.submoda_name
 
542
        submodb_path = sub_path + '.' + self.submodb_name
 
543
 
 
544
        self.assertEqual([('__getattribute__', 'mod5'),
 
545
                          '_replace',
 
546
                          ('_import', 'root5'),
 
547
                          ('import', self.root_name, []),
 
548
                          ('__getattribute__', 'submoda5'),
 
549
                          '_replace',
 
550
                          ('_import', 'sub5'),
 
551
                          ('import', sub_path, []),
 
552
                          ('__getattribute__', 'var2'),
 
553
                          '_replace',
 
554
                          ('_import', 'mod5'),
 
555
                          ('import', mod_path, []),
 
556
                          ('__getattribute__', 'var4'),
 
557
                          '_replace',
 
558
                          ('_import', 'submoda5'),
 
559
                          ('import', submoda_path, []),
 
560
                          ('__getattribute__', 'var5'),
 
561
                          '_replace',
 
562
                          ('_import', 'submodb5'),
 
563
                          ('import', submodb_path, []),
 
564
                         ], self.actions)