From d32d7115f342de3a9793afa92db0767e0ebd9c7a Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 6 Apr 2013 07:00:00 -0700 Subject: [PATCH] Fix sys.path setup to prepend '.', rather than append. Also, remove '..' and allow running only from the root directory. --- test/README | 1 + test/run_examples_test.py | 4 ++-- test/run_readelf_tests.py | 1 - test/test_arm_support.py | 8 ++++---- test/test_callframe.py | 3 +-- test/test_dwarf_expr.py | 3 +-- test/test_dwarf_lineprogram.py | 3 +-- test/test_dwarf_structs.py | 3 +-- test/test_utils.py | 3 +-- test/utils.py | 14 +++++++++++--- 10 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 test/README diff --git a/test/README b/test/README new file mode 100644 index 0000000..2ee6883 --- /dev/null +++ b/test/README @@ -0,0 +1 @@ +All tests should be run from the root development directory of pyelftools diff --git a/test/run_examples_test.py b/test/run_examples_test.py index 06e8585..10f8c1e 100755 --- a/test/run_examples_test.py +++ b/test/run_examples_test.py @@ -9,7 +9,7 @@ #------------------------------------------------------------------------------- import os, sys import logging -sys.path.insert(0, '.') + from test.utils import run_exe, is_in_rootdir, dump_output_to_temp_files @@ -54,7 +54,7 @@ def run_example_and_compare(example_path): if rc != 0: testlog.info('.......ERROR - example returned error code %s' % rc) return False - + # Comparison is done as lists of lines, to avoid EOL problems if example_out.split() == ref_str.split(): return True diff --git a/test/run_readelf_tests.py b/test/run_readelf_tests.py index d910123..fc64dbc 100755 --- a/test/run_readelf_tests.py +++ b/test/run_readelf_tests.py @@ -13,7 +13,6 @@ from difflib import SequenceMatcher from optparse import OptionParser import logging import platform -sys.path.insert(0, '.') # to load *our* test, not Python's test from test.utils import run_exe, is_in_rootdir, dump_output_to_temp_files diff --git a/test/test_arm_support.py b/test/test_arm_support.py index 804e1df..fe35051 100644 --- a/test/test_arm_support.py +++ b/test/test_arm_support.py @@ -8,15 +8,15 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys import os -sys.path.extend(['.', '..']) +from test.utils import setup_syspath; setup_syspath() from elftools.elf.elffile import ELFFile class TestARMSupport(unittest.TestCase): def test_hello(self): - with open(os.path.join('test', 'testfiles', 'simple_gcc.elf.arm'), 'rb') as f: + with open(os.path.join('test', 'testfiles', + 'simple_gcc.elf.arm'), 'rb') as f: elf = ELFFile(f) self.assertEqual(elf.get_machine_arch(), 'ARM') @@ -26,5 +26,5 @@ class TestARMSupport(unittest.TestCase): self.assertEqual(elf.num_segments(), 2) if __name__ == '__main__': - sys.exit(unittest.main()) + unittest.main() diff --git a/test/test_callframe.py b/test/test_callframe.py index 6ea0aeb..546ce29 100644 --- a/test/test_callframe.py +++ b/test/test_callframe.py @@ -8,9 +8,8 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys -sys.path.extend(['.', '..']) +from test.utils import setup_syspath; setup_syspath() from elftools.common.py3compat import BytesIO from elftools.dwarf.callframe import ( CallFrameInfo, CIE, FDE, instruction_name, CallFrameInstruction, diff --git a/test/test_dwarf_expr.py b/test/test_dwarf_expr.py index 7c81ab3..0314804 100644 --- a/test/test_dwarf_expr.py +++ b/test/test_dwarf_expr.py @@ -8,9 +8,8 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys -sys.path.extend(('..', '.')) +from test.utils import setup_syspath; setup_syspath() from elftools.dwarf.descriptions import ExprDumper, set_global_machine_arch from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_dwarf_lineprogram.py b/test/test_dwarf_lineprogram.py index 2a88972..8d1b4ab 100644 --- a/test/test_dwarf_lineprogram.py +++ b/test/test_dwarf_lineprogram.py @@ -8,9 +8,8 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys -sys.path.extend(['.', '..']) +from test.utils import setup_syspath; setup_syspath() from elftools.common.py3compat import BytesIO, iteritems from elftools.dwarf.lineprogram import LineProgram, LineState, LineProgramEntry from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_dwarf_structs.py b/test/test_dwarf_structs.py index 9f6926a..9ca575b 100644 --- a/test/test_dwarf_structs.py +++ b/test/test_dwarf_structs.py @@ -8,9 +8,8 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys -sys.path.extend(['.', '..']) +from test.utils import setup_syspath; setup_syspath() from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_utils.py b/test/test_utils.py index bc54780..00e6137 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -8,10 +8,9 @@ try: import unittest2 as unittest except ImportError: import unittest -import sys from random import randint -sys.path.extend(['.', '..']) +from test.utils import setup_syspath; setup_syspath() from elftools.common.py3compat import int2byte, BytesIO from elftools.common.utils import (parse_cstring_from_stream, preserve_stream_pos) diff --git a/test/utils.py b/test/utils.py index dc6784d..57ae62c 100644 --- a/test/utils.py +++ b/test/utils.py @@ -10,6 +10,14 @@ import os, sys, subprocess, tempfile from elftools.common.py3compat import bytes2str +def setup_syspath(): + """ Setup sys.path so that tests pick up local pyelftools before the + installed one when run from development directory. + """ + if sys.path[0] != '.': + sys.path.insert(0, '.') + + def run_exe(exe_path, args): """ Runs the given executable as a subprocess, given the list of arguments. Captures its return code (rc) and stdout and @@ -21,14 +29,14 @@ def run_exe(exe_path, args): proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE) proc_stdout = proc.communicate()[0] return proc.returncode, bytes2str(proc_stdout) - + def is_in_rootdir(): """ Check whether the current dir is the root dir of pyelftools """ dirstuff = os.listdir('.') return 'test' in dirstuff and 'elftools' in dirstuff - + def dump_output_to_temp_files(testlog, *args): """ Dumps the output strings given in 'args' to temp files: one for each @@ -42,4 +50,4 @@ def dump_output_to_temp_files(testlog, *args): file.write(s) file.close() testlog.info('@@ Output #%s dumped to file: %s' % (i + 1, path)) - + -- 2.30.2