| 1 | """test linking against pysam. |
| 2 | """ |
| 3 | |
| 4 | import unittest |
| 5 | import os |
| 6 | import subprocess |
| 7 | import pysam |
| 8 | |
| 9 | from TestUtils import LINKDIR |
| 10 | |
| 11 | |
| 12 | def check_import(statement): |
| 13 | try: |
| 14 | output = subprocess.check_output( |
| 15 | statement, stderr=subprocess.STDOUT, shell=True) |
| 16 | except subprocess.CalledProcessError as exc: |
| 17 | if b"ImportError" in exc.output: |
| 18 | raise ImportError( |
| 19 | "module could not be imported: {}".format(str(exc.output))) |
| 20 | else: |
| 21 | raise |
| 22 | |
| 23 | |
| 24 | def check_pass(statement): |
| 25 | try: |
| 26 | output = subprocess.check_output( |
| 27 | statement, stderr=subprocess.STDOUT, shell=True) |
| 28 | except subprocess.CalledProcessError as exc: |
| 29 | raise ValueError("{}: {}".format(exc, exc.output)) |
| 30 | if b"FAILED" in output: |
| 31 | raise ValueError("module tests failed") |
| 32 | return True |
| 33 | |
| 34 | |
| 35 | @unittest.skipUnless( |
| 36 | os.environ.get("PYSAM_LINKING_TESTS", None), |
| 37 | "enable linking tests by setting PYSAM_LINKING_TESTS environment variable") |
| 38 | class TestLinking(unittest.TestCase): |
| 39 | |
| 40 | package_name = "link_with_rpath" |
| 41 | |
| 42 | def setUp(self): |
| 43 | self.workdir = os.path.join(LINKDIR, self.package_name) |
| 44 | |
| 45 | def test_package_can_be_installed(self): |
| 46 | subprocess.check_output( |
| 47 | "cd {} && rm -rf build && python setup.py install".format( |
| 48 | self.workdir), |
| 49 | shell=True) |
| 50 | |
| 51 | |
| 52 | @unittest.skipUnless( |
| 53 | os.environ.get("PYSAM_LINKING_TESTS", None), |
| 54 | "enable linking tests by setting PYSAM_LINKING_TESTS environment variable") |
| 55 | class TestLinkWithRpath(TestLinking): |
| 56 | |
| 57 | package_name = "link_with_rpath" |
| 58 | |
| 59 | def test_package_tests_pass(self): |
| 60 | self.assertTrue(check_pass( |
| 61 | "cd {} && python test_module.py".format(os.path.join(self.workdir, "tests")))) |
| 62 | |
| 63 | |
| 64 | @unittest.skipUnless( |
| 65 | os.environ.get("PYSAM_LINKING_TESTS", None), |
| 66 | "enable linking tests by setting PYSAM_LINKING_TESTS environment variable") |
| 67 | class TestLinkWithoutRpath(TestLinking): |
| 68 | |
| 69 | package_name = "link_without_rpath" |
| 70 | |
| 71 | def test_package_tests_fail_on_import(self): |
| 72 | |
| 73 | self.assertRaises( |
| 74 | ImportError, |
| 75 | check_import, |
| 76 | "cd {} && python test_module.py".format(os.path.join(self.workdir, "tests"))) |
| 77 | |
| 78 | def test_package_tests_pass_if_ld_library_path_set(self): |
| 79 | |
| 80 | pysam_libraries = pysam.get_libraries() |
| 81 | pysam_libdirs, pysam_libs = zip( |
| 82 | *[os.path.split(x) for x in pysam_libraries]) |
| 83 | pysam_libdir = pysam_libdirs[0] |
| 84 | |
| 85 | self.assertTrue(check_pass( |
| 86 | "export LD_LIBRARY_PATH={}:$PATH && cd {} && python test_module.py".format( |
| 87 | pysam_libdir, |
| 88 | os.path.join(self.workdir, "tests")))) |
| 89 | |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | unittest.main() |