From 21db0b98a6261290464c2308b5ca9670c6ea76e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 16 Sep 2022 18:59:53 +0200 Subject: [PATCH] donate-cpu: small `LibraryIncludes` regular expression improvements / fixed/improved tests (#4473) * test_donate_cpu_lib.py: added more tests * test_donate_cpu_lib.py: fixed non-working assert * donate_cpu_lib.py: use `\t` in `LibraryIncludes` regular expressions instead of an actual TAB character * donate_cpu_lib.py: bumped version * donate_cpu_lib.py: use non-capturing group in `LibraryIncludes` regular expressions --- tools/donate_cpu_lib.py | 4 ++-- tools/test_donate_cpu_lib.py | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index ed4198fe9..948c35bd0 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -15,7 +15,7 @@ import shlex # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic # changes) -CLIENT_VERSION = "1.3.35" +CLIENT_VERSION = "1.3.36" # Timeout for analysis with Cppcheck in seconds CPPCHECK_TIMEOUT = 30 * 60 @@ -672,7 +672,7 @@ class LibraryIncludes: for library, includes in include_mappings.items(): re_includes = [re.escape(inc) for inc in includes] - re_expr = '^[ \t]*#[ \t]*include[ \t]*(' + '|'.join(re_includes) + ')' + re_expr = '^[ \\t]*#[ \\t]*include[ \\t]*(?:' + '|'.join(re_includes) + ')' re_obj = re.compile(re_expr, re.MULTILINE) self.__library_includes_re[library] = re_obj diff --git a/tools/test_donate_cpu_lib.py b/tools/test_donate_cpu_lib.py index 13be80a3a..c82907670 100644 --- a/tools/test_donate_cpu_lib.py +++ b/tools/test_donate_cpu_lib.py @@ -24,11 +24,31 @@ def _test_library_includes(tmpdir, libs, content): src_file = os.path.join(str(tmpdir), "file.cpp") with open(src_file, 'w') as f: f.write(content) - assert libs.sort() == library_includes.get_libraries(str(tmpdir)).sort() + libs.sort() + libs_found = library_includes.get_libraries(str(tmpdir)) + libs_found.sort() + assert libs == libs_found def test_library_includes(tmpdir): _test_library_includes(tmpdir, ['posix', 'gnu'], '') _test_library_includes(tmpdir, ['posix', 'gnu'], '#include ') _test_library_includes(tmpdir, ['posix', 'gnu', 'boost'], '#include ') _test_library_includes(tmpdir, ['posix', 'gnu', 'python'], '#include "Python.h"') - _test_library_includes(tmpdir, ['posix', 'gnu', 'lua', 'opengl', 'qt'], '#include \n#include \n#include "lua.h"') + _test_library_includes(tmpdir, ['posix', 'gnu', 'libcerror', 'lua', 'opengl', 'qt'], '#include \n#include \r#include "lua.h"\r\n#include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'microsoft_sal'], ' #include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'googletest'], '\t#include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'microsoft_atl'], ' \t #include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'cairo'], '\t #include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'gtk'], ' \t#include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'bsd'], '#include \r\n') + _test_library_includes(tmpdir, ['posix', 'gnu', 'libcurl'], '#include \r') + _test_library_includes(tmpdir, ['posix', 'gnu', 'sqlite3'], '#include \n') + _test_library_includes(tmpdir, ['posix', 'gnu', 'openmp'], '# include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'mfc'], '#\tinclude ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'ruby'], '# \tinclude "ruby.h"') + _test_library_includes(tmpdir, ['posix', 'gnu', 'zlib'], '#\t include ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'pcre'], '#include') + _test_library_includes(tmpdir, ['posix', 'gnu', 'pcre'], '#include "pcre.h"') + _test_library_includes(tmpdir, ['posix', 'gnu', 'opengl'], '#include\t ') + _test_library_includes(tmpdir, ['posix', 'gnu', 'nspr'], '#include\t"prtypes.h"') + _test_library_includes(tmpdir, ['posix', 'gnu', 'lua'], '#include \t')