source: tailor/vcpx/tests/shwrap.py @ 1513

Revision 1513, 3.6 KB checked in by lele@…, 5 years ago (diff)

Be a little more generic in the test

Line 
1# -*- mode: python; coding: iso-8859-1 -*-
2# :Progetto: vcpx -- Test shell wrappers
3# :Creato:   mar 20 apr 2004 16:49:23 CEST
4# :Autore:   Lele Gaifax <lele@nautilus.homeip.net>
5# :Licenza:  GNU General Public License
6#
7
8from unittest import TestCase
9from vcpx.shwrap import ExternalCommand, PIPE
10from sys import platform
11from tempfile import gettempdir
12
13
14class SystemCommand(TestCase):
15    """Perform some basic tests of the wrapper"""
16
17    def testExitStatusForTrue(self):
18        """Verify ExternalCommand exit_status of ``true``.
19        """
20
21        if platform != 'win32':
22            c = ExternalCommand(['true'])
23        else:
24            c = ExternalCommand(['cmd','/c exit 0'])
25        c.execute()
26        self.assertEqual(c.exit_status, 0)
27
28    def testExitStatusForFalse(self):
29        """Verify ExternalCommand exit_status of ``false``.
30        """
31
32        if platform != 'win32':
33            c = ExternalCommand(['false'])
34        else:
35            c = ExternalCommand(['cmd','/c exit 1'])
36        c.execute()
37        self.assertNotEqual(c.exit_status, 0)
38
39    def testExitStatusUnknownCommand(self):
40        """Verify ExternalCommand raise OSError for non existing command.
41        """
42
43        c = ExternalCommand(['/does/not/exist'])
44        self.assertRaises(OSError, c.execute)
45
46    def testStandardOutput(self):
47        """Verify that ExternalCommand redirects stdout."""
48
49        if platform != 'win32':
50            c = ExternalCommand(['echo'])
51        else:
52            c = ExternalCommand(['cmd','/c','echo'])
53        out = c.execute("ciao", stdout=PIPE)[0]
54        self.assertEqual(out.read(), "ciao\n")
55
56        if platform != 'win32':
57            out = c.execute('-n', stdout=PIPE)[0]
58            self.assertEqual(out.read(), '')
59
60        out = c.execute("ciao")[0]
61        self.assertEqual(out, None)
62
63    def testStandardError(self):
64        """Verify that ExternalCommand redirects stderr."""
65
66        c = ExternalCommand(['darcs', 'ciao'])
67        out, err = c.execute("ciao", stdout=PIPE, stderr=PIPE)
68        self.assert_("darcs failed" in err.read())
69
70    def testWorkingDir(self):
71        """Verify that the given command is executed in the specified
72        working directory.
73        """
74
75        tempdir = gettempdir()
76        if platform != 'win32':
77            c = ExternalCommand(['pwd'], tempdir)
78        else:
79            c = ExternalCommand(['cmd','/c','cd'], tempdir)
80        out = c.execute(stdout=PIPE)[0]
81        self.assertEqual(out.read(), tempdir+"\n")
82
83    def testStringification(self):
84        """Verify the conversion from sequence of args to string"""
85
86        c = ExternalCommand(['some spaces here'])
87        self.assertEqual(str(c), '$ "some spaces here"')
88
89        c = ExternalCommand(['a "double quoted" arg'])
90        self.assertEqual(str(c), r'$ "a \"double quoted\" arg"')
91
92        c = ExternalCommand([r'a \" backslashed quote mark\\'])
93        self.assertEqual(str(c), r'$ "a \\\" backslashed quote mark\\\\"')
94
95    def testSplittedExecution(self):
96        """Verify the mechanism that avoids too long command lines"""
97
98        args = [str(i) * 20 for i in range(10)]
99        if platform != 'win32':
100            c = ExternalCommand(['echo'])
101        else:
102            c = ExternalCommand(['cmd','/c','echo'])
103        c.MAX_CMDLINE_LENGTH = 30
104        out = c.execute(args, stdout=PIPE)[0]
105        self.assertEqual(out.read(), '\n'.join([args[i]+' '+args[i+1]
106                                                for i in range(0,10,2)])+'\n')
107
108        c = ExternalCommand(['echo'])
109        c.MAX_CMDLINE_LENGTH = None
110        out = c.execute(args, stdout=PIPE)[0]
111        self.assertEqual(out.read(), ' '.join(args)+'\n')
Note: See TracBrowser for help on using the repository browser.