blob: 6c7eb20c47177175662c28fa3053465070e5d9d6 [file] [log] [blame]
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001"""Tests for distutils.spawn."""
2import unittest
Tarek Ziadé861d6442009-06-02 16:18:55 +00003import os
4import time
Éric Araujob344dd02011-02-02 21:38:37 +00005from test.support import captured_stdout, run_unittest
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00006
Tarek Ziadé861d6442009-06-02 16:18:55 +00007from 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):
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +000015
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)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000023 self.assertEqual(res, wanted)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +000024
Tarek Ziadé861d6442009-06-02 16:18:55 +000025
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 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 os.name == 'posix':
45 exe = os.path.join(tmpdir, 'foo.sh')
46 self.write_file(exe, '#!/bin/sh\nexit 0')
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
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +000054def test_suite():
55 return unittest.makeSuite(SpawnTestCase)
56
57if __name__ == "__main__":
Éric Araujob344dd02011-02-02 21:38:37 +000058 run_unittest(test_suite())