1# (C) Copyright 2007-2019 Enthought, Inc., Austin, TX 2# All rights reserved. 3# 4# This software is provided without warranty under the terms of the BSD 5# license included in LICENSE.txt and may be redistributed only 6# under the conditions described in the aforementioned license. The license 7# is also available online at http://www.enthought.com/licenses/BSD.txt 8# Thanks for using Enthought open source! 9""" Base class for Egg-based test cases. """ 10 11 12import pkg_resources 13from os.path import dirname, join 14 15from traits.testing.unittest_tools import unittest 16 17 18class EggBasedTestCase(unittest.TestCase): 19 """ Base class for Egg-based test cases. """ 20 21 def setUp(self): 22 """ Prepares the test fixture before each test method is called. """ 23 24 # The location of the 'eggs' directory. 25 self.egg_dir = join(dirname(__file__), 'eggs') 26 27 def _add_egg(self, filename, working_set=None): 28 """ Create and add a distribution from the specified '.egg'. """ 29 30 if working_set is None: 31 working_set = pkg_resources.working_set 32 33 # The eggs must be in our egg directory! 34 filename = join(dirname(__file__), 'eggs', filename) 35 36 # Create a distribution for the egg. 37 distributions = pkg_resources.find_distributions(filename) 38 39 # Add the distributions to the working set (this makes any Python 40 # modules in the eggs available for importing). 41 for distribution in distributions: 42 working_set.add(distribution) 43 44 def _add_eggs_on_path(self, path, working_set=None): 45 """ Add all eggs found on the path to a working set. """ 46 47 if working_set is None: 48 working_set = pkg_resources.working_set 49 50 environment = pkg_resources.Environment(path) 51 52 # 'find_plugins' identifies those distributions that *could* be added 53 # to the working set without version conflicts or missing requirements. 54 distributions, errors = working_set.find_plugins(environment) 55 # Py2 tests was checking that len(errors) > 0. This did not work on 56 # Py3. Test changed to check the len(distributions) 57 if len(distributions) == 0: 58 raise SystemError('Cannot find eggs %s' % errors) 59 60 # Add the distributions to the working set (this makes any Python 61 # modules in the eggs available for importing). 62 for distribution in distributions: 63 working_set.add(distribution) 64