summaryrefslogtreecommitdiffstats
path: root/tools/scan-build-py/tests/unit/test_runner.py
blob: ea10051d8506f52dbe4713ac2788f345c55c8856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.

import libscanbuild.runner as sut
from . import fixtures
import unittest
import re
import os
import os.path


def run_analyzer(content, opts):
    with fixtures.TempDir() as tmpdir:
        filename = os.path.join(tmpdir, 'test.cpp')
        with open(filename, 'w') as handle:
            handle.write(content)

        opts.update({
            'directory': os.getcwd(),
            'clang': 'clang',
            'file': filename,
            'language': 'c++',
            'analyze': ['--analyze', '-x', 'c++', filename],
            'output': ['-o', tmpdir]})
        spy = fixtures.Spy()
        result = sut.run_analyzer(opts, spy.call)
        return (result, spy.arg)


class RunAnalyzerTest(unittest.TestCase):

    def test_run_analyzer(self):
        content = "int div(int n, int d) { return n / d; }"
        (result, fwds) = run_analyzer(content, dict())
        self.assertEqual(None, fwds)
        self.assertEqual(0, result['exit_code'])

    def test_run_analyzer_crash(self):
        content = "int div(int n, int d) { return n / d }"
        (result, fwds) = run_analyzer(content, dict())
        self.assertEqual(None, fwds)
        self.assertEqual(1, result['exit_code'])

    def test_run_analyzer_crash_and_forwarded(self):
        content = "int div(int n, int d) { return n / d }"
        (_, fwds) = run_analyzer(content, {'output_failures': True})
        self.assertEqual('crash', fwds['error_type'])
        self.assertEqual(1, fwds['exit_code'])
        self.assertTrue(len(fwds['error_output']) > 0)


class SetAnalyzerOutputTest(fixtures.TestCase):

    def test_not_defined(self):
        with fixtures.TempDir() as tmpdir:
            opts = {'output_dir': tmpdir}
            spy = fixtures.Spy()
            sut.set_analyzer_output(opts, spy.call)
            self.assertTrue(os.path.exists(spy.arg['output'][1]))
            self.assertTrue(os.path.isdir(spy.arg['output'][1]))

    def test_html(self):
        with fixtures.TempDir() as tmpdir:
            opts = {'output_dir': tmpdir, 'output_format': 'html'}
            spy = fixtures.Spy()
            sut.set_analyzer_output(opts, spy.call)
            self.assertTrue(os.path.exists(spy.arg['output'][1]))
            self.assertTrue(os.path.isdir(spy.arg['output'][1]))

    def test_plist_html(self):
        with fixtures.TempDir() as tmpdir:
            opts = {'output_dir': tmpdir, 'output_format': 'plist-html'}
            spy = fixtures.Spy()
            sut.set_analyzer_output(opts, spy.call)
            self.assertTrue(os.path.exists(spy.arg['output'][1]))
            self.assertTrue(os.path.isfile(spy.arg['output'][1]))

    def test_plist(self):
        with fixtures.TempDir() as tmpdir:
            opts = {'output_dir': tmpdir, 'output_format': 'plist'}
            spy = fixtures.Spy()
            sut.set_analyzer_output(opts, spy.call)
            self.assertTrue(os.path.exists(spy.arg['output'][1]))
            self.assertTrue(os.path.isfile(spy.arg['output'][1]))


class ReportFailureTest(fixtures.TestCase):

    def assertUnderFailures(self, path):
        self.assertEqual('failures', os.path.basename(os.path.dirname(path)))

    def test_report_failure_create_files(self):
        with fixtures.TempDir() as tmpdir:
            # create input file
            filename = os.path.join(tmpdir, 'test.c')
            with open(filename, 'w') as handle:
                handle.write('int main() { return 0')
            uname_msg = ' '.join(os.uname()) + os.linesep
            error_msg = 'this is my error output'
            # execute test
            opts = {'directory': os.getcwd(),
                    'clang': 'clang',
                    'file': filename,
                    'report': ['-fsyntax-only', '-E', filename],
                    'language': 'c',
                    'output_dir': tmpdir,
                    'error_type': 'other_error',
                    'error_output': error_msg,
                    'exit_code': 13}
            sut.report_failure(opts)
            # verify the result
            result = dict()
            pp_file = None
            for root, _, files in os.walk(tmpdir):
                keys = [os.path.join(root, name) for name in files]
                for key in keys:
                    with open(key, 'r') as handle:
                        result[key] = handle.readlines()
                    if re.match(r'^(.*/)+clang(.*)\.i$', key):
                        pp_file = key

            # prepocessor file generated
            self.assertUnderFailures(pp_file)
            # info file generated and content dumped
            info_file = pp_file + '.info.txt'
            self.assertIn(info_file, result)
            self.assertEqual('Other Error\n', result[info_file][1])
            self.assertEqual(uname_msg, result[info_file][3])
            # error file generated and content dumped
            error_file = pp_file + '.stderr.txt'
            self.assertIn(error_file, result)
            self.assertEqual([error_msg], result[error_file])


class AnalyzerTest(unittest.TestCase):

    def test_set_language(self):
        def test(expected, input):
            spy = fixtures.Spy()
            self.assertEqual(spy.success, sut.language_check(input, spy.call))
            self.assertEqual(expected, spy.arg['language'])

        l = 'language'
        f = 'file'
        i = 'c++'
        test('c',   {f: 'file.c', l: 'c', i: False})
        test('c++', {f: 'file.c', l: 'c++', i: False})
        test('c++', {f: 'file.c', i: True})
        test('c',   {f: 'file.c', i: False})
        test('c++', {f: 'file.cxx', i: False})
        test('c-cpp-output',   {f: 'file.i', i: False})
        test('c++-cpp-output', {f: 'file.i', i: True})
        test('c-cpp-output',   {f: 'f.i', l: 'c-cpp-output', i: True})

    def test_arch_loop(self):
        def test(input):
            spy = fixtures.Spy()
            sut.arch_check(input, spy.call)
            return spy.arg

        input = {'key': 'value'}
        self.assertEqual(input, test(input))

        input = {'archs_seen': ['i386']}
        self.assertEqual({'arch': 'i386'}, test(input))

        input = {'archs_seen': ['ppc']}
        self.assertEqual(None, test(input))

        input = {'archs_seen': ['i386', 'ppc']}
        self.assertEqual({'arch': 'i386'}, test(input))

        input = {'archs_seen': ['i386', 'sparc']}
        result = test(input)
        self.assertTrue(result == {'arch': 'i386'} or
                        result == {'arch': 'sparc'})


@sut.require([])
def method_without_expecteds(opts):
    return 0


@sut.require(['this', 'that'])
def method_with_expecteds(opts):
    return 0


@sut.require([])
def method_exception_from_inside(opts):
    raise Exception('here is one')


class RequireDecoratorTest(unittest.TestCase):

    def test_method_without_expecteds(self):
        self.assertEqual(method_without_expecteds(dict()), 0)
        self.assertEqual(method_without_expecteds({}), 0)
        self.assertEqual(method_without_expecteds({'this': 2}), 0)
        self.assertEqual(method_without_expecteds({'that': 3}), 0)

    def test_method_with_expecteds(self):
        self.assertRaises(KeyError, method_with_expecteds, dict())
        self.assertRaises(KeyError, method_with_expecteds, {})
        self.assertRaises(KeyError, method_with_expecteds, {'this': 2})
        self.assertRaises(KeyError, method_with_expecteds, {'that': 3})
        self.assertEqual(method_with_expecteds({'this': 0, 'that': 3}), 0)

    def test_method_exception_not_caught(self):
        self.assertRaises(Exception, method_exception_from_inside, dict())