Passed
Pull Request — master (#202)
by Jan
03:23
created

tests_oval_graph.test_tools.TestTools.find_files()   A

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 4
nop 2
1
import json
2
import os
3
import re
4
import tempfile
5
import uuid
6
from pathlib import Path
7
8
import pytest
9
10
TOP_PATH = Path(__file__).parent
11
12
13
class TestTools():
14
    @staticmethod
15
    def find_files(file_name, search_path):
16
        result = []
17
        # root, directory, files
18
        for root, _, files in os.walk(search_path):
19
            for filename in files:
20
                if file_name in filename:
21
                    result.append(os.path.abspath(os.path.join(root, filename)))
22
        return result
23
24
    @staticmethod
25
    def get_text_file(src):
26
        path = TOP_PATH / src
27
        with open(path, 'r') as data:
28
            return data.readlines()
29
30
    @staticmethod
31
    def get_random_dir_in_tmp():
32
        return os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
33
34 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
    def compare_results_html(result):
36
        result_ = TestTools.get_text_file(result)
37
        reference_pattern = TestTools.get_text_file(
38
            'test_commands/test_data/referenc_pattern_html_report.txt')
39
        prefix_start = '<script>var data_of_tree = '
40
        prefix_end = ';</script><div>\n'
41
        data_in_html = ""
42
        matched = False
43
        for row in result_:
44
            if prefix_start in row and prefix_end in row:
45
                matched = True
46
                data_in_html = row
47
        assert matched
48
49
        tmp_json_str = data_in_html.replace(prefix_start, '').replace(prefix_end, '')
50
        tmp_json = json.loads(tmp_json_str)
51
        data_in_html = prefix_start + json.dumps(tmp_json, indent=4, sort_keys=False) + prefix_end
52
53
        count_row = 0
54
        rule_name = 'xccdforgssgprojectcontentrulepackageabrtremoved'
55
        for row in reference_pattern:
56
            if row in data_in_html or rule_name in row:
57
                count_row += 1
58
        assert count_row == len(reference_pattern)
59
60
    @staticmethod
61
    def get_data_json(src):
62
        path = TOP_PATH / src
63
        with open(path, 'r') as data:
64
            return json.load(data)
65
66
    @staticmethod
67
    def compare_results_json(result):
68
        result = TestTools.get_data_json(result)
69
        reference_result = TestTools.get_data_json(
70
            'test_commands/test_data/referenc_result_data_json.json')
71
        rule_name = "xccdf_org.ssgproject.content_rule_package_abrt_removed"
72
        result_rule_name = [
73
            x for x in result.keys() if re.search(
74
                rule_name, x)]
75
        assert result[result_rule_name[0]] == reference_result[rule_name]
76
77
    @staticmethod
78
    def find_all_in_string(regex, count, string):
79
        assert len(re.findall(regex, string)) == count
80
81
    @staticmethod
82
    def get_questions_not_selected(capsys, client, result):
83
        out = client.get_questions()[0].choices
84
        assert out == result
85
        captured = capsys.readouterr()
86
        regex = r'rule_package_\w+_removed +\(Not selected\)'
87
        TestTools.find_all_in_string(regex, 6, captured.out)
88
89
    @staticmethod
90
    def get_questions_with_option_show_failed_rules(client):
91
        out = client.get_questions()[0].choices
92
        rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
93
        assert out[0] == rule1
94
        with pytest.raises(Exception, match="list index out of range"):
95
            assert out[2] is None
96