| 1 | """Tests for distutils.dist.""" |
| 2 | |
| 3 | import distutils.cmd |
| 4 | import distutils.dist |
| 5 | import os |
| 6 | import shutil |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
| 11 | from test.test_support import TESTFN |
| 12 | |
| 13 | |
| 14 | class test_dist(distutils.cmd.Command): |
| 15 | """Sample distutils extension command.""" |
| 16 | |
| 17 | user_options = [ |
| 18 | ("sample-option=", "S", "help text"), |
| 19 | ] |
| 20 | |
| 21 | def initialize_options(self): |
| 22 | self.sample_option = None |
| 23 | |
| 24 | |
| 25 | class TestDistribution(distutils.dist.Distribution): |
| 26 | """Distribution subclasses that avoids the default search for |
| 27 | configuration files. |
| 28 | |
| 29 | The ._config_files attribute must be set before |
| 30 | .parse_config_files() is called. |
| 31 | """ |
| 32 | |
| 33 | def find_config_files(self): |
| 34 | return self._config_files |
| 35 | |
| 36 | |
| 37 | class DistributionTestCase(unittest.TestCase): |
| 38 | |
| 39 | def setUp(self): |
| 40 | self.argv = sys.argv[:] |
| 41 | del sys.argv[1:] |
| 42 | |
| 43 | def tearDown(self): |
| 44 | sys.argv[:] = self.argv |
| 45 | |
| 46 | def create_distribution(self, configfiles=()): |
| 47 | d = TestDistribution() |
| 48 | d._config_files = configfiles |
| 49 | d.parse_config_files() |
| 50 | d.parse_command_line() |
| 51 | return d |
| 52 | |
| 53 | def test_command_packages_unspecified(self): |
| 54 | sys.argv.append("build") |
| 55 | d = self.create_distribution() |
| 56 | self.assertEqual(d.get_command_packages(), ["distutils.command"]) |
| 57 | |
| 58 | def test_command_packages_cmdline(self): |
| 59 | sys.argv.extend(["--command-packages", |
| 60 | "foo.bar,distutils.tests", |
| 61 | "test_dist", |
| 62 | "-Ssometext", |
| 63 | ]) |
| 64 | d = self.create_distribution() |
| 65 | # let's actually try to load our test command: |
| 66 | self.assertEqual(d.get_command_packages(), |
| 67 | ["distutils.command", "foo.bar", "distutils.tests"]) |
| 68 | cmd = d.get_command_obj("test_dist") |
| 69 | self.assert_(isinstance(cmd, test_dist)) |
| 70 | self.assertEqual(cmd.sample_option, "sometext") |
| 71 | |
| 72 | def test_command_packages_configfile(self): |
| 73 | sys.argv.append("build") |
| 74 | f = open(TESTFN, "w") |
| 75 | try: |
| 76 | print >>f, "[global]" |
| 77 | print >>f, "command_packages = foo.bar, splat" |
| 78 | f.close() |
| 79 | d = self.create_distribution([TESTFN]) |
| 80 | self.assertEqual(d.get_command_packages(), |
| 81 | ["distutils.command", "foo.bar", "splat"]) |
| 82 | |
| 83 | # ensure command line overrides config: |
| 84 | sys.argv[1:] = ["--command-packages", "spork", "build"] |
| 85 | d = self.create_distribution([TESTFN]) |
| 86 | self.assertEqual(d.get_command_packages(), |
| 87 | ["distutils.command", "spork"]) |
| 88 | |
| 89 | # Setting --command-packages to '' should cause the default to |
| 90 | # be used even if a config file specified something else: |
| 91 | sys.argv[1:] = ["--command-packages", "", "build"] |
| 92 | d = self.create_distribution([TESTFN]) |
| 93 | self.assertEqual(d.get_command_packages(), ["distutils.command"]) |
| 94 | |
| 95 | finally: |
| 96 | os.unlink(TESTFN) |
| 97 | |
| 98 | |
| 99 | def test_suite(): |
| 100 | return unittest.makeSuite(DistributionTestCase) |