blob: 803a9995d81330d5af50c473b8ee819773144d15 [file] [log] [blame]
Eli Benderskyb9223602011-12-28 10:06:55 +02001#-------------------------------------------------------------------------------
2# test/utils.py
3#
4# Some common utils for tests
5#
6# Eli Bendersky (eliben@gmail.com)
7# This code is in the public domain
8#-------------------------------------------------------------------------------
9import os, subprocess, tempfile
Eli Bendersky26e41c42011-12-28 09:21:14 +020010
11
12def run_exe(exe_path, args):
13 """ Runs the given executable as a subprocess, given the
14 list of arguments. Captures its return code (rc) and stdout and
15 returns a pair: rc, stdout_str
16 """
17 popen_cmd = [exe_path] + args
18 if os.path.splitext(exe_path)[1] == '.py':
19 popen_cmd.insert(0, 'python')
20 proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE)
21 proc_stdout = proc.communicate()[0]
22 return proc.returncode, proc_stdout
23
24
25def is_in_rootdir():
26 """ Check whether the current dir is the root dir of pyelftools
27 """
28 dirstuff = os.listdir('.')
29 return 'test' in dirstuff and 'elftools' in dirstuff
30
Eli Benderskyb9223602011-12-28 10:06:55 +020031
32def dump_output_to_temp_files(testlog, *args):
33 """ Dumps the output strings given in 'args' to temp files: one for each
34 arg.
35 """
36 for i, s in enumerate(args):
37 fd, path = tempfile.mkstemp(
38 prefix='out' + str(i + 1) + '_',
39 suffix='.stdout')
40 file = os.fdopen(fd, 'w')
41 file.write(s)
42 file.close()
43 testlog.info('@@ Output #%s dumped to file: %s' % (i + 1, path))
44