From c074991e067af25f3a2b3d6c7a44218d48761bc7 Mon Sep 17 00:00:00 2001 From: Tyler Reddy Date: Mon, 15 Jun 2015 11:31:30 +0100 Subject: [PATCH] -added new test_imports.py module to enforce the ban on relative imports in unit testing modules (Issue #189) -manually repaired unit testing modules caught by above unit testing of import statements --- testsuite/MDAnalysisTests/test_altloc.py | 2 +- testsuite/MDAnalysisTests/test_analysis.py | 2 +- testsuite/MDAnalysisTests/test_atomgroup.py | 2 +- testsuite/MDAnalysisTests/test_coordinates.py | 6 +-- testsuite/MDAnalysisTests/test_imports.py | 49 +++++++++++++++++++ testsuite/MDAnalysisTests/test_mol2.py | 2 +- testsuite/MDAnalysisTests/test_streamio.py | 4 +- .../MDAnalysisTests/test_timestep_api.py | 4 +- 8 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 testsuite/MDAnalysisTests/test_imports.py diff --git a/testsuite/MDAnalysisTests/test_altloc.py b/testsuite/MDAnalysisTests/test_altloc.py index 25c1dcfeaa4..889f03bffe0 100644 --- a/testsuite/MDAnalysisTests/test_altloc.py +++ b/testsuite/MDAnalysisTests/test_altloc.py @@ -17,7 +17,7 @@ import tempfile import os from numpy.testing import * -from .datafiles import altloc +from MDAnalysisTests.datafiles import altloc class TestAltloc(TestCase): diff --git a/testsuite/MDAnalysisTests/test_analysis.py b/testsuite/MDAnalysisTests/test_analysis.py index 024fefa7edf..96111adcd58 100644 --- a/testsuite/MDAnalysisTests/test_analysis.py +++ b/testsuite/MDAnalysisTests/test_analysis.py @@ -32,7 +32,7 @@ import itertools import warnings -from MDAnalysis.tests.datafiles import PSF, DCD, FASTA, PDB_helix, PDB_HOLE, XTC_HOLE, GRO, XTC, waterDCD, waterPSF +from MDAnalysisTests.datafiles import PSF, DCD, FASTA, PDB_helix, PDB_HOLE, XTC_HOLE, GRO, XTC, waterDCD, waterPSF from MDAnalysisTests import executable_not_found_runtime diff --git a/testsuite/MDAnalysisTests/test_atomgroup.py b/testsuite/MDAnalysisTests/test_atomgroup.py index 0f830dd8149..5f284ca599a 100644 --- a/testsuite/MDAnalysisTests/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/test_atomgroup.py @@ -32,7 +32,7 @@ import tempfile import itertools -from . import knownfailure +from MDAnalysisTests import knownfailure try: from numpy.testing import assert_ diff --git a/testsuite/MDAnalysisTests/test_coordinates.py b/testsuite/MDAnalysisTests/test_coordinates.py index d161a3b2658..47f575529b0 100644 --- a/testsuite/MDAnalysisTests/test_coordinates.py +++ b/testsuite/MDAnalysisTests/test_coordinates.py @@ -27,7 +27,7 @@ from nose.plugins.attrib import attr import warnings -from .datafiles import ( +from MDAnalysisTests.datafiles import ( PSF, DCD, DCD_empty, PDB_small, XPDB_small, PDB_closed, PDB_multiframe, PDB, CRD, XTC, TRR, GRO, DMS, CONECT, XYZ, XYZ_bz2, XYZ_psf, PRM, TRJ, TRJ_bz2, PRMpbc, TRJpbc_bz2, PRMncdf, NCDF, PQR, @@ -38,7 +38,7 @@ DLP_CONFIG, DLP_CONFIG_order, DLP_CONFIG_minimal, DLP_HISTORY, DLP_HISTORY_order, DLP_HISTORY_minimal) -from . import knownfailure +from MDAnalysisTests import knownfailure import os import shutil @@ -2978,7 +2978,7 @@ def test_datareader_VE(): class _TestLammpsData_Coords(TestCase): """Tests using a .data file for loading single frame. - All topology loading from .data is done in test_topology + All topology loading from MDAnalysisTests.data is done in test_topology """ def setUp(self): diff --git a/testsuite/MDAnalysisTests/test_imports.py b/testsuite/MDAnalysisTests/test_imports.py new file mode 100644 index 00000000000..cb53894575c --- /dev/null +++ b/testsuite/MDAnalysisTests/test_imports.py @@ -0,0 +1,49 @@ +# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 +# +# MDAnalysis --- http://www.MDAnalysis.org +# Copyright (c) 2006-2015 Naveen Michaud-Agrawal, Elizabeth J. Denning, Oliver Beckstein +# and contributors (see AUTHORS for the full list) +# +# Released under the GNU Public Licence, v2 or any higher version +# +# Please cite your use of MDAnalysis in published work: +# +# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein. +# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. +# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 +# +from __future__ import print_function +import MDAnalysis + +from numpy.testing import * +from nose.plugins.attrib import attr + +#import os +#import errno +#import tempfile +#import itertools +#import warnings +import glob + +from MDAnalysis.tests.datafiles import PSF, DCD, FASTA, PDB_helix, PDB_HOLE, XTC_HOLE, GRO, XTC, waterDCD, waterPSF + + +class TestRelativeImports(TestCase): + '''Relative imports are banned in unit testing modules (Issue #189), so run tests to enforce this policy.''' + + def setUp(self): + self.path_to_testing_modules = './' + + def tearDown(self): + del self.path_to_testing_modules + + def test_relative_imports(self): + list_testing_modules = glob.glob(self.path_to_testing_modules + '*.py') + for testing_module in list_testing_modules: + relative_import_counts = 0 + with open(testing_module, 'r') as test_module_file_object: + for line in test_module_file_object: + if 'from .' in line and 'import' in line and not 'test_imports' in testing_module: + relative_import_counts += 1 + assert_equal(relative_import_counts, 0, "A relative import statement was found in module {module_name}.".format(module_name = testing_module)) diff --git a/testsuite/MDAnalysisTests/test_mol2.py b/testsuite/MDAnalysisTests/test_mol2.py index 72381456c41..318a043928b 100644 --- a/testsuite/MDAnalysisTests/test_mol2.py +++ b/testsuite/MDAnalysisTests/test_mol2.py @@ -17,7 +17,7 @@ import os from numpy.testing import * -from .datafiles import mol2_molecules, mol2_molecule, mol2_broken_molecule +from MDAnalysisTests.datafiles import mol2_molecules, mol2_molecule, mol2_broken_molecule from MDAnalysis import Universe diff --git a/testsuite/MDAnalysisTests/test_streamio.py b/testsuite/MDAnalysisTests/test_streamio.py index e09ab11b574..15217031a0c 100644 --- a/testsuite/MDAnalysisTests/test_streamio.py +++ b/testsuite/MDAnalysisTests/test_streamio.py @@ -19,8 +19,8 @@ import MDAnalysis import MDAnalysis.core.util as util import MDAnalysis.tests.datafiles as datafiles -from .test_coordinates import RefAdKSmall -from . import knownfailure +from MDAnalysisTests.test_coordinates import RefAdKSmall +from MDAnalysisTests import knownfailure import StringIO import cStringIO diff --git a/testsuite/MDAnalysisTests/test_timestep_api.py b/testsuite/MDAnalysisTests/test_timestep_api.py index 297fe4f7f86..b6a940ee1a3 100644 --- a/testsuite/MDAnalysisTests/test_timestep_api.py +++ b/testsuite/MDAnalysisTests/test_timestep_api.py @@ -24,11 +24,11 @@ assert_array_almost_equal) from nose.plugins.attrib import attr from nose.tools import assert_not_equal -from . import knownfailure +from MDAnalysisTests import knownfailure import MDAnalysis as mda -from .datafiles import PSF, DCD, DCD_empty, PDB_small, XPDB_small, PDB_closed, PDB_multiframe, \ +from MDAnalysisTests.datafiles import PSF, DCD, DCD_empty, PDB_small, XPDB_small, PDB_closed, PDB_multiframe, \ PDB, CRD, XTC, TRR, GRO, DMS, CONECT, \ XYZ, XYZ_bz2, XYZ_psf, PRM, TRJ, TRJ_bz2, PRMpbc, TRJpbc_bz2, PRMncdf, NCDF, PQR, \ PDB_sub_dry, TRR_sub_sol, PDB_sub_sol, TRZ, TRZ_psf, LAMMPSdata, LAMMPSdata_mini, \