WrongArgsBear   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A create_arguments() 0 3 1
1
import os
2
import sys
3
import json
4
import unittest
5
6
from coalib.bearlib.abstractions.ExternalBearWrap import external_bear_wrap
7
from coalib.results.Diff import Diff
8
from coalib.results.Result import Result
9
from coalib.settings.Section import Section
10
from coalib.results.SourceRange import SourceRange
11
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
12
from coalib.settings.FunctionMetadata import FunctionMetadata
13
14
15
def get_testfile_path(name):
16
    """
17
    Gets the full path to a testfile inside the same directory.
18
19
    :param name: The filename of the testfile to get the full path for.
20
    :return:     The full path to given testfile name.
21
    """
22
    return os.path.join(os.path.dirname(os.path.realpath(__file__)),
23
                        name)
24
25
26
class ExternalBearWrapComponentTest(unittest.TestCase):
27
28
    class Dummy:
29
        pass
30
31
    class TestBear:
32
33
        @staticmethod
34
        def create_arguments():
35
            return (os.path.join(
36
                os.path.dirname(__file__),
37
                "test_external_bear.py"),)
38
39
    class WrongArgsBear:
40
41
        @staticmethod
42
        def create_arguments():
43
            return 1
44
45
    def setUp(self):
46
        self.section = Section("TEST_SECTION")
47
48
        self.test_program_path = get_testfile_path("test_external_bear.py")
49
50
        self.testfile_path = get_testfile_path("test_file.txt")
51
        with open(self.testfile_path, mode="r") as fl:
52
            self.testfile_content = fl.read().splitlines(keepends=True)
53
54
    def test_decorator_invalid_parameters(self):
55
        with self.assertRaises(ValueError) as cm:
56
            external_bear_wrap("exec", invalid_arg=88)
57
        self.assertEqual(
58
            str(cm.exception),
59
            "Invalid keyword arguments provided: 'invalid_arg'")
60
61
    def test_decorator_invalid_parameter_types(self):
62
        # Provide some invalid severity maps.
63
        with self.assertRaises(TypeError):
64
            external_bear_wrap(executable=1337)
65
66
    def test_get_executable(self):
67
        uut = (external_bear_wrap("exec")(self.TestBear))
68
        self.assertEqual(uut.get_executable(), "exec")
69
70
    def test_create_arguments_fail(self):
71
        uut = (external_bear_wrap("exec")(self.Dummy))
72
        self.assertEqual(uut.create_arguments(), ())
73
74
    def test_create_arguments_non_iterable(self):
75
        uut = (external_bear_wrap("exec")
76
               (self.WrongArgsBear)
77
               (self.section, None))
78
        with self.assertRaises(TypeError):
79
            res = list(uut.run(self.testfile_path, self.testfile_content))
80
81
    def test_invalid_output(self):
82
        broken_json = json.dumps([{'broken': "JSON"}])[:-1]
83
        uut = (external_bear_wrap("exec")(self.Dummy)(self.section, None))
84
        with self.assertRaises(ValueError):
85
            # Something needs to be done with the result otherwise
86
            # parse_output will not yield and thus will not raise the ValueError
87
            list(uut.parse_output(broken_json, "some_file"))
88
89
    def test_setting_desc(self):
90
        uut = (external_bear_wrap("exec",
91
                                  settings={
92
                                     "asetting": ("", bool),
93
                                     "bsetting": ("", bool, True),
94
                                     "csetting": ("My desc.", bool, False),
95
                                     "dsetting": ("Another desc", bool),
96
                                     "esetting": ("", int, None)
97
                                     })(self.Dummy))
98
        metadata = uut.get_metadata()
99
        self.assertEqual(metadata.non_optional_params["asetting"][0],
100
                         FunctionMetadata.str_nodesc)
101
        self.assertEqual(metadata.optional_params["bsetting"][0],
102
                         FunctionMetadata.str_nodesc + " " +
103
                         FunctionMetadata.str_optional.format(True))
104
        self.assertEqual(metadata.optional_params["csetting"][0], "My desc." +
105
                         " " + FunctionMetadata.str_optional.format(False))
106
        self.assertEqual(metadata.non_optional_params["dsetting"][0],
107
                         "Another desc")
108
        self.assertEqual(metadata.optional_params["esetting"][0],
109
                         FunctionMetadata.str_nodesc + " " +
110
                         FunctionMetadata.str_optional.format(None))
111
112
    def test_optional_settings(self):
113
        uut = (external_bear_wrap(sys.executable, settings={
114
            "set_normal_severity": ("", bool),
115
            "set_sample_dbg_msg": ("", bool, False),
116
            "not_set_different_msg": ("", bool, True)})
117
               (self.TestBear)
118
               (self.section, None))
119
        results = list(uut.run(self.testfile_path, self.testfile_content,
120
                               set_normal_severity=False))
121
        expected = [
122
            Result(
123
                origin=uut,
124
                message="This is wrong",
125
                affected_code=(SourceRange.from_values(self.testfile_path, 1),),
126
                severity=RESULT_SEVERITY.MAJOR
127
                ),
128
            Result(
129
                origin=uut,
130
                message="This is wrong too",
131
                affected_code=(SourceRange.from_values(self.testfile_path, 3),),
132
                severity=RESULT_SEVERITY.INFO)]
133
        self.assertEqual(results, expected)
134
135
        results = list(uut.run(self.testfile_path, self.testfile_content,
136
                               set_normal_severity=True))
137
        expected = [
138
            Result(
139
                origin=uut,
140
                message="This is wrong",
141
                affected_code=(SourceRange.from_values(self.testfile_path, 1),),
142
                severity=RESULT_SEVERITY.NORMAL
143
                ),
144
            Result(
145
                origin=uut,
146
                message="This is wrong too",
147
                affected_code=(SourceRange.from_values(self.testfile_path, 3),),
148
                severity=RESULT_SEVERITY.NORMAL)]
149
        self.assertEqual(results, expected)
150
151
    def test_settings(self):
152
        uut = (external_bear_wrap(sys.executable, settings={
153
            "set_normal_severity": ("", bool),
154
            "set_sample_dbg_msg": ("", bool, False),
155
            "not_set_different_msg": ("", bool, True)})
156
               (self.TestBear)
157
               (self.section, None))
158
        results = list(uut.run(self.testfile_path, self.testfile_content,
159
                               set_normal_severity=False,
160
                               set_sample_dbg_msg=True,
161
                               not_set_different_msg=False))
162
        expected = [
163
            Result(
164
                origin=uut,
165
                message="This is wrong",
166
                affected_code=(SourceRange.from_values(self.testfile_path, 1),),
167
                severity=RESULT_SEVERITY.MAJOR,
168
                debug_msg="Sample debug message"
169
                ),
170
            Result(
171
                origin=uut,
172
                message="Different message",
173
                affected_code=(SourceRange.from_values(self.testfile_path, 3),),
174
                severity=RESULT_SEVERITY.INFO)]
175
        self.assertEqual(results, expected)
176