Example #1
0
      def collect(self):
        try:
          os.environ['LIBTBX_SKIP_PYTEST'] = '1'
          import importlib
          # Guess the module import path from the location of this file
          test_module = self.fspath.dirpath().basename
          # We must be directly inside the root of a configured module.
          # If this module isn't configured, then we don't want to run tests.
          if not libtbx.env.has_module(test_module):
            return
          run_tests_module = test_module + "." + self.fspath.purebasename
          run_tests = importlib.import_module(run_tests_module)
        finally:
          del os.environ['LIBTBX_SKIP_PYTEST']

        for test in run_tests.tst_list:
          from six import string_types
          if isinstance(test, string_types):
            testfile = test
            testparams = []
            testname = 'main'
          else:
            testfile = test[0]
            testparams = [str(s) for s in test[1:]]
            testname = "_".join(str(p) for p in testparams)

          full_command = testfile.replace("$D", os.path.dirname(run_tests.__file__)). \
                                  replace("$B", libtbx.env.under_build(test_module))
          shortpath = testfile.replace("$D/", "").replace("$B/", "build/")
          pytest_file_object = pytest.File(shortpath, self.session)
          yield LibtbxTest(testname, pytest_file_object, full_command, testparams)
Example #2
0
            def collect(self):
                try:
                    os.environ['LIBTBX_SKIP_PYTEST'] = '1'
                    import importlib
                    run_tests = importlib.import_module(
                        caller_run_tests_module)
                finally:
                    del os.environ['LIBTBX_SKIP_PYTEST']

                for test in run_tests.tst_list:
                    if isinstance(test, basestring):
                        testfile = test
                        testparams = []
                        testname = 'main'
                    else:
                        testfile = test[0]
                        testparams = [str(s) for s in test[1:]]
                        testname = "_".join(str(p) for p in testparams)

                    full_command = testfile.replace("$D", os.path.dirname(run_tests.__file__)). \
                                            replace("$B", libtbx.env.under_build(caller))
                    shortpath = testfile.replace("$D/",
                                                 "").replace("$B/", "build/")
                    pytest_file_object = pytest.File(shortpath, self.session)
                    yield LibtbxTest(testname, pytest_file_object,
                                     full_command, testparams)
Example #3
0
def pytest_collection(session: _pytest.main.Session):
    """
    See :func:`_pytest.hookspec.pytest_collection_modifyitems` for documentation.
    Also see the "`Writing Plugins <https://docs.pytest.org/en/latest/writing_plugins.html>`_"
    guide.
    """
    if is_nait_mode():
        f = pytest.File(py.path.local(__file__), session)
        if session.config.option.concurrent:
            session.items = [
                _NanaimoItem(f, session.config.option.file_or_dir.copy())
            ]
            session.testscollected = 1
        else:
            session.items = []
            for fixture_name in session.config.option.file_or_dir:
                session.items.append(_NanaimoItem(f, [fixture_name]))
            session.testscollected = len(session.items)
        return True
Example #4
0
def _test_from_list_entry(entry, runtests_file, parent):
    """
    Create a LibTBXTest entry from a tst_list entry

    Arguments:
        entry (str or Iterable or Callable): The entry in the tst_list. This can
            be a string filename, a list of filename and arguments, or an
            inline function call (which will be skipped).
        file (py.path.local):   The run_tests filename that this entry was from

        parent (pytest.Node):   The parent node for the test

    Returns:
        LibTBXTest: The pytest test object to execute
    """

    # Accumulate markers to apply to the test
    markers = [pytest.mark.usefixtures("tmpdir")]

    # Extract the file, parameter information
    if isinstance(entry, six.string_types):
        testfile = entry
        testparams = []
        testname = "main"
    elif hasattr(entry, "__iter__"):
        testfile = entry[0]
        testparams = [str(x) for x in entry[1:]]
        testname = "_".join(str(p) for p in testparams)
    elif callable(entry):
        # Only a couple of these cases exist and awkward enough that we
        # can afford to skip them
        markers.append(
            pytest.mark.skip(
                "Callable inside run_tests.py not supported in pytest bridge"
            )
        )
        testfile = runtests_file.strpath
        testparams = []
        testname = "inline"

    # Expand the test file into a real path
    module = runtests_file.dirpath()
    # Convert any placeholder values to absolute paths
    full_command = testfile.replace("$D", module.strpath).replace(
        "$B", libtbx.env.under_build(module.basename)
    )

    # Handle hard-coded behaviour

    # Hard-coded ignore tests
    custom_test_marks = [
        (
            py.path.local(libtbx.env.dist_path("libtbx"))
            / "test_utils"
            / "__init__.py",
            pytest.mark.xfail(
                reason="libtbx/test_utils/__init__.py, insanely, asserts on stack trace length"
            ),
        )
    ]
    if libtbx.env.has_module("dials_regression"):
        custom_test_marks.append(
            (
                py.path.local(libtbx.env.dist_path("dials_regression")),
                pytest.mark.skip("dials_regression has no tests"),
            )
        )

    for path, reason in custom_test_marks:
        if path.common(py.path.local(full_command)) == path:
            markers.append(reason)

    # Skip anything in mmtbx if no monomer library present
    if libtbx.env.has_module("mmtbx"):
        lib = py.path.local(libtbx.env.dist_path("mmtbx"))
        has_env = "MMTBX_CCP4_MONOMER_LIB" in os.environ or "CLIBD_MON" in os.environ
        if py.path.local(full_command).common(lib) == lib and not has_env:
            markers.append(
                pytest.mark.skip(
                    reason="No monomer library - set MMTBX_CCP4_MONOMER_LIB or CLIBD_MON"
                )
            )

    # Generate a short path to use as the name
    # shortpath = testfile.replace("$D/", module.basename + "/").replace("$B/", module.basename+"/build/")
    shortpath = testfile.replace("$D/", "").replace("$B/", "build/")
    # Create a file parent object
    pytest_file_object = pytest.File(shortpath, parent)
    logger.info("Found libtbx test %s::%s", shortpath, testname)

    test = LibTBXTest(
        testname, pytest_file_object, full_command, testparams, markers=markers
    )

    return test