Skip to content

Commit

Permalink
test/cli/clang-import_test.py: create files in temporary folder / som…
Browse files Browse the repository at this point in the history
…e cleanups (#6733)

this should fix running the tests with multiple workers
  • Loading branch information
firewave committed Sep 17, 2024
1 parent d8debc2 commit c3b9131
Showing 1 changed file with 41 additions and 54 deletions.
95 changes: 41 additions & 54 deletions test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re
import subprocess
import sys

import pytest
from testutils import cppcheck, assert_cppcheck

Expand All @@ -20,7 +19,7 @@
pytest.skip(allow_module_level=True)


def get_debug_section(title, stdout):
def __get_debug_section(title, stdout):
s = re.sub(r'0x[0-9a-fA-F]+', '0x12345678', stdout)
s = re.sub(r'nestedIn: Struct', 'nestedIn: Class', s)
s = re.sub(r'classDef: struct', 'classDef: class', s)
Expand All @@ -43,91 +42,79 @@ def get_debug_section(title, stdout):
return s[pos1:pos2-1]


def check_symbol_database(code):
testfile = 'test.cpp'
def __check_symbol_database(tmpdir, code):
testfile = os.path.join(tmpdir, 'test.cpp')
with open(testfile, 'w+t') as f:
f.write(code)
ret1, stdout1, _ = cppcheck(['--clang', '--debug', '-v', testfile])
ret2, stdout2, _ = cppcheck(['--debug', '-v', testfile])
os.remove(testfile)
assert 0 == ret1, stdout1
assert 0 == ret2, stdout2
assert get_debug_section('### Symbol database', stdout1) == get_debug_section('### Symbol database', stdout2)
assert __get_debug_section('### Symbol database', stdout1) == __get_debug_section('### Symbol database', stdout2)


def check_ast(code):
testfile = 'test.cpp'
def __check_ast(tmpdir, code):
testfile = os.path.join(tmpdir, 'test.cpp')
with open(testfile, 'w+t') as f:
f.write(code)
ret1, stdout1, _ = cppcheck(['--clang', '--debug', '-v', testfile])
ret2, stdout2, _ = cppcheck(['--debug', '-v', testfile])
os.remove(testfile)
assert 0 == ret1, stdout1
assert 0 == ret2, stdout1
assert get_debug_section('##AST', stdout1) == get_debug_section('##AST', stdout2)


def todo_check_ast(code):
testfile = 'test.cpp'
with open(testfile, 'w+t') as f:
f.write(code)
ret1, stdout1, _ = cppcheck(['--clang', '--debug', '-v', testfile])
ret2, stdout2, _ = cppcheck(['--debug', '-v', testfile])
os.remove(testfile)
assert 0 == ret1, stdout1
assert 0 == ret2, stdout2
assert get_debug_section('##AST', stdout1) != get_debug_section('##AST', stdout2)
assert __get_debug_section('##AST', stdout1) == __get_debug_section('##AST', stdout2)



def test_symbol_database_1():
check_symbol_database('int main(){return 0;}')
def test_symbol_database_1(tmpdir):
__check_symbol_database(tmpdir, 'int main(){return 0;}')

def test_symbol_database_2():
check_symbol_database('struct Foo { void f(); }; void Foo::f() {}')
def test_symbol_database_2(tmpdir):
__check_symbol_database(tmpdir, 'struct Foo { void f(); }; void Foo::f() {}')

def test_symbol_database_3():
check_symbol_database('struct Fred { int a; }; int b; void f(int c, int d) { int e; }')
def test_symbol_database_3(tmpdir):
__check_symbol_database(tmpdir, 'struct Fred { int a; }; int b; void f(int c, int d) { int e; }')

def test_symbol_database_4():
check_symbol_database('void f(const int x) {}')
def test_symbol_database_4(tmpdir):
__check_symbol_database(tmpdir, 'void f(const int x) {}')

def test_symbol_database_5():
check_symbol_database('void f(int);')
def test_symbol_database_5(tmpdir):
__check_symbol_database(tmpdir, 'void f(int);')

def test_symbol_database_6():
check_symbol_database('inline static int foo(int x) { return x; }')
def test_symbol_database_6(tmpdir):
__check_symbol_database(tmpdir, 'inline static int foo(int x) { return x; }')

def test_symbol_database_7():
check_symbol_database('struct S {int x;}; void f(struct S *s) {}')
def test_symbol_database_7(tmpdir):
__check_symbol_database(tmpdir, 'struct S {int x;}; void f(struct S *s) {}')

def test_symbol_database_class_access_1():
check_symbol_database('class Fred { void foo ( ) {} } ;')
def test_symbol_database_class_access_1(tmpdir):
__check_symbol_database(tmpdir, 'class Fred { void foo ( ) {} } ;')

def test_symbol_database_class_access_2():
check_symbol_database('class Fred { protected: void foo ( ) {} } ;')
def test_symbol_database_class_access_2(tmpdir):
__check_symbol_database(tmpdir, 'class Fred { protected: void foo ( ) {} } ;')

def test_symbol_database_class_access_3():
check_symbol_database('class Fred { public: void foo ( ) {} } ;')
def test_symbol_database_class_access_3(tmpdir):
__check_symbol_database(tmpdir, 'class Fred { public: void foo ( ) {} } ;')

def test_symbol_database_operator():
check_symbol_database('struct Fred { void operator=(int x); };')
def test_symbol_database_operator(tmpdir):
__check_symbol_database(tmpdir, 'struct Fred { void operator=(int x); };')

def test_symbol_database_struct_1():
check_symbol_database('struct S {};')
def test_symbol_database_struct_1(tmpdir):
__check_symbol_database(tmpdir, 'struct S {};')

def test_ast_calculations():
check_ast('int x = 5; int y = (x + 4) * 2;')
check_ast('long long dostuff(int x) { return x ? 3 : 5; }')
def test_ast_calculations(tmpdir):
__check_ast(tmpdir, 'int x = 5; int y = (x + 4) * 2;')
__check_ast(tmpdir, 'long long dostuff(int x) { return x ? 3 : 5; }')

def test_ast_control_flow():
check_ast('void foo(int x) { if (x > 5){} }')
check_ast('int dostuff() { for (int x = 0; x < 10; x++); }')
check_ast('void foo(int x) { switch (x) {case 1: break; } }')
check_ast('void foo(int a, int b, int c) { foo(a,b,c); }')
def test_ast_control_flow(tmpdir):
__check_ast(tmpdir, 'void foo(int x) { if (x > 5){} }')
__check_ast(tmpdir, 'int dostuff() { for (int x = 0; x < 10; x++); }')
__check_ast(tmpdir, 'void foo(int x) { switch (x) {case 1: break; } }')
__check_ast(tmpdir, 'void foo(int a, int b, int c) { foo(a,b,c); }')

def test_ast():
check_ast('struct S { int x; }; S* foo() { return new S(); }')
def test_ast(tmpdir):
__check_ast(tmpdir, 'struct S { int x; }; S* foo() { return new S(); }')

def test_log(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
Expand Down

1 comment on commit c3b9131

@firewave
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boost is suddenly installed in macOS.

Before:

<stdin>:1:10: fatal error: 'boost/config.hpp' file not found
#include <boost/config.hpp>
         ^~~~~~~~~~~~~~~~~~
1 error generated.
Boost not completely present or not working, skipping syntax check with g++.

After:

Boost found and working, checking syntax with g++ now.
/Users/runner/work/cppcheck/cppcheck/test/cfg/boost.cpp:90:49: error: expected ';' after expression
    BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc) {
                                                ^
                                                ;
/Users/runner/work/cppcheck/cppcheck/test/cfg/boost.cpp:90:37: error: use of undeclared identifier 'future_errc'
    BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc) {
                                    ^
/Users/runner/work/cppcheck/cppcheck/test/cfg/boost.cpp:92:9: error: use of undeclared identifier 'no_state'
        no_state
        ^
/Users/runner/work/cppcheck/cppcheck/test/cfg/boost.cpp:94:47: error: expected ';' after expression
    BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
                                              ^
                                              ;
/Users/runner/work/cppcheck/cppcheck/test/cfg/boost.cpp:94:35: error: use of undeclared identifier 'future_errc'
    BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
                                  ^
5 errors generated.
make: *** [checkcfg] Error 1

Please sign in to comment.