summaryrefslogtreecommitdiff
path: root/distutils2/tests/test_run.py
blob: eacec913f5d6163f854fa710e1fd698d23fa9446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Tests for distutils2.run."""

import os
import sys
import textwrap
from StringIO import StringIO

from distutils2 import install
from distutils2.tests import unittest, support
from distutils2.run import main

from distutils2.tests.support import assert_python_ok, assert_python_failure

# setup script that uses __file__
setup_using___file__ = """\
__file__

from distutils2.run import setup
setup()
"""

setup_prints_cwd = """\
import os
print os.getcwd()

from distutils2.run import setup
setup()
"""


class RunTestCase(support.TempdirManager,
                  support.LoggingCatcher,
                  unittest.TestCase):

    maxDiff = None

    # TODO restore the tests removed six months ago and port them to pysetup

    def test_install(self):
        # making sure install returns 0 or 1 exit codes
        project = os.path.join(os.path.dirname(__file__), 'package.tgz')
        install_path = self.mkdtemp()
        old_get_path = install.get_path
        install.get_path = lambda path: install_path
        old_mod = os.stat(install_path).st_mode
        os.chmod(install_path, 0)
        old_stderr = sys.stderr
        sys.stderr = StringIO()
        try:
            self.assertFalse(install.install(project))
            self.assertEqual(main(['install', 'blabla']), 1)
        finally:
            sys.stderr = old_stderr
            os.chmod(install_path, old_mod)
            install.get_path = old_get_path

    def get_pythonpath(self):
        pythonpath = os.environ.get('PYTHONPATH')
        d2parent = os.path.dirname(os.path.dirname(__file__))  # XXX buggy
        if pythonpath is not None:
            pythonpath = os.pathsep.join((pythonpath, d2parent))
        else:
            pythonpath = d2parent
        return pythonpath

    def call_pysetup(self, *args):
        _, out, err = assert_python_ok('-m', 'distutils2.run', *args,
                                       PYTHONPATH=self.get_pythonpath())
        return out, err

    def call_pysetup_fail(self, *args):
        _, out, err = assert_python_failure('-m', 'distutils2.run', *args,
                                            PYTHONPATH=self.get_pythonpath())
        return out, err

    def test_show_help(self):
        # smoke test, just makes sure some help is displayed
        out, err = self.call_pysetup('--help')
        self.assertGreater(out, '')
        self.assertEqual(err, '')

    def test_list_commands(self):
        out, err = self.call_pysetup('run', '--list-commands')
        # check that something is displayed
        self.assertGreater(out, '')
        self.assertEqual(err, '')

        # make sure the manual grouping of commands is respected
        check_position = out.find('  check: ')
        build_position = out.find('  build: ')
        self.assertTrue(check_position, out)  # "out" printed as debugging aid
        self.assertTrue(build_position, out)
        self.assertLess(check_position, build_position, out)

    # TODO test that custom commands don't break --list-commands

    def test_unknown_command_option(self):
        out, err = self.call_pysetup_fail('run', 'build', '--unknown')
        self.assertGreater(out, '')
        # sadly this message comes straight from the getopt module and can't be
        # modified to use repr instead of str for the unknown option; to be
        # changed when the command line parsers are replaced by something clean
        self.assertEqual(err.splitlines(),
                         ['error: option --unknown not recognized'])

    def test_invalid_command(self):
        out, err = self.call_pysetup_fail('run', 'com#mand')
        self.assertGreater(out, 1)
        self.assertEqual(err.splitlines(),
                         ["error: invalid command name 'com#mand'"])

    def test_unknown_command(self):
        out, err = self.call_pysetup_fail('run', 'invalid_command')
        self.assertGreater(out, 1)
        self.assertEqual(err.splitlines(),
                         ["error: command 'invalid_command' not recognized"])

    def test_unknown_action(self):
        out, err = self.call_pysetup_fail('invalid_action')
        self.assertGreater(out, 1)
        self.assertEqual(err.splitlines(),
                         ["error: action 'invalid_action' not recognized"])

    def test_setupcfg_parsing(self):
        # #14733: pysetup used to parse setup.cfg too late
        project_dir = self.mkdtemp()
        os.chdir(project_dir)
        custompy = textwrap.dedent(
            """\
            from distutils2.command.cmd import Command

            class custom(Command):

                user_options = []

                def initialize_options(self):
                    pass

                def finalize_options(self):
                    pass

                def run(self):
                    print 'custom: ok'
            """)
        setupcfg = textwrap.dedent(
            """\
            [global]
            commands = custom.custom
            """)
        self.write_file('custom.py', custompy)
        self.write_file('setup.cfg', setupcfg)

        out, err = self.call_pysetup('run', 'custom')
        self.assertEqual(out.splitlines(), ['custom: ok'])


def test_suite():
    return unittest.makeSuite(RunTestCase)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")