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