nose-devs / nose

nose is nicer testing for python
http://readthedocs.org/docs/nose/en/latest/
1.36k stars 395 forks source link

FileNotFoundError: [Errno 2] No such file or directory: '' #1037

Open davidak opened 7 years ago

davidak commented 7 years ago

I'm packaging a python package for NixOS that uses nose for testing.

The tests run locally and on Travis CI, but fails on build with Nix (Package Manager of NixOS).

https://travis-ci.org/davidak/PyZufall/jobs/203049456

Error with Nix

======================================================================
ERROR: Failure: FileNotFoundError ([Errno 2] No such file or directory: '')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/nix/store/fphi96k30wxi3kx2ax5mcdnnbqw5sk86-python3.5-nose-1.3.7/lib/python3.5/site-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/nix/store/fphi96k30wxi3kx2ax5mcdnnbqw5sk86-python3.5-nose-1.3.7/lib/python3.5/site-packages/nose/loader.py", line 417, in loadTestsFromName
    addr.filename, addr.module)
  File "/nix/store/fphi96k30wxi3kx2ax5mcdnnbqw5sk86-python3.5-nose-1.3.7/lib/python3.5/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/nix/store/fphi96k30wxi3kx2ax5mcdnnbqw5sk86-python3.5-nose-1.3.7/lib/python3.5/site-packages/nose/importer.py", line 86, in importFromDir
    if (self.sameModule(old, filename)
  File "/nix/store/fphi96k30wxi3kx2ax5mcdnnbqw5sk86-python3.5-nose-1.3.7/lib/python3.5/site-packages/nose/importer.py", line 131, in sameModule
    if _samefile(mod_path, new_path):
  File "/nix/store/qqq6vq5lpshvbgsx8nhzh9mpjvjjr5wm-python3-3.5.3/lib/python3.5/genericpath.py", line 90, in samefile
    s1 = os.stat(f1)
FileNotFoundError: [Errno 2] No such file or directory: ''

Full output: https://gist.github.com/davidak/7a8ff78b5fc08864fd23fda63ae5995b

What is happening there and how can i fix it?


nosetests version 1.3.7

Mekk commented 6 years ago

I faced the same error and digged a bit (using nose 1.3.7 pip-installed on my Ubuntu). Here is what happens in my case:

  1. I have ./tests directory with various test«sth».py files and a few utl«sth».py files

  2. In crashing case, the function sameModule from importer.py is called with args , 'utl_helpers.pyc'.

  3. It doesn't find __path__ attribute, so enters elif for __file__

  4. As __file__ is equal to 'utl_helpers.pyc', the function _dirname_if_file returns just '' for it (os.path.dirname('utl_helpers.pyc') yields '')

  5. So mod_paths is calculated to be ['']

  6. Later on this '' is passed to _samefile and crashes.

Mekk commented 6 years ago

I am not 100% sure what exactly triggers the error (probably having utility.py imported from more than one test). Still, the problem disappeared once I changed _dirname_if_file to:

    def _dirname_if_file(self, filename):
        # We only take the dirname if we have a path to a non-dir,
        # because taking the dirname of a symlink to a directory does not
        # give the actual directory parent.
        if os.path.isdir(filename):
            return filename
        else:
            return os.path.dirname(os.path.abspath(filename))

(added os.path.abspath)

mayu2010 commented 4 years ago

I think the reason is that something in package's "__init__.py" breaks nose importer logic. I had the same problem, when I removed

__path__ = __file__

from my '__init__.py' file, the problem was fixed.