From 4e154778241a1ddab63d2a911956e8c6b8ef4d44 Mon Sep 17 00:00:00 2001 From: "Gabriele N. Tornetta" Date: Thu, 30 Apr 2026 14:13:07 +0100 Subject: [PATCH] chore: improve test coverage --- austin/format/compress.py | 4 ++-- test/format/test_compress.py | 33 ++++++++++++++++++++++++++++++ test/test_aio.py | 23 +++++++++++++++++++++ test/test_base.py | 39 ++++++++++++++++++++++++++++++++++++ test/test_semver.py | 4 ++++ test/test_simple.py | 12 +++++++++++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 test/test_base.py diff --git a/austin/format/compress.py b/austin/format/compress.py index 8e2e77e..7d6b5d7 100644 --- a/austin/format/compress.py +++ b/austin/format/compress.py @@ -110,10 +110,10 @@ def main() -> None: try: with ( - AustinFileReader(args.input) as fin, + open(args.input) as _fin, open(args.output or args.input, "w") as fout, ): - compress(fin, fout, args.counts) + compress(AustinFileReader(_fin), fout, args.counts) except FileNotFoundError: print(f"No such input file: {args.input}") exit(1) diff --git a/test/format/test_compress.py b/test/format/test_compress.py index f2df303..7b9616e 100644 --- a/test/format/test_compress.py +++ b/test/format/test_compress.py @@ -23,9 +23,13 @@ import io from pathlib import Path +from unittest.mock import patch + +import pytest from austin.format.collapsed_stack import AustinFileReader from austin.format.compress import compress +from austin.format.compress import main def test_compress(datapath: Path): @@ -58,3 +62,32 @@ def test_compress_counts(datapath: Path): print(compressed.getvalue()) msg = "Expected sample not found in compressed output" raise AssertionError(msg) + + +def test_compress_main(datapath: Path, tmp_path: Path): + output = tmp_path / "output.out" + with patch( + "sys.argv", ["austin-compress", str(datapath / "austin.out"), str(output)] + ): + main() + assert output.exists() + assert output.stat().st_size > 0 + + +def test_compress_main_counts(datapath: Path, tmp_path: Path): + output = tmp_path / "output_counts.out" + with patch( + "sys.argv", + ["austin-compress", "--counts", str(datapath / "austin.out"), str(output)], + ): + main() + assert output.exists() + + +def test_compress_main_missing_file(tmp_path: Path): + missing = tmp_path / "nonexistent.out" + output = tmp_path / "out.out" + with patch("sys.argv", ["austin-compress", str(missing), str(output)]): + with pytest.raises(SystemExit) as exc_info: + main() + assert exc_info.value.code == 1 diff --git a/test/test_aio.py b/test/test_aio.py index 3e40ad7..064ef66 100644 --- a/test/test_aio.py +++ b/test/test_aio.py @@ -24,6 +24,7 @@ import asyncio import sys from pathlib import Path +from unittest.mock import MagicMock import pytest from pytest import raises @@ -159,3 +160,25 @@ async def test_async_bad_options(): await austin.start( ["-I", "1000", "python", "-c", "for i in range(1000000): print(i)"] ) + + +def test_async_terminate_not_running(): + austin = TestAsyncAustin() + with raises(AustinError): + austin.terminate() + + +def test_async_terminate_process_already_gone(): + austin = TestAsyncAustin() + mock_proc = MagicMock() + mock_proc.terminate.side_effect = ProcessLookupError + austin._proc = mock_proc + with raises(AustinError): + austin.terminate() + + +@pytest.mark.asyncio +async def test_async_wait_not_running(): + austin = TestAsyncAustin() + with raises(AustinError): + await austin.wait() diff --git a/test/test_base.py b/test/test_base.py new file mode 100644 index 0000000..a76966e --- /dev/null +++ b/test/test_base.py @@ -0,0 +1,39 @@ +from pytest import raises + +from austin.base import AustinState +from austin.errors import AustinError +from austin.simple import SimpleAustin + + +class _SimpleAustin(SimpleAustin): + def on_sample(self, sample): + pass + + +def test_is_running_initially_false(): + austin = _SimpleAustin() + assert not austin.is_running() + + +def test_get_arguments_initially_none(): + austin = _SimpleAustin() + assert austin.get_arguments() is None + + +def test_state_initially_not_started(): + austin = _SimpleAustin() + assert austin.state is AustinState.NOT_STARTED + + +def test_check_version_too_old(): + austin = _SimpleAustin() + austin._meta["austin"] = "3.9.0" + with raises(AustinError, match="Incompatible Austin version"): + austin._check_version() + + +def test_check_version_none(): + austin = _SimpleAustin() + # _meta has no "austin" key → version returns (0, 0, 0) < minimum + with raises(AustinError): + austin._check_version() diff --git a/test/test_semver.py b/test/test_semver.py index 7f4c843..579128f 100644 --- a/test/test_semver.py +++ b/test/test_semver.py @@ -16,3 +16,7 @@ ) def test_semver(version, semver): assert _to_semver(version) == semver + + +def test_semver_none(): + assert _to_semver(None) == (0, 0, 0) diff --git a/test/test_simple.py b/test/test_simple.py index 87332c2..900b069 100644 --- a/test/test_simple.py +++ b/test/test_simple.py @@ -101,6 +101,18 @@ def assert_binary_path(path: Path): assert Path(TestSimpleAustin().binary_path).resolve() == path.resolve() +def test_simple_terminate_not_running(): + austin = TestSimpleAustin() + with raises(AustinError): + austin.terminate() + + +def test_simple_wait_not_running(): + austin = TestSimpleAustin() + with raises(AustinError): + austin.wait() + + def test_binary_path_cwd(): with tempfile.TemporaryDirectory() as tempdir: old_cwd = Path.cwd()