~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to python/mozbuild/mozbuild/test/compilation/test_warnings.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This Source Code Form is subject to the terms of the Mozilla Public
2
 
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
3
 
# You can obtain one at http://mozilla.org/MPL/2.0/.
4
 
 
5
 
import os.path
6
 
import unittest
7
 
 
8
 
from tempfile import NamedTemporaryFile
9
 
 
10
 
from mozbuild.compilation.warnings import CompilerWarning
11
 
from mozbuild.compilation.warnings import WarningsCollector
12
 
from mozbuild.compilation.warnings import WarningsDatabase
13
 
 
14
 
 
15
 
CLANG_TESTS = [
16
 
    ('foobar.cpp:123:10: warning: you messed up [-Wfoo]',
17
 
     'foobar.cpp', 123, 10, 'you messed up', '-Wfoo')
18
 
]
19
 
 
20
 
MSVC_TESTS = [
21
 
    ("C:/mozilla-central/test/foo.cpp(793) : warning C4244: 'return' : "
22
 
     "conversion from 'double' to 'uint32_t', possible loss of data",
23
 
     'C:/mozilla-central/test/foo.cpp', 793, 'C4244',
24
 
     "'return' : conversion from 'double' to 'uint32_t', possible loss of "
25
 
         'data')
26
 
]
27
 
 
28
 
CURRENT_LINE = 1
29
 
 
30
 
def get_warning():
31
 
    global CURRENT_LINE
32
 
 
33
 
    w = CompilerWarning()
34
 
    w['filename'] = '/foo/bar/baz.cpp'
35
 
    w['line'] = CURRENT_LINE
36
 
    w['column'] = 12
37
 
    w['message'] = 'This is irrelevant'
38
 
 
39
 
    CURRENT_LINE += 1
40
 
 
41
 
    return w
42
 
 
43
 
class TestCompilerWarning(unittest.TestCase):
44
 
    def test_equivalence(self):
45
 
        w1 = CompilerWarning()
46
 
        w2 = CompilerWarning()
47
 
 
48
 
        s = set()
49
 
 
50
 
        # Empty warnings should be equal.
51
 
        self.assertEqual(w1, w2)
52
 
 
53
 
        s.add(w1)
54
 
        s.add(w2)
55
 
 
56
 
        self.assertEqual(len(s), 1)
57
 
 
58
 
        w1['filename'] = '/foo.c'
59
 
        w2['filename'] = '/bar.c'
60
 
 
61
 
        self.assertNotEqual(w1, w2)
62
 
 
63
 
        s = set()
64
 
        s.add(w1)
65
 
        s.add(w2)
66
 
 
67
 
        self.assertEqual(len(s), 2)
68
 
 
69
 
        w1['filename'] = '/foo.c'
70
 
        w1['line'] = 5
71
 
        w2['line'] = 5
72
 
 
73
 
        w2['filename'] = '/foo.c'
74
 
        w1['column'] = 3
75
 
        w2['column'] = 3
76
 
 
77
 
        self.assertEqual(w1, w2)
78
 
 
79
 
class TestWarningsParsing(unittest.TestCase):
80
 
    def test_clang_parsing(self):
81
 
        for source, filename, line, column, message, flag in CLANG_TESTS:
82
 
            collector = WarningsCollector(resolve_files=False)
83
 
            warning = collector.process_line(source)
84
 
 
85
 
            self.assertIsNotNone(warning)
86
 
 
87
 
            self.assertEqual(warning['filename'], filename)
88
 
            self.assertEqual(warning['line'], line)
89
 
            self.assertEqual(warning['column'], column)
90
 
            self.assertEqual(warning['message'], message)
91
 
            self.assertEqual(warning['flag'], flag)
92
 
 
93
 
    def test_msvc_parsing(self):
94
 
        for source, filename, line, flag, message in MSVC_TESTS:
95
 
            collector = WarningsCollector(resolve_files=False)
96
 
            warning = collector.process_line(source)
97
 
 
98
 
            self.assertIsNotNone(warning)
99
 
 
100
 
            self.assertEqual(warning['filename'], filename)
101
 
            self.assertEqual(warning['line'], line)
102
 
            self.assertEqual(warning['flag'], flag)
103
 
            self.assertEqual(warning['message'], message)
104
 
 
105
 
class TestWarningsDatabase(unittest.TestCase):
106
 
    def test_basic(self):
107
 
        db = WarningsDatabase()
108
 
 
109
 
        self.assertEqual(len(db), 0)
110
 
 
111
 
        for i in xrange(10):
112
 
            db.insert(get_warning(), compute_hash=False)
113
 
 
114
 
        self.assertEqual(len(db), 10)
115
 
 
116
 
        warnings = list(db)
117
 
        self.assertEqual(len(warnings), 10)
118
 
 
119
 
    def test_hashing(self):
120
 
        """Ensure that hashing files on insert works."""
121
 
        db = WarningsDatabase()
122
 
 
123
 
        temp = NamedTemporaryFile()
124
 
        temp.write('x' * 100)
125
 
        temp.flush()
126
 
 
127
 
        w = CompilerWarning()
128
 
        w['filename'] = temp.name
129
 
        w['line'] = 1
130
 
        w['column'] = 4
131
 
        w['message'] = 'foo bar'
132
 
 
133
 
        # Should not throw.
134
 
        db.insert(w)
135
 
 
136
 
        w['filename'] = 'DOES_NOT_EXIST'
137
 
 
138
 
        with self.assertRaises(Exception):
139
 
            db.insert(w)
140
 
 
141
 
    def test_pruning(self):
142
 
        """Ensure old warnings are removed from database appropriately."""
143
 
        db = WarningsDatabase()
144
 
 
145
 
        source_files = []
146
 
        for i in xrange(1, 21):
147
 
            temp = NamedTemporaryFile()
148
 
            temp.write('x' * (100 * i))
149
 
            temp.flush()
150
 
 
151
 
            # Keep reference so it doesn't get GC'd and deleted.
152
 
            source_files.append(temp)
153
 
 
154
 
            w = CompilerWarning()
155
 
            w['filename'] = temp.name
156
 
            w['line'] = 1
157
 
            w['column'] = i * 10
158
 
            w['message'] = 'irrelevant'
159
 
 
160
 
            db.insert(w)
161
 
 
162
 
        self.assertEqual(len(db), 20)
163
 
 
164
 
        # If we change a source file, inserting a new warning should nuke the
165
 
        # old one.
166
 
        source_files[0].write('extra')
167
 
        source_files[0].flush()
168
 
 
169
 
        w = CompilerWarning()
170
 
        w['filename'] = source_files[0].name
171
 
        w['line'] = 1
172
 
        w['column'] = 50
173
 
        w['message'] = 'replaced'
174
 
 
175
 
        db.insert(w)
176
 
 
177
 
        self.assertEqual(len(db), 20)
178
 
 
179
 
        warnings = list(db.warnings_for_file(source_files[0].name))
180
 
        self.assertEqual(len(warnings), 1)
181
 
        self.assertEqual(warnings[0]['column'], w['column'])
182
 
 
183
 
        # If we delete the source file, calling prune should call the warnings
184
 
        # to go away.
185
 
        old_filename = source_files[0].name
186
 
        del source_files[0]
187
 
 
188
 
        self.assertFalse(os.path.exists(old_filename))
189
 
 
190
 
        db.prune()
191
 
        self.assertEqual(len(db), 19)