Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.build_scripts.""" |
| 2 | |
| 3 | import os |
| 4 | import unittest |
| 5 | |
| 6 | from distutils.command.build_scripts import build_scripts |
| 7 | from distutils.core import Distribution |
Tarek Ziadé | 68c5095 | 2009-02-13 23:04:17 +0000 | [diff] [blame] | 8 | from distutils import sysconfig |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 9 | |
| 10 | from distutils.tests import support |
Éric Araujo | b344dd0 | 2011-02-02 21:38:37 +0000 | [diff] [blame^] | 11 | from test.support import run_unittest |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 12 | |
| 13 | |
Fred Drake | edcac8f | 2004-08-03 18:53:07 +0000 | [diff] [blame] | 14 | class BuildScriptsTestCase(support.TempdirManager, |
| 15 | support.LoggingSilencer, |
| 16 | unittest.TestCase): |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 17 | |
| 18 | def test_default_settings(self): |
| 19 | cmd = self.get_build_scripts_cmd("/foo/bar", []) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 20 | self.assertTrue(not cmd.force) |
| 21 | self.assertTrue(cmd.build_dir is None) |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 22 | |
| 23 | cmd.finalize_options() |
| 24 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 25 | self.assertTrue(cmd.force) |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 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: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 41 | self.assertTrue(name in built) |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 42 | |
| 43 | def get_build_scripts_cmd(self, target, scripts): |
Martin v. Löwis | 4085f03 | 2004-08-26 05:44:02 +0000 | [diff] [blame] | 44 | import sys |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 45 | dist = Distribution() |
| 46 | dist.scripts = scripts |
| 47 | dist.command_obj["build"] = support.DummyCommand( |
| 48 | build_scripts=target, |
Martin v. Löwis | 4085f03 | 2004-08-26 05:44:02 +0000 | [diff] [blame] | 49 | force=1, |
| 50 | executable=sys.executable |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 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") |
Éric Araujo | c6d7ead | 2010-11-06 02:58:56 +0000 | [diff] [blame] | 75 | try: |
| 76 | f.write(text) |
| 77 | finally: |
| 78 | f.close() |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 79 | |
Tarek Ziadé | 68c5095 | 2009-02-13 23:04:17 +0000 | [diff] [blame] | 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 |
Tarek Ziadé | 939f9c8 | 2009-04-25 12:53:56 +0000 | [diff] [blame] | 96 | old = sysconfig.get_config_vars().get('VERSION') |
Tarek Ziadé | 68c5095 | 2009-02-13 23:04:17 +0000 | [diff] [blame] | 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: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 106 | self.assertTrue(name in built) |
Fred Drake | 2c9f889 | 2004-06-21 16:15:22 +0000 | [diff] [blame] | 107 | |
| 108 | def test_suite(): |
| 109 | return unittest.makeSuite(BuildScriptsTestCase) |
Fred Drake | 2202157 | 2004-06-25 19:04:21 +0000 | [diff] [blame] | 110 | |
| 111 | if __name__ == "__main__": |
Éric Araujo | b344dd0 | 2011-02-02 21:38:37 +0000 | [diff] [blame^] | 112 | run_unittest(test_suite()) |