From 7d759a976b4986debe17d3a602160d5c03eef644 Mon Sep 17 00:00:00 2001 From: Ueslei Carvalho Date: Wed, 8 Mar 2023 19:27:19 -0300 Subject: [PATCH 1/2] fix: move scanapi/__main__.py to scanapi/cli.py to debug with pdb works --- scanapi/__init__.py | 2 +- scanapi/__main__.py | 111 +--------------------------------------- scanapi/cli.py | 110 +++++++++++++++++++++++++++++++++++++++ tests/unit/test_main.py | 2 +- 4 files changed, 114 insertions(+), 111 deletions(-) create mode 100644 scanapi/cli.py diff --git a/scanapi/__init__.py b/scanapi/__init__.py index 01fa9b6c..698b825d 100644 --- a/scanapi/__init__.py +++ b/scanapi/__init__.py @@ -1,4 +1,4 @@ -from scanapi.__main__ import main +from scanapi.cli import main name = "scanapi" diff --git a/scanapi/__main__.py b/scanapi/__main__.py index 2145c86b..4e28416e 100644 --- a/scanapi/__main__.py +++ b/scanapi/__main__.py @@ -1,110 +1,3 @@ -import logging +from .cli import main -import click -import yaml -from pkg_resources import get_distribution -from rich.logging import RichHandler - -from scanapi.exit_code import ExitCode -from scanapi.scan import scan -from scanapi.settings import settings - -CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) - -dist = get_distribution("scanapi") - - -@click.group() -@click.version_option(version=dist.version) -def main(): - """Automated Testing and Documentation for your REST API.""" - - -@main.command(context_settings=CONTEXT_SETTINGS) -@click.argument("spec_path", type=click.Path(exists=True), required=False) -@click.option( - "-o", - "--output-path", - "output_path", - type=click.Path(), - help="Report output path.", -) -@click.option( - "-nr", - "--no-report", - "no_report", - is_flag=True, - help="Run ScanAPI without generating report.", -) -@click.option( - "-b", - "--browser", - "open_browser", - is_flag=True, - help="Open the results file using a browser", -) -@click.option( - "-c", - "--config-path", - "config_path", - type=click.Path(exists=True), - help="Configuration file path.", -) -@click.option( - "-t", - "--template", - "template", - type=click.Path(exists=True), - help="Custom report template path. The template must be a .jinja file.", -) -@click.option( - "-ll", - "--log-level", - "log_level", - type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), - default="INFO", - help="Set the debug logging level for the program.", -) -def run( - spec_path, - output_path, - no_report, - config_path, - template, - log_level, - open_browser, -): - """ - Automated Testing and Documentation for your REST API. - SPEC_PATH argument is the API specification file path. - """ - logging.basicConfig( - level=log_level, - format="%(message)s", - datefmt="[%X]", - handlers=[ - RichHandler( - show_time=False, markup=True, show_path=(log_level == "DEBUG") - ) - ], - ) - logger = logging.getLogger(__name__) - - click_preferences = { - "spec_path": spec_path, - "output_path": output_path, - "no_report": no_report, - "config_path": config_path, - "template": template, - "open_browser": open_browser, - } - - try: - settings.save_preferences(**click_preferences) - except yaml.YAMLError as e: - error_message = "Error loading configuration file." - error_message = "{}\nPyYAML: {}".format(error_message, str(e)) - logger.error(error_message) - raise SystemExit(ExitCode.USAGE_ERROR) - - scan() +main() diff --git a/scanapi/cli.py b/scanapi/cli.py new file mode 100644 index 00000000..c44bc32f --- /dev/null +++ b/scanapi/cli.py @@ -0,0 +1,110 @@ +import logging + +import click +import yaml +from pkg_resources import get_distribution +from rich.logging import RichHandler + +from scanapi.exit_code import ExitCode +from scanapi.scan import scan +from scanapi.settings import settings + +CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) + +dist = get_distribution("scanapi") + + +@click.group() +@click.version_option(version=dist.version) +def main(): + """Automated Testing and Documentation for your REST API.""" + + +@main.command(context_settings=CONTEXT_SETTINGS) +@click.argument("spec_path", type=click.Path(exists=True), required=False) +@click.option( + "-o", + "--output-path", + "output_path", + type=click.Path(), + help="Report output path.", +) +@click.option( + "-nr", + "--no-report", + "no_report", + is_flag=True, + help="Run ScanAPI without generating report.", +) +@click.option( + "-b", + "--browser", + "open_browser", + is_flag=True, + help="Open the results file using a browser", +) +@click.option( + "-c", + "--config-path", + "config_path", + type=click.Path(exists=True), + help="Configuration file path.", +) +@click.option( + "-t", + "--template", + "template", + type=click.Path(exists=True), + help="Custom report template path. The template must be a .jinja file.", +) +@click.option( + "-ll", + "--log-level", + "log_level", + type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), + default="INFO", + help="Set the debug logging level for the program.", +) +def run( + spec_path, + output_path, + no_report, + config_path, + template, + log_level, + open_browser, +): + """ + Automated Testing and Documentation for your REST API. + SPEC_PATH argument is the API specification file path. + """ + logging.basicConfig( + level=log_level, + format="%(message)s", + datefmt="[%X]", + handlers=[ + RichHandler( + show_time=False, markup=True, show_path=(log_level == "DEBUG") + ) + ], + ) + logger = logging.getLogger(__name__) + + click_preferences = { + "spec_path": spec_path, + "output_path": output_path, + "no_report": no_report, + "config_path": config_path, + "template": template, + "open_browser": open_browser, + } + + try: + settings.save_preferences(**click_preferences) + except yaml.YAMLError as e: + error_message = "Error loading configuration file." + error_message = f"{error_message}\nPyYAML: {e}" + logger.error(error_message) + raise SystemExit(ExitCode.USAGE_ERROR) + + scan() diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 02907eaf..cae010fc 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -4,7 +4,7 @@ from click.testing import CliRunner from pytest import mark -from scanapi.__main__ import run +from scanapi.cli import run log = logging.getLogger(__name__) runner = CliRunner() From 7205e8c67d66c4632d2e034cee57c27f12ff02d2 Mon Sep 17 00:00:00 2001 From: Ueslei Carvalho Date: Wed, 8 Mar 2023 19:27:28 -0300 Subject: [PATCH 2/2] chore: update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e8e5fb0..04201a16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Move content of `scanapi/__main__.py` to `scanapi/cli.py` to enable debug with `pdb` [#501](https://github.com/scanapi/scanapi/issues/501) + ## [2.8.2] - 2023-03-06 ### Fixed - Content field not rendered properly on Chrome [#551](https://github.com/scanapi/scanapi/issues/551)