Skip to content

Commit

Permalink
test: improvements to test_fork
Browse files Browse the repository at this point in the history
Don't dangle an opened file, and use csv to parse the ad-hoc output of the
forked program
  • Loading branch information
nedbat committed Nov 22, 2023
1 parent 17bc309 commit ba2c7c2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import csv
import glob
import os
import os.path
Expand All @@ -14,6 +15,7 @@
import sys
import textwrap

from pathlib import Path
from typing import Any

import pytest
Expand Down Expand Up @@ -364,19 +366,19 @@ def test_fork(self) -> None:
self.make_file("fork.py", """\
import os
print(f"parent={os.getpid()}", flush=True)
print(f"parent,{os.getpid()}", flush=True)
ret = os.fork()
if ret == 0:
print(f"child={os.getpid()}", flush=True)
print(f"child,{os.getpid()}", flush=True)
else:
os.waitpid(ret, 0)
""")
total_lines = 6

self.set_environ("COVERAGE_DEBUG_FILE", "debug.out")
out = self.run_command("coverage run --debug=pid,process,trace -p fork.py")
pids = {key:int(pid) for line in out.splitlines() for key, pid in [line.split("=")]}
pids = {key:int(pid) for key, pid in csv.reader(out.splitlines())}
assert set(pids) == {"parent", "child"}
self.assert_doesnt_exist(".coverage")

Expand Down Expand Up @@ -407,7 +409,7 @@ def test_fork(self) -> None:
data.read()
assert line_counts(data)["fork.py"] == total_lines

debug_text = open("debug.out").read()
debug_text = Path("debug.out").read_text()
ppid = pids["parent"]
cpid = pids["child"]
assert ppid != cpid
Expand Down

0 comments on commit ba2c7c2

Please sign in to comment.