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