Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,33 @@ 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"]
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
os: [ubuntu-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand All @@ -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

Expand All @@ -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

Expand Down
53 changes: 53 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import string

import pytest

from . import utils
from .utils import get_random_string


@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
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_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
39 changes: 37 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
Expand All @@ -36,3 +36,38 @@ 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(" "):
counter += 1
if counter >= _MAX_RETRIES:
msg = "Infinite loop"
raise ValueError(msg)
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)
Loading