Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions swagger_coverage_py/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion swagger_coverage_py/results_writers/base_schemas_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import pathlib
import platform
import re
import urllib
Expand Down Expand Up @@ -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()
Expand All @@ -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}"

Expand Down