|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +from hey_cli.runner import CommandRunner |
| 6 | +from hey_cli.governance import GovernanceEngine |
| 7 | + |
| 8 | +class TestCommandRunnerHandoff(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.gov = MagicMock(spec=GovernanceEngine) |
| 11 | + self.runner = CommandRunner(governance=self.gov) |
| 12 | + self.handoff_path = os.path.expanduser("~/.hey_cwd_handoff") |
| 13 | + if os.path.exists(self.handoff_path): |
| 14 | + os.remove(self.handoff_path) |
| 15 | + |
| 16 | + def tearDown(self): |
| 17 | + if os.path.exists(self.handoff_path): |
| 18 | + os.remove(self.handoff_path) |
| 19 | + |
| 20 | + @patch("subprocess.run") |
| 21 | + def test_run_command_captures_pwd(self, mock_run): |
| 22 | + # Simulate a command that includes the HEY_CWD_HANDOFF marker |
| 23 | + mock_result = MagicMock() |
| 24 | + mock_result.returncode = 0 |
| 25 | + # Mocking the output of '(cd /tmp) ; printf "\nHEY_CWD_HANDOFF:%s\n" "$(pwd)"' |
| 26 | + mock_result.stdout = "some output\nHEY_CWD_HANDOFF:/tmp\n" |
| 27 | + mock_run.return_value = mock_result |
| 28 | + |
| 29 | + code, out = self.runner.run_command("cd /tmp", capture_pwd=True) |
| 30 | + |
| 31 | + # Verify the handoff file was created with the correct path |
| 32 | + self.assertTrue(os.path.exists(self.handoff_path)) |
| 33 | + with open(self.handoff_path, "r") as f: |
| 34 | + self.assertEqual(f.read().strip(), "/tmp") |
| 35 | + |
| 36 | + # Verify the marker was stripped from the output |
| 37 | + self.assertEqual(out.strip(), "some output") |
| 38 | + |
| 39 | + # Verify the command was wrapped correctly |
| 40 | + mock_run.assert_called_once() |
| 41 | + called_cmd = mock_run.call_args[0][0] |
| 42 | + self.assertIn("HEY_CWD_HANDOFF", called_cmd) |
| 43 | + |
| 44 | + @patch("subprocess.run") |
| 45 | + def test_run_command_no_capture_pwd(self, mock_run): |
| 46 | + mock_result = MagicMock() |
| 47 | + mock_result.returncode = 0 |
| 48 | + mock_result.stdout = "normal output\n" |
| 49 | + mock_run.return_value = mock_result |
| 50 | + |
| 51 | + code, out = self.runner.run_command("ls", capture_pwd=False) |
| 52 | + |
| 53 | + self.assertFalse(os.path.exists(self.handoff_path)) |
| 54 | + self.assertEqual(out.strip(), "normal output") |
| 55 | + |
| 56 | + # Verify the command was NOT wrapped |
| 57 | + mock_run.assert_called_once_with( |
| 58 | + "ls", shell=True, stdout=-1, stderr=-2, text=True |
| 59 | + ) |
| 60 | + |
| 61 | + @patch("platform.system") |
| 62 | + @patch("subprocess.run") |
| 63 | + def test_run_command_windows_handoff(self, mock_run, mock_platform): |
| 64 | + mock_platform.return_value = "Windows" |
| 65 | + |
| 66 | + mock_result = MagicMock() |
| 67 | + mock_result.returncode = 0 |
| 68 | + mock_result.stdout = "some output\nHEY_CWD_HANDOFF:C:\\Temp\n" |
| 69 | + mock_run.return_value = mock_result |
| 70 | + |
| 71 | + # We need to mock os.getcwd and os.path.normpath to match Windows style in this test |
| 72 | + with patch("os.getcwd", return_value="C:\\Users\\Test"), \ |
| 73 | + patch("os.path.expanduser", return_value=self.handoff_path): |
| 74 | + |
| 75 | + code, out = self.runner.run_command("cd C:\\Temp", capture_pwd=True) |
| 76 | + |
| 77 | + # Verify the command was wrapped using Windows CMD syntax |
| 78 | + called_cmd = mock_run.call_args[0][0] |
| 79 | + self.assertEqual(called_cmd, '("cd C:\\Temp") & echo. & echo HEY_CWD_HANDOFF:%CD%') |
| 80 | + |
| 81 | + # Verify the handoff file was created |
| 82 | + self.assertTrue(os.path.exists(self.handoff_path)) |
| 83 | + with open(self.handoff_path, "r") as f: |
| 84 | + self.assertEqual(f.read().strip(), "C:\\Temp") |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + unittest.main() |
0 commit comments