3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2011
6 # Loosely based on bzrlib's test_source.py
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 """Source level Python tests."""
29 from samba
.tests
import (
34 def get_python_source_files():
35 """Iterate over all Python source files."""
36 library_dir
= os
.path
.join(os
.path
.dirname(__file__
), "..", "..", "samba")
38 for root
, dirs
, files
in os
.walk(library_dir
):
41 yield os
.path
.abspath(os
.path
.join(root
, f
))
43 bindir
= os
.path
.join(os
.path
.dirname(__file__
), "..", "..", "..", "..", "bin")
44 for f
in os
.listdir(bindir
):
45 p
= os
.path
.abspath(os
.path
.join(bindir
, f
))
46 if not os
.path
.islink(p
):
48 target
= os
.readlink(p
)
49 if os
.path
.dirname(target
).endswith("scripting/bin"):
53 def get_source_file_contents():
54 """Iterate over the contents of all python files."""
55 for fname
in get_python_source_files():
59 if e
.errno
== errno
.ENOENT
:
60 warnings
.warn("source file %s broken link?" % fname
)
71 class TestSource(TestCase
):
73 def test_copyright(self
):
74 """Test that all Python files have a valid copyright statement."""
77 copyright_re
= re
.compile('#\\s*copyright.*(?=\n)', re
.I
)
79 for fname
, text
in get_source_file_contents():
80 if fname
.endswith("ms_schema.py"):
81 # FIXME: Not sure who holds copyright on ms_schema.py
83 match
= copyright_re
.search(text
)
85 incorrect
.append((fname
, 'no copyright line found\n'))
88 help_text
= ["Some files have missing or incorrect copyright"
92 for fname
, comment
in incorrect
:
93 help_text
.append(fname
)
94 help_text
.append((' ' * 4) + comment
)
96 self
.fail('\n'.join(help_text
))
99 """Test that all .py files have a GPL disclaimer."""
103 # This program is free software; you can redistribute it and/or modify
104 # it under the terms of the GNU General Public License as published by
105 # the Free Software Foundation; either version 3 of the License, or
106 # (at your option) any later version.
108 # This program is distributed in the hope that it will be useful,
109 # but WITHOUT ANY WARRANTY; without even the implied warranty of
110 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
111 # GNU General Public License for more details.
113 # You should have received a copy of the GNU General Public License
114 # along with this program. If not, see <http://www.gnu.org/licenses/>.
116 gpl_re
= re
.compile(re
.escape(gpl_txt
), re
.MULTILINE
)
118 for fname
, text
in get_source_file_contents():
119 if not gpl_re
.search(text
):
120 incorrect
.append(fname
)
123 help_text
= ['Some files have missing or incomplete GPL statement',
125 for fname
in incorrect
:
126 help_text
.append((' ' * 4) + fname
)
128 self
.fail('\n'.join(help_text
))
130 def _push_file(self
, dict_
, fname
, line_no
):
131 if fname
not in dict_
:
132 dict_
[fname
] = [line_no
]
134 dict_
[fname
].append(line_no
)
136 def _format_message(self
, dict_
, message
):
137 files
= ["%s: %s" % (f
, ', '.join([str(i
+ 1) for i
in lines
]))
138 for f
, lines
in dict_
.items()]
140 return message
+ '\n\n %s' % ('\n '.join(files
))
142 def _iter_source_files_lines(self
):
143 for fname
, text
in get_source_file_contents():
144 lines
= text
.splitlines(True)
145 last_line_no
= len(lines
) - 1
146 for line_no
, line
in enumerate(lines
):
147 yield fname
, line_no
, line
149 def test_no_tabs(self
):
150 """Check that there are no tabs in Python files."""
152 for fname
, line_no
, line
in self
._iter
_source
_files
_lines
():
154 self
._push
_file
(tabs
, fname
, line_no
)
156 self
.fail(self
._format
_message
(tabs
,
157 'Tab characters were found in the following source files.'
158 '\nThey should either be replaced by "\\t" or by spaces:'))
160 def test_unix_newlines(self
):
161 """Check for unix new lines."""
162 illegal_newlines
= {}
163 for fname
, line_no
, line
in self
._iter
_source
_files
_lines
():
164 if not line
.endswith('\n') or line
.endswith('\r\n'):
165 self
._push
_file
(illegal_newlines
, fname
, line_no
)
167 self
.fail(self
._format
_message
(illegal_newlines
,
168 'Non-unix newlines were found in the following source files:'))