1"""Tests for distutils.command.build_scripts."""
2
3import os
4import unittest
5
6from distutils.command.build_scripts import build_scripts
7from distutils.core import Distribution
8import sysconfig
9
10from distutils.tests import support
11from test.test_support import run_unittest
12
13
14class BuildScriptsTestCase(support.TempdirManager,
15                           support.LoggingSilencer,
16                           unittest.TestCase):
17
18    def test_default_settings(self):
19        cmd = self.get_build_scripts_cmd("/foo/bar", [])
20        self.assertTrue(not cmd.force)
21        self.assertTrue(cmd.build_dir is None)
22
23        cmd.finalize_options()
24
25        self.assertTrue(cmd.force)
26        self.assertEqual(cmd.build_dir, "/foo/bar")
27
28    def test_build(self):
29        source = self.mkdtemp()
30        target = self.mkdtemp()
31        expected = self.write_sample_scripts(source)
32
33        cmd = self.get_build_scripts_cmd(target,
34                                         [os.path.join(source, fn)
35                                          for fn in expected])
36        cmd.finalize_options()
37        cmd.run()
38
39        built = os.listdir(target)
40        for name in expected:
41            self.assertTrue(name in built)
42
43    def get_build_scripts_cmd(self, target, scripts):
44        import sys
45        dist = Distribution()
46        dist.scripts = scripts
47        dist.command_obj["build"] = support.DummyCommand(
48            build_scripts=target,
49            force=1,
50            executable=sys.executable
51            )
52        return build_scripts(dist)
53
54    def write_sample_scripts(self, dir):
55        expected = []
56        expected.append("script1.py")
57        self.write_script(dir, "script1.py",
58                          ("#! /usr/bin/env python2.3\n"
59                           "# bogus script w/ Python sh-bang\n"
60                           "pass\n"))
61        expected.append("script2.py")
62        self.write_script(dir, "script2.py",
63                          ("#!/usr/bin/python\n"
64                           "# bogus script w/ Python sh-bang\n"
65                           "pass\n"))
66        expected.append("shell.sh")
67        self.write_script(dir, "shell.sh",
68                          ("#!/bin/sh\n"
69                           "# bogus shell script w/ sh-bang\n"
70                           "exit 0\n"))
71        return expected
72
73    def write_script(self, dir, name, text):
74        f = open(os.path.join(dir, name), "w")
75        try:
76            f.write(text)
77        finally:
78            f.close()
79
80    def test_version_int(self):
81        source = self.mkdtemp()
82        target = self.mkdtemp()
83        expected = self.write_sample_scripts(source)
84
85
86        cmd = self.get_build_scripts_cmd(target,
87                                         [os.path.join(source, fn)
88                                          for fn in expected])
89        cmd.finalize_options()
90
91        # http://bugs.python.org/issue4524
92        #
93        # On linux-g++-32 with command line `./configure --enable-ipv6
94        # --with-suffix=3`, python is compiled okay but the build scripts
95        # failed when writing the name of the executable
96        old = sysconfig.get_config_vars().get('VERSION')
97        sysconfig._CONFIG_VARS['VERSION'] = 4
98        try:
99            cmd.run()
100        finally:
101            if old is not None:
102                sysconfig._CONFIG_VARS['VERSION'] = old
103
104        built = os.listdir(target)
105        for name in expected:
106            self.assertTrue(name in built)
107
108def test_suite():
109    return unittest.makeSuite(BuildScriptsTestCase)
110
111if __name__ == "__main__":
112    run_unittest(test_suite())
113