def get_unisolated_imported_module(self): ''' get_module() should return a requested module, which should remain in sys.modules as it has been loaded unisolated. The same module should be returned on successive invocations. The same module should be returned from different contexts ''' same_context = ExecutionContext() same_context.add_import_path(self._nonsrc_path(same_context)) different_context = ExecutionContext(isolate_imports=False) different_context.add_import_path(self._nonsrc_path(different_context)) for mod in ('import_me', 'module_with_state'): spec = lancelot.Spec(same_context) spec.get_module(mod).should_be(Type(types.ModuleType)) same_module = same_context.get_module(mod) spec.get_module(mod).should_be(SameAs(same_module)) sys_spec = lancelot.Spec(sys.modules) sys_spec.it().should_contain(mod) different_module = different_context.get_module(mod) spec.get_module(mod).should_be(SameAs(different_module)) same_context.cleanup_imports() different_context.cleanup_imports()
def get_unisolated_module_type(self): ''' get_type() should return a requested module type, given the fully qualified name including module. The same type should be returned on successive invocations. The same type should also be returned from different contexts. State in a module should not be isolated within each context''' _types = {'StateAlteringClass': 'module_with_state'} execution_context = ExecutionContext(isolate_imports=False) execution_context.add_import_path(self._nonsrc_path(execution_context)) for _type, mod in _types.items(): fully_qualified_name = '%s.%s' % (mod, _type) same_type = execution_context.get_type(fully_qualified_name) lancelot.Spec(same_type.__name__).it().should_be(_type) spec = lancelot.Spec(execution_context) spec.get_type(fully_qualified_name).should_be(SameAs(same_type)) other_context = ExecutionContext(isolate_imports=False) other_context.add_import_path(self._nonsrc_path(other_context)) different_type = other_context.get_type(fully_qualified_name) spec.get_type(fully_qualified_name).should_be( SameAs(different_type)) instances = same_type(), different_type() spec = lancelot.Spec(instances[0]) spec.when(spec.alter_state()) spec.then(spec.get_state()).should_contain(1) spec.then(instances[1].get_state).should_contain(1) execution_context.cleanup_imports() other_context.cleanup_imports()
def get_unisolated_module_type(self): ''' get_type() should return a requested module type, given the fully qualified name including module. The same type should be returned on successive invocations. The same type should also be returned from different contexts. State in a module should not be isolated within each context''' _types = {'StateAlteringClass':'module_with_state'} execution_context = ExecutionContext(isolate_imports=False) execution_context.add_import_path(self._nonsrc_path(execution_context)) for _type, mod in _types.items(): fully_qualified_name = '%s.%s' % (mod, _type) same_type = execution_context.get_type(fully_qualified_name) lancelot.Spec(same_type.__name__).it().should_be(_type) spec = lancelot.Spec(execution_context) spec.get_type(fully_qualified_name).should_be(SameAs(same_type)) other_context = ExecutionContext(isolate_imports=False) other_context.add_import_path(self._nonsrc_path(other_context)) different_type = other_context.get_type(fully_qualified_name) spec.get_type(fully_qualified_name).should_be( SameAs(different_type)) instances = same_type(), different_type() spec = lancelot.Spec(instances[0]) spec.when(spec.alter_state()) spec.then(spec.get_state()).should_contain(1) spec.then(instances[1].get_state).should_contain(1) execution_context.cleanup_imports() other_context.cleanup_imports()
def cleansup_sys_modules(self): ''' cleanup_imports() should not choke on module that were imported but are no longer in sys.modules ''' execution_context = ExecutionContext() path = self._nonsrc_path(execution_context) execution_context.add_import_path(path) execution_context.get_module('import_me') spec = lancelot.Spec(execution_context) del (sys.modules['import_me']) spec.cleanup_imports().should_not_raise(Exception)
def cleansup_sys_modules(self): ''' cleanup_imports() should not choke on module that were imported but are no longer in sys.modules ''' execution_context = ExecutionContext() path = self._nonsrc_path(execution_context) execution_context.add_import_path(path) execution_context.get_module('import_me') spec = lancelot.Spec(execution_context) del(sys.modules['import_me']) spec.cleanup_imports().should_not_raise(Exception)
def imports_twisted(self): ''' Bug #497245: cannot import twisted ''' from os.path import join, exists for location in sys.path: pkg = join(location, join('twisted', '__init__.py')) if exists(pkg) \ or exists(pkg + 'c') \ or exists(pkg + 'o'): twisted_found = True break lancelot.Spec(twisted_found).it().should_be(True) context = ExecutionContext(isolate_imports=False) #TODO: isolated?! context.add_import_path(self._nonsrc_path(context)) spec = lancelot.Spec(context) spec.get_module('import_twisted').should_be(Type(types.ModuleType)) spec.get_module('twisted').should_be(Type(types.ModuleType)) context.cleanup_imports()