From 2dd3bfed6e3f93dc1fa8706ea70bd8f0d1cda196 Mon Sep 17 00:00:00 2001 From: "stone-24tch3r (aider)" <100294019+stone-w4tch3r@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:30:54 +0500 Subject: [PATCH] test: add tests for cli_wrapper.py in PROJROOT/tests directory --- PROJROOT/tests/test_cli_wrapper.py | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 PROJROOT/tests/test_cli_wrapper.py diff --git a/PROJROOT/tests/test_cli_wrapper.py b/PROJROOT/tests/test_cli_wrapper.py new file mode 100644 index 0000000..29de4da --- /dev/null +++ b/PROJROOT/tests/test_cli_wrapper.py @@ -0,0 +1,40 @@ +import unittest +from unittest.mock import patch +from shared.cli_wrapper import CLIWrapper + +class TestCLIWrapper(unittest.TestCase): + + def test_sanitize_input(self): + test_cases = [ + ("normal input", "normal input"), + ("input with spaces", "'input with spaces'"), + ("input with 'quotes'", "'input with '\"'\"quotes'\"'\"'"), + ("input with $special &chars", "'input with $special &chars'"), + ] + for input_str, expected_output in test_cases: + with self.subTest(input_str=input_str): + self.assertEqual(CLIWrapper.sanitize_input(input_str), expected_output) + + @patch('subprocess.run') + def test_execute_command_success(self, mock_run): + mock_run.return_value.stdout = "Command output" + mock_run.return_value.returncode = 0 + + output, return_code = CLIWrapper.execute_command("test command") + + self.assertEqual(output, "Command output") + self.assertEqual(return_code, 0) + mock_run.assert_called_once_with("test command", shell=True, check=True, text=True, capture_output=True) + + @patch('subprocess.run') + def test_execute_command_failure(self, mock_run): + mock_run.side_effect = subprocess.CalledProcessError(1, "test command", stdout="Error output") + + output, return_code = CLIWrapper.execute_command("test command") + + self.assertEqual(output, "Error output") + self.assertEqual(return_code, 1) + mock_run.assert_called_once_with("test command", shell=True, check=True, text=True, capture_output=True) + +if __name__ == '__main__': + unittest.main()