|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | +import platform |
| 4 | + |
| 5 | +from jentic_openapi_common.subprocess import ( |
| 6 | + run_checked, |
| 7 | + SubprocessExecutionResult, |
| 8 | + SubprocessExecutionError, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestSubprocessModule(unittest.TestCase): |
| 13 | + """Test cases for the custom subprocess module.""" |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + """Set up test fixtures.""" |
| 17 | + self.is_windows = platform.system() == "Windows" |
| 18 | + self.is_unix = platform.system() in ("Linux", "Darwin") |
| 19 | + |
| 20 | + @unittest.skipUnless( |
| 21 | + platform.system() in ("Linux", "Darwin", "Windows"), "Requires a supported operating system" |
| 22 | + ) |
| 23 | + def test_successful_command(self): |
| 24 | + """Test running a successful command.""" |
| 25 | + if self.is_windows: |
| 26 | + cmd = ["cmd", "/c", "echo", "hello"] |
| 27 | + else: |
| 28 | + cmd = ["echo", "hello"] |
| 29 | + |
| 30 | + result = run_checked(cmd) |
| 31 | + |
| 32 | + self.assertIsInstance(result, SubprocessExecutionResult) |
| 33 | + self.assertEqual(result.returncode, 0) |
| 34 | + self.assertIn("hello", result.stdout) |
| 35 | + self.assertEqual(result.stderr, "") |
| 36 | + |
| 37 | + @unittest.skipUnless(platform.system() in ("Linux", "Darwin"), "Requires Unix-like system") |
| 38 | + def test_unix_ls_command(self): |
| 39 | + """Test ls command on Unix systems.""" |
| 40 | + result = run_checked(["ls", "/"]) |
| 41 | + |
| 42 | + self.assertEqual(result.returncode, 0) |
| 43 | + self.assertIsInstance(result.stdout, str) |
| 44 | + self.assertTrue(len(result.stdout) > 0) |
| 45 | + |
| 46 | + @unittest.skipUnless(platform.system() == "Windows", "Requires Windows system") |
| 47 | + def test_windows_dir_command(self): |
| 48 | + """Test dir command on Windows systems.""" |
| 49 | + result = run_checked(["cmd", "/c", "dir", "C:\\"]) |
| 50 | + |
| 51 | + self.assertEqual(result.returncode, 0) |
| 52 | + self.assertIsInstance(result.stdout, str) |
| 53 | + self.assertTrue(len(result.stdout) > 0) |
| 54 | + |
| 55 | + @unittest.skipUnless( |
| 56 | + platform.system() in ("Linux", "Darwin", "Windows"), "Requires a supported operating system" |
| 57 | + ) |
| 58 | + def test_python_version_command(self): |
| 59 | + """Test running python --version command.""" |
| 60 | + # Use sys.executable to ensure we're using the right Python |
| 61 | + result = run_checked([sys.executable, "--version"]) |
| 62 | + |
| 63 | + self.assertEqual(result.returncode, 0) |
| 64 | + self.assertIn("Python", result.stdout) |
| 65 | + |
| 66 | + def test_nonexistent_command(self): |
| 67 | + """Test running a command that doesn't exist.""" |
| 68 | + with self.assertRaises(SubprocessExecutionError) as cm: |
| 69 | + run_checked(["nonexistent_command_12345"]) |
| 70 | + |
| 71 | + self.assertEqual(cm.exception.returncode, -1) |
| 72 | + self.assertEqual(cm.exception.cmd, ["nonexistent_command_12345"]) |
| 73 | + |
| 74 | + @unittest.skipUnless( |
| 75 | + platform.system() in ("Linux", "Darwin", "Windows"), "Requires a supported operating system" |
| 76 | + ) |
| 77 | + def test_command_with_non_zero_exit_code(self): |
| 78 | + """Test command that exits with non-zero code.""" |
| 79 | + if self.is_windows: |
| 80 | + cmd = ["cmd", "/c", "exit", "1"] |
| 81 | + else: |
| 82 | + cmd = ["sh", "-c", "exit 1"] |
| 83 | + |
| 84 | + # Without fail_on_error, should not raise exception |
| 85 | + result = run_checked(cmd) |
| 86 | + self.assertEqual(result.returncode, 1) |
| 87 | + |
| 88 | + # With fail_on_error=True, should raise exception |
| 89 | + with self.assertRaises(SubprocessExecutionError) as cm: |
| 90 | + run_checked(cmd, fail_on_error=True) |
| 91 | + |
| 92 | + self.assertEqual(cm.exception.returncode, 1) |
| 93 | + self.assertEqual(cm.exception.cmd, cmd) |
| 94 | + |
| 95 | + @unittest.skipUnless( |
| 96 | + platform.system() in ("Linux", "Darwin"), "Requires Unix-like system for stderr test" |
| 97 | + ) |
| 98 | + def test_command_with_stderr(self): |
| 99 | + """Test command that produces stderr output.""" |
| 100 | + # Use a command that writes to stderr |
| 101 | + result = run_checked(["sh", "-c", "echo 'error message' >&2"]) |
| 102 | + |
| 103 | + self.assertEqual(result.returncode, 0) |
| 104 | + self.assertEqual(result.stdout, "") |
| 105 | + self.assertIn("error message", result.stderr) |
| 106 | + |
| 107 | + def test_encoding_parameter(self): |
| 108 | + """Test custom encoding parameter.""" |
| 109 | + if self.is_windows: |
| 110 | + cmd = ["cmd", "/c", "echo", "hello"] |
| 111 | + else: |
| 112 | + cmd = ["echo", "hello"] |
| 113 | + |
| 114 | + result = run_checked(cmd, encoding="utf-8") |
| 115 | + |
| 116 | + self.assertEqual(result.returncode, 0) |
| 117 | + self.assertIn("hello", result.stdout) |
| 118 | + self.assertIsInstance(result.stdout, str) |
| 119 | + |
| 120 | + def test_subprocess_execution_result_initialization(self): |
| 121 | + """Test SubprocessExecutionResult initialization.""" |
| 122 | + result = SubprocessExecutionResult(0, "stdout", "stderr") |
| 123 | + |
| 124 | + self.assertEqual(result.returncode, 0) |
| 125 | + self.assertEqual(result.stdout, "stdout") |
| 126 | + self.assertEqual(result.stderr, "stderr") |
| 127 | + |
| 128 | + # Test with defaults |
| 129 | + result_defaults = SubprocessExecutionResult(1) |
| 130 | + self.assertEqual(result_defaults.returncode, 1) |
| 131 | + self.assertEqual(result_defaults.stdout, "") |
| 132 | + self.assertEqual(result_defaults.stderr, "") |
| 133 | + |
| 134 | + def test_subprocess_execution_error_initialization(self): |
| 135 | + """Test SubprocessExecutionError initialization.""" |
| 136 | + cmd = ["test", "command"] |
| 137 | + error = SubprocessExecutionError(cmd, 1, "out", "err") |
| 138 | + |
| 139 | + self.assertEqual(error.cmd, ["test", "command"]) |
| 140 | + self.assertEqual(error.returncode, 1) |
| 141 | + self.assertEqual(error.stdout, "out") |
| 142 | + self.assertEqual(error.stderr, "err") |
| 143 | + |
| 144 | + # Test message formatting |
| 145 | + error_msg = str(error) |
| 146 | + self.assertIn("['test', 'command']", error_msg) |
| 147 | + self.assertIn("exit code 1", error_msg) |
| 148 | + self.assertIn("out", error_msg) |
| 149 | + self.assertIn("err", error_msg) |
| 150 | + |
| 151 | + @unittest.skipUnless(platform.system() in ("Linux", "Darwin"), "Requires Unix-like system") |
| 152 | + def test_empty_command_output(self): |
| 153 | + """Test command that produces no output.""" |
| 154 | + result = run_checked(["true"]) # Unix command that does nothing and succeeds |
| 155 | + |
| 156 | + self.assertEqual(result.returncode, 0) |
| 157 | + self.assertEqual(result.stdout, "") |
| 158 | + self.assertEqual(result.stderr, "") |
| 159 | + |
| 160 | + @unittest.skipUnless( |
| 161 | + platform.system() in ("Linux", "Darwin", "Windows"), "Requires a supported operating system" |
| 162 | + ) |
| 163 | + def test_command_with_arguments(self): |
| 164 | + """Test command with multiple arguments.""" |
| 165 | + if self.is_windows: |
| 166 | + cmd = ["cmd", "/c", "echo", "hello", "world"] |
| 167 | + else: |
| 168 | + cmd = ["echo", "hello", "world"] |
| 169 | + |
| 170 | + result = run_checked(cmd) |
| 171 | + |
| 172 | + self.assertEqual(result.returncode, 0) |
| 173 | + self.assertIn("hello", result.stdout) |
| 174 | + self.assertIn("world", result.stdout) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + unittest.main() |
0 commit comments