nose-devs / nose

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

nosetests cannot load modules in vagrant environment #964

Open nabheet opened 8 years ago

nabheet commented 8 years ago

My code is in /vagrant where the local directory from my mac host is mounted. I get the following error when I run my tests

`(vagrant)-bash-4.1$ nosetests --verbosity=2 --match='some-regex' someFolder/someFile.py Failure: ImportError (No module named vagrant) ... ERROR

ERROR: Failure: ImportError (No module named vagrant)

Traceback (most recent call last): File "/vagrant/lib/python2.6/site-packages/nose/loader.py", line 418, in loadTestsFromName addr.filename, addr.module) File "/vagrant/lib/python2.6/site-packages/nose/importer.py", line 47, in importFromPath return self.importFromDir(dir_path, fqname) File "/vagrant/lib/python2.6/site-packages/nose/importer.py", line 79, in importFromDir fh, filename, desc = find_module(part, path) ImportError: No module named vagrant


Ran 1 test in 0.002s

FAILED (errors=1) (vagrant)-bash-4.1$`

I think the problem is in https://github.com/nose-devs/nose/blob/master/nose/importer.py at line 45 dir_path = os.sep.join(path_parts). This is a problem when path_parts just ends up with a list of one element which is [''] and the it tries to join it with the os.sep, it results in [''] which causes problems later.

I tried modifying the code to: if dir_path == '': dir_path = '/'

Apparently this might have fixed that particular problem but it cause a similar load issue on a different module.

So I just ended up defining a subfolder for the application in the Vagrantfile like this:

config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder ".", "/vagrant/subfolder"

Now my tests run just fine. I don't have a fix for importer.py yet, but wanted to share the issue and possible workaround with other people in case they encounter the same problem.

nabheet commented 8 years ago

might be similar to #518.