Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #215 from Honny1/fix-details-tests
Browse files Browse the repository at this point in the history
Fix details
  • Loading branch information
Honny1 authored Nov 23, 2021
2 parents 76d40e0 + f8db143 commit 7cb5d42
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 71 deletions.
2 changes: 1 addition & 1 deletion oval_graph/arf_xml_parser/_comments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .global_namespaces import namespaces


class _Comments:
class _Comments: # pylint: disable=R0903
def __init__(self, oval_definitions):
self.oval_definitions = oval_definitions

Expand Down
2 changes: 1 addition & 1 deletion oval_graph/arf_xml_parser/_oval_scan_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
STR_NEGATE_BOOL = {'true': 'false', 'false': 'true'}


class _OVALScanDefinitions:
class _OVALScanDefinitions: # pylint: disable=R0903
def __init__(self, definitions, oval_definitions, report_data):
self.definitions = definitions
self.comments_parser = _Comments(oval_definitions)
Expand Down
4 changes: 2 additions & 2 deletions oval_graph/arf_xml_parser/_test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
MAX_MESSAGE_LEN = 99


class _TestInfo:
class _TestInfo: # pylint: disable=R0903
def __init__(self, report_data):
self.report_data = report_data
self.oval_definitions = self._get_oval_definitions()
Expand Down Expand Up @@ -124,7 +124,7 @@ def _get_ref_var(self, element, collected_object):
@staticmethod
def _complete_message(item, var_id):
if len(item.text) == MAX_MESSAGE_LEN and var_id[:item.text.find('(')] in var_id:
return "{}{})".format(item.text[:item.text.find('(') + 1], var_id)
return f"{item.text[:item.text.find('(') + 1]}{var_id})"
return item.text

@staticmethod
Expand Down
15 changes: 7 additions & 8 deletions oval_graph/arf_xml_parser/arf_xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ def __init__(self, src):
if not self.validate(self.arf_schemas_path):
start_red_color = '\033[91m'
end_red_color = '\033[0m'
message = "{}Warning: This file is not valid arf report.{}".format(
start_red_color, end_red_color)
print(message, file=sys.stderr)
message = "Warning: This file is not valid arf report."
print(f"{start_red_color}{message}{end_red_color}", file=sys.stderr)
try:
self.used_rules, self.not_tested_rules = self._get_rules_in_profile()
self.report_data_href = list(self.used_rules.values())[0]['href']
Expand All @@ -37,8 +36,8 @@ def __init__(self, src):
self.definitions, self.oval_definitions, self.report_data).get_scan()
except BaseException as error:
raise ValueError(
'This file "{}" is not arf report file or there are no results'.format(
self.src)) from error
f'This file "{self.src}" is not arf report file or there are no results'
) from error

def validate(self, xsd_path):
xsd_path = str(LOCAL_DATA_DIR / xsd_path)
Expand Down Expand Up @@ -105,9 +104,9 @@ def _get_definition_of_rule(self, rule_id):

if rule_id in self.not_tested_rules:
raise NotTestedRule(
'Rule "{}" is {}, so there are no results.'
.format(rule_id, self.not_tested_rules[rule_id]))
raise ValueError('404 rule "{}" not found!'.format(rule_id))
f'Rule "{rule_id}" is {self.not_tested_rules[rule_id]}, so there are no results.'
)
raise ValueError(f'404 rule "{rule_id}" not found!')

def get_oval_tree(self, rule_id):
return Builder.dict_of_rule_to_oval_tree(
Expand Down
2 changes: 1 addition & 1 deletion oval_graph/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def catch_errors(client_class, params):
except ERRORS as error:
if client.verbose:
traceback.print_exc()
print('{}Error: {}{}'.format(C_RED, error, C_END))
print(f'{C_RED}Error: {error}{C_END}')


def main(client):
Expand Down
6 changes: 3 additions & 3 deletions oval_graph/command_line_client/arf_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def file_is_empty(path):

def save_dict_as_json(self, dict_, src):
if os.path.isfile(src) and not self.file_is_empty(src):
with open(src, "r") as file_:
with open(src, "r", encoding="utf-8") as file_:
data = json.load(file_)
dict_.update(data)
with open(src, "w+") as file_:
with open(src, "w+", encoding="utf-8") as file_:
json.dump(dict_, file_)

def _get_rule_key(self, rule):
Expand All @@ -41,7 +41,7 @@ def _get_rule_key(self, rule):
def prepare_data(self, rules):
out = []
rule = None
out_oval_tree_dict = dict()
out_oval_tree_dict = {}
for rule in rules['rules']:
try:
out_oval_tree_dict[self._get_rule_key(rule)] = self.create_dict_of_rule(rule)
Expand Down
9 changes: 5 additions & 4 deletions oval_graph/command_line_client/client_arf_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ def search_rules_id(self):
wanted_rules = self._get_wanted_rules(self.arf_xml_parser.used_rules.keys())
not_tested_rule = self.get_matched_not_tested_rules()
if not wanted_rules and not not_tested_rule:
raise ValueError('404 rule "{}" not found!'.format(self.rule_name))
raise ValueError(f'404 rule "{self.rule_name}" not found!')
if self.rule_name in not_tested_rule:
raise NotTestedRule(
'Rule "{}" is {}, so there are no results.'
.format(self.rule_name, self.arf_xml_parser.not_tested_rules[self.rule_name]))
raise NotTestedRule((
f'Rule "{self.rule_name}" '
f'is {self.arf_xml_parser.not_tested_rules[self.rule_name]},'
' so there are no results.'))
return wanted_rules
8 changes: 5 additions & 3 deletions oval_graph/command_line_client/client_html_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _put_to_dict_oval_trees(self, dict_oval_trees, rule):
raise NotImplementedError

def _prepare_data(self, rules):
dict_oval_trees = dict()
dict_oval_trees = {}
paths_to_generated_rules = []
if len(rules['rules']) == 1:
self.selected_only_one_rule = True
Expand All @@ -50,7 +50,7 @@ def _prepare_data(self, rules):
except NotTestedRule as error:
start_red_color = '\033[91m'
end_red_color = '\033[0m'
message = '{}{}{}'.format(start_red_color, str(error), end_red_color)
message = f'{start_red_color}{str(error)}{end_red_color}'
raise NotTestedRule(message) from error
if self.all_in_one:
path = self.get_save_src('rules' + self._get_date())
Expand All @@ -63,7 +63,7 @@ def _get_src_for_one_graph(self, rule):

@staticmethod
def get_file_name(rule):
return "{}{}.html".format(START_OF_FILE_NAME, rule)
return f"{START_OF_FILE_NAME}{rule}.html"

def get_save_src(self, rule):
if self.out is not None:
Expand Down Expand Up @@ -103,12 +103,14 @@ def _open_web_browser(self, path_to_result):
is_firefox_installed = self._is_firefox_installed()
if is_firefox_installed:
command = ["firefox", path_to_result]
# pylint: disable=bad-option-value,R1732
browser = Popen(command, stdout=PIPE, stderr=PIPE)
self.web_browsers.append(browser)
time.sleep(0.2)
else:
default_web_browser_name = webbrowser.get().name
command = [default_web_browser_name, path_to_result]
# pylint: disable=bad-option-value,R1732
browser = Popen(command, stdout=PIPE, stderr=PIPE)
self.web_browsers.append(browser)
time.sleep(0.2)
Expand Down
7 changes: 3 additions & 4 deletions oval_graph/command_line_client/client_json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ def _get_rows_of_unselected_rules(self):
raise NotImplementedError

def get_json_data_file(self):
with open(self.source_filename, 'r') as file_:
with open(self.source_filename, 'r', encoding="utf-8") as file_:
try:
return json.load(file_)
except json.JSONDecodeError as error:
raise ValueError(
'Used file "{}" is not valid json.'.format(
self.source_filename)) from error
f'Used file "{self.source_filename}" is not valid json.') from error

def search_rules_id(self):
rules = self._get_wanted_rules(self.json_data_file.keys())
if not rules:
raise ValueError('404 rule "{}" not found!'.format(self.rule_name))
raise ValueError(f'404 rule "{self.rule_name}" not found!')
return rules
10 changes: 5 additions & 5 deletions oval_graph/html_builder/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _get_data_of_graphs_in_js(self, dict_of_rules):
json_of_graphs = {self._remove_unfit_chars(key): value
for key, value in dict_of_rules.items()}
data = str(json.dumps(json_of_graphs))
return "var data_of_tree = {};".format(data)
return f"var data_of_tree = {data};"

def _get_titles_and_places_for_graph(self, dict_of_rules):
rules_html = E.div({'id': 'graphs'})
Expand All @@ -104,15 +104,15 @@ def _get_titles_and_places_for_graph(self, dict_of_rules):
@staticmethod
def _get_part(part):
out = ''
with open(LOCAL_DATA_DIR / part, "r") as data_file:
with open(LOCAL_DATA_DIR / part, "r", encoding="utf-8") as data_file:
out = ''.join(data_file.readlines())
return out

@staticmethod
def print_output_message(src, rules):
if len(rules) > 1:
rule_names = "\n" + "\n".join(rules)
print('Rules "{}" done!'.format(rule_names), file=sys.stderr)
print(f'Rules "{rule_names}" done!', file=sys.stderr)
else:
print('Rule "{}" done!'.format(rules.pop()), file=sys.stderr)
print('Result is saved:"{}"'.format(src), file=sys.stderr)
print(f'Rule "{rules.pop()}" done!', file=sys.stderr)
print(f'Result is saved:"{src}"', file=sys.stderr)
9 changes: 3 additions & 6 deletions oval_graph/oval_tree/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _get_node_style(self):
def _get_negation_character(value):
return (
'<strong>'
'<span class="' + VALUE_TO_BOOTSTRAP_COLOR[value] + '">NOT</strong>'
f'<span class="{VALUE_TO_BOOTSTRAP_COLOR[value]}">NOT</strong>'
'</span>'
)

Expand Down Expand Up @@ -141,11 +141,8 @@ def to_js_tree_dict(self, hide_passing_tests=False):
label = self._get_label()
if self.tree.test_result_details:
self.tree.test_result_details['result'] = (
' <span class="label {color_tag}">{result}</span>'
.format(
color_tag=BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
result=self.result,
))
f' <span class="label {BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons["color"]]}">'
f'{self.result}</span>')
out = {'text':
'{negation} <strong><span class="{icon}">{label}</span></strong>'
' <span class="label {color_tag}">{tag}</span>'
Expand Down
3 changes: 1 addition & 2 deletions oval_graph/parts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ $("#close").click(function () {
var data = JSON.parse(JSON.stringify(data_of_tree));

jQuery.each(data, function (rule, data) {
rule_id = "#" + rule.replace(/[\_\-\.]/g, "");
var rule_id = "#" + rule.replace(/[\_\-\.]/g, "");
show_graph(rule_id, data);
});

function search() {
var input = document.getElementById("Search");
var filter = input.value.toLowerCase();
var nodes = document.getElementsByClassName('target');
Array.from(document.getElementsByClassName('target')).forEach(function (node) {
if (node.children.item(0).innerText.toLowerCase().includes(filter)) {
node.style.display = "block";
Expand Down
8 changes: 2 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@


def get_long_description():
try:
with open('README.md', encoding='utf8') as fd:
return fd.read()
except TypeError:
with open('README.md') as fd:
return fd.read()
with open('README.md', encoding='utf8') as readme:
return readme.read()


setup(name='oval_graph',
Expand Down
4 changes: 2 additions & 2 deletions test_commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ tmp_json_file_src="${tmp_dir_src}/data.json"
test_result=0
overall_test_result=0

test_if_is_instaled_oval_graph() {
test_if_is_installed_oval_graph() {
rpm -q $name >/dev/null 2>&1
is_rpm_installed=$?
pip list | grep -F $name
Expand Down Expand Up @@ -258,7 +258,7 @@ if [ "$_arg_install_oval_graph" = "on" ]; then
install_package_from_source
fi

test_if_is_instaled_oval_graph
test_if_is_installed_oval_graph
clean "${tmp_json_file_src}"
help_tests
bad_args_tests
Expand Down
4 changes: 2 additions & 2 deletions tests_oval_graph/test_commands/test_command_arf_to_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import subprocess
from pathlib import Path
from re import search

import pytest

Expand Down Expand Up @@ -33,7 +33,7 @@ def test_command_arf_to_graph_with_verbose():
stderr=subprocess.STDOUT)
# Reads path to file from verbose output
src_regex = r"\"(.*?)\"$"
src = re.search(src_regex, out.decode('utf-8')).group(1)
src = search(src_regex, out.decode('utf-8')).group(1)
file_src = Path(__file__).parent.parent.parent / src
TestTools.compare_results_html(file_src)

Expand Down
10 changes: 5 additions & 5 deletions tests_oval_graph/test_commands/test_command_arf_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

def run_commad_and_save_output_to_file(parameters):
path = str(TestTools.get_random_path_in_tmp()) + '.json'
with open(path, 'w+') as output:
with open(path, 'w+', encoding="utf-8") as output:
subprocess.check_call(parameters, stdout=output)
return path


def test_command_arf_to_json():
path = str(TestTools.get_random_path_in_tmp()) + '.json'
out = subprocess.check_output(ARF_TO_JSON)
with open(path, "w+") as data:
with open(path, "w+", encoding="utf-8") as data:
data.writelines(out.decode('utf-8'))
TestTools.compare_results_json(path)

Expand All @@ -34,7 +34,7 @@ def test_command_parameter_all():
".",
]
src = run_commad_and_save_output_to_file(command)
with open(src, "r") as data:
with open(src, "r", encoding="utf-8") as data:
rules = json.load(data)
assert len(rules.keys()) == 184

Expand All @@ -48,7 +48,7 @@ def test_command_parameter_all_and_show_failed_rules():
r'_package_\w+_removed'
]
src = run_commad_and_save_output_to_file(command)
with open(src, "r") as data:
with open(src, "r", encoding="utf-8") as data:
rules = json.load(data)
assert len(rules.keys()) == 1

Expand All @@ -67,6 +67,6 @@ def test_command_with_parameter_out():
command.append('-o' + src)
subprocess.check_call(command)

with open(src, "r") as data:
with open(src, "r", encoding="utf-8") as data:
rules = json.load(data)
assert len(rules.keys()) == 4
8 changes: 4 additions & 4 deletions tests_oval_graph/test_commands/test_command_json_to_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_command_json_to_graph():
path = str(TestTools.get_random_path_in_tmp()) + '.json'
out = subprocess.check_output(ARF_TO_JSON)

with open(path, "w+") as output:
with open(path, "w+", encoding="utf-8") as output:
output.writelines(out.decode('utf-8'))

command = [*COMMAND_START,
Expand All @@ -34,7 +34,7 @@ def test_command_json_to_graph():
def test_command_json_to_graph_with_verbose():
path = str(TestTools.get_random_path_in_tmp()) + '.json'
out = subprocess.check_output(ARF_TO_JSON)
with open(path, "w+") as output:
with open(path, "w+", encoding="utf-8") as output:
output.writelines(out.decode('utf-8'))

command = [*COMMAND_START,
Expand All @@ -61,10 +61,10 @@ def test_command_parameter_all():
TEST_ARF_XML_PATH,
'.'
]
with open(path, 'w+') as output:
with open(path, 'w+', encoding="utf-8") as output:
subprocess.check_call(command, stdout=output)

with open(path, "r") as data:
with open(path, "r", encoding="utf-8") as data:
rules = json.load(data)
assert len(rules.keys()) == 184
out_path = TestTools.get_random_path_in_tmp()
Expand Down
Loading

0 comments on commit 7cb5d42

Please sign in to comment.