Scrapy, a fast high-level web crawling & scraping framework for Python.
more reliable way to set subprocess PYTHONPATH in tests
Pablo Hoffman3904f5f55a62
diff --git a/scrapy/tests/test_cmdline/__init__.py b/scrapy/tests/test_cmdline/__init__.pyindex 9327e262f..4bc2a0789 100644--- a/scrapy/tests/test_cmdline/__init__.py+++ b/scrapy/tests/test_cmdline/__init__.py@@ -3,12 +3,13 @@ import osfrom subprocess import Popen, PIPEimport unittest+from scrapy.utils.test import get_pythonpath+class CmdlineTest(unittest.TestCase):def setUp(self):self.env = os.environ.copy()- if 'PYTHONPATH' in os.environ:- self.env['PYTHONPATH'] = os.environ['PYTHONPATH']+ self.env['PYTHONPATH'] = get_pythonpath()self.env['SCRAPY_SETTINGS_MODULE'] = 'scrapy.tests.test_cmdline.settings'def _execute(self, *new_args, **kwargs):diff --git a/scrapy/tests/test_commands.py b/scrapy/tests/test_commands.pyindex 2495a0123..e3448e934 100644--- a/scrapy/tests/test_commands.py+++ b/scrapy/tests/test_commands.py@@ -8,7 +8,7 @@ from tempfile import mkdtempfrom twisted.trial import unittestfrom scrapy.utils.python import retry_on_eintr-+from scrapy.utils.test import get_pythonpathclass ProjectTest(unittest.TestCase):project_name = 'testproject'@@ -19,8 +19,7 @@ class ProjectTest(unittest.TestCase):self.proj_path = join(self.temp_path, self.project_name)self.proj_mod_path = join(self.proj_path, self.project_name)self.env = os.environ.copy()- if 'PYTHONPATH' in os.environ:- self.env['PYTHONPATH'] = os.environ['PYTHONPATH']+ self.env['PYTHONPATH'] = get_pythonpath()def tearDown(self):rmtree(self.temp_path)diff --git a/scrapy/utils/test.py b/scrapy/utils/test.pyindex 87cbc3ab8..42cdb906a 100644--- a/scrapy/utils/test.py+++ b/scrapy/utils/test.py@@ -2,13 +2,10 @@This module contains some assorted functions used in tests"""-import os+import os, sysfrom twisted.trial.unittest import SkipTest-from scrapy.crawler import Crawler-from scrapy.settings import CrawlerSettings-def libxml2debug(testfunction):"""Decorator for debugging libxml2 memory leaks inside a function.@@ -51,6 +48,9 @@ def get_crawler(settings_dict=None):will be used as the settings present in the settings module of theCrawlerSettings."""+ from scrapy.crawler import Crawler+ from scrapy.settings import CrawlerSettings+class SettingsModuleMock(object):passsettings_module = SettingsModuleMock()@@ -59,3 +59,10 @@ def get_crawler(settings_dict=None):setattr(settings_module, k, v)settings = CrawlerSettings(settings_module)return Crawler(settings)++def get_pythonpath():+ """Return a PYTHONPATH suitable to use in processes so that they find this+ installation of Scrapy"""+ sep = ';' if sys.platform == 'win32' else ':'+ scrapy_path = __import__('scrapy').__path__[0]+ return os.path.dirname(scrapy_path) + sep + os.environ.get('PYTHONPATH', '')