|
16 | 16 | # You should have received a copy of the GNU General Public License |
17 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
| 19 | +import sys |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
19 | 23 | from donate_cpu_lib import * |
20 | 24 |
|
21 | 25 | def _test_library_includes(tmpdir, libs, content, libinc_obj=None): |
@@ -79,3 +83,46 @@ def test_get_assertion_failures(): |
79 | 83 | # not an assertion failure message |
80 | 84 | assert get_assertion_failures("Assertion `x' failed.\n") == [] |
81 | 85 | assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == [] |
| 86 | + |
| 87 | +def _write_script(path, content): |
| 88 | + with open(path, 'wt') as f: |
| 89 | + f.write('#!/usr/bin/env python3\nimport sys\n' + content) |
| 90 | + os.chmod(path, 0o755) |
| 91 | + |
| 92 | +def _scan_package_assertion_failures(tmpdir, monkeypatch, gdb_stdout): |
| 93 | + # -j2 run: two child processes crash with an assertion failure |
| 94 | + _write_script(os.path.join(str(tmpdir), 'cppcheck'), |
| 95 | + 'sys.stderr.write("cppcheck: lib/checkb.cpp:20: void checkB(): Assertion `y\' failed.\\n"\n' |
| 96 | + ' "cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n"\n' |
| 97 | + ' "src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n"\n' |
| 98 | + ' "src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n")\n' |
| 99 | + 'sys.exit(1)\n') |
| 100 | + # gdb run on src/a.c |
| 101 | + _write_script(os.path.join(str(tmpdir), 'gdb'), |
| 102 | + 'sys.stdout.write({!r})\n'.format(gdb_stdout) + |
| 103 | + 'sys.stderr.write("cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n")\n') |
| 104 | + monkeypatch.setenv('PATH', str(tmpdir) + os.pathsep + os.environ['PATH']) |
| 105 | + monkeypatch.setattr('donate_cpu_lib.__jobs', '-j2') |
| 106 | + returncode, stacktrace, _, _, _, _ = scan_package(str(tmpdir), str(tmpdir), []) |
| 107 | + assert returncode == -6 |
| 108 | + return stacktrace |
| 109 | + |
| 110 | +@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb') |
| 111 | +def test_scan_package_assertion_failure(tmpdir, monkeypatch): |
| 112 | + # only the assertion failure from the gdb run is put in the stack trace |
| 113 | + stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, |
| 114 | + 'Checking src/a.c ...\n\nProgram received signal SIGABRT, Aborted.\n' |
| 115 | + '#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n') |
| 116 | + assert stacktrace == 'Checking src/a.c ...\n\n' \ |
| 117 | + "lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \ |
| 118 | + 'Program received signal SIGABRT, Aborted.\n' \ |
| 119 | + '#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n' |
| 120 | + |
| 121 | +@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb') |
| 122 | +def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch): |
| 123 | + # no stack trace from gdb => the assertion failures from the first run are used |
| 124 | + stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, '') |
| 125 | + assert stacktrace == "lib/checkb.cpp:20: void checkB(): Assertion `y' failed.\n" \ |
| 126 | + "lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \ |
| 127 | + 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \ |
| 128 | + 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' |
0 commit comments