From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 121504 invoked by alias); 26 Jul 2017 16:01:23 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 79372 invoked by uid 89); 26 Jul 2017 16:00:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: smtp.eu.adacore.com Received: from mel.act-europe.fr (HELO smtp.eu.adacore.com) (194.98.77.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 26 Jul 2017 16:00:56 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 4AD9481395; Wed, 26 Jul 2017 18:00:50 +0200 (CEST) Received: from smtp.eu.adacore.com ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OMQ4hCB6DYUV; Wed, 26 Jul 2017 18:00:50 +0200 (CEST) Received: from cacatoes.act-europe.fr (cacatoes.act-europe.fr [IPv6:2a02:2ab8:224:1:f21f:afff:fe4c:b838]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.eu.adacore.com (Postfix) with ESMTPSA id 2CB858139C; Wed, 26 Jul 2017 18:00:50 +0200 (CEST) From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Pierre-Marie de Rodat Subject: [PATCH 1/2] Introduce testsuite support to run Python tests Date: Wed, 26 Jul 2017 16:01:00 -0000 Message-Id: <20170726160040.6516-2-derodat@adacore.com> In-Reply-To: <20170726160040.6516-1-derodat@adacore.com> References: <20170726160040.6516-1-derodat@adacore.com> X-IsSubscribed: yes X-SW-Source: 2017-07/txt/msg01678.txt.bz2 gcc/testsuite/ * lib/gcc-python.exp: New test library. * python/testutils.py: New Python helper. --- gcc/testsuite/lib/gcc-python.exp | 95 +++++++++++++++++++++++++++++++++++++++ gcc/testsuite/python/testutils.py | 45 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 gcc/testsuite/lib/gcc-python.exp create mode 100644 gcc/testsuite/python/testutils.py diff --git a/gcc/testsuite/lib/gcc-python.exp b/gcc/testsuite/lib/gcc-python.exp new file mode 100644 index 00000000000..30cf74a87ac --- /dev/null +++ b/gcc/testsuite/lib/gcc-python.exp @@ -0,0 +1,95 @@ +# Copyright (C) 2017 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# . + +# Helpers to run a Python interpreter + +load_lib "remote.exp" + +# Return whether a working Python interpreter is available. + +proc check-python-available { args } { + set result [local_exec "python -c print(\"Hello\")" "" "" 300] + + set status [lindex $result 0] + set output [string trim [lindex $result 1]] + + if { $status != 0 || $output != "Hello" } { + return 0 + } else { + return 1 + } +} + +# Run the SCRIPT_PY Python script. Add one PASSing (FAILing) test per output +# line that starts with "PASS: " ("FAIL: "). Also fail for any other output +# line and for non-zero exit status code. +# +# The Python script can access Python modules and packages in the +# $srcdir/python directory. + +proc python-test { script_py } { + global srcdir + + set testname testname-for-summary + + # This assumes that we are three frames down from dg-test, and that + # it still stores the filename of the testcase in a local variable "name". + # A cleaner solution would require a new DejaGnu release. + upvar 2 prog src_file + + set asm_file "[file rootname [file tail $src_file]].o" + set script_py_path "[file dirname $src_file]/$script_py" + + set old_pythonpath [getenv "PYTHONPATH"] + set support_dir "$srcdir/python" + if { $old_pythonpath == "" } { + setenv "PYTHONPATH" $support_dir + } else { + setenv "PYTHONPATH" "$support_dir:$PYTHONPATH" + } + + set commandline "python $script_py_path $asm_file" + set timeout 300 + + verbose -log "Executing: $commandline (timeout = $timeout)" 2 + set result [local_exec $commandline "" "" $timeout] + + set status [lindex $result 0] + set output [lindex $result 1] + + if { $status != 0 } { + fail [concat "$testname: $script_py stopped with non-zero status" \ + " code ($status)"] + } + + foreach line [split $output "\n"] { + if { $line == "" } { + continue + } + if { [regexp "^PASS: (.*)" $line dummy message] } { + pass "$testname/$script_py: $message" + continue + } + if { [regexp "^FAIL: (.*)" $line dummy message] } { + fail "$testname/$script_py: $message" + continue + } + + fail "$testname/$script_py: spurious output: $line" + } + + setenv "PYTHONPATH" $old_pythonpath +} diff --git a/gcc/testsuite/python/testutils.py b/gcc/testsuite/python/testutils.py new file mode 100644 index 00000000000..503105ad9d0 --- /dev/null +++ b/gcc/testsuite/python/testutils.py @@ -0,0 +1,45 @@ +# Copyright (C) 2017 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# . + +# Helpers to drive a testcase + +def print_pass(message): + """Emit a PASS message. + + :param str message: Message to emit. + """ + print('PASS: {}'.format(message)) + + +def print_fail(message): + """Emit a FAIL message. + + :param str message: Message to emit. + """ + print('FAIL: {}'.format(message)) + + +def check(predicate, message): + """ + If `predicate` is True, emit a PASS message, otherwise emit a FAIL one. + + :param bool predicate: Whether the test should pass. + :param str message: Message to emit. + """ + if predicate: + print_pass(message) + else: + print_fail(message) -- 2.13.0