1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
7 # As a special exception, if other files instantiate templates or use macros
8 # or inline functions from this file, or you compile this file and link it
9 # with other works to produce a work based on this file, this file
10 # does not by itself cause the resulting work to be covered
11 # by the GNU General Public License.
13 # However the source code for this file must still be made available
14 # in accordance with section (3) of the GNU General Public License.
16 # This exception does not invalidate any other reasons why a work based
17 # on this file might be covered by the GNU General Public License.
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
21 """Unit tests for :py:mod:`pyi2ncommon.call_helpers`."""
23 from __future__ import absolute_import
28 from src import call_helpers
29 from src.type_helpers import is_unicode
32 class CallHelperTester(unittest.TestCase):
33 """Tester for module :py:mod:`pyi2ncommon.call_helpers`."""
35 def test_call_and_capture(self):
36 """Test call_and_capture with command ls -a /."""
37 cmd = ['ls', '-a', '/']
38 return_code, out_data, err_data = call_helpers.call_and_capture(cmd)
39 self.assertEqual(return_code, 0)
40 self.assertEqual(err_data, [])
42 # get contents of root dir ourselves
43 true_contents = os.listdir('/') + ['.', '..']
46 self.assertEqual(sorted(out_data), sorted(true_contents))
49 self.assertTrue(all(isinstance(entry, str) for entry in out_data),
50 "Output is not of type str!")
52 def test_call_and_capture_bytes(self):
53 """Test call_and_capture with command ls -a /."""
54 cmd = ['ls', '-a', '/']
55 return_code, out_data, err_data = \
56 call_helpers.call_and_capture(cmd, universal_newlines=False)
57 self.assertEqual(return_code, 0)
58 self.assertEqual(err_data, [])
60 # get contents of root dir ourselves
61 true_contents = os.listdir(b'/') + [b'.', b'..']
63 # compare to out_data (ignoring order of entries)
64 self.assertEqual(sorted(out_data), sorted(true_contents))
67 self.assertFalse(is_unicode(out_data[0]), "Output is unicode!")
69 def test_call_and_capture_err(self):
70 """Test call_and_capture with invalid ls command."""
72 return_code, out_data, err_data = call_helpers.call_and_capture(cmd)
73 self.assertEqual(return_code, 2)
74 self.assertEqual(out_data, [])
75 self.assertEqual(len(err_data), 2)
76 # "ls: invalid option -- 'e'" in some form and language
77 message = err_data[0].strip() + ' ' # in case the 'e' is at the end
78 self.assertTrue(message.startswith('ls: '))
79 self.assertIn('option', message.lower())
80 self.assertIn('--', message)
81 self.assertTrue(' e ' in message or "'e'" in message,
82 msg="Output did not mention option 'e': " + message)
83 message = err_data[1].strip()
84 self.assertIn('ls --help', message)
85 self.assertIn('information', message.lower())
88 if __name__ == '__main__':