Commit 177f896f authored by Martin Jeřábek's avatar Martin Jeřábek

testfw: minor code restructuring, formatting

parent 022303f4
......@@ -10,7 +10,7 @@ from os.path import abspath
from .log import MyLogRecord
d = Path(abspath(__file__)).parent
func_cov_dir = os.path.join(str(d.parent), "build/functional_coverage")
func_cov_dir = d.parent / "build/functional_coverage"
def setup_logging() -> None:
......@@ -132,10 +132,8 @@ def test(obj, config, strict, create_ghws, vunit_args):
if cfg_key in config:
tests.append(factory(ui, lib, config[cfg_key], build, base, create_ghws=create_ghws))
if not os.path.exists(func_cov_dir):
os.makedirs(func_cov_dir);
os.makedirs(os.path.join(func_cov_dir, "html"))
os.makedirs(os.path.join(func_cov_dir, "coverage_data"))
(func_cov_dir / "html").mkdir(parents=True, exist_ok=True)
(func_cov_dir / "coverage_data").mkdir(parents=True, exist_ok=True)
for t in tests:
t.add_sources()
......
......@@ -114,8 +114,8 @@ def add_common_sources(lib, ui) -> None:
def get_common_modelsim_init_files() -> List[str]:
modelsim_init_files = '../lib/test_lib.tcl,modelsim_init.tcl'
modelsim_init_files = [str(d/x) for x in modelsim_init_files.split(',')]
modelsim_init_files = ['../lib/test_lib.tcl', 'modelsim_init.tcl']
modelsim_init_files = [str(d/x) for x in modelsim_init_files]
return modelsim_init_files
def get_common_sim_flags() -> List[str]:
......@@ -134,10 +134,10 @@ def add_flags(ui, lib, build) -> None:
lib.add_compile_option("ghdl.flags", ["-fprofile-arcs", "-ftest-coverage", "-fpsl"])
elab_flags = ["-Wl,-lgcov"]
elab_flags.append("-Wl,--coverage");
elab_flags.append("-Wl,-no-pie");
elab_flags.append("-fpsl");
ui.set_sim_option("ghdl.elab_flags",elab_flags)
elab_flags.append("-Wl,--coverage")
elab_flags.append("-Wl,-no-pie")
elab_flags.append("-fpsl")
ui.set_sim_option("ghdl.elab_flags", elab_flags)
# Global simulation flags
sim_flags = get_common_sim_flags()
......
......@@ -68,7 +68,7 @@ class FeatureTests(TestsBase):
psl_opts = self.create_psl_cov_file_opt(name)
tb.add_config(name, generics=generics, sim_options=psl_opts)
else:
tb.add_config(name, generics=generics)
tb.add_config(name, generics=generics)
self.add_modelsim_gui_file(tb, default, 'feature', init_files)
return self._check_for_unconfigured()
......
......@@ -7,6 +7,8 @@ from os.path import join, abspath
from pathlib import Path
import json
from yattag import Doc
from typing import Tuple
test_dir = Path(Path(abspath(__file__)).parent).parent
build_dir = os.path.join(str(test_dir.absolute()), "build")
......@@ -19,16 +21,14 @@ dut_top = " "
log = logging.getLogger(__name__)
def merge_psl_coverage_files(out_file, in_file_prefix):
def merge_psl_coverage_files(out_file: str, in_file_prefix: str) -> None:
"""
Merge PSL coverage details from multiple files to single file
"""
if(out_file.startswith(in_file_prefix)):
log.error("File name for merging should not have the same prefix as merged files")
system.exit(0)
if (out_file.startswith(in_file_prefix)):
raise ValueError("File name for merging should not have the same prefix as merged files")
json_out_path = os.path.join(func_cov_dir, out_file)
json_out_file = open(json_out_path, 'w')
json_out_list = []
for filename in os.listdir(psl_dir):
if (not (filename.startswith(in_file_prefix) and \
......@@ -37,8 +37,8 @@ def merge_psl_coverage_files(out_file, in_file_prefix):
in_filename = os.path.join(psl_dir, filename)
print("Merging JSON PSL coverage from: {}\n".format(in_filename))
json_in_file = open(in_filename, 'r')
json_obj = json.load(json_in_file)
with open(in_filename, 'r') as json_in_file:
json_obj = json.load(json_in_file)
# Add test name to each PSL point
for psl_point in json_obj["details"]:
......@@ -46,9 +46,8 @@ def merge_psl_coverage_files(out_file, in_file_prefix):
json_out_list.extend(json_obj["details"])
json_str = json.dumps(json_out_list, indent=1)
json_out_file.write(json_str)
json_out_file.close()
with open(json_out_path, 'w') as json_out_file:
json.dump(json_out_list, json_out_file, indent=1)
def collapse_psl_coverage_files(non_collapsed):
......@@ -112,7 +111,7 @@ def collapse_psl_coverage_files(non_collapsed):
return collapsed
def get_collapsed_file_name(psl_point):
def get_collapsed_file_name(psl_point) -> str:
"""
Create unique file name for collapsed PSL points
"""
......@@ -126,7 +125,7 @@ def get_collapsed_file_name(psl_point):
return file_name
def load_json_psl_coverage(filename):
def load_json_psl_coverage(filename: str):
"""
Load PSL Coverage JSON file to JSON object.
"""
......@@ -134,8 +133,8 @@ def load_json_psl_coverage(filename):
# Read JSON string from file
log.info("Loading JSON PSL output: {}".format(psl_cov_path))
json_file = open(psl_cov_path, 'r')
return json.load(json_file)
with open(psl_cov_path, 'r') as json_file:
return json.load(json_file)
def split_json_coverage_by_file(json):
......@@ -167,7 +166,7 @@ def add_html_table_header(doc, tag, text, headers, back_color="White"):
text(header)
def calc_coverage_results(psl_points, psl_type):
def calc_coverage_results(psl_points, psl_type) -> Tuple[int,int]:
"""
Calculate coverage results from list of PSL points in JSON format.
"""
......@@ -181,16 +180,15 @@ def calc_coverage_results(psl_points, psl_type):
ok += 1
else:
nok +=1
return [ok, nok]
return ok, nok
def calc_coverage_color(coverage):
def calc_coverage_color(coverage: float) -> str:
"""
Return color based on coverage result.
"""
if (coverage < 0 or coverage > 100):
log("Invalid coverage input should be between 0 - 100 %")
return
raise ValueError("Invalid coverage input should be between 0 - 100 %")
if (coverage > 90):
return "Lime"
......@@ -202,10 +200,10 @@ def calc_coverage_color(coverage):
return "Red"
def print_cov_cell_percentage(doc, tag, text, psl_points, coverage_type, merge_abs_vals):
def print_cov_cell_percentage(doc, tag, text, psl_points, coverage_type, merge_abs_vals) -> None:
"""
"""
[ok, nok] = calc_coverage_results(psl_points, coverage_type)
ok, nok = calc_coverage_results(psl_points, coverage_type)
summ = max(1, ok + nok)
percents = ok/summ * 100
color = calc_coverage_color(percents)
......@@ -228,8 +226,8 @@ def print_cov_cell_percentage(doc, tag, text, psl_points, coverage_type, merge_a
text("{}%".format(percents))
else:
with tag('td', bgcolor="Silver"):
text("NA")
text("NA")
def add_psl_html_header(doc, tag, text, filename, psl_points):
"""
......@@ -267,7 +265,7 @@ def add_non_colapsed_psl_table_entry(doc, tag, text, psl_point, def_bg_color="Wh
"""
with tag('td'):
text(psl_point["name"].split(".")[-1])
text(psl_point["name"].split(".")[-1])
with tag('td'):
text(psl_point["test"])
with tag('td', width="50%", style="word-break:break-all;"):
......@@ -335,14 +333,13 @@ def add_psl_table_entry(doc, tag, text, psl_point, def_bg_color="White"):
create_psl_file_page(file_name, psl_point["colapsed_points"]);
def create_psl_file_page(filename, psl_points):
def create_psl_file_page(filename: str, psl_points):
"""
Create HTML file with list of PSL coverage statements.
"""
parsed_file_name = os.path.basename(filename)
html_cov_path = os.path.join(html_dir,
"{}.html".format(parsed_file_name))
html_file = open(html_cov_path, 'w')
doc, tag, text = Doc().tagtext()
......@@ -364,8 +361,8 @@ def create_psl_file_page(filename, psl_points):
if (psl_point["directive"] == psl_type["type"]):
add_psl_table_entry(doc, tag, text, psl_point)
html_file.write(doc.getvalue())
html_file.close()
with open(html_cov_path, 'w', encoding='utf-8') as html_file:
html_file.write(doc.getvalue())
def create_psl_file_refs_table(doc, tag, text, psl_by_files):
......@@ -395,7 +392,6 @@ def create_psl_report(psl_by_files, psl_orig):
create_psl_file_page(file_name, psl_list)
html_rep_path = os.path.join(func_cov_dir, "functional_coverage_report.html")
html_file = open(html_rep_path, 'w')
doc, tag, text = Doc().tagtext()
......@@ -408,8 +404,8 @@ def create_psl_report(psl_by_files, psl_orig):
add_html_table_header(doc, tag, text, header, back_color="Peru")
create_psl_file_refs_table(doc, tag, text, psl_by_files)
html_file.write(doc.getvalue())
html_file.close()
with open(html_rep_path, 'w', encoding='utf-8') as html_file:
html_file.write(doc.getvalue())
if __name__ == "__main__":
......
......@@ -89,7 +89,7 @@ class SanityTests(TestsBase):
psl_opts = self.create_psl_cov_file_opt(name)
tb.add_config(name, generics=generics, sim_options=psl_opts)
else:
tb.add_config(name, generics=generics)
tb.add_config(name, generics=generics)
self.add_modelsim_gui_file(tb, default, 'sanity')
return True
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment