/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""These tests are tests about the source code of bzrlib itself.
19
20
They are useful for testing code quality, checking coverage metric etc.
21
"""
22
23
# import system imports here
24
import os
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
25
import re
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
26
import sys
27
28
#import bzrlib specific imports here
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
29
from bzrlib import (
30
    osutils,
31
    )
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
32
import bzrlib.branch
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
33
from bzrlib.tests import TestCase, TestSkipped
34
35
36
class TestSourceHelper(TestCase):
37
38
    def source_file_name(self, package):
39
        """Return the path of the .py file for package."""
40
        path = package.__file__
41
        if path[-1] in 'co':
42
            return path[:-1]
43
        else:
44
            return path
45
46
47
class TestApiUsage(TestSourceHelper):
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
48
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
49
    def find_occurences(self, rule, filename):
50
        """Find the number of occurences of rule in a file."""
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
51
        occurences = 0
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
52
        source = file(filename, 'r')
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
53
        for line in source:
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
54
            if line.find(rule) > -1:
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
55
                occurences += 1
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
56
        return occurences
57
58
    def test_branch_working_tree(self):
59
        """Test that the number of uses of working_tree in branch is stable."""
60
        occurences = self.find_occurences('self.working_tree()',
1526 by Robert Collins
Bugfix to source testing logic to get the right path when .py is returned by __file__
61
                                          self.source_file_name(bzrlib.branch))
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
62
        # do not even think of increasing this number. If you think you need to
63
        # increase it, then you almost certainly are doing something wrong as
64
        # the relationship from working_tree to branch is one way.
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
65
        # Note that this is an exact equality so that when the number drops, 
66
        #it is not given a buffer but rather has this test updated immediately.
67
        self.assertEqual(0, occurences)
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
68
69
    def test_branch_WorkingTree(self):
70
        """Test that the number of uses of working_tree in branch is stable."""
71
        occurences = self.find_occurences('WorkingTree',
1526 by Robert Collins
Bugfix to source testing logic to get the right path when .py is returned by __file__
72
                                          self.source_file_name(bzrlib.branch))
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
73
        # do not even think of increasing this number. If you think you need to
74
        # increase it, then you almost certainly are doing something wrong as
75
        # the relationship from working_tree to branch is one way.
76
        # This number should be 4 (import NoWorkingTree and WorkingTree, 
77
        # raise NoWorkingTree from working_tree(), and construct a working tree
78
        # there) but a merge that regressed this was done before this test was
79
        # written. Note that this is an exact equality so that when the number
80
        # drops, it is not given a buffer but rather this test updated
81
        # immediately.
1773.4.3 by Martin Pool
[merge] bzr.dev
82
        self.assertEqual(2, occurences)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
83
84
85
class TestSource(TestSourceHelper):
86
87
    def get_bzrlib_dir(self):
88
        """Get the path to the root of bzrlib"""
89
        source = self.source_file_name(bzrlib)
90
        source_dir = os.path.dirname(source)
91
92
        # Avoid the case when bzrlib is packaged in a zip file
93
        if not os.path.isdir(source_dir):
94
            raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
95
                              % source_dir)
96
        return source_dir
97
98
    def get_source_files(self):
99
        """yield all source files for bzr and bzrlib"""
100
        bzrlib_dir = self.get_bzrlib_dir()
101
102
        # This is the front-end 'bzr' script
103
        bzr_path = self.get_bzr_path()
104
        yield bzr_path
105
106
        for root, dirs, files in os.walk(bzrlib_dir):
107
            for f in files:
108
                if not f.endswith('.py'):
109
                    continue
110
                yield osutils.pathjoin(root, f)
111
112
    def get_source_file_contents(self):
113
        for fname in self.get_source_files():
114
            f = open(fname, 'rb')
115
            try:
116
                text = f.read()
117
            finally:
118
                f.close()
119
            yield fname, text
120
121
    def is_exception(self, fname):
122
        """Certain files are allowed to be different"""
123
        if '/util/' in fname:
124
            # We don't require external utilities to be (C) Canonical Ltd
125
            return True
126
127
        exceptions = ['bzrlib/lsprof.py',
128
                      'bzrlib/conflicts.py',
129
                      'bzrlib/iterablefile.py',
130
                      'bzrlib/patches.py',
131
                      'bzrlib/tests/test_patches.py',
132
                     ]
133
        for exc in exceptions:
134
            if fname.endswith(exc):
135
                return True
136
137
        return False
138
139
    def test_copyright(self):
140
        """Test that all .py files have a valid copyright statement"""
141
        # These are files which contain a different copyright statement
142
        # and that is okay.
143
        incorrect = []
144
145
        copyright_re = re.compile('#\\s*copyright.*\n', re.I)
146
        copyright_canonical_re = re.compile(
147
            r'# Copyright \(C\) ' # Opening "# Copyright (C)"
148
            r'(\d+)(, \d+)*' # Followed by a series of dates
149
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
150
            )
151
152
        for fname, text in self.get_source_file_contents():
153
            if self.is_exception(fname):
154
                continue
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
155
            match = copyright_canonical_re.search(text)
156
            if not match:
157
                match = copyright_re.search(text)
158
                if match:
159
                    incorrect.append((fname, 'found: %s' % (match.group(),)))
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
160
                else:
161
                    incorrect.append((fname, 'no copyright line found\n'))
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
162
            else:
163
                if 'by Canonical' in match.group():
164
                    incorrect.append((fname,
165
                        'should not have: "by Canonical": %s'
166
                        % (match.group(),)))
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
167
168
        if incorrect:
169
            help_text = ["Some files have missing or incorrect copyright"
170
                         " statements.",
171
                         "Please add '# Copyright (C) 2006 Canonical Ltd'"
172
                         " to these files:",
173
                         "",
174
                        ]
175
            for fname, comment in incorrect:
176
                help_text.append(fname)
177
                help_text.append((' '*4) + comment)
178
179
            self.fail('\n'.join(help_text))
180
181
    def test_gpl(self):
182
        """Test that all .py files have a GPL disclaimer"""
183
        incorrect = []
184
185
        gpl_txt = """
186
# This program is free software; you can redistribute it and/or modify
187
# it under the terms of the GNU General Public License as published by
188
# the Free Software Foundation; either version 2 of the License, or
189
# (at your option) any later version.
190
#
191
# This program is distributed in the hope that it will be useful,
192
# but WITHOUT ANY WARRANTY; without even the implied warranty of
193
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
194
# GNU General Public License for more details.
195
#
196
# You should have received a copy of the GNU General Public License
197
# along with this program; if not, write to the Free Software
198
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
199
"""
200
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
201
202
        for fname, text in self.get_source_file_contents():
203
            if self.is_exception(fname):
204
                continue
205
            if not gpl_re.search(text):
206
                incorrect.append(fname)
207
208
        if incorrect:
209
            help_text = ['Some files have missing or incomplete GPL statement',
210
                         'Please fix the following files to have text:',
211
                         gpl_txt
212
                        ]
213
            for fname in incorrect:
214
                help_text.append((' '*4) + fname)
215
216
            self.fail('\n'.join(help_text))