2014-02-28 10:19:02 -07:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import unittest
|
|
|
|
|
2014-05-02 15:06:54 -06:00
|
|
|
from collector import modules
|
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
|
|
|
|
log = logging.getLogger('datadog.test')
|
|
|
|
|
|
|
|
default_target = 'DEFAULT'
|
|
|
|
specified_target = 'SPECIFIED'
|
|
|
|
has_been_mutated = False
|
|
|
|
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
class TestModuleLoad(unittest.TestCase):
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def setUp(self):
|
|
|
|
sys.modules[__name__].has_been_mutated = True
|
|
|
|
if 'tests.target_module' in sys.modules:
|
|
|
|
del sys.modules['tests.target_module']
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def tearDown(self):
|
|
|
|
sys.modules[__name__].has_been_mutated = False
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def test_cached_module(self):
|
|
|
|
"""Modules already in the cache should be reused"""
|
|
|
|
self.assertTrue(modules.load('%s:has_been_mutated' % __name__))
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def test_cache_population(self):
|
|
|
|
"""Python module cache should be populated"""
|
2014-07-02 11:33:11 -07:00
|
|
|
self.assertTrue('tests.target_module' not in sys.modules)
|
2014-02-28 10:19:02 -07:00
|
|
|
modules.load('tests.target_module')
|
|
|
|
self.assertTrue('tests.target_module' in sys.modules)
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def test_modname_load_default(self):
|
|
|
|
"""When the specifier contains no module name, any provided default
|
|
|
|
should be used"""
|
2014-08-05 20:48:55 +02:00
|
|
|
self.assertEqual(
|
2014-02-28 10:19:02 -07:00
|
|
|
modules.load(
|
|
|
|
'tests.target_module',
|
|
|
|
'default_target'),
|
|
|
|
'DEFAULT'
|
|
|
|
)
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def test_modname_load_specified(self):
|
|
|
|
"""When the specifier contains a module name, any provided default
|
|
|
|
should be overridden"""
|
2014-08-05 20:48:55 +02:00
|
|
|
self.assertEqual(
|
2014-02-28 10:19:02 -07:00
|
|
|
modules.load(
|
|
|
|
'tests.target_module:specified_target',
|
|
|
|
'default_target'),
|
|
|
|
'SPECIFIED'
|
|
|
|
)
|
2014-07-01 14:27:12 -07:00
|
|
|
|
2014-02-28 10:19:02 -07:00
|
|
|
def test_pathname_load_finds_package(self):
|
|
|
|
""""Loading modules by absolute path should correctly set the name of
|
|
|
|
the loaded module to include any package containing it."""
|
|
|
|
m = modules.load(os.getcwd() + '/tests/target_module.py')
|
2014-08-05 20:48:55 +02:00
|
|
|
self.assertEqual(m.__name__, 'tests.target_module')
|