From 23c6de34610080ba38e44b04622486f918e53809 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Tue, 8 Dec 2015 15:45:51 +0200 Subject: [PATCH] Can specify a working directory for tests. Closes #326. --- backends.py | 5 +++-- interpreter.py | 13 +++++++++++-- meson_test.py | 2 +- test cases/common/100 test workdir/meson.build | 6 ++++++ test cases/common/100 test workdir/opener.c | 12 ++++++++++++ test cases/failing/23 rel testdir/meson.build | 4 ++++ test cases/failing/23 rel testdir/simple.c | 3 +++ 7 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test cases/common/100 test workdir/meson.build create mode 100644 test cases/common/100 test workdir/opener.c create mode 100644 test cases/failing/23 rel testdir/meson.build create mode 100644 test cases/failing/23 rel testdir/simple.c diff --git a/backends.py b/backends.py index 03ecd4b29..7f7c3381c 100644 --- a/backends.py +++ b/backends.py @@ -21,7 +21,7 @@ from coredata import MesonException class TestSerialisation: def __init__(self, name, fname, is_cross, exe_wrapper, is_parallel, cmd_args, env, - should_fail, valgrind_args, timeout, extra_paths): + should_fail, valgrind_args, timeout, workdir, extra_paths): self.name = name self.fname = fname self.is_cross = is_cross @@ -32,6 +32,7 @@ class TestSerialisation: self.should_fail = should_fail self.valgrind_args = valgrind_args self.timeout = timeout + self.workdir = workdir self.extra_paths = extra_paths # This class contains the basic functionality that is needed by all backends. @@ -303,7 +304,7 @@ class Backend(): cmd_args.append(a) ts = TestSerialisation(t.get_name(), fname, is_cross, exe_wrapper, t.is_parallel, cmd_args, t.env, t.should_fail, t.valgrind_args, - t.timeout, extra_paths) + t.timeout, t.workdir, extra_paths) arr.append(ts) pickle.dump(arr, datafile) diff --git a/interpreter.py b/interpreter.py index f4c09a485..e7f7a3a40 100644 --- a/interpreter.py +++ b/interpreter.py @@ -528,7 +528,7 @@ class RunTargetHolder(InterpreterObject): self.held_object = build.RunTarget(name, command, args, subdir) class Test(InterpreterObject): - def __init__(self, name, exe, is_parallel, cmd_args, env, should_fail, valgrind_args, timeout): + def __init__(self, name, exe, is_parallel, cmd_args, env, should_fail, valgrind_args, timeout, workdir): InterpreterObject.__init__(self) self.name = name self.exe = exe @@ -538,6 +538,7 @@ class Test(InterpreterObject): self.should_fail = should_fail self.valgrind_args = valgrind_args self.timeout = timeout + self.workdir = workdir def get_exe(self): return self.exe @@ -1723,9 +1724,17 @@ class Interpreter(): if not isinstance(should_fail, bool): raise InterpreterException('Keyword argument should_fail must be a boolean.') timeout = kwargs.get('timeout', 30) + if 'workdir' in kwargs: + workdir = kwargs['workdir'] + if not isinstance(workdir, str): + raise InterpreterException('Workdir keyword argument must be a string.') + if not os.path.isabs(workdir): + raise InterpreterException('Workdir keyword argument must be an absolute path.') + else: + workdir = None if not isinstance(timeout, int): raise InterpreterException('Timeout must be an integer.') - t = Test(args[0], args[1].held_object, par, cmd_args, env, should_fail, valgrind_args, timeout) + t = Test(args[0], args[1].held_object, par, cmd_args, env, should_fail, valgrind_args, timeout, workdir) if is_base_test: self.build.tests.append(t) mlog.debug('Adding test "', mlog.bold(args[0]), '".', sep='') diff --git a/meson_test.py b/meson_test.py index 8061215f9..a7ba7ded4 100755 --- a/meson_test.py +++ b/meson_test.py @@ -104,7 +104,7 @@ def run_single_test(wrap, test): if len(test.extra_paths) > 0: child_env['PATH'] = child_env['PATH'] + ';'.join([''] + test.extra_paths) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - env=child_env) + env=child_env, cwd=test.workdir) timed_out = False try: (stdo, stde) = p.communicate(timeout=test.timeout) diff --git a/test cases/common/100 test workdir/meson.build b/test cases/common/100 test workdir/meson.build new file mode 100644 index 000000000..1323a17a9 --- /dev/null +++ b/test cases/common/100 test workdir/meson.build @@ -0,0 +1,6 @@ +project('test workdir', 'c') + +exe = executable('opener', 'opener.c') + +test('basic', exe, workdir : meson.source_root()) +test('shouldfail', exe, should_fail : true) diff --git a/test cases/common/100 test workdir/opener.c b/test cases/common/100 test workdir/opener.c new file mode 100644 index 000000000..43c53ce02 --- /dev/null +++ b/test cases/common/100 test workdir/opener.c @@ -0,0 +1,12 @@ +// This test only succeeds if run in the source root dir. + +#include + +int main(int arg, char **argv) { + FILE *f = fopen("opener.c", "r"); + if(f) { + fclose(f); + return 0; + } + return 1; +} diff --git a/test cases/failing/23 rel testdir/meson.build b/test cases/failing/23 rel testdir/meson.build new file mode 100644 index 000000000..c10558b34 --- /dev/null +++ b/test cases/failing/23 rel testdir/meson.build @@ -0,0 +1,4 @@ +project('nonabs workdir', 'c') + +exe = executable('simple', 'simple.c') +test('simple', exe, workdir : '.') diff --git a/test cases/failing/23 rel testdir/simple.c b/test cases/failing/23 rel testdir/simple.c new file mode 100644 index 000000000..11b7fad8e --- /dev/null +++ b/test cases/failing/23 rel testdir/simple.c @@ -0,0 +1,3 @@ +int main(int argc, char **argv) { + return 0; +}