From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id E029A38754A1 for ; Tue, 22 Dec 2020 11:39:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E029A38754A1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mliska@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id CDF00B71B; Tue, 22 Dec 2020 11:39:13 +0000 (UTC) From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Subject: [PATCH] Add pytest for a GCOV test-case To: gcc-patches@gcc.gnu.org Message-ID: Date: Tue, 22 Dec 2020 12:39:13 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Dec 2020 11:39:16 -0000 Hello. The patch adds a new test for an existing GCOV test-case. Newly added run-gcov-pytest parses JSON format produced by GCOV and runs pytest on it. Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Ready to be installed? Thanks, Martin gcc/testsuite/ChangeLog: PR gcov-profile/98273 * lib/gcov.exp: Add run-gcov-pytest function which runs pytest. * g++.dg/gcov/pr98273.C: New test. * g++.dg/gcov/gcov.py: New test. * g++.dg/gcov/test-pr98273.py: New test. --- gcc/testsuite/g++.dg/gcov/gcov.py | 10 ++++++++ gcc/testsuite/g++.dg/gcov/pr98273.C | 24 +++++++++++++++++++ gcc/testsuite/g++.dg/gcov/test-pr98273.py | 27 ++++++++++++++++++++++ gcc/testsuite/lib/gcov.exp | 28 +++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 gcc/testsuite/g++.dg/gcov/gcov.py create mode 100644 gcc/testsuite/g++.dg/gcov/pr98273.C create mode 100644 gcc/testsuite/g++.dg/gcov/test-pr98273.py diff --git a/gcc/testsuite/g++.dg/gcov/gcov.py b/gcc/testsuite/g++.dg/gcov/gcov.py new file mode 100644 index 00000000000..a8c4ea9ae71 --- /dev/null +++ b/gcc/testsuite/g++.dg/gcov/gcov.py @@ -0,0 +1,10 @@ +import gzip +import json +import os + + +def gcov_from_env(): + # return parsed JSON content a GCOV_PATH file + json_filename = os.environ['GCOV_PATH'] + '.gcov.json.gz' + json_data = gzip.open(json_filename).read() + return json.loads(json_data) diff --git a/gcc/testsuite/g++.dg/gcov/pr98273.C b/gcc/testsuite/g++.dg/gcov/pr98273.C new file mode 100644 index 00000000000..bfa83cbe4d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/gcov/pr98273.C @@ -0,0 +1,24 @@ +/* PR gcov-profile/98273 */ + +/* { dg-options "--coverage -std=c++11" } */ +/* { dg-do run { target native } } */ + +int +main () +{ + int i = 42; + { + auto f = [] () { + auto g = [] () {}; + g (); + g (); + }; + f (); + } + ++i; + ++i; + ++i; + return 45 - i; +} + +/* { dg-final { run-gcov-pytest pr98273.C "test-pr98273.py" } } */ diff --git a/gcc/testsuite/g++.dg/gcov/test-pr98273.py b/gcc/testsuite/g++.dg/gcov/test-pr98273.py new file mode 100644 index 00000000000..6cb39d10c1e --- /dev/null +++ b/gcc/testsuite/g++.dg/gcov/test-pr98273.py @@ -0,0 +1,27 @@ +from gcov import gcov_from_env + +import pytest + + +@pytest.fixture(scope='function', autouse=True) +def gcov(): + return gcov_from_env() + + +def test_basics(gcov): + files = gcov['files'] + assert len(files) == 1 + functions = files[0]['functions'] + assert len(functions) == 3 + + +def test_lines(gcov): + lines = gcov['files'][0]['lines'] + linesdict = {} + for line in lines: + linesdict[int(line['line_number'])] = line + + assert linesdict[21]['function_name'] == 'main' + assert linesdict[15]['function_name'] == '_ZZ4mainENKUlvE_clEv' + assert (linesdict[12]['function_name'] + == '_ZZZ4mainENKUlvE_clEvENKUlvE_clEv') diff --git a/gcc/testsuite/lib/gcov.exp b/gcc/testsuite/lib/gcov.exp index 9276aead06b..dd589d4dd8a 100644 --- a/gcc/testsuite/lib/gcov.exp +++ b/gcc/testsuite/lib/gcov.exp @@ -247,6 +247,34 @@ proc verify-calls { testname testcase file } { return $failed } +proc run-gcov-pytest { args } { + global GCOV + global srcdir subdir + # Extract the test file name from the arguments. + set testcase [lindex $args 0] + + verbose "Running $GCOV $testcase in $srcdir/$subdir" 2 + set testcase [remote_download host $testcase] + set result [remote_exec host $GCOV "$testcase -i"] + + set pytest_script [lindex $args 1] + setenv GCOV_PATH $testcase + verbose "pytest_script: $pytest_script" 2 + spawn -noecho python3 -m pytest --color=no -rA -s --tb=no $srcdir/$subdir/$pytest_script + + set prefix "\[^\r\n\]*" + expect { + -re "FAILED($prefix)\[^\r\n\]+\r\n" { + fail "$expect_out(1,string)" + exp_continue + } + -re "PASSED($prefix)\[^\r\n\]+\r\n" { + pass "$expect_out(1,string)" + exp_continue + } + } +} + # Called by dg-final to run gcov and analyze the results. # # ARGS consists of the optional strings "branches" and/or "calls", -- 2.29.2