diff --git a/poetry.lock b/poetry.lock index 6e58aca6..71b12d77 100644 --- a/poetry.lock +++ b/poetry.lock @@ -11,9 +11,6 @@ files = [ {file = "astroid-3.3.6.tar.gz", hash = "sha256:6aaea045f938c735ead292204afdb977a36e989522b7833ef6fea94de743f442"}, ] -[package.dependencies] -typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""} - [[package]] name = "black" version = "24.10.0" @@ -51,8 +48,6 @@ mypy-extensions = ">=0.4.3" packaging = ">=22.0" pathspec = ">=0.9.0" platformdirs = ">=2" -tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} -typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""} [package.extras] colorama = ["colorama (>=0.4.3)"] @@ -349,7 +344,6 @@ files = [ [package.dependencies] pastel = ">=0.2.1,<0.3.0" pyyaml = ">=6.0.2,<7.0.0" -tomli = {version = ">=1.2.2", markers = "python_version < \"3.11\""} [package.extras] poetry-plugin = ["poetry (>=1.0,<2.0)"] @@ -368,17 +362,11 @@ files = [ [package.dependencies] astroid = ">=3.3.5,<=3.4.0-dev0" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} -dill = [ - {version = ">=0.2", markers = "python_version < \"3.11\""}, - {version = ">=0.3.7", markers = "python_version >= \"3.12\""}, - {version = ">=0.3.6", markers = "python_version >= \"3.11\" and python_version < \"3.12\""}, -] +dill = {version = ">=0.3.7", markers = "python_version >= \"3.12\""} isort = ">=4.2.5,<5.13.0 || >5.13.0,<6" mccabe = ">=0.6,<0.8" platformdirs = ">=2.2.0" -tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} tomlkit = ">=0.10.1" -typing-extensions = {version = ">=3.10.0", markers = "python_version < \"3.10\""} [package.extras] spelling = ["pyenchant (>=3.2,<4.0)"] @@ -467,47 +455,6 @@ urllib3 = ">=1.21.1,<3" socks = ["PySocks (>=1.5.6,!=1.5.7)"] use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] -[[package]] -name = "tomli" -version = "2.2.1" -description = "A lil' TOML parser" -optional = false -python-versions = ">=3.8" -files = [ - {file = "tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249"}, - {file = "tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6"}, - {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ece47d672db52ac607a3d9599a9d48dcb2f2f735c6c2d1f34130085bb12b112a"}, - {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6972ca9c9cc9f0acaa56a8ca1ff51e7af152a9f87fb64623e31d5c83700080ee"}, - {file = "tomli-2.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c954d2250168d28797dd4e3ac5cf812a406cd5a92674ee4c8f123c889786aa8e"}, - {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:8dd28b3e155b80f4d54beb40a441d366adcfe740969820caf156c019fb5c7ec4"}, - {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:e59e304978767a54663af13c07b3d1af22ddee3bb2fb0618ca1593e4f593a106"}, - {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:33580bccab0338d00994d7f16f4c4ec25b776af3ffaac1ed74e0b3fc95e885a8"}, - {file = "tomli-2.2.1-cp311-cp311-win32.whl", hash = "sha256:465af0e0875402f1d226519c9904f37254b3045fc5084697cefb9bdde1ff99ff"}, - {file = "tomli-2.2.1-cp311-cp311-win_amd64.whl", hash = "sha256:2d0f2fdd22b02c6d81637a3c95f8cd77f995846af7414c5c4b8d0545afa1bc4b"}, - {file = "tomli-2.2.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4a8f6e44de52d5e6c657c9fe83b562f5f4256d8ebbfe4ff922c495620a7f6cea"}, - {file = "tomli-2.2.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:8d57ca8095a641b8237d5b079147646153d22552f1c637fd3ba7f4b0b29167a8"}, - {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e340144ad7ae1533cb897d406382b4b6fede8890a03738ff1683af800d54192"}, - {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db2b95f9de79181805df90bedc5a5ab4c165e6ec3fe99f970d0e302f384ad222"}, - {file = "tomli-2.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:40741994320b232529c802f8bc86da4e1aa9f413db394617b9a256ae0f9a7f77"}, - {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:400e720fe168c0f8521520190686ef8ef033fb19fc493da09779e592861b78c6"}, - {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:02abe224de6ae62c19f090f68da4e27b10af2b93213d36cf44e6e1c5abd19fdd"}, - {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b82ebccc8c8a36f2094e969560a1b836758481f3dc360ce9a3277c65f374285e"}, - {file = "tomli-2.2.1-cp312-cp312-win32.whl", hash = "sha256:889f80ef92701b9dbb224e49ec87c645ce5df3fa2cc548664eb8a25e03127a98"}, - {file = "tomli-2.2.1-cp312-cp312-win_amd64.whl", hash = "sha256:7fc04e92e1d624a4a63c76474610238576942d6b8950a2d7f908a340494e67e4"}, - {file = "tomli-2.2.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f4039b9cbc3048b2416cc57ab3bda989a6fcf9b36cf8937f01a6e731b64f80d7"}, - {file = "tomli-2.2.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:286f0ca2ffeeb5b9bd4fcc8d6c330534323ec51b2f52da063b11c502da16f30c"}, - {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a92ef1a44547e894e2a17d24e7557a5e85a9e1d0048b0b5e7541f76c5032cb13"}, - {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9316dc65bed1684c9a98ee68759ceaed29d229e985297003e494aa825ebb0281"}, - {file = "tomli-2.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e85e99945e688e32d5a35c1ff38ed0b3f41f43fad8df0bdf79f72b2ba7bc5272"}, - {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ac065718db92ca818f8d6141b5f66369833d4a80a9d74435a268c52bdfa73140"}, - {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:d920f33822747519673ee656a4b6ac33e382eca9d331c87770faa3eef562aeb2"}, - {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a198f10c4d1b1375d7687bc25294306e551bf1abfa4eace6650070a5c1ae2744"}, - {file = "tomli-2.2.1-cp313-cp313-win32.whl", hash = "sha256:d3f5614314d758649ab2ab3a62d4f2004c825922f9e370b29416484086b264ec"}, - {file = "tomli-2.2.1-cp313-cp313-win_amd64.whl", hash = "sha256:a38aa0308e754b0e3c67e344754dff64999ff9b513e691d0e786265c93583c69"}, - {file = "tomli-2.2.1-py3-none-any.whl", hash = "sha256:cb55c73c5f4408779d0cf3eef9f762b9c9f147a77de7b258bef0a5628adc85cc"}, - {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"}, -] - [[package]] name = "tomlkit" version = "0.13.2" @@ -519,17 +466,6 @@ files = [ {file = "tomlkit-0.13.2.tar.gz", hash = "sha256:fff5fe59a87295b278abd31bec92c15d9bc4a06885ab12bcea52c71119392e79"}, ] -[[package]] -name = "typing-extensions" -version = "4.12.2" -description = "Backported and Experimental Type Hints for Python 3.8+" -optional = false -python-versions = ">=3.8" -files = [ - {file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"}, - {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, -] - [[package]] name = "urllib3" version = "2.2.3" @@ -549,5 +485,5 @@ zstd = ["zstandard (>=0.18.0)"] [metadata] lock-version = "2.0" -python-versions = ">=3.9,<=3.13" -content-hash = "3aec273c1e00118fc77b4194de9c65bbe627cae5ab37d3cffa8b64ff84ebf426" +python-versions = ">=3.12" +content-hash = "f63036f3ccb0d460bb53ea237079bebf0da82570902757a5cddedf354f98960a" diff --git a/pyproject.toml b/pyproject.toml index b2fbc04f..3d1cd765 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,9 @@ poethepoet = "^0.31.1" [tool.poe.tasks] format = "black ./tests" lint = "pylint ./tests" -tests = "python tests/run_tests.py" +example-test = "python tests/run_tests.py --test-name example_test" +restart-test = "python tests/run_tests.py --test-name restart" +tests = ["example-test", "restart-test"] pre-commit = ["format", "lint", "tests"] [build-system] diff --git a/tests/run_tests.py b/tests/run_tests.py index a7ac16b6..a2f50ca2 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,44 +1,109 @@ +""" +run_tests.py + +Command Line Interface to run a test by its name. The name should be placed at ./tests folder. +It's suposed that you run it through `poetry` package management and `poe` task manager, but you +can run it with `python` if you installed the packages properly, in a isolated or not isolated +environment (althought we recommend the isolated environment). + +All tests will run as a spwaned subprocess and what happens will be logged to a temporary directory +(we defined, in linux, /tmp/floresta-func-tests/): + +```bash +# The default way to run all tests +poetry run poe tests + +# The default way to run a separated test (see the ones -- or define one -- in pyproject.toml) +poetry run poe example-test + +# This will do the same thing in the isolated environment +poetry run python tests/run_tests.py --test-name example_test + +# You can even define the `data_dir` to logs +poetry run python tests/run_tests.py --test-name example_test --data-dir $HOME/my/path + +# If you have a proper environment wit all necessary packages installed +# it can be possible to run without poetry +python tests/run_tests.py --test-name example_test --data-dir $HOME/my/path +``` +""" + import os import subprocess import time +import argparse +import tempfile -BASE_DIR = "/tmp/data" +BASE_DIR = os.path.normpath(os.path.join(tempfile.gettempdir(), "floresta-func-tests")) SUCCESS_EMOJI = "✔" FAILURE_EMOJI = "❌" -tests = ["example_test", "restart"] def main(): - print("Creating work dir") - data_dir = BASE_DIR + f"run-{time.time()}/" - print(f'writing stuff to {data_dir}') + """ + Create a CLI called `run_tests` with calling arguments + + usage: run_tests [-h] [-d DATA_DIR] [-t TEST_NAME] + + tool to help with function testing of Floresta + + options: + -h, --help show this help message and exit + -d, --data-dir DATA_DIR data directory of the run_tests's functional test logs + -t, --test-name TEST_NAME the name of the file to be tested (without .py) + """ + # Structure the CLI + parser = argparse.ArgumentParser( + prog="run_tests", + description="tool to help with function testing of Floresta", + ) + parser.add_argument( + "-d", + "--data-dir", + help="data directory of the %(prog)s's functional test logs", + default=BASE_DIR, + ) + + parser.add_argument( + "-t", "--test-name", help="the name of the file to be tested (without .py)" + ) + + # Parse arguments of CLI + args = parser.parse_args() + + # Define the data-dir and create it + data_dir = os.path.normpath(os.path.join(args.data_dir, args.test_name)) if not os.path.isdir(data_dir): os.makedirs(data_dir) - failures = [] - for test_name in tests: - test_dir = "./tests/" + test_name - log_dir = data_dir + test_name.replace(".py", ".log") - log_file = open(log_dir, "wt") - test = subprocess.Popen(["python", test_dir + ".py"], - stdout=log_file, stderr=log_file) - test.wait() - if test.returncode != 0: - print(f"Test {test_name} not passed {FAILURE_EMOJI}") - failures.append(test_name) - else: - print(f"Test {test_name} passed {SUCCESS_EMOJI}") - - if len(failures) > 0: - print(f"{FAILURE_EMOJI} {len(failures)} tests failed") - for failure in failures: - # show logs for failed tests - log_dir = data_dir + failure.replace(".py", ".log") - print(f"Logs for {failure}:") - with open(log_dir, "rt") as log_file: - print(log_file.read()) - - raise Exception("Tests failed") - -if __name__ == '__main__': + + # Setup directories and filenames for the specific test + test_dir = os.path.abspath(os.path.dirname(__file__)) + test_filename = os.path.normpath(os.path.join(test_dir, f"{args.test_name}.py")) + test_logname = os.path.normpath(os.path.join(data_dir, f"{int(time.time())}.log")) + print(f"Writing stuff to {test_logname}") + + # Now start the test + with open(test_logname, "wt", encoding="utf-8") as log_file: + cli = ["python", test_filename] + cli_msg = " ".join(cli) + print(f"running '{cli_msg}") + with subprocess.Popen(cli, stdout=log_file, stderr=log_file) as test: + test.wait() + + # Check the test, if failed, log the results + # if passed, just show that worked + if test.returncode != 0: + print(f"Test {args.test_name} not passed {FAILURE_EMOJI}") + with open(test_logname, "rt", encoding="utf-8") as log_file: + print(log_file.read()) + raise RuntimeError("Tests failed") + + print(f"Test {args.test_name} passed {SUCCESS_EMOJI}") + + # Add some \n to better visualization on terminal + print() + + +if __name__ == "__main__": main()