2017-11-12 13:00:08 +01:00
|
|
|
# Copyright (C) 2017 Cenk Gündoğan <cenk.guendogan@haw-hamburg.de>
|
|
|
|
# 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
2016-03-05 15:59:35 +01:00
|
|
|
# 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
#
|
|
|
|
# This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
# General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
# directory for more details.
|
|
|
|
|
2017-11-10 13:22:26 +01:00
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2017-10-24 14:55:15 +02:00
|
|
|
import time
|
2017-10-25 15:24:25 +02:00
|
|
|
from traceback import extract_tb, print_tb
|
2017-11-10 13:22:26 +01:00
|
|
|
|
2017-10-25 15:24:25 +02:00
|
|
|
import pexpect
|
2017-11-10 13:22:26 +01:00
|
|
|
|
2017-10-25 15:24:25 +02:00
|
|
|
PEXPECT_PATH = os.path.dirname(pexpect.__file__)
|
|
|
|
RIOTBASE = os.environ['RIOTBASE'] or \
|
2017-12-20 13:34:52 +01:00
|
|
|
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
|
2017-10-25 15:24:25 +02:00
|
|
|
|
2017-11-16 11:54:08 +01:00
|
|
|
# Setting an empty 'TESTRUNNER_START_DELAY' environment variable use the
|
|
|
|
# default value (3)
|
|
|
|
MAKE_TERM_STARTED_DELAY = int(os.environ.get('TESTRUNNER_START_DELAY') or 3)
|
|
|
|
|
|
|
|
|
2017-10-25 15:24:25 +02:00
|
|
|
def list_until(l, cond):
|
2017-11-15 14:51:39 +01:00
|
|
|
return l[:([i for i, e in enumerate(l) if cond(e)][0])]
|
2016-03-05 15:59:35 +01:00
|
|
|
|
2017-12-20 13:34:52 +01:00
|
|
|
|
2017-11-12 12:59:45 +01:00
|
|
|
def find_exc_origin(exc_info):
|
|
|
|
pos = list_until(extract_tb(exc_info),
|
2017-11-15 14:51:39 +01:00
|
|
|
lambda frame: frame[0].startswith(PEXPECT_PATH)
|
2017-12-20 13:34:52 +01:00
|
|
|
)[-1]
|
2017-11-30 10:02:58 +01:00
|
|
|
return (pos[3], os.path.relpath(os.path.abspath(pos[0]), RIOTBASE), pos[1])
|
2017-11-12 12:59:45 +01:00
|
|
|
|
2017-12-20 13:34:52 +01:00
|
|
|
|
2016-04-08 22:39:49 +02:00
|
|
|
def run(testfunc, timeout=10, echo=True, traceback=False):
|
2016-03-05 15:59:35 +01:00
|
|
|
env = os.environ.copy()
|
2017-10-25 15:24:25 +02:00
|
|
|
child = pexpect.spawnu("make term", env=env, timeout=timeout)
|
2017-10-24 14:55:15 +02:00
|
|
|
|
|
|
|
# on many platforms, the termprog needs a short while to be ready...
|
2017-11-16 11:54:08 +01:00
|
|
|
time.sleep(MAKE_TERM_STARTED_DELAY)
|
2017-10-24 14:55:15 +02:00
|
|
|
|
2016-03-05 15:59:35 +01:00
|
|
|
if echo:
|
|
|
|
child.logfile = sys.stdout
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_output(('make', 'reset'), env=env,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# make reset yields error on some boards even if successful
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
testfunc(child)
|
2017-10-25 15:24:25 +02:00
|
|
|
except pexpect.TIMEOUT:
|
2017-11-30 10:02:58 +01:00
|
|
|
trace = find_exc_origin(sys.exc_info()[2])
|
|
|
|
print("Timeout in expect script at \"%s\" (%s:%d)" % trace)
|
2016-04-08 22:39:49 +02:00
|
|
|
if traceback:
|
|
|
|
print_tb(sys.exc_info()[2])
|
2016-03-05 15:59:35 +01:00
|
|
|
return 1
|
2017-11-12 13:00:08 +01:00
|
|
|
except pexpect.EOF:
|
2017-11-30 10:02:58 +01:00
|
|
|
trace = find_exc_origin(sys.exc_info()[2])
|
|
|
|
print("Unexpected end of file in expect script at \"%s\" (%s:%d)" % trace)
|
2017-11-12 13:00:08 +01:00
|
|
|
if traceback:
|
|
|
|
print_tb(sys.exc_info()[2])
|
|
|
|
return 1
|
2016-03-05 15:59:35 +01:00
|
|
|
finally:
|
|
|
|
print("")
|
2017-11-12 13:00:08 +01:00
|
|
|
try:
|
|
|
|
os.killpg(os.getpgid(child.pid), signal.SIGKILL)
|
|
|
|
except ProcessLookupError:
|
|
|
|
print("Process already stopped")
|
2016-03-05 15:59:35 +01:00
|
|
|
|
2017-11-12 13:00:08 +01:00
|
|
|
child.close()
|
2016-03-05 15:59:35 +01:00
|
|
|
return 0
|