Can set test cmd arguments and environment variables.
This commit is contained in:
parent
ae06ca2afc
commit
11cfb3ce22
|
@ -96,12 +96,14 @@ def do_conf_file(src, dst, confdata):
|
||||||
os.replace(dst_tmp, dst)
|
os.replace(dst_tmp, dst)
|
||||||
|
|
||||||
class TestSerialisation:
|
class TestSerialisation:
|
||||||
def __init__(self, name, fname, is_cross, exe_wrapper, is_parallel):
|
def __init__(self, name, fname, is_cross, exe_wrapper, is_parallel, cmd_args, env):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.fname = fname
|
self.fname = fname
|
||||||
self.is_cross = is_cross
|
self.is_cross = is_cross
|
||||||
self.exe_runner = exe_wrapper
|
self.exe_runner = exe_wrapper
|
||||||
self.is_parallel = is_parallel
|
self.is_parallel = is_parallel
|
||||||
|
self.cmd_args = cmd_args
|
||||||
|
self.env = env
|
||||||
|
|
||||||
# It may seem a bit silly that this Backend class exists on its own
|
# It may seem a bit silly that this Backend class exists on its own
|
||||||
# rather than being a part of NinjaBackend, which is the only class
|
# rather than being a part of NinjaBackend, which is the only class
|
||||||
|
@ -498,7 +500,8 @@ class NinjaBackend(Backend):
|
||||||
exe_wrapper = self.environment.cross_info.get('exe_wrapper', None)
|
exe_wrapper = self.environment.cross_info.get('exe_wrapper', None)
|
||||||
else:
|
else:
|
||||||
exe_wrapper = None
|
exe_wrapper = None
|
||||||
ts = TestSerialisation(t.get_name(), fname, is_cross, exe_wrapper, t.is_parallel)
|
ts = TestSerialisation(t.get_name(), fname, is_cross, exe_wrapper,
|
||||||
|
t.is_parallel, t.cmd_args, t.env)
|
||||||
arr.append(ts)
|
arr.append(ts)
|
||||||
pickle.dump(arr, datafile)
|
pickle.dump(arr, datafile)
|
||||||
|
|
||||||
|
|
|
@ -338,11 +338,13 @@ class SharedLibraryHolder(BuildTargetHolder):
|
||||||
super().__init__(build.SharedLibrary, name, subdir, is_cross, sources, environment, kwargs)
|
super().__init__(build.SharedLibrary, name, subdir, is_cross, sources, environment, kwargs)
|
||||||
|
|
||||||
class Test(InterpreterObject):
|
class Test(InterpreterObject):
|
||||||
def __init__(self, name, exe, is_parallel):
|
def __init__(self, name, exe, is_parallel, cmd_args, env):
|
||||||
InterpreterObject.__init__(self)
|
InterpreterObject.__init__(self)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.exe = exe
|
self.exe = exe
|
||||||
self.is_parallel = is_parallel
|
self.is_parallel = is_parallel
|
||||||
|
self.cmd_args = cmd_args
|
||||||
|
self.env = env
|
||||||
|
|
||||||
def get_exe(self):
|
def get_exe(self):
|
||||||
return self.exe
|
return self.exe
|
||||||
|
@ -835,7 +837,26 @@ class Interpreter():
|
||||||
par = kwargs.get('is_parallel', True)
|
par = kwargs.get('is_parallel', True)
|
||||||
if not isinstance(par, bool):
|
if not isinstance(par, bool):
|
||||||
raise InterpreterException('Keyword argument is_parallel must be a boolean.')
|
raise InterpreterException('Keyword argument is_parallel must be a boolean.')
|
||||||
t = Test(args[0], args[1].target, par)
|
cmd_args = kwargs.get('args', [])
|
||||||
|
if not isinstance(cmd_args, list):
|
||||||
|
cmd_args = [cmd_args]
|
||||||
|
for i in cmd_args:
|
||||||
|
if not isinstance(i, str):
|
||||||
|
raise InterpreterException('Command line arguments must be strings')
|
||||||
|
envlist = kwargs.get('env', [])
|
||||||
|
if not isinstance(envlist, list):
|
||||||
|
envlist = [envlist]
|
||||||
|
env = {}
|
||||||
|
for e in envlist:
|
||||||
|
if '=' not in e:
|
||||||
|
raise InterpreterException('Env var definition must be of type key=val.')
|
||||||
|
(k, val) = e.split('=', 1)
|
||||||
|
k = k.strip()
|
||||||
|
val = val.strip()
|
||||||
|
if ' ' in k:
|
||||||
|
raise InterpreterException('Env var key must not have spaces in it.')
|
||||||
|
env[k] = val
|
||||||
|
t = Test(args[0], args[1].target, par, cmd_args, env)
|
||||||
self.build.tests.append(t)
|
self.build.tests.append(t)
|
||||||
mlog.debug('Adding test "', mlog.bold(args[0]), '".', sep='')
|
mlog.debug('Adding test "', mlog.bold(args[0]), '".', sep='')
|
||||||
|
|
||||||
|
|
|
@ -39,26 +39,27 @@ def write_log(logfile, test_name, result_str, stdo, stde):
|
||||||
logfile.write(stde)
|
logfile.write(stde)
|
||||||
logfile.write('\n-------\n\n')
|
logfile.write('\n-------\n\n')
|
||||||
|
|
||||||
def run_single_test(wrap, fname, is_cross, exe_runner):
|
def run_single_test(wrap, test):
|
||||||
global tests_failed
|
global tests_failed
|
||||||
if is_cross:
|
if test.is_cross:
|
||||||
if exe_runner is None:
|
if test.exe_runner is None:
|
||||||
# 'Can not run test on cross compiled executable
|
# 'Can not run test on cross compiled executable
|
||||||
# because there is no execute wrapper.
|
# because there is no execute wrapper.
|
||||||
cmd = None
|
cmd = None
|
||||||
else:
|
else:
|
||||||
cmd = [exe_runner, fname]
|
cmd = [exe_runner, test.fname]
|
||||||
else:
|
else:
|
||||||
cmd = [fname]
|
cmd = [test.fname]
|
||||||
if cmd is None:
|
if cmd is None:
|
||||||
res = 'SKIP'
|
res = 'SKIP'
|
||||||
duration = 0.0
|
duration = 0.0
|
||||||
stdo = 'Not run because can not execute cross compiled binaries.'
|
stdo = 'Not run because can not execute cross compiled binaries.'
|
||||||
stde = ''
|
stde = ''
|
||||||
else:
|
else:
|
||||||
cmd = wrap + cmd
|
cmd = wrap + cmd + test.cmd_args
|
||||||
starttime = time.time()
|
starttime = time.time()
|
||||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
|
env=test.env)
|
||||||
(stdo, stde) = p.communicate()
|
(stdo, stde) = p.communicate()
|
||||||
endtime = time.time()
|
endtime = time.time()
|
||||||
duration = endtime - starttime
|
duration = endtime - starttime
|
||||||
|
@ -113,11 +114,10 @@ def run_tests(options, datafilename):
|
||||||
if not test.is_parallel:
|
if not test.is_parallel:
|
||||||
drain_futures(futures)
|
drain_futures(futures)
|
||||||
futures = []
|
futures = []
|
||||||
res = run_single_test(wrap, test.fname, test.is_cross, test.exe_runner)
|
res = run_single_test(wrap, t)
|
||||||
print_stats(numlen, tests, test.name, res, i, logfile)
|
print_stats(numlen, tests, test.name, res, i, logfile)
|
||||||
else:
|
else:
|
||||||
f = executor.submit(run_single_test, wrap, test.fname,
|
f = executor.submit(run_single_test, wrap, test)
|
||||||
test.is_cross, test.exe_runner)
|
|
||||||
futures.append((f, numlen, tests, test.name, i, logfile))
|
futures.append((f, numlen, tests, test.name, i, logfile))
|
||||||
drain_futures(futures)
|
drain_futures(futures)
|
||||||
print('\nFull log written to %s.' % logfilename)
|
print('\nFull log written to %s.' % logfilename)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<string.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if(argc != 3) {
|
||||||
|
fprintf(stderr, "Incorrect number of arguments.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(strcmp(argv[1], "first") != 0) {
|
||||||
|
fprintf(stderr, "First argument is wrong.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(strcmp(argv[2], "second") != 0) {
|
||||||
|
fprintf(stderr, "Second argument is wrong.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<string.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
if(strcmp(getenv("first"), "val1") != 0) {
|
||||||
|
fprintf(stderr, "First envvar is wrong.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(strcmp(getenv("second"), "val2") != 0) {
|
||||||
|
fprintf(stderr, "Second envvar is wrong.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
project('test features', 'c')
|
||||||
|
|
||||||
|
e1 = executable('cmd_args', 'cmd_args.c')
|
||||||
|
e2 = executable('envvars', 'envvars.c')
|
||||||
|
|
||||||
|
test('command line arguments', e1, args : ['first', 'second'])
|
||||||
|
test('environment variables', e2, env : ['first=val1', 'second=val2'])
|
Loading…
Reference in New Issue