| 1 |
#!/usr/bin/env py.test |
| 2 |
# Copyright (C) 2011-2014 Anders Logg |
| 3 |
# |
| 4 |
# This file is part of DOLFIN. |
| 5 |
# |
| 6 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser 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. |
| 10 |
# |
| 11 |
# DOLFIN 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 Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import print_function |
| 20 |
import os, re |
| 21 |
import pytest |
| 22 |
|
| 23 |
def pytest_generate_tests(metafunc): |
| 24 |
# List of C++ tests |
| 25 |
cpp_tests = [dolfin_error, uint, includes] |
| 26 |
|
| 27 |
# List of Python tests |
| 28 |
python_tests = [dolfin_error, raise_exception] |
| 29 |
|
| 30 |
# Source dir (generated by CMake) |
| 31 |
topdir = "@CMAKE_SOURCE_DIR@" |
| 32 |
assert os.path.isfile(os.path.join(topdir, 'dolfin', 'dolfin.h')) |
| 33 |
assert os.path.isfile(os.path.join(topdir, 'site-packages', |
| 34 |
'dolfin', '__init__.py')) |
| 35 |
|
| 36 |
# Check C++ files |
| 37 |
cpp_call = {'topdir': topdir, |
| 38 |
'subdir': "dolfin", |
| 39 |
'suffixes':[".cpp", ".h"]} |
| 40 |
|
| 41 |
python_call = {'topdir': topdir, |
| 42 |
'subdir': "site-packages", |
| 43 |
'suffixes':[".py"]} |
| 44 |
|
| 45 |
for test in python_tests: |
| 46 |
python_call['test'] = test |
| 47 |
metafunc.addcall(funcargs=python_call) |
| 48 |
|
| 49 |
for test in cpp_tests: |
| 50 |
cpp_call['test'] = test |
| 51 |
metafunc.addcall(funcargs=cpp_call) |
| 52 |
|
| 53 |
|
| 54 |
def test_codingstyle(topdir, subdir, suffixes, test): |
| 55 |
"Main function for performing tests" |
| 56 |
|
| 57 |
# Iterate over all files |
| 58 |
for subdir, dirs, files in os.walk(os.path.join(topdir, subdir)): |
| 59 |
for filename in files: |
| 60 |
|
| 61 |
# Only consider files with given suffix |
| 62 |
if not len([1 for suffix in suffixes if filename.endswith(suffix)]) > 0: |
| 63 |
continue |
| 64 |
|
| 65 |
# Read file |
| 66 |
f = open(os.path.join(subdir, filename), "r") |
| 67 |
code = f.read() |
| 68 |
f.close() |
| 69 |
|
| 70 |
# Perform all tests |
| 71 |
result = test(code, filename) |
| 72 |
|
| 73 |
def dolfin_error(code, filename): |
| 74 |
"Test for use of dolfin_error vs error" |
| 75 |
|
| 76 |
# Skip exceptions |
| 77 |
exceptions = ["log.h", "log.cpp", "Logger.h", "Logger.cpp", |
| 78 |
"pugixml.cpp", "meshconvert.py", "abaqus.py", |
| 79 |
"MPI.cpp"] |
| 80 |
if filename in exceptions: |
| 81 |
return True |
| 82 |
|
| 83 |
# Check for error(...) |
| 84 |
if re.search(r"\berror\(", code) is None: |
| 85 |
return True |
| 86 |
|
| 87 |
# Write an error message |
| 88 |
assert False, "*** error() used in %s when dolfin_error() should be used" % filename |
| 89 |
return False |
| 90 |
|
| 91 |
def raise_exception(code, filename): |
| 92 |
"Test for use of dolfin_error vs raising exception" |
| 93 |
|
| 94 |
# Skip exceptions |
| 95 |
exceptions = ["meshconvert.py", "expressions.py", "form.py", |
| 96 |
"assembling.py", "multistagescheme.py", |
| 97 |
"rushlarsenschemes.py", "expression.py", |
| 98 |
"functionspace.py", "constant.py", |
| 99 |
"specialfunctions.py", "function.py", |
| 100 |
"ordereddict.py", "abaqus.py", "sge.py", "pjobs.py", |
| 101 |
"generatepythonrst.py", "CppHeaderParser.py", |
| 102 |
"subdomains.py", "plotting.py", "multimeshfunction.py"] |
| 103 |
if filename in exceptions: |
| 104 |
return True |
| 105 |
|
| 106 |
# Check for raising of exception |
| 107 |
if re.search(r"\braise\b", code) is None: |
| 108 |
return True |
| 109 |
|
| 110 |
# Write an error message |
| 111 |
assert False, "* Warning: exception raised in %s when dolfin_error() should be used" % filename |
| 112 |
return False |
| 113 |
|
| 114 |
def uint(code, filename): |
| 115 |
"Test for use of uint" |
| 116 |
|
| 117 |
# Skip exceptions |
| 118 |
exceptions = ["XMLMeshValueCollection.h", "XMLMesh.cpp", |
| 119 |
"XMLMeshFunction.h", "XMLFile.h"] |
| 120 |
if filename in exceptions: |
| 121 |
return True |
| 122 |
|
| 123 |
# Check for raising of exception |
| 124 |
if re.search(r"\buint\b", code) is None: |
| 125 |
return True |
| 126 |
|
| 127 |
# Write an error message |
| 128 |
assert False, "* Warning: uint is used in %s when std::size_t should be used" % filename |
| 129 |
return False |
| 130 |
|
| 131 |
def includes(code, filename): |
| 132 |
"Test of not including dolfin_foo.h which bloat compilation units" |
| 133 |
|
| 134 |
# Skip exceptions |
| 135 |
exceptions = ['dolfin.h'] |
| 136 |
if filename in exceptions: |
| 137 |
return True |
| 138 |
|
| 139 |
# Check for raising of exception |
| 140 |
# NOTE: Improve this pattern if it generates false positives |
| 141 |
r = re.search(r"\bdolfin_[a-zA-Z]*\.h\b", code) |
| 142 |
if r is None: |
| 143 |
return True |
| 144 |
|
| 145 |
# Write an error message |
| 146 |
assert False, "* Warning: %s should not be included in %s" % (r.group(), filename) |
| 147 |
return False |
| 148 |
|
| 149 |
if __name__ == "__main__": |
| 150 |
import sys |
| 151 |
sys.exit(pytest.main()) |