diff -r 3328e388cb28 Lib/packaging/tests/test_support.py --- a/Lib/packaging/tests/test_support.py Tue Feb 28 08:06:01 2012 +0000 +++ b/Lib/packaging/tests/test_support.py Wed Feb 29 21:34:40 2012 +0100 @@ -1,23 +1,23 @@ import os +import logging import tempfile +from packaging import logger from packaging.dist import Distribution from packaging.tests import support, unittest -class TestingSupportTestCase(unittest.TestCase): - - def test_fake_dec(self): - @support.fake_dec(1, 2, k=3) - def func(arg0, *args, **kargs): - return arg0, args, kargs - self.assertEqual(func(-1, -2, k=-3), (-1, (-2,), {'k': -3})) +class TestingSupportTempdirManager(unittest.TestCase): def test_TempdirManager(self): files = {} class Tester(support.TempdirManager, unittest.TestCase): + def runTest(self2): + # This method is needed for back portability + pass + def test_mktempfile(self2): tmpfile = self2.mktempfile() files['test_mktempfile'] = tmpfile.name @@ -60,6 +60,7 @@ for name in ('test_mktempfile', 'test_mkdtemp', 'test_write_file', 'test_create_dist', 'test_assertIsFile', 'test_assertIsNotFile'): + tester.setUp() try: getattr(tester, name)() @@ -71,8 +72,120 @@ self.assertFalse(os.path.exists(files[name])) -def test_suite(): - return unittest.makeSuite(TestingSupportTestCase) +class EnvironRestorerTestCase(unittest.TestCase): -if __name__ == "__main__": - unittest.main(defaultTest="test_suite") + def setUp(self): + self.saved_env = os.environ + os.environ['EnvironRestorerKey'] = 'setUpValue' + + def tearDown(self): + del os.environ['EnvironRestorerKey'] + os.environ = self.saved_env + + def test_EnvironRestorer(self): + + class Tester(support.EnvironRestorer, unittest.TestCase): + + # Keys to restore + restore_environ = ['NO_KEY_42', 'EnvironRestorerKey'] + + # Manipulate the keys here + def runTest(self2): + os.environ['NO_KEY_42'] = '42' + os.environ['EnvironRestorerKey'] = 'test_EnvironRestorer' + + # run tests + tester = Tester() + for name in ('runTest',): + tester.setUp() + try: + getattr(tester, name)() + finally: + tester.tearDown() + + # check the environment for changes + self.assertNotIn('NO_KEY_42', os.environ) + self.assertEqual(os.environ['EnvironRestorerKey'], 'setUpValue') + + +class LoggingCatcherTestCase(unittest.TestCase): + + def test_LoggingCatcher(self): + + class Tester(support.LoggingCatcher, unittest.TestCase): + + def runTest(self2): + self.assertFalse(self2.loghandler.shouldFlush()) + + # Log sentinel messages + logger.debug('debug_msg') + logger.info('info_msg') + logger.warning('warn_msg') + logger.error('error_msg') + logger.critical('critical_msg') + + # the default level is warning + self.assertEqual(self2.get_logs(flush=False), ['warn_msg']) + + # the other levels + self.assertEqual( + self2.get_logs(level=logging.DEBUG, flush=False), + ['debug_msg']) + + self.assertEqual( + self2.get_logs(level=logging.INFO, flush=False), + ['info_msg']) + + self.assertEqual( + self2.get_logs(level=logging.ERROR, flush=False), + ['error_msg']) + + self.assertEqual( + self2.get_logs(level=logging.CRITICAL, flush=False), + ['critical_msg']) + + # check flush + self2.loghandler.flush() + logger.warning('warn_message_2') + logger.info('info_message_2') + self.assertEqual(self2.get_logs(), ['warn_message_2']) + self.assertEqual(self2.get_logs(level=logging.INFO), []) + + # run tests + tester = Tester() + for name in ('runTest',): + tester.setUp() + try: + getattr(tester, name)() + finally: + tester.tearDown() + + +class TestingSupportOthers(unittest.TestCase): + + def test_fake_dec(self): + @support.fake_dec(1, 2, k=3) + def func(arg0, *args, **kargs): + return arg0, args, kargs + self.assertEqual(func(-1, -2, k= -3), (-1, (-2,), {'k':-3})) + + def test_DummyCommand(self): + cmd = support.DummyCommand(attrFoo='Foo', attr42=42) + cmd.ensure_finalized() + self.assertEqual(cmd.attrFoo, 'Foo') + self.assertEqual(cmd.attr42, 42) + + def test_TestDistribution(self): + distro = support.TestDistribution() + distro._config_files = ('config1',) + self.assertEqual(distro.find_config_files(), ('config1',)) + + def test_Inputs(self): + inputs = [0, 1, 2] + faker = support.Inputs(*inputs) + answers = [faker() for _ in range(3)] + self.assertEqual(answers, inputs) + + +if __name__ == '__main__': + unittest.main()