python-hospital/hospital

AssertionError: Path must be within the project

Closed this issue · 2 comments

Here is a sample traceback.

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "<virtualenv>lib/python2.7/site-packages/hospital/wsgi.py", line 122, in __call__
    suite = self.load_tests()
  File "<virtualenv>lib/python2.7/site-packages/hospital/cli.py", line 102, in load_tests
    start_dir, pattern='*'))
  File "<virtualenv>lib/python2.7/site-packages/hospital/loading.py", line 97, in discover
    top_level_dir=None)
  File "/usr/lib/python2.7/unittest/loader.py", line 204, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/usr/lib/python2.7/unittest/loader.py", line 250, in _find_tests
    name = self._get_name_from_path(full_path)
  File "/usr/lib/python2.7/unittest/loader.py", line 224, in _get_name_from_path
    assert not _relpath.startswith('..'), "Path must be within the project"
AssertionError: Path must be within the project

It is related to healthcheck discovery.
https://docs.python.org/2.7/library/unittest.html#unittest.TestLoader.discover mentions:

All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately.

But hospital's current implementation uses a start_dir that is outside project in some cases, as an example if you try to discover tests from standard library (try hospital-cli xml.etree).

Here is another traceback, a bit different but related to bad loading of packages/modules outside working directory:

$ hospital-cli xml.etree
EE
======================================================================
ERROR: ElementTree (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: ElementTree
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 127, in <module>
    from . import ElementPath
ValueError: Attempted relative import in non-package


======================================================================
ERROR: ElementInclude (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: ElementInclude
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/usr/lib/python2.7/xml/etree/ElementInclude.py", line 52, in <module>
    from . import ElementTree
ValueError: Attempted relative import in non-package