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