Skip to content

Commit

Permalink
Merge branch 'develop' into fmt-core
Browse files Browse the repository at this point in the history
  • Loading branch information
RMeli authored Jan 1, 2025
2 parents 43fb0dd + b8fe34b commit 65ed20a
Show file tree
Hide file tree
Showing 19 changed files with 1,068 additions and 612 deletions.
3 changes: 2 additions & 1 deletion testsuite/MDAnalysisTests/import/fork_called.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"""Tests whether os.fork() is called as a side effect when importing MDAnalysis.
See PR #1794 for details."""

with mock.patch('os.fork') as os_dot_fork:
with mock.patch("os.fork") as os_dot_fork:
import MDAnalysis

assert not os_dot_fork.called
17 changes: 10 additions & 7 deletions testsuite/MDAnalysisTests/import/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

"""Test if importing MDAnalysis has unwanted side effects (PR #1794)."""

@pytest.mark.skipif(os.name == 'nt',
reason="fork-related import checks irrelevant on Windows")

@pytest.mark.skipif(
os.name == "nt", reason="fork-related import checks irrelevant on Windows"
)
class TestMDAImport(object):
# Tests concerning importing MDAnalysis.
def test_os_dot_fork_not_called(self):
Expand All @@ -37,7 +39,7 @@ def test_os_dot_fork_not_called(self):
# no previously imported modules interfere with it. It is therefore
# offloaded to the script "fork_called.py".
loc = os.path.dirname(os.path.realpath(__file__))
script = os.path.join(loc, 'fork_called.py')
script = os.path.join(loc, "fork_called.py")
encoding = sys.stdout.encoding
if encoding is None:
encoding = "utf-8"
Expand All @@ -47,16 +49,17 @@ def test_os_dot_fork_not_called(self):
# CalledProcessError. That error's output member then contains the
# failed script's stderr and we can print it:
try:
out = subprocess.check_output([sys.executable, script],
stderr=subprocess.STDOUT)\
.decode(encoding)
out = subprocess.check_output(
[sys.executable, script], stderr=subprocess.STDOUT
).decode(encoding)
except subprocess.CalledProcessError as err:
print(err.output)
raise(err)
raise (err)

def test_os_dot_fork_not_none(self):
# In MDAnalysis.core.universe, os.fork is set to None prior to importing
# the uuid module and restored afterwards (see PR #1794 for details).
# This tests asserts that os.fork has been restored.
import MDAnalysis

assert os.fork is not None
5 changes: 4 additions & 1 deletion testsuite/MDAnalysisTests/utils/test_authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@

import MDAnalysis


def test_package_authors():
assert len(MDAnalysis.__authors__) > 0, 'Could not find the list of authors'
assert (
len(MDAnalysis.__authors__) > 0
), "Could not find the list of authors"
35 changes: 25 additions & 10 deletions testsuite/MDAnalysisTests/utils/test_datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def test_failed_import(monkeypatch):
# Putting this test first to avoid datafiles already being loaded
errmsg = "MDAnalysisTests package not installed."

monkeypatch.setitem(sys.modules, 'MDAnalysisTests.datafiles', None)
monkeypatch.setitem(sys.modules, "MDAnalysisTests.datafiles", None)

if 'MDAnalysis.tests.datafiles' in sys.modules:
monkeypatch.delitem(sys.modules, 'MDAnalysis.tests.datafiles')
if "MDAnalysis.tests.datafiles" in sys.modules:
monkeypatch.delitem(sys.modules, "MDAnalysis.tests.datafiles")

with pytest.raises(ImportError, match=errmsg):
import MDAnalysis.tests.datafiles
Expand All @@ -42,20 +42,35 @@ def test_import():
try:
import MDAnalysis.tests.datafiles
except ImportError:
pytest.fail("Failed to 'import MDAnalysis.tests.datafiles --- install MDAnalysisTests")
pytest.fail(
"Failed to 'import MDAnalysis.tests.datafiles --- install MDAnalysisTests"
)


def test_all_exports():
import MDAnalysisTests.datafiles
missing = [name for name in dir(MDAnalysisTests.datafiles)
if
not name.startswith('_') and name not in MDAnalysisTests.datafiles.__all__ and name != 'MDAnalysisTests']

missing = [
name
for name in dir(MDAnalysisTests.datafiles)
if not name.startswith("_")
and name not in MDAnalysisTests.datafiles.__all__
and name != "MDAnalysisTests"
]
assert_equal(missing, [], err_msg="Variables need to be added to __all__.")


def test_export_variables():
import MDAnalysisTests.datafiles
import MDAnalysis.tests.datafiles
missing = [name for name in MDAnalysisTests.datafiles.__all__
if name not in dir(MDAnalysis.tests.datafiles)]
assert_equal(missing, [], err_msg="Variables not exported to MDAnalysis.tests.datafiles")

missing = [
name
for name in MDAnalysisTests.datafiles.__all__
if name not in dir(MDAnalysis.tests.datafiles)
]
assert_equal(
missing,
[],
err_msg="Variables not exported to MDAnalysis.tests.datafiles",
)
104 changes: 64 additions & 40 deletions testsuite/MDAnalysisTests/utils/test_duecredit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,68 @@
# duecredit itself is not needed in the name space but this is a
# convenient way to skip all tests if duecredit is not installed
# (see https://github.com/MDAnalysis/mdanalysis/issues/1906)
pytest.importorskip('duecredit')
pytest.importorskip("duecredit")

@pytest.mark.skipif((os.environ.get('DUECREDIT_ENABLE', 'yes').lower()
in ('no', '0', 'false')),
reason=
"duecredit is explicitly disabled with DUECREDIT_ENABLE=no")

@pytest.mark.skipif(
(
os.environ.get("DUECREDIT_ENABLE", "yes").lower()
in ("no", "0", "false")
),
reason="duecredit is explicitly disabled with DUECREDIT_ENABLE=no",
)
class TestDuecredit(object):

def test_duecredit_active(self):
assert mda.due.active == True

@pytest.mark.parametrize("module,path,citekey", [
("MDAnalysis", "MDAnalysis", "10.25080/majora-629e541a-00e"),
("MDAnalysis", "MDAnalysis", "10.1002/jcc.21787"),
])
@pytest.mark.parametrize(
"module,path,citekey",
[
("MDAnalysis", "MDAnalysis", "10.25080/majora-629e541a-00e"),
("MDAnalysis", "MDAnalysis", "10.1002/jcc.21787"),
],
)
def test_duecredit_collector_primary(self, module, path, citekey):
assert mda.due.citations[(path, citekey)].cites_module == True

# note: citekeys are *all lower case*
@pytest.mark.parametrize("module,path,citekey", [
("MDAnalysis.analysis.psa",
"pathsimanalysis.psa",
"10.1371/journal.pcbi.1004568"),
("MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
"10.1063/1.4922445"),
("MDAnalysis.analysis.leaflet",
"MDAnalysis.analysis.leaflet",
"10.1002/jcc.21787"),
("MDAnalysis.lib.qcprot",
"MDAnalysis.lib.qcprot",
"10.1107/s0108767305015266"),
("MDAnalysis.lib.qcprot",
"MDAnalysis.lib.qcprot",
"qcprot2"),
("MDAnalysis.analysis.encore",
"MDAnalysis.analysis.encore",
"10.1371/journal.pcbi.1004415"),
("MDAnalysis.analysis.dssp",
"MDAnalysis.analysis.dssp",
"10.1002/bip.360221211")
])
@pytest.mark.parametrize(
"module,path,citekey",
[
(
"MDAnalysis.analysis.psa",
"pathsimanalysis.psa",
"10.1371/journal.pcbi.1004568",
),
(
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
"10.1063/1.4922445",
),
(
"MDAnalysis.analysis.leaflet",
"MDAnalysis.analysis.leaflet",
"10.1002/jcc.21787",
),
(
"MDAnalysis.lib.qcprot",
"MDAnalysis.lib.qcprot",
"10.1107/s0108767305015266",
),
("MDAnalysis.lib.qcprot", "MDAnalysis.lib.qcprot", "qcprot2"),
(
"MDAnalysis.analysis.encore",
"MDAnalysis.analysis.encore",
"10.1371/journal.pcbi.1004415",
),
(
"MDAnalysis.analysis.dssp",
"MDAnalysis.analysis.dssp",
"10.1002/bip.360221211",
),
],
)
def test_duecredit_collector_analysis_modules(self, module, path, citekey):
importlib.import_module(module)
assert mda.due.citations[(path, citekey)].cites_module == True
Expand All @@ -85,17 +105,21 @@ def test_duecredit_mmtf(self):
# doesn't trigger on import but on use of either parser or reader
u = mda.Universe(MMTF)

assert mda.due.citations[('MDAnalysis.coordinates.MMTF',
'10.1371/journal.pcbi.1005575')].cites_module
assert mda.due.citations[('MDAnalysis.topology.MMTFParser',
'10.1371/journal.pcbi.1005575')].cites_module
assert mda.due.citations[
("MDAnalysis.coordinates.MMTF", "10.1371/journal.pcbi.1005575")
].cites_module
assert mda.due.citations[
("MDAnalysis.topology.MMTFParser", "10.1371/journal.pcbi.1005575")
].cites_module

@pytest.mark.skipif(not HAS_H5PY, reason="h5py not installed")
def test_duecredit_h5md(self):
# doesn't trigger on import but on use of either reader or writer
u = mda.Universe(TPR_xvf, H5MD_xvf)

assert mda.due.citations[('MDAnalysis.coordinates.H5MD',
'10.25080/majora-1b6fd038-005')].cites_module
assert mda.due.citations[('MDAnalysis.coordinates.H5MD',
'10.1016/j.cpc.2014.01.018')].cites_module
assert mda.due.citations[
("MDAnalysis.coordinates.H5MD", "10.25080/majora-1b6fd038-005")
].cites_module
assert mda.due.citations[
("MDAnalysis.coordinates.H5MD", "10.1016/j.cpc.2014.01.018")
].cites_module
11 changes: 6 additions & 5 deletions testsuite/MDAnalysisTests/utils/test_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@


def test_failure():
"""Fail if the MDA_FAILURE_TEST environment variable is set.
"""
if u'MDA_FAILURE_TEST' in os.environ:
"""Fail if the MDA_FAILURE_TEST environment variable is set."""
if "MDA_FAILURE_TEST" in os.environ:
# Have a file open to trigger an output from the open_files plugin.
f = open('./failure.txt', 'w')
raise AssertionError("the MDA_FAILURE_TEST environment variable is set")
f = open("./failure.txt", "w")
raise AssertionError(
"the MDA_FAILURE_TEST environment variable is set"
)
22 changes: 14 additions & 8 deletions testsuite/MDAnalysisTests/utils/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

path_to_testing_modules = MDAnalysisTests.__path__[0]
# Exclusion path relative to MDAnalysisTests
exclusions = ['/plugins', '/data']
exclusions = ["/plugins", "/data"]


def is_excluded(path):
leaf = path[len(path_to_testing_modules):]
leaf = path[len(path_to_testing_modules) :]
return leaf in exclusions


Expand All @@ -42,20 +42,26 @@ def get_file_paths():
for dirpath, dirnames, files in os.walk(path_to_testing_modules):
if is_excluded(dirpath):
continue
for f in filter(lambda x: x.endswith('.py'), files):
for f in filter(lambda x: x.endswith(".py"), files):
fpath = os.path.join(dirpath, f)
if is_excluded(fpath):
continue
paths.append(fpath)
return paths


@pytest.mark.parametrize('testing_module', get_file_paths())
@pytest.mark.parametrize("testing_module", get_file_paths())
def test_relative_import(testing_module):
with open(testing_module, 'r') as test_module_file_object:
with open(testing_module, "r") as test_module_file_object:
for lineno, line in enumerate(test_module_file_object, start=1):
if 'from .' in line and 'import' in line \
and not 'test_imports' in testing_module:
if (
"from ." in line
and "import" in line
and not "test_imports" in testing_module
):
raise AssertionError(
"A relative import statement was found in "
"module {testing_module} at linenumber {lineno}.".format(**vars()))
"module {testing_module} at linenumber {lineno}.".format(
**vars()
)
)
7 changes: 5 additions & 2 deletions testsuite/MDAnalysisTests/utils/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ def buffer():


def _assert_in(output, string):
assert string in output, "Output '{0}' does not match required format '{1}'.".format(output.replace('\r', '\\r'), string.replace('\r', '\\r'))

assert (
string in output
), "Output '{0}' does not match required format '{1}'.".format(
output.replace("\r", "\\r"), string.replace("\r", "\\r")
)
Loading

0 comments on commit 65ed20a

Please sign in to comment.