Scrapy, a fast high-level web crawling & scraping framework for Python.
Exit non-zero when crawl closes via CLOSESPIDER_ERRORCOUNT (#8082)
Adrian2af4532c9dfa
diff --git a/scrapy/commands/__init__.py b/scrapy/commands/__init__.pyindex 08213b574..d85cac2e7 100644--- a/scrapy/commands/__init__.py+++ b/scrapy/commands/__init__.py@@ -236,6 +236,19 @@ class BaseRunSpiderCommand(ScrapyCommand):)return crawler+ def _run_crawler(+ self, spidercls: type[Spider] | str, opts: argparse.Namespace+ ) -> None:+ assert self.crawler_process is not None+ crawler = self._create_crawler(spidercls)+ self.crawler_process.crawl(crawler, **opts.spargs)+ self.crawler_process.start()+ if (+ self.crawler_process.bootstrap_failed+ or crawler.stats.get_value("finish_reason") == "closespider_errorcount"+ ):+ self.exitcode = 1+def _warn_if_feeds_unused(self, sender: Crawler, **kwargs: Any) -> None:if (sender.settings.getdict("FEEDS")diff --git a/scrapy/commands/crawl.py b/scrapy/commands/crawl.pyindex 7cc761142..8315ca8be 100644--- a/scrapy/commands/crawl.py+++ b/scrapy/commands/crawl.py@@ -25,10 +25,4 @@ class Command(BaseRunSpiderCommand):raise UsageError("running 'scrapy crawl' with more than one spider is not supported")- spname = args[0]-- assert self.crawler_process- self.crawler_process.crawl(self._create_crawler(spname), **opts.spargs)- self.crawler_process.start()- if self.crawler_process.bootstrap_failed:- self.exitcode = 1+ self._run_crawler(args[0], opts)diff --git a/scrapy/commands/runspider.py b/scrapy/commands/runspider.pyindex 09d268f1e..ebaa60cfe 100644--- a/scrapy/commands/runspider.py+++ b/scrapy/commands/runspider.py@@ -56,11 +56,4 @@ class Command(BaseRunSpiderCommand):spclasses = list(iter_spider_classes(module))if not spclasses:raise UsageError(f"No spider found in file: {filename}\n")- spidercls = spclasses.pop()-- assert self.crawler_process- self.crawler_process.crawl(self._create_crawler(spidercls), **opts.spargs)- self.crawler_process.start()-- if self.crawler_process.bootstrap_failed:- self.exitcode = 1+ self._run_crawler(spclasses.pop(), opts)diff --git a/tests/test_command_crawl.py b/tests/test_command_crawl.pyindex 5306e3bf8..5261df562 100644--- a/tests/test_command_crawl.py+++ b/tests/test_command_crawl.py@@ -47,13 +47,14 @@ class MySpider(scrapy.Spider):returnyield"""- log = self.get_log(spider_code, proj_path)+ returncode, _, log = self.crawl(spider_code, proj_path)assert "[myspider] DEBUG: It works!" in logassert ("Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor"in log)assert "Spider closed (finished)" in log+ assert returncode == 0def test_output(self, proj_path: Path) -> None:spider_code = """@@ -117,6 +118,22 @@ class MySpider(scrapy.Spider):"error: Please use only one of -o/--output and -O/--overwrite-output" in log)+ def test_errorcount_exit_code(self, proj_path: Path) -> None:+ spider_code = """+import scrapy++class MySpider(scrapy.Spider):+ name = 'myspider'+ custom_settings = {'CLOSESPIDER_ERRORCOUNT': 1}++ async def start(self):+ raise Exception('Expected exception')+ yield+"""+ returncode, _, err = self.crawl(spider_code, proj_path)+ assert "Spider closed (closespider_errorcount)" in err+ assert returncode != 0+def test_default_reactor(self, proj_path: Path) -> None:spider_code = """import scrapydiff --git a/tests/test_command_runspider.py b/tests/test_command_runspider.pyindex fa1763d21..113b655ed 100644--- a/tests/test_command_runspider.py+++ b/tests/test_command_runspider.py@@ -160,6 +160,22 @@ class MySpider(scrapy.Spider):assert "start" in logassert "badspider.py" in log, log+ def test_errorcount_exit_code(self, tmp_path: Path) -> None:+ spider_code = """+import scrapy++class MySpider(scrapy.Spider):+ name = 'myspider'+ custom_settings = {'CLOSESPIDER_ERRORCOUNT': 1}++ async def start(self):+ raise Exception('Expected exception')+ yield+"""+ returncode, _, err = self.runspider(tmp_path, spider_code)+ assert "Spider closed (closespider_errorcount)" in err+ assert returncode != 0+def test_asyncio_enabled_true(self, tmp_path: Path) -> None:log = self.get_log(tmp_path,