PageRenderTime 38ms CodeModel.GetById 11ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/Lib/distutils/tests/test_build_scripts.py

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