| 1 |
import json |
| 2 |
import os |
| 3 |
from unittest.mock import MagicMock |
| 4 |
|
| 5 |
import briefcase_debugger |
| 6 |
import briefcase_debugger.pdb |
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
def test_no_env_vars(monkeypatch, capsys): |
| 11 |
"""Nothing happens, when no env vars are set.""" |
| 12 |
os_environ = {} |
| 13 |
monkeypatch.setattr(os, "environ", os_environ) |
| 14 |
|
| 15 |
# start test function |
| 16 |
briefcase_debugger.start_remote_debugger() |
| 17 |
|
| 18 |
captured = capsys.readouterr() |
| 19 |
assert captured.out == "" |
| 20 |
assert captured.err == "" |
| 21 |
|
| 22 |
|
| 23 |
def test_no_debugger_verbose(monkeypatch, capsys): |
| 24 |
"""Nothing happens except a short message, when only verbose is requested.""" |
| 25 |
os_environ = {} |
| 26 |
os_environ["BRIEFCASE_DEBUG"] = "1" |
| 27 |
monkeypatch.setattr(os, "environ", os_environ) |
| 28 |
|
| 29 |
# start test function |
| 30 |
briefcase_debugger.start_remote_debugger() |
| 31 |
|
| 32 |
captured = capsys.readouterr() |
| 33 |
assert ( |
| 34 |
captured.out |
| 35 |
== "No 'BRIEFCASE_DEBUGGER' environment variable found. Debugger not starting.\n" |
| 36 |
) |
| 37 |
assert captured.err == "" |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize("verbose", [True, False]) |
| 41 |
@pytest.mark.parametrize( |
| 42 |
("host_os", "expected_host_cmds"), |
| 43 |
[ |
| 44 |
("Windows", ["telnet somehost 9999"]), |
| 45 |
("Darwin", ["nc somehost 9999"]), |
| 46 |
("Linux", ["nc somehost 9999"]), |
| 47 |
("UnknownOS", ["nc somehost 9999", "telnet somehost 9999"]), |
| 48 |
], |
| 49 |
) |
| 50 |
def test_with_debugger(monkeypatch, host_os, expected_host_cmds, capsys, verbose): |
| 51 |
"""Normal debug session.""" |
| 52 |
os_environ = {} |
| 53 |
os_environ["BRIEFCASE_DEBUG"] = "1" if verbose else "0" |
| 54 |
os_environ["BRIEFCASE_DEBUGGER"] = json.dumps( |
| 55 |
{ |
| 56 |
"debugger": "pdb", |
| 57 |
"host": "somehost", |
| 58 |
"port": 9999, |
| 59 |
"host_os": host_os, |
| 60 |
} |
| 61 |
) |
| 62 |
monkeypatch.setattr(os, "environ", os_environ) |
| 63 |
|
| 64 |
fake_remote_pdb = MagicMock() |
| 65 |
monkeypatch.setattr(briefcase_debugger.pdb, "RemotePdb", fake_remote_pdb) |
| 66 |
|
| 67 |
# start test function |
| 68 |
briefcase_debugger.start_remote_debugger() |
| 69 |
|
| 70 |
fake_remote_pdb.assert_called_once_with( |
| 71 |
"somehost", |
| 72 |
9999, |
| 73 |
quiet=True, |
| 74 |
) |
| 75 |
|
| 76 |
captured = capsys.readouterr() |
| 77 |
assert "Waiting for debugger to attach..." in captured.out |
| 78 |
for cmd in expected_host_cmds: |
| 79 |
assert cmd in captured.out |
| 80 |
assert captured.err == "" |