Commit | Line | Data |
---|---|---|
dae4fa8a CH |
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 | ||
196ee5cd | 21 | """Unit tests for :py:mod:`pyi2ncommon.call_helpers`.""" |
dae4fa8a CH |
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): | |
196ee5cd | 33 | """Tester for module :py:mod:`pyi2ncommon.call_helpers`.""" |
dae4fa8a CH |
34 | |
35 | def test_call_and_capture(self): | |
196ee5cd | 36 | """Test call_and_capture with command ls -a /.""" |
dae4fa8a CH |
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): | |
196ee5cd | 53 | """Test call_and_capture with command ls -a /.""" |
dae4fa8a CH |
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): | |
196ee5cd | 70 | """Test call_and_capture with invalid ls command.""" |
dae4fa8a CH |
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) | |
99d8e26b CH |
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()) | |
dae4fa8a CH |
86 | |
87 | ||
88 | if __name__ == '__main__': | |
89 | unittest.main() |