|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +sys.path.insert(0, os.path.abspath('.')) |
| 7 | + |
| 8 | +@patch('pica.cli.subprocess.run') |
| 9 | +@patch('builtins.input') |
| 10 | +@patch('pica.cli.print_banner') |
| 11 | +@patch('pica.cli.find_scripts') |
| 12 | +def test_pica_cli_exit(mock_find_scripts, mock_print_banner, mock_input, mock_subprocess_run): |
| 13 | + """ |
| 14 | + Test that pica_cli.py's main function exits correctly when 'q' is entered. |
| 15 | + """ |
| 16 | + mock_find_scripts.return_value = [('Dummy Script', '/path/to/dummy_script.py')] # Provide a dummy script |
| 17 | + mock_input.side_effect = ['q'] # Simulate user entering 'q' |
| 18 | + |
| 19 | + with pytest.raises(SystemExit) as e: |
| 20 | + import pica_cli |
| 21 | + pica_cli.main() |
| 22 | + |
| 23 | + assert e.type == SystemExit |
| 24 | + assert e.value.code == 0 |
| 25 | + mock_print_banner.assert_called_once() |
| 26 | + mock_find_scripts.assert_called_once() |
| 27 | + mock_input.assert_called_once() |
| 28 | + |
| 29 | +@patch('pica.main.tk.Tk') |
| 30 | +@patch('pica.main.PICALauncherApp') |
| 31 | +@patch('pica.main.multiprocessing.set_start_method') |
| 32 | +@patch('pica.main.multiprocessing.freeze_support') |
| 33 | +def test_run_pica_main(mock_freeze_support, mock_set_start_method, mock_pica_launcher_app, mock_tk_tk): |
| 34 | + """ |
| 35 | + Test that run_pica.py's main function initializes the GUI and |
| 36 | + sets multiprocessing start method. |
| 37 | + """ |
| 38 | + mock_root = MagicMock() |
| 39 | + mock_tk_tk.return_value = mock_root |
| 40 | + |
| 41 | + # Ensure main() is called |
| 42 | + import pica.main |
| 43 | + pica.main.main() |
| 44 | + |
| 45 | + mock_tk_tk.assert_called_once() |
| 46 | + mock_pica_launcher_app.assert_called_once_with(mock_root) |
| 47 | + mock_root.mainloop.assert_called_once() |
0 commit comments