diff --git a/swagger_coverage_py/reporter.py b/swagger_coverage_py/reporter.py index 4fc8b8d..0f4467e 100644 --- a/swagger_coverage_py/reporter.py +++ b/swagger_coverage_py/reporter.py @@ -24,7 +24,7 @@ def __init__(self, api_name: str, host: str, verify: bool = True): def __get_output_dir(self): output_dir = "swagger-coverage-output" - subdir = re.match(r"(^\w*)://(.*)", self.host).group(2) + subdir = re.match(r"(^\w*)://(.*)", self.host).group(2).replace(".", "_").replace(":", "_") return f"{output_dir}/{subdir}" def __get_ignored_paths_from_config(self) -> List[str]: @@ -71,30 +71,80 @@ def setup( paths_to_delete=self.ignored_paths, ) - def generate_report(self): - inner_location = "swagger-coverage-commandline/bin/swagger-coverage-commandline" - + import platform + import subprocess + import os + from pathlib import Path + import logging + + logger = logging.getLogger(__name__) + + def _generate_report_windows( + self + ): + base_dir = os.path.join(os.path.dirname(__file__), "swagger-coverage-commandline") + lib_path = os.path.join(base_dir, "lib", "*") + + classpath = lib_path.replace("/", os.sep) + command = [ + "java", + "-cp", classpath, + "com.github.viclovsky.swagger.coverage.CommandLine", + "-s", self.swagger_doc_file, + "-i", self.output_dir, + ] + if self.swagger_coverage_config: + command.extend(["-c", self.swagger_coverage_config]) + + if not DEBUG_MODE: + subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + else: + subprocess.run(command, check=True) + + def generate_report( + self + ): + Path(self.output_dir).mkdir(parents=True, exist_ok=True) + + inner_location = os.path.join( + "swagger-coverage-commandline", + "bin", + "swagger-coverage-commandline.bat" + if platform.system() == "Windows" + else "swagger-coverage-commandline", + ) cmd_path = os.path.join(os.path.dirname(__file__), inner_location) - assert Path( - cmd_path - ).exists(), ( + assert Path(cmd_path).exists(), ( f"No commandline tools is found in following locations:\n{cmd_path}\n" ) + command = [cmd_path, "-s", self.swagger_doc_file, "-i", self.output_dir] if self.swagger_coverage_config: command.extend(["-c", self.swagger_coverage_config]) - # Adjust the file paths for Windows if platform.system() == "Windows": - command = [arg.replace("/", "\\") for arg in command] - - # Suppress all output if not in debug mode + self._generate_report_windows() + return + if not DEBUG_MODE: - with open(os.devnull, 'w') as devnull: - subprocess.run(command, stdout=devnull, stderr=devnull) + try: + subprocess.run( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=True, + ) + except subprocess.CalledProcessError as e: + raise RuntimeError( + "swagger-coverage CommandLine failed. " + f"Command: {e.cmd}\n" + f"Exit code: {e.returncode}\n" + f"STDOUT:\n{e.stdout}\n" + f"STDERR:\n{e.stderr}" + ) from e else: - subprocess.run(command) - + subprocess.run(command, check=True) def cleanup_input_files(self): shutil.rmtree(self.output_dir, ignore_errors=True) diff --git a/swagger_coverage_py/results_writers/base_schemas_manager.py b/swagger_coverage_py/results_writers/base_schemas_manager.py index 01248fa..3c71d6e 100644 --- a/swagger_coverage_py/results_writers/base_schemas_manager.py +++ b/swagger_coverage_py/results_writers/base_schemas_manager.py @@ -1,5 +1,6 @@ import json import os +import pathlib import platform import re import urllib @@ -128,7 +129,7 @@ def _get_header_params(self) -> list: return self._get_other_request_params(params_key="headers", params_in="header") def __get_output_subdir(self): - return re.match(r"(^\w*)://(.*)", self._uri.host).group(2) + return re.match(r"(^\w*)://(.*)", self._uri.host).group(2).replace(".", "_").replace(":", "_") def write_schema(self): schema_dict = self._get_schema() @@ -137,6 +138,7 @@ def write_schema(self): "/", "-" ).replace(":", "_") path_ = f"swagger-coverage-output/{self.__get_output_subdir()}" + pathlib.Path(path_).mkdir(parents=True, exist_ok=True) file_path = f"{path_}/{file_name}".split("?")[0] file_path = f"{file_path} ({rnd}).{API_DOCS_FORMAT}"