Skip to content

Commit

Permalink
test_generate_patches.py: Add test_read_incrementally_from_subprocess()
Browse files Browse the repository at this point in the history
Mark this test as exploratory (that is, not testing the final code), and
slow (because it uses time.sleep()).  To do this, configure available
marks via newly introduced 'pytest.ini'.

https://docs.pytest.org/en/stable/how-to/mark.html#registering-marks
https://docs.pytest.org/en/stable/how-to/usage.html#specifying-which-tests-to-run

This test uses a helper script in newly introduced tests/helpers/
subdirectory.
  • Loading branch information
jnareb committed Jul 12, 2024
1 parent 897ce4b commit 0068628
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
addopts = --strict-markers
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
explore: exploratory tests (deselect with '-m "not explore"')
13 changes: 13 additions & 0 deletions tests/helpers/spew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python
"""Generates output incrementally, flushing after each line"""
import time

print("Split line first part", end="", flush=True)
time.sleep(0.6)
print(", second part", flush=True)

print("2nd line", flush=True)
time.sleep(1.0)
print("3rd line (last line)", flush=True)

# end of spew.py
33 changes: 33 additions & 0 deletions tests/test_generate_patches.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-
"""Test cases for `src/diffannotator/generate_patches.py` module"""
import subprocess
import time
from pathlib import Path

import pytest

from diffannotator.annotate import annotate_single_diff
from diffannotator.generate_patches import GitRepo

Expand Down Expand Up @@ -81,3 +85,32 @@ def test_format_patch(tmp_path: Path):
"format_patch() created patches for all 24 commits in trunk"
assert all([path.suffix == '.patch' for path in patches_paths]), \
"all created files have '.patch' suffix"


@pytest.mark.slow
@pytest.mark.explore
def test_incrementally_from_subprocess():
"""Exploratory test to examine reading process output incrementally, line by line"""
process = subprocess.Popen(
# -u :: Force the stdout and stderr streams to be unbuffered.
["python", "-u", "tests/helpers/spew.py"],
bufsize=1, # line buffered
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
encoding='utf-8',
text=True,
)

start = time.time()
elapsed_times = []
while process.poll() is None:
data = process.stdout.readline()
if data:
elapsed_times.append(time.time() - start)

return_code = process.wait()

assert return_code == 0, \
"process finished without errors"
assert elapsed_times[-1] - elapsed_times[0] > 1.0, \
"got first line more than 1.0 seconds earlier than last line"

0 comments on commit 0068628

Please sign in to comment.