Skip to content

Commit

Permalink
Pytest ignores ROOT tests if ROOT is not installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed May 7, 2021
1 parent c8c5b64 commit ca2cbbc
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lena/flow/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
try:
from future_builtins import zip
except ModuleNotFoundError:
except ImportError:
# not existent in Python 3.9
pass
import collections
Expand Down
8 changes: 6 additions & 2 deletions lena/flow/read_root_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import inspect
import sys

import ROOT

import lena


Expand All @@ -29,6 +27,11 @@ def __init__(self, types=None, keys=None, selector=None):
If *selector* is given, both *types* and *keys* must
be omitted, or :exc:`.LenaValueError` is raised.
"""
try:
import ROOT
except ImportError:
raise ImportError("ROOT not installed")

if selector is not None:
if keys or types:
raise lena.core.LenaValueError(
Expand Down Expand Up @@ -104,6 +107,7 @@ def run(self, flow):
don't save yielded values to a list,
or make proper copies of them in advance.
"""
import ROOT
for val in flow:
data, context = lena.flow.get_data_context(val)

Expand Down
3 changes: 0 additions & 3 deletions lena/flow/read_root_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def __init__(self, branches=None, get_entry=None):
e.g. *copy.deepcopy* in *get_entry*.
Otherwise all items collected will be the last read value.
"""
# todo: should this class belong
# to lena.flow or lena.input, lena.readers?

# This loads other classes faster,
# and if ROOT is not installed,
# still enables "from lena.flow import ReadROOTTree",
Expand Down
2 changes: 0 additions & 2 deletions lena/output/write_root_tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# needs ROOT installed
from __future__ import print_function

import array
import collections
import copy
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ filterwarnings =
ignore:::markupsafe

## Ignore warnings from other packages ##

#
## raised when using collections module (for named tuples).
# /usr/lib64/python3.7/site-packages/markupsafe/__init__.py:13: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
try:
import ROOT
except ImportError:
collect_ignore_glob = ["*/root/*"]
# otherwise will have problems either with tox,
# or when executing pytest directly
collect_ignore_glob += ["root/*"]
23 changes: 22 additions & 1 deletion tests/root/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import pytest

from .test_write_root_tree import test_run
# can't import it here, because
# conftest will be always run irrespective of ROOT availability
# from .test_write_root_tree import test_run

# to mark all files from this directory as 'ROOT', see this answer:
# https://stackoverflow.com/a/57046943/952234
# copy here:
"""
import pathlib
import pytest
def pytest_collection_modifyitems(config, items):
# python 3.4/3.5 compat: rootdir = pathlib.Path(str(config.rootdir))
rootdir = pathlib.Path(config.rootdir)
for item in items:
rel_path = pathlib.Path(item.fspath).relative_to(rootdir)
mark_name = next((part for part in rel_path.parts if part.endswith('_tests')), '').rstrip('_tests')
if mark_name:
mark = getattr(pytest.mark, mark_name)
item.add_marker(mark)
"""

@pytest.fixture(scope="session")
def rootfile():
from .test_write_root_tree import test_run
# other ROOT tests depend on the existence of this file
# test_run will be run twice: once here, and also when tested.
filename = test_run()
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ deps =
pytest-mock
commands =
pytest --doctest-modules lena tests
# for tox to find installed ROOT bindings
passenv = PYTHONPATH
sitepackages = true

0 comments on commit ca2cbbc

Please sign in to comment.