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
+# <http://www.gnu.org/licenses/>.
+
+# 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
+# <http://www.gnu.org/licenses/>.
+
+# 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

Reply via email to