Skip to content

Leverage markdown and fixtures to write intentions #27

Description

@ELC

Since intentions are plain text, it would be great to have a plain text file intentions.md in the same folder as the test to have the intentions, one per line.

I managed to do this with the following snippet:

intentions.md file:

SRP: All the code in {module} should follow Single Responsability Principle.
OCP: All the code in {module} should follow Open Close Principle.
LSP: All the code in {module} should follow Liskov Substitution Principle.
ISP: All the code in {module} should follow Interface Segregation Principle.
DIP: All the code in {module} should follow Dependency Inversion Principle.

conftest.py file:

def extract_intentions(file_path: Path) -> tuple[list[str], list[str]]:
    intentions_file = file_path.parent / "intentions.md"

    if not intentions_file.exists():
        msg = "Intention files does not exists"
        raise ValueError(msg)

    intentions_raw = intentions_file.read_text(encoding="utf-8").split("\n")
    intention_names, _, intentions = list[list[str]](
        zip(
            *(intention_raw.partition(":") for intention_raw in intentions_raw),
            strict=False,
        )
    )

    clean_intentions_names = [
        intention_name
        for raw_intention_name in intention_names
        if (intention_name := raw_intention_name.strip())
    ]

    clean_intentions = [
        intention
        for raw_intention in intentions
        if (intention := raw_intention.strip())
    ]

    if not clean_intentions:
        msg = "Intention file is empty"
        raise ValueError(msg)

    return clean_intentions_names, clean_intentions


@pytest.fixture
def intentions(request: pytest.FixtureRequest) -> tuple[list[str], list[str]]:
    return extract_intentions(Path(request.node.location[0]))

This may be specific to pytest but having the library automatically register convient fixtures like this would improve the developer experience

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions