951b3ea82d5cf6e3755e46b4a794c3195a094a1f
[pyi2ncommon] / test / test_call_helpers.py
1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
3 #
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
6 #
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.
12 #
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.
15 #
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.
18 #
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
20
21 """Unit tests for :py:mod:`pyi2ncommon.call_helpers`."""
22
23 from __future__ import absolute_import
24
25 import unittest
26 import os
27
28 from src import call_helpers
29 from src.type_helpers import is_unicode
30
31
32 class CallHelperTester(unittest.TestCase):
33     """Tester for module :py:mod:`pyi2ncommon.call_helpers`."""
34
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, [])
41
42         # get contents of root dir ourselves
43         true_contents = os.listdir('/') + ['.', '..']
44
45         # compare to out_data
46         self.assertEqual(sorted(out_data), sorted(true_contents))
47
48         # check type
49         self.assertTrue(all(isinstance(entry, str) for entry in out_data),
50                         "Output is not of type str!")
51
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, [])
59
60         # get contents of root dir ourselves
61         true_contents = os.listdir(b'/') + [b'.', b'..']
62
63         # compare to out_data (ignoring order of entries)
64         self.assertEqual(sorted(out_data), sorted(true_contents))
65
66         # check type
67         self.assertFalse(is_unicode(out_data[0]), "Output is unicode!")
68
69     def test_call_and_capture_err(self):
70         """Test call_and_capture with invalid ls command."""
71         cmd = ['ls', '-e']
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         self.assertEqual(err_data[0], "ls: invalid option -- 'e'")
77         self.assertEqual(err_data[1], "Try 'ls --help' for more information.")
78
79
80 if __name__ == '__main__':
81     unittest.main()