Skip to content

Commit a89c789

Browse files
committed
test: a test for --save-signal
1 parent b2b23c4 commit a89c789

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

coverage/cmdline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ def do_help(
822822

823823
def do_signal_save(self, _signum: int, _frame: types.FrameType | None) -> None:
824824
""" Signal handler to save coverage report """
825-
print("Saving coverage data ...")
825+
print("Saving coverage data ...", flush=True)
826826
self.coverage.save()
827827

828828
def do_run(self, options: optparse.Values, args: list[str]) -> int:

tests/helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import os.path
1515
import re
16+
import shlex
1617
import shutil
1718
import subprocess
1819
import sys
@@ -47,7 +48,7 @@ def _correct_encoding() -> str:
4748
return encoding
4849

4950

50-
def subprocess_popen(cmd: str) -> subprocess.Popen[bytes]:
51+
def subprocess_popen(cmd: str, shell: bool=True) -> subprocess.Popen[bytes]:
5152
"""Run a command in a subprocess.
5253
5354
Returns the Popen object.
@@ -66,9 +67,12 @@ def subprocess_popen(cmd: str) -> subprocess.Popen[bytes]:
6667
sub_env = dict(os.environ)
6768
sub_env["PYTHONIOENCODING"] = _correct_encoding()
6869

70+
if not shell:
71+
cmd = shlex.split(cmd) # type: ignore[assignment]
72+
6973
proc = subprocess.Popen(
7074
cmd,
71-
shell=True,
75+
shell=shell,
7276
env=sub_env,
7377
stdin=subprocess.PIPE,
7478
stdout=subprocess.PIPE,

tests/test_process.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import os.path
1212
import platform
1313
import re
14+
import signal
1415
import stat
1516
import sys
1617
import textwrap
18+
import time
1719

1820
from pathlib import Path
1921
from typing import Any
@@ -27,7 +29,7 @@
2729

2830
from tests import testenv
2931
from tests.coveragetest import CoverageTest, TESTS_DIR
30-
from tests.helpers import re_line, re_lines, re_lines_text
32+
from tests.helpers import re_line, re_lines, re_lines_text, subprocess_popen
3133

3234

3335
class ProcessTest(CoverageTest):
@@ -458,6 +460,33 @@ def test_os_exit(self, patch: bool) -> None:
458460
else:
459461
assert seen < total_lines
460462

463+
@pytest.mark.skipif(env.WINDOWS, reason="Windows can't do --save-signal")
464+
@pytest.mark.parametrize("send", [False, True])
465+
def test_save_signal(self, send: bool) -> None:
466+
# PyPy on Ubuntu seems to need more time for things to happen.
467+
base_time = 0.75 if (env.PYPY and env.LINUX) else 0.0
468+
self.make_file("loop.py", """\
469+
import time
470+
print("Starting", flush=True)
471+
while True:
472+
time.sleep(.02)
473+
""")
474+
proc = subprocess_popen("coverage run --save-signal=USR1 loop.py", shell=False)
475+
time.sleep(base_time + .25)
476+
if send:
477+
proc.send_signal(signal.SIGUSR1)
478+
time.sleep(base_time + .25)
479+
proc.kill()
480+
proc.wait(timeout=base_time + .25)
481+
stdout, _ = proc.communicate()
482+
assert b"Starting" in stdout
483+
if send:
484+
self.assert_exists(".coverage")
485+
assert b"Saving coverage data" in stdout
486+
else:
487+
self.assert_doesnt_exist(".coverage")
488+
assert b"Saving coverage data" not in stdout
489+
461490
def test_warnings_during_reporting(self) -> None:
462491
# While fixing issue #224, the warnings were being printed far too
463492
# often. Make sure they're not any more.

0 commit comments

Comments
 (0)