Guest User

Untitled

a guest
Jan 19th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | None | 0 0
  1. """Tests for distutils.cmd."""
  2. import os
  3.  
  4. from packaging.command.cmd import Command
  5. from packaging.dist import Distribution
  6. from packaging.errors import PackagingOptionError
  7. from packaging.tests import support, unittest
  8.  
  9.  
  10. class MyCmd(Command):
  11.  
  12.     user_options = [
  13.         ('test-option-1=', None,
  14.          "An option for testing defaults"),
  15.         ('test-option-2=', None,
  16.          "A second option for testing defaults"),
  17.     ]
  18.     def initialize_options(self):
  19.         self.test_option_1 = None
  20.         self.test_option_2 = None
  21.  
  22.     def finalize_options(self):
  23.         if self.test_option_1 is None:
  24.             self.test_option_1 = "Kitten"
  25.         if self.test_option_2 is None:
  26.             self.test_option_2 = "Puppy"
  27.  
  28. class CommandTestCase(support.LoggingCatcher,
  29.                       unittest.TestCase):
  30.  
  31.     def setUp(self):
  32.         super(CommandTestCase, self).setUp()
  33.         dist = Distribution()
  34.         self.cmd = MyCmd(dist)
  35.  
  36.     def test_make_file(self):
  37.         cmd = self.cmd
  38.  
  39.         # making sure it raises when infiles is not a string or a list/tuple
  40.         self.assertRaises(TypeError, cmd.make_file,
  41.                           infiles=1, outfile='', func='func', args=())
  42.  
  43.         # making sure execute gets called properly
  44.         def _execute(func, args, exec_msg, level):
  45.             self.assertEqual(exec_msg, 'generating out from in')
  46.         cmd.force = True
  47.         cmd.execute = _execute
  48.         cmd.make_file(infiles='in', outfile='out', func='func', args=())
  49.  
  50.     def test_dump_options(self):
  51.         cmd = self.cmd
  52.         cmd.option1 = 1
  53.         cmd.option2 = 1
  54.         cmd.user_options = [('option1', '', ''), ('option2', '', '')]
  55.         cmd.dump_options()
  56.  
  57.         wanted = ["command options for 'MyCmd':", '  option1 = 1',
  58.                   '  option2 = 1']
  59.         msgs = self.get_logs()
  60.         self.assertEqual(msgs, wanted)
  61.  
  62.     def test_ensure_string(self):
  63.         cmd = self.cmd
  64.         cmd.option1 = 'ok'
  65.         cmd.ensure_string('option1')
  66.  
  67.         cmd.option2 = None
  68.         cmd.ensure_string('option2', 'xxx')
  69.         self.assertTrue(hasattr(cmd, 'option2'))
  70.  
  71.         cmd.option3 = 1
  72.         self.assertRaises(PackagingOptionError, cmd.ensure_string, 'option3')
  73.  
  74.     def test_ensure_string_list(self):
  75.         cmd = self.cmd
  76.         cmd.option1 = 'ok,dok'
  77.         cmd.ensure_string_list('option1')
  78.         self.assertEqual(cmd.option1, ['ok', 'dok'])
  79.  
  80.         cmd.yes_string_list = ['one', 'two', 'three']
  81.         cmd.yes_string_list2 = 'ok'
  82.         cmd.ensure_string_list('yes_string_list')
  83.         cmd.ensure_string_list('yes_string_list2')
  84.         self.assertEqual(cmd.yes_string_list, ['one', 'two', 'three'])
  85.         self.assertEqual(cmd.yes_string_list2, ['ok'])
  86.  
  87.         cmd.not_string_list = ['one', 2, 'three']
  88.         cmd.not_string_list2 = object()
  89.         self.assertRaises(PackagingOptionError,
  90.                           cmd.ensure_string_list, 'not_string_list')
  91.  
  92.         self.assertRaises(PackagingOptionError,
  93.                           cmd.ensure_string_list, 'not_string_list2')
  94.  
  95.     def test_ensure_filename(self):
  96.         cmd = self.cmd
  97.         cmd.option1 = __file__
  98.         cmd.ensure_filename('option1')
  99.         cmd.option2 = 'xxx'
  100.         self.assertRaises(PackagingOptionError, cmd.ensure_filename, 'option2')
  101.  
  102.     def test_ensure_dirname(self):
  103.         cmd = self.cmd
  104.         cmd.option1 = os.path.dirname(__file__) or os.curdir
  105.         cmd.ensure_dirname('option1')
  106.         cmd.option2 = 'xxx'
  107.         self.assertRaises(PackagingOptionError, cmd.ensure_dirname, 'option2')
  108.  
  109.  
  110.     def test_get_reinitialized_command(self):
  111.         cmd = self.cmd
  112.         cmd.test_option_1 = "Horse"
  113.         cmd.test_option_2 = "Goat"
  114.         cmd.ensure_finalized()
  115.         cmd = cmd.get_reinitialized_command(cmd)
  116.         cmd.ensure_finalized()
  117.         self.assertEqual(cmd.test_option_1, "Kitten")
  118.         self.assertEqual(cmd.test_option_2, "Puppy")
  119.  
  120.         dist = Distribution()
  121.         cmd = MyCmd(dist)
  122.         cmd.ensure_finalized()
  123.         cmd = cmd.get_reinitialized_command(cmd, test_option_1="Rabbit")
  124.         cmd.ensure_finalized()
  125.         self.assertEqual(cmd.test_option_1, "Rabbit")
  126.         self.assertEqual(cmd.test_option_2, "Puppy")
  127.  
  128.  
  129.  
  130. def test_suite():
  131.     return unittest.makeSuite(CommandTestCase)
  132.  
  133. if __name__ == '__main__':
  134.     unittest.main(defaultTest='test_suite')
Add Comment
Please, Sign In to add comment