From 920bb2755ee781ae398eacef02c0eea9d776eee7 Mon Sep 17 00:00:00 2001 From: Mikhail Manturov <2442833+insolor@users.noreply.github.com> Date: Mon, 2 Nov 2026 05:31:32 +0000 Subject: [PATCH 1/3] Fix string generation, randomly causing tests fail. --- .github/workflows/test.yml | 36 +++++++++++++++++++++++++++++------- tests/test_utils.py | 24 ++++++++++++++++++++++++ tests/utils.py | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 tests/test_utils.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0ed285a..41454ca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,8 +7,31 @@ on: branches: [ master ] jobs: + build-test-exe: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v7 + with: + submodules: true + + - name: Install fasm + uses: stevenwdv/setup-fasm@v2 + + - name: Compile test exe file + run: make -C ./tests/ build + + - name: Upload test exe file + uses: actions/upload-artifact@v4 + with: + name: test-exe + path: tests/test64.exe + if-no-files-found: error + test: + needs: build-test-exe strategy: + fail-fast: false matrix: python-version: ["3.10", "3.11", "3.12"] os: [ubuntu-latest, windows-latest] @@ -20,6 +43,12 @@ jobs: with: submodules: true + - name: Download test exe file + uses: actions/download-artifact@v4 + with: + name: test-exe + path: tests + - name: Install poetry run: pipx install poetry @@ -30,13 +59,6 @@ jobs: cache: "poetry" cache-dependency-path: poetry.lock - - name: Install fasm - uses: stevenwdv/setup-fasm@v2 - - - name: Compile test exe file - run: | - make -C ./tests/ build - - name: Install library and dependencies run: poetry install --no-interaction diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..3cc5883 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,24 @@ +import string + +import pytest + +from . import utils +from .utils import get_random_string + + +@pytest.mark.parametrize("length", range(1, 33)) +def test_get_random_string(length: int): + generated_string = get_random_string(length) + words = generated_string.split(" ") + + assert len(generated_string) == length + assert generated_string == generated_string.strip() + assert all(words) + assert all(set(word) <= set(string.ascii_lowercase) for word in words) + + +def test_get_random_string_retries_when_truncated_at_separator(monkeypatch: pytest.MonkeyPatch): + generated_words = iter(["ab", "c", "d", "ef"]) + monkeypatch.setattr(utils, "get_random_word", lambda _: next(generated_words)) + + assert get_random_string(3) == "d e" diff --git a/tests/utils.py b/tests/utils.py index 380a049..7468182 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ from pathlib import Path _MAX_RETRIES = 10 -GENERATED_STRING_CHARACTERS = string.ascii_lowercase + " " +GENERATED_STRING_CHARACTERS = string.ascii_lowercase def possible_to_run_exe() -> bool: @@ -25,7 +25,7 @@ def get_exe_stdout(exe_path: Path) -> list[str]: return list(result.decode().splitlines()) -def get_random_string(length: int, not_equals: str | None = None) -> str: +def get_random_word(length: int, not_equals: str | None = None) -> str: counter = 0 while True: generated_string = "".join(random.choices(GENERATED_STRING_CHARACTERS, k=length)) @@ -36,3 +36,34 @@ def get_random_string(length: int, not_equals: str | None = None) -> str: if counter >= _MAX_RETRIES: msg = "Infinite loop" raise ValueError(msg) + + +def get_random_string(length: int, not_equals: str | None = None) -> str: + if length == 0: + if not_equals is None or not_equals != "": + return "" + msg = "Infinite loop" + raise ValueError(msg) + + counter = 0 + while True: + words = [] + generated_length = 0 + while generated_length <= length: + word = get_random_word(random.randint(1, length)) + words.append(word) + generated_length += len(word) + if len(words) > 1: + generated_length += 1 + + generated_string = " ".join(words)[:length] + if generated_string.endswith(" "): + continue + + if not_equals is None or generated_string != not_equals: + return generated_string + + counter += 1 + if counter >= _MAX_RETRIES: + msg = "Infinite loop" + raise ValueError(msg) From 4308ae5cb116db6a96e4bdb00ab9327a7d6dc1c4 Mon Sep 17 00:00:00 2001 From: Mikhail Manturov <2442833+insolor@users.noreply.github.com> Date: Mon, 2 Nov 2026 05:32:08 +0000 Subject: [PATCH 2/3] Run tests on python 3.13 and 3.14 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41454ca..5598ef3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,7 +33,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.10", "3.11", "3.12"] + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} From 9654acbaa5e1092aeb6024a6475b11edf9eefb41 Mon Sep 17 00:00:00 2001 From: Mikhail Manturov <2442833+insolor@users.noreply.github.com> Date: Mon, 2 Nov 2026 05:44:57 +0000 Subject: [PATCH 3/3] Add more tests for utils --- tests/test_utils.py | 31 ++++++++++++++++++++++++++++++- tests/utils.py | 4 ++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 3cc5883..7b49f1c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,9 +6,13 @@ from .utils import get_random_string -@pytest.mark.parametrize("length", range(1, 33)) +@pytest.mark.parametrize("length", range(33)) def test_get_random_string(length: int): generated_string = get_random_string(length) + if length == 0: + assert generated_string == "" + return + words = generated_string.split(" ") assert len(generated_string) == length @@ -17,8 +21,33 @@ def test_get_random_string(length: int): assert all(set(word) <= set(string.ascii_lowercase) for word in words) +def test_get_random_string_avoids_not_equals(monkeypatch: pytest.MonkeyPatch): + generated_words = iter(["abc", "x", "def", "x"]) + monkeypatch.setattr(utils, "get_random_word", lambda _: next(generated_words)) + + assert get_random_string(3, not_equals="abc") == "def" + + def test_get_random_string_retries_when_truncated_at_separator(monkeypatch: pytest.MonkeyPatch): generated_words = iter(["ab", "c", "d", "ef"]) monkeypatch.setattr(utils, "get_random_word", lambda _: next(generated_words)) assert get_random_string(3) == "d e" + + +def test_get_random_string_bounds_separator_retries(monkeypatch: pytest.MonkeyPatch): + generated_words = ["ab", "c"] + calls = 0 + + def get_separator_ending_word(_: int) -> str: + nonlocal calls + word = generated_words[calls % len(generated_words)] + calls += 1 + return word + + monkeypatch.setattr(utils, "get_random_word", get_separator_ending_word) + + with pytest.raises(ValueError, match="Infinite loop"): + get_random_string(3) + + assert calls == utils._MAX_RETRIES * len(generated_words) # noqa: SLF001 diff --git a/tests/utils.py b/tests/utils.py index 7468182..eb9a2a7 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -58,6 +58,10 @@ def get_random_string(length: int, not_equals: str | None = None) -> str: generated_string = " ".join(words)[:length] if generated_string.endswith(" "): + counter += 1 + if counter >= _MAX_RETRIES: + msg = "Infinite loop" + raise ValueError(msg) continue if not_equals is None or generated_string != not_equals: