Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 1 | """Tests for distutils.spawn.""" |
| 2 | import unittest |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 3 | import os |
| 4 | import time |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 5 | from test.support import captured_stdout, run_unittest |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 6 | |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 7 | from distutils.spawn import _nt_quote_args |
| 8 | from distutils.spawn import spawn, find_executable |
| 9 | from distutils.errors import DistutilsExecError |
| 10 | from distutils.tests import support |
| 11 | |
| 12 | class SpawnTestCase(support.TempdirManager, |
| 13 | support.LoggingSilencer, |
| 14 | unittest.TestCase): |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 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.assertEquals(res, wanted) |
| 24 | |
Tarek Ziadé | 861d644 | 2009-06-02 16:18:55 +0000 | [diff] [blame] | 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 | 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 Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 54 | def test_suite(): |
| 55 | return unittest.makeSuite(SpawnTestCase) |
| 56 | |
| 57 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 58 | run_unittest(test_suite()) |