1"""Tests for distutils.spawn."""
2import unittest
3import os
4import time
5from test.test_support import captured_stdout, run_unittest
6
7from distutils.spawn import _nt_quote_args
8from distutils.spawn import spawn, find_executable
9from distutils.errors import DistutilsExecError
10from distutils.tests import support
11
12class SpawnTestCase(support.TempdirManager,
13                    support.LoggingSilencer,
14                    unittest.TestCase):
15
16    def test_nt_quote_args(self):
17
18        for (args, wanted) in ((['with space', 'nospace'],
19                                ['"with space"', 'nospace']),
20                               (['nochange', 'nospace'],
21                                ['nochange', 'nospace'])):
22            res = _nt_quote_args(args)
23            self.assertEqual(res, wanted)
24
25
26    @unittest.skipUnless(os.name in ('nt', 'posix'),
27                         'Runs only under posix or nt')
28    def test_spawn(self):
29        tmpdir = self.mkdtemp()
30
31        # creating something executable
32        # through the shell that returns 1
33        if os.name == 'posix':
34            exe = os.path.join(tmpdir, 'foo.sh')
35            self.write_file(exe, '#!/bin/sh\nexit 1')
36            os.chmod(exe, 0777)
37        else:
38            exe = os.path.join(tmpdir, 'foo.bat')
39            self.write_file(exe, 'exit 1')
40
41        os.chmod(exe, 0777)
42        self.assertRaises(DistutilsExecError, spawn, [exe])
43
44        # now something that works
45        if os.name == 'posix':
46            exe = os.path.join(tmpdir, 'foo.sh')
47            self.write_file(exe, '#!/bin/sh\nexit 0')
48            os.chmod(exe, 0777)
49        else:
50            exe = os.path.join(tmpdir, 'foo.bat')
51            self.write_file(exe, 'exit 0')
52
53        os.chmod(exe, 0777)
54        spawn([exe])  # should work without any error
55
56def test_suite():
57    return unittest.makeSuite(SpawnTestCase)
58
59if __name__ == "__main__":
60    run_unittest(test_suite())
61