From 006862843e2156d926cfde937cd74b5a58daea09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Nar=C4=99bski?= Date: Fri, 12 Jul 2024 15:44:50 +0200 Subject: [PATCH] test_generate_patches.py: Add test_read_incrementally_from_subprocess() 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. --- pytest.ini | 5 +++++ tests/helpers/spew.py | 13 +++++++++++++ tests/test_generate_patches.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 pytest.ini create mode 100644 tests/helpers/spew.py diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..dc841db --- /dev/null +++ b/pytest.ini @@ -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"') diff --git a/tests/helpers/spew.py b/tests/helpers/spew.py new file mode 100644 index 0000000..b852e86 --- /dev/null +++ b/tests/helpers/spew.py @@ -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 diff --git a/tests/test_generate_patches.py b/tests/test_generate_patches.py index 69912ea..59c5e19 100644 --- a/tests/test_generate_patches.py +++ b/tests/test_generate_patches.py @@ -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 @@ -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"