Can set test cmd arguments and environment variables.

pull/15/head
Jussi Pakkanen 11 years ago
parent ae06ca2afc
commit 11cfb3ce22
  1. 7
      backends.py
  2. 25
      interpreter.py
  3. 20
      meson_test.py
  4. 18
      test cases/common/48 test args/cmd_args.c
  5. 15
      test cases/common/48 test args/envvars.c
  6. 7
      test cases/common/48 test args/meson.build

@ -96,12 +96,14 @@ def do_conf_file(src, dst, confdata):
os.replace(dst_tmp, dst)
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.fname = fname
self.is_cross = is_cross
self.exe_runner = exe_wrapper
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
# 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)
else:
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)
pickle.dump(arr, datafile)

@ -338,11 +338,13 @@ class SharedLibraryHolder(BuildTargetHolder):
super().__init__(build.SharedLibrary, name, subdir, is_cross, sources, environment, kwargs)
class Test(InterpreterObject):
def __init__(self, name, exe, is_parallel):
def __init__(self, name, exe, is_parallel, cmd_args, env):
InterpreterObject.__init__(self)
self.name = name
self.exe = exe
self.is_parallel = is_parallel
self.cmd_args = cmd_args
self.env = env
def get_exe(self):
return self.exe
@ -835,7 +837,26 @@ class Interpreter():
par = kwargs.get('is_parallel', True)
if not isinstance(par, bool):
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)
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('\n-------\n\n')
def run_single_test(wrap, fname, is_cross, exe_runner):
def run_single_test(wrap, test):
global tests_failed
if is_cross:
if exe_runner is None:
if test.is_cross:
if test.exe_runner is None:
# 'Can not run test on cross compiled executable
# because there is no execute wrapper.
cmd = None
else:
cmd = [exe_runner, fname]
cmd = [exe_runner, test.fname]
else:
cmd = [fname]
cmd = [test.fname]
if cmd is None:
res = 'SKIP'
duration = 0.0
stdo = 'Not run because can not execute cross compiled binaries.'
stde = ''
else:
cmd = wrap + cmd
cmd = wrap + cmd + test.cmd_args
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()
endtime = time.time()
duration = endtime - starttime
@ -113,11 +114,10 @@ def run_tests(options, datafilename):
if not test.is_parallel:
drain_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)
else:
f = executor.submit(run_single_test, wrap, test.fname,
test.is_cross, test.exe_runner)
f = executor.submit(run_single_test, wrap, test)
futures.append((f, numlen, tests, test.name, i, logfile))
drain_futures(futures)
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…
Cancel
Save