Eli Bendersky | b922360 | 2011-12-28 10:06:55 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 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 | #------------------------------------------------------------------------------- |
Eli Bendersky | ccdb554 | 2013-03-31 14:19:21 -0700 | [diff] [blame] | 9 | import os, sys, subprocess, tempfile |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 10 | |
Eli Bendersky | dd71c43 | 2013-04-08 06:38:57 -0700 | [diff] [blame] | 11 | # This module should not import elftools before setup_syspath() is called! |
| 12 | # See the Hacking Guide in the documentation for more details. |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 13 | |
Eli Bendersky | d32d711 | 2013-04-06 07:00:00 -0700 | [diff] [blame] | 14 | def setup_syspath(): |
| 15 | """ Setup sys.path so that tests pick up local pyelftools before the |
| 16 | installed one when run from development directory. |
| 17 | """ |
| 18 | if sys.path[0] != '.': |
| 19 | sys.path.insert(0, '.') |
| 20 | |
| 21 | |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 22 | def run_exe(exe_path, args): |
| 23 | """ Runs the given executable as a subprocess, given the |
| 24 | list of arguments. Captures its return code (rc) and stdout and |
| 25 | returns a pair: rc, stdout_str |
| 26 | """ |
| 27 | popen_cmd = [exe_path] + args |
| 28 | if os.path.splitext(exe_path)[1] == '.py': |
Eli Bendersky | ccdb554 | 2013-03-31 14:19:21 -0700 | [diff] [blame] | 29 | popen_cmd.insert(0, sys.executable) |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 30 | proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE) |
| 31 | proc_stdout = proc.communicate()[0] |
Eli Bendersky | dd71c43 | 2013-04-08 06:38:57 -0700 | [diff] [blame] | 32 | from elftools.common.py3compat import bytes2str |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame] | 33 | return proc.returncode, bytes2str(proc_stdout) |
Eli Bendersky | d32d711 | 2013-04-06 07:00:00 -0700 | [diff] [blame] | 34 | |
Eli Bendersky | 26e41c4 | 2011-12-28 09:21:14 +0200 | [diff] [blame] | 35 | |
| 36 | def is_in_rootdir(): |
| 37 | """ Check whether the current dir is the root dir of pyelftools |
| 38 | """ |
Eli Bendersky | 69c3a90 | 2013-04-08 06:45:01 -0700 | [diff] [blame] | 39 | return os.path.isdir('test') and os.path.isdir('elftools') |
Eli Bendersky | d32d711 | 2013-04-06 07:00:00 -0700 | [diff] [blame] | 40 | |
Eli Bendersky | b922360 | 2011-12-28 10:06:55 +0200 | [diff] [blame] | 41 | |
| 42 | def dump_output_to_temp_files(testlog, *args): |
| 43 | """ Dumps the output strings given in 'args' to temp files: one for each |
| 44 | arg. |
| 45 | """ |
| 46 | for i, s in enumerate(args): |
| 47 | fd, path = tempfile.mkstemp( |
| 48 | prefix='out' + str(i + 1) + '_', |
| 49 | suffix='.stdout') |
| 50 | file = os.fdopen(fd, 'w') |
| 51 | file.write(s) |
| 52 | file.close() |
| 53 | testlog.info('@@ Output #%s dumped to file: %s' % (i + 1, path)) |
Eli Bendersky | d32d711 | 2013-04-06 07:00:00 -0700 | [diff] [blame] | 54 | |