# The software in this package is distributed under the GNU General # Public License version 2 (with a special exception described below). # # A copy of GNU General Public License (GPL) is included in this distribution, # in the file COPYING.GPL. # # As a special exception, if other files instantiate templates or use macros # or inline functions from this file, or you compile this file and link it # with other works to produce a work based on this file, this file # does not by itself cause the resulting work to be covered # by the GNU General Public License. # # However the source code for this file must still be made available # in accordance with section (3) of the GNU General Public License. # # This exception does not invalidate any other reasons why a work based # on this file might be covered by the GNU General Public License. # # Copyright (c) 2016-2018 Intra2net AG """Unit tests for :py:mod:`pyi2ncommon.call_helpers`.""" import unittest import os from src import call_helpers from src.type_helpers import is_unicode class CallHelperTester(unittest.TestCase): """Tester for module :py:mod:`pyi2ncommon.call_helpers`.""" def test_call_and_capture(self): """Test call_and_capture with command ls -a /.""" cmd = ['ls', '-a', '/'] return_code, out_data, err_data = call_helpers.call_and_capture(cmd) self.assertEqual(return_code, 0) self.assertEqual(err_data, []) # get contents of root dir ourselves true_contents = os.listdir('/') + ['.', '..'] # compare to out_data self.assertEqual(sorted(out_data), sorted(true_contents)) # check type self.assertTrue(all(isinstance(entry, str) for entry in out_data), "Output is not of type str!") def test_call_and_capture_bytes(self): """Test call_and_capture with command ls -a /.""" cmd = ['ls', '-a', '/'] return_code, out_data, err_data = \ call_helpers.call_and_capture(cmd, universal_newlines=False) self.assertEqual(return_code, 0) self.assertEqual(err_data, []) # get contents of root dir ourselves true_contents = os.listdir(b'/') + [b'.', b'..'] # compare to out_data (ignoring order of entries) self.assertEqual(sorted(out_data), sorted(true_contents)) # check type self.assertFalse(is_unicode(out_data[0]), "Output is unicode!") def test_call_and_capture_err(self): """Test call_and_capture with invalid ls command.""" cmd = ['ls', '-e'] return_code, out_data, err_data = call_helpers.call_and_capture(cmd) self.assertEqual(return_code, 2) self.assertEqual(out_data, []) self.assertEqual(len(err_data), 2) # "ls: invalid option -- 'e'" in some form and language message = err_data[0].strip() + ' ' # in case the 'e' is at the end self.assertTrue(message.startswith('ls: ')) self.assertIn('option', message.lower()) self.assertIn('--', message) self.assertTrue(' e ' in message or "'e'" in message, msg="Output did not mention option 'e': " + message) message = err_data[1].strip() self.assertIn('ls --help', message) self.assertIn('information', message.lower()) if __name__ == '__main__': unittest.main()