Skip to content

Commit

Permalink
chore: add python project template skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 13, 2024
1 parent 522d31e commit de019bb
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/helloworld/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
main module initialization
set up for module execution
"""

from helloworld.main import main

__all__ = ["main"]
10 changes: 10 additions & 0 deletions src/helloworld/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
helloworld module execution
Entrypoint when running module: python -m helloworld
"""

from helloworld import main as cli

if __name__ == "__main__": # pragma: no cover
cli()
23 changes: 23 additions & 0 deletions src/helloworld/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
helloworld tool main module
"""

import sys

from rich.traceback import install

from helloworld import prompt

install() # setup rich


def main() -> None:
"""
Runs the cli application code.
"""

prompt.say(" ".join(sys.argv[1:]))


if __name__ == "__main__": # pragma: no cover
main()
19 changes: 19 additions & 0 deletions src/helloworld/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Writes text prompts.
"""


def words(message: str) -> str:
r"""
Generates the string message.
"""

return message.rstrip()


def say(message: str) -> None:
r"""
Writes the message.
"""

print(words(message))
Empty file added src/helloworld/py.typed
Empty file.
Empty file added tests/__init__.py
Empty file.
85 changes: 85 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Cli/Main Tests
"""

import os.path
import subprocess # nosec
import sys
from typing import Any, List, Union

import pytest
from _pytest.capture import CaptureFixture, CaptureResult

# Our Project
from helloworld import main as cli

unit_test_data: List[List[str]] = [
[
"hello",
"hello",
],
[
"hello world!",
"hello world!",
],
]

# [[input, expected], ...]
integration_test_data: List[List[Union[str, List[str]]]] = [
[
"Hello World!",
"Hello World!",
],
]


@pytest.mark.parametrize("message,expected", unit_test_data)
def test_method_with_input(message: str, expected: List[str], capsys: CaptureFixture) -> None:
"""Runs the class methods against all of our test data."""

captured_out: List[str]
expected_out: List[str]

sys.argv = ["helloworld"] + message.split(" ")

# discard previous output
captured: CaptureResult[Any] = capsys.readouterr()
cli()
captured = capsys.readouterr() # capture new output

# captured_out = captured.out.split("\n")
captured_out = [*captured.out]
# expected_out = [str(number) for number in expected]
expected_out = [*expected]

print(f"{captured_out} == {expected_out}")
assert all(e == o for e, o in zip(captured_out, expected_out))


@pytest.mark.parametrize("message,expected", integration_test_data)
def test_script(message: str, expected: List[str]) -> None:
"""Runs the main script against all of our test data."""

program_input: str = ""

process: subprocess.CompletedProcess = subprocess.run(
[
"python",
os.path.dirname("src/helloworld/"),
message,
],
check=False,
input=program_input,
stdout=subprocess.PIPE,
) # nosec

program_output: str = process.stdout.decode("utf-8").strip()
# program_output: str = process.stdout.decode("utf-8")
# program_out: List[str] = program_output.split("\n")
program_out: List[str] = [*program_output]

# expected_out: List[int] = [str(number) for number in expected]
expected_out: List[str] = [*expected]

print(f" program_out: {program_out}\n==\nexpected_out: {expected_out}")
assert all(e == o for e, o in zip(program_out, expected_out))

0 comments on commit de019bb

Please sign in to comment.