1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2011
4 # Loosely based on bzrlib's test_source.py
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Source level Python tests."""
28 from samba.tests import (
33 def get_python_source_files():
34 """Iterate over all Python source files."""
35 library_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "samba"))
36 assert os.path.isdir(library_dir), library_dir
38 for root, dirs, files in os.walk(library_dir):
41 yield os.path.abspath(os.path.join(root, f))
43 bindir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "bin"))
44 assert os.path.isdir(bindir), bindir
45 for f in os.listdir(bindir):
46 p = os.path.abspath(os.path.join(bindir, f))
47 if not os.path.islink(p):
49 target = os.readlink(p)
50 if os.path.dirname(target).endswith("scripting/bin"):
52 wafsambadir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "buildtools", "wafsamba"))
53 assert os.path.isdir(wafsambadir), wafsambadir
54 for root, dirs, files in os.walk(wafsambadir):
57 yield os.path.abspath(os.path.join(root, f))
60 def get_source_file_contents():
61 """Iterate over the contents of all python files."""
62 for fname in get_python_source_files():
64 f = io.open(fname, mode='r', encoding='utf-8')
66 if e.errno == errno.ENOENT:
67 warnings.warn("source file %s broken link?" % fname)
78 class TestSource(TestCase):
80 def test_copyright(self):
81 """Test that all Python files have a valid copyright statement."""
84 copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
86 for fname, text in get_source_file_contents():
87 if fname.endswith("ms_schema.py"):
88 # FIXME: Not sure who holds copyright on ms_schema.py
90 if "wafsamba" in fname:
91 # FIXME: No copyright headers in wafsamba
93 match = copyright_re.search(text)
95 incorrect.append((fname, 'no copyright line found\n'))
99 "Some files have missing or incorrect copyright"
101 for fname, comment in incorrect:
102 help_text.append(fname)
103 help_text.append((' ' * 4) + comment)
105 self.fail('\n'.join(help_text))
108 """Test that all .py files have a GPL disclaimer."""
112 # This program is free software; you can redistribute it and/or modify
113 # it under the terms of the GNU General Public License as published by
114 # the Free Software Foundation; either version 3 of the License, or
115 # (at your option) any later version.
117 # This program is distributed in the hope that it will be useful,
118 # but WITHOUT ANY WARRANTY; without even the implied warranty of
119 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
120 # GNU General Public License for more details.
122 # You should have received a copy of the GNU General Public License
123 # along with this program. If not, see <http://www.gnu.org/licenses/>.
125 gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
127 for fname, text in get_source_file_contents():
128 if "wafsamba" in fname:
129 # FIXME: License to wafsamba hasn't been clarified yet
131 if fname.endswith("/python/samba/subunit/run.py"):
132 # Imported from subunit/testtools, which are dual
135 if not gpl_re.search(text):
136 incorrect.append(fname)
139 help_text = ['Some files have missing or incomplete GPL statement',
141 for fname in incorrect:
142 help_text.append((' ' * 4) + fname)
144 self.fail('\n'.join(help_text))
146 def _push_file(self, dict_, fname, line_no):
147 if fname not in dict_:
148 dict_[fname] = [line_no]
150 dict_[fname].append(line_no)
152 def _format_message(self, dict_, message):
153 files = ["%s: %s" % (f, ', '.join([str(i + 1) for i in lines]))
154 for f, lines in dict_.items()]
156 return message + '\n\n %s' % ('\n '.join(files))
158 def _iter_source_files_lines(self):
159 for fname, text in get_source_file_contents():
160 lines = text.splitlines(True)
161 last_line_no = len(lines) - 1
162 for line_no, line in enumerate(lines):
163 yield fname, line_no, line
165 def test_no_tabs(self):
166 """Check that there are no tabs in Python files."""
168 for fname, line_no, line in self._iter_source_files_lines():
170 self._push_file(tabs, fname, line_no)
172 self.fail(self._format_message(tabs,
173 'Tab characters were found in the following source files.'
174 '\nThey should either be replaced by "\\t" or by spaces:'))
176 def test_unix_newlines(self):
177 """Check for unix new lines."""
178 illegal_newlines = {}
179 for fname, line_no, line in self._iter_source_files_lines():
180 if not line.endswith('\n') or line.endswith('\r\n'):
181 self._push_file(illegal_newlines, fname, line_no)
183 self.fail(self._format_message(illegal_newlines,
184 'Non-unix newlines were found in the following source files:'))
186 def test_trailing_whitespace(self):
187 """Check that there is not trailing whitespace in Python files."""
188 trailing_whitespace = {}
189 for fname, line_no, line in self._iter_source_files_lines():
190 if line.rstrip("\n").endswith(" "):
191 self._push_file(trailing_whitespace, fname, line_no)
192 if trailing_whitespace:
193 self.fail(self._format_message(trailing_whitespace,
194 'Trailing whitespace was found in the following source files.'))
196 def test_shebang_lines(self):
197 """Check that files with shebang lines and only those are executable."""
198 files_with_shebang = {}
199 files_without_shebang= {}
200 for fname, line_no, line in self._iter_source_files_lines():
203 executable = (os.stat(fname).st_mode & 0o111)
204 has_shebang = line.startswith("#!")
205 if has_shebang and not executable:
206 self._push_file(files_with_shebang, fname, line_no)
207 if not has_shebang and executable:
208 self._push_file(files_without_shebang, fname, line_no)
209 if files_with_shebang:
210 self.fail(self._format_message(files_with_shebang,
211 'Files with shebang line that are not executable:'))
212 if files_without_shebang:
213 self.fail(self._format_message(files_without_shebang,
214 'Files without shebang line that are executable:'))