test_install_data.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """Tests for distutils.command.install_data."""
  2. import os
  3. import unittest
  4. from distutils.command.install_data import install_data
  5. from distutils.tests import support
  6. from test.support import run_unittest
  7. class InstallDataTestCase(support.TempdirManager,
  8. support.LoggingSilencer,
  9. support.EnvironGuard,
  10. unittest.TestCase):
  11. def test_simple_run(self):
  12. pkg_dir, dist = self.create_dist()
  13. cmd = install_data(dist)
  14. cmd.install_dir = inst = os.path.join(pkg_dir, 'inst')
  15. # data_files can contain
  16. # - simple files
  17. # - a tuple with a path, and a list of file
  18. one = os.path.join(pkg_dir, 'one')
  19. self.write_file(one, 'xxx')
  20. inst2 = os.path.join(pkg_dir, 'inst2')
  21. two = os.path.join(pkg_dir, 'two')
  22. self.write_file(two, 'xxx')
  23. cmd.data_files = [one, (inst2, [two])]
  24. self.assertEqual(cmd.get_inputs(), [one, (inst2, [two])])
  25. # let's run the command
  26. cmd.ensure_finalized()
  27. cmd.run()
  28. # let's check the result
  29. self.assertEqual(len(cmd.get_outputs()), 2)
  30. rtwo = os.path.split(two)[-1]
  31. self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
  32. rone = os.path.split(one)[-1]
  33. self.assertTrue(os.path.exists(os.path.join(inst, rone)))
  34. cmd.outfiles = []
  35. # let's try with warn_dir one
  36. cmd.warn_dir = 1
  37. cmd.ensure_finalized()
  38. cmd.run()
  39. # let's check the result
  40. self.assertEqual(len(cmd.get_outputs()), 2)
  41. self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
  42. self.assertTrue(os.path.exists(os.path.join(inst, rone)))
  43. cmd.outfiles = []
  44. # now using root and empty dir
  45. cmd.root = os.path.join(pkg_dir, 'root')
  46. inst3 = os.path.join(cmd.install_dir, 'inst3')
  47. inst4 = os.path.join(pkg_dir, 'inst4')
  48. three = os.path.join(cmd.install_dir, 'three')
  49. self.write_file(three, 'xx')
  50. cmd.data_files = [one, (inst2, [two]),
  51. ('inst3', [three]),
  52. (inst4, [])]
  53. cmd.ensure_finalized()
  54. cmd.run()
  55. # let's check the result
  56. self.assertEqual(len(cmd.get_outputs()), 4)
  57. self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
  58. self.assertTrue(os.path.exists(os.path.join(inst, rone)))
  59. def test_suite():
  60. return unittest.makeSuite(InstallDataTestCase)
  61. if __name__ == "__main__":
  62. run_unittest(test_suite())