Ejemplo n.º 1
0
from __future__ import print_function, unicode_literals, absolute_import, division
import pkg_resources

from reahl.stubble import EasterEgg


#first we need some stub classes which in real life would be provided
# by another egg as entry points:
class StubClass1(object):
    pass


class StubClass2(object):
    pass


#then, we initialise the EasterEgg:
stub_egg = EasterEgg()
pkg_resources.working_set.add(stub_egg)

#then, use 1 of two methods to add the stubbed entry point classes:
group_name = 'xxx'
stub_egg.add_entry_point_from_line(group_name,
                                   'test1 = examples.setuptools:StubClass1')
stub_egg.add_entry_point(group_name, 'test2', StubClass2)

#now, the actual code (which is presumably being tested)
#  (we just print out each class it finds...)
for i in pkg_resources.iter_entry_points(group_name):
    print(i.load())
Ejemplo n.º 2
0
class EasterEggTests(object):
    class TestClass1(object):
        pass

    def setUp(self):
        self.group_name = 'abc'
        self.stub_egg = EasterEgg()
        self.saved_working_set = pkg_resources.working_set
        pkg_resources.working_set = pkg_resources.WorkingSet()
        pkg_resources.working_set.add(self.stub_egg)

    def tearDown(self):
        pkg_resources.working_set = self.saved_working_set

    @istest
    def test_adding_entry_points_affect_entry_point_map(self):
        self.stub_egg.add_entry_point_from_line(
            self.group_name,
            'test1 = reahl.stubble_dev.test_easteregg:EasterEggTests.TestClass1'
        )

        self.stub_egg.add_entry_point(self.group_name, 'test2', TestClass2)

        epmap = self.stub_egg.get_entry_map()

        assert list(epmap.keys()) == [self.group_name]
        name_to_entry_point = list(epmap.values())[0]
        assert len(list(name_to_entry_point.keys())) == 2

        assert isinstance(name_to_entry_point['test1'],
                          pkg_resources.EntryPoint)
        assert name_to_entry_point['test1'].load() is EasterEggTests.TestClass1
        assert isinstance(name_to_entry_point['test2'],
                          pkg_resources.EntryPoint)
        assert name_to_entry_point['test2'].load() is TestClass2

        self.stub_egg.clear()
        assert not self.stub_egg.get_entry_map()

    @istest
    def test_resource_api(self):
        test_file = NamedTemporaryFile(mode='wb+')
        dirname, file_name = os.path.split(test_file.name)

        self.stub_egg.location = dirname
        self.stub_egg.activate()

        assert pkg_resources.resource_exists(self.stub_egg.as_requirement(),
                                             file_name)
        assert not pkg_resources.resource_exists(
            self.stub_egg.as_requirement(), 'IDoNotExist')

        contents = b'asdd '
        test_file.write(contents)
        test_file.flush()

        as_string = pkg_resources.resource_string(
            self.stub_egg.as_requirement(), file_name)
        assert as_string == contents

        as_file = pkg_resources.resource_stream(self.stub_egg.as_requirement(),
                                                file_name)
        assert as_file.read() == contents

    @istest
    def test_resource_api_from_module_name(self):
        test_file = NamedTemporaryFile(mode='wb+', suffix='.py')
        dirname, file_name = os.path.split(test_file.name)

        self.stub_egg.location = dirname
        self.stub_egg.activate()

        module_name = file_name.split('.')[0]
        assert pkg_resources.resource_exists(module_name, '')
        assert pkg_resources.resource_filename(module_name, '') == dirname