1"""Tests for distutils.spawn."""
2import unittest
3import sys
4import os
5from test.support import run_unittest, unix_shell
6
7from distutils.spawn import _nt_quote_args
8from distutils.spawn import spawn
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 sys.platform != 'win32':
34            exe = os.path.join(tmpdir, 'foo.sh')
35            self.write_file(exe, '#!%s\nexit 1' % unix_shell)
36        else:
37            exe = os.path.join(tmpdir, 'foo.bat')
38            self.write_file(exe, 'exit 1')
39
40        os.chmod(exe, 0o777)
41        self.assertRaises(DistutilsExecError, spawn, [exe])
42
43        # now something that works
44        if sys.platform != 'win32':
45            exe = os.path.join(tmpdir, 'foo.sh')
46            self.write_file(exe, '#!%s\nexit 0' % unix_shell)
47        else:
48            exe = os.path.join(tmpdir, 'foo.bat')
49            self.write_file(exe, 'exit 0')
50
51        os.chmod(exe, 0o777)
52        spawn([exe])  # should work without any error
53
54def test_suite():
55    return unittest.makeSuite(SpawnTestCase)
56
57if __name__ == "__main__":
58    run_unittest(test_suite())
59