From 1a5edea3eab76ff06038cdb4c33faa4af4158e2b Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Mon, 14 Oct 2024 11:27:59 +0000 Subject: [PATCH] test: make tests platform-agnostic and faster Add a `-d`/`--dump` test. --- tests/test_memsparkline.py | 49 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tests/test_memsparkline.py b/tests/test_memsparkline.py index 49bc8ee..9e5f5c9 100644 --- a/tests/test_memsparkline.py +++ b/tests/test_memsparkline.py @@ -18,6 +18,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +from __future__ import annotations + import os import re import shlex @@ -27,6 +29,7 @@ from pathlib import Path PYTHON = sys.executable +PYTHON_SLEEP_COMMAND = [PYTHON, "-c", "import time; time.sleep(0.1)"] TEST_PATH = Path(__file__).resolve().parent COMMAND = shlex.split(os.environ.get("MEMSPARKLINE_COMMAND", "")) @@ -56,6 +59,10 @@ def run( return output +def sleep_command(duration: float = 0.1) -> list[str]: + return [PYTHON, "-c", f"import time; time.sleep({duration})"] + + class TestMemsparkline(unittest.TestCase): def test_usage(self) -> None: assert re.search("^usage", run(check=False)) @@ -63,39 +70,36 @@ def test_usage(self) -> None: def test_version(self) -> None: assert re.search("\\d+\\.\\d+\\.\\d+", run("-v", return_stdout=True)) - -@unittest.skipUnless(os.name == "posix", "requires a POSIX OS") -class TestMemsparklinePOSIX(unittest.TestCase): def test_basic(self) -> None: - assert re.search("(?s).*avg:.*max:", run("sleep", "1")) + assert re.search("(?s).*avg:.*max:", run(*sleep_command())) def test_length(self) -> None: - stderr = run("-l", "10", "-w", "10", "sleep", "1") + stderr = run("-l", "10", "-w", "10", *sleep_command(0.3)) assert re.search("(?m)\\r[^ ]{10} \\d+\\.\\d\\n avg", stderr) def test_mem_format(self) -> None: - stderr = run("-l", "10", "-w", "10", "-m", "%0.2f", "sleep", "1") + stderr = run("-l", "10", "-w", "10", "-m", "%0.2f", *sleep_command(0.3)) assert re.search("(?m)\\r[^ ]{10} \\d+\\.\\d{2}\\n avg", stderr) def test_time_format(self) -> None: - stderr = run("-l", "10", "-t", "%d:%05d:%06.3f", "sleep", "1") + stderr = run("-l", "10", "-t", "%d:%05d:%06.3f", *sleep_command()) assert re.search("(?m)time: \\d+\\:\\d{5}:\\d{2}\\.\\d{3}\\n", stderr) def test_wait_1(self) -> None: - stderr = run("-w", "2000", "sleep", "1") + stderr = run("-w", "2000", *sleep_command()) assert len(stderr.split("\n")) == 5 def test_wait_2(self) -> None: - stderr = run("-n", "-w", "100", "sleep", "1") + stderr = run("-n", "-w", "50", *sleep_command(0.5)) assert len(stderr.split("\n")) in range(10, 15) def test_quiet(self) -> None: - stderr = run("-q", "sleep", "1") + stderr = run("-q", *sleep_command()) assert re.search("^ avg", stderr) @@ -113,28 +117,29 @@ def test_double_dash(self) -> None: def test_two_double_dashes(self) -> None: assert "\n" in run("--", *COMMAND, "--", "ls", "-l", return_stdout=True) + def test_dump(self) -> None: + dump_path = Path(TEST_PATH, "dump.log") + if dump_path.exists(): + dump_path.unlink() + + run("-q", "-w", "100", "-d", str(dump_path), *sleep_command(0.2)) + + lines = dump_path.read_text().splitlines() + + assert len(lines) == 3 + assert all(re.match(r"\d+", line) for line in lines) + def test_output(self) -> None: output_path = Path(TEST_PATH, "output.log") if output_path.exists(): output_path.unlink() for _ in range(2): - run("-q", "-o", str(output_path), "sleep", "1") + run("-q", "-o", str(output_path), *sleep_command()) text = output_path.read_text() assert len(text.split("\n")) == 7 -@unittest.skipUnless(os.name == "nt", "requires Windows") -class TestMemsparklineWindows(unittest.TestCase): - def test_cmd_basic(self) -> None: - assert re.search("(?s).*avg:.*max:", run("cmd.exe", "/c", "dir")) - - def test_python_sleep(self) -> None: - stderr = run("-w", "2000", PYTHON, "-c", "import time; time.sleep(1)") - - assert len(stderr.split("\n")) == 5 - - if __name__ == "__main__": unittest.main()