Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

added tests for scrapy-ctl commands

Ismael Carnales0b68d4bbef48
diff --git a/scrapy/command/commands/genspider.py b/scrapy/command/commands/genspider.py
index 1bab825cd..048eec2b4 100644
--- a/scrapy/command/commands/genspider.py
+++ b/scrapy/command/commands/genspider.py
@@ -2,6 +2,7 @@ import shutil
import string
from os import listdir
from os.path import join, dirname, abspath, exists
+import sys
import scrapy
from scrapy.spider import spiders
@@ -62,7 +63,7 @@ class Command(ScrapyCommand):
if spider and not opts.force:
print "Spider '%s' already exists in module:" % domain
print " %s" % spider.__module__
- return
+ sys.exit(1)
template_file = self._find_template(opts.template)
if template_file:
diff --git a/scrapy/command/commands/startproject.py b/scrapy/command/commands/startproject.py
index ba4b50629..6b9664326 100644
--- a/scrapy/command/commands/startproject.py
+++ b/scrapy/command/commands/startproject.py
@@ -3,6 +3,7 @@
import os
import string
import re
+import sys
import scrapy
from scrapy.command import ScrapyCommand
@@ -44,7 +45,12 @@ class Command(ScrapyCommand):
else:
message = 'Project names must contain only letters, numbers and underscores'
print "Invalid project name: %s\n\n%s" % (project_name, message)
+ sys.exit(1)
else:
+ if os.path.exists(project_name):
+ print "%s dir already exists" % project_name
+ sys.exit(1)
+
project_root_path = project_name
roottpl = os.path.join(PROJECT_TEMPLATES_PATH, 'root')
diff --git a/scrapy/tests/test_scrapy_ctl.py b/scrapy/tests/test_scrapy_ctl.py
new file mode 100644
index 000000000..f8177143b
--- /dev/null
+++ b/scrapy/tests/test_scrapy_ctl.py
@@ -0,0 +1,117 @@
+import os
+from os.path import exists, join
+import subprocess
+from shutil import rmtree
+import sys
+from tempfile import mkdtemp
+from unittest import TestCase
+
+import scrapy
+from scrapy.conf import settings
+
+
+class ProjectTest(TestCase):
+ project_name = 'testproject'
+
+ def setUp(self):
+ self.temp_path = mkdtemp()
+ self.cwd = self.temp_path
+
+ def tearDown(self):
+ rmtree(self.temp_path)
+
+ def call(self, new_args, **kwargs):
+ out = os.tmpfile()
+ args = [sys.executable, '-m', 'scrapy.command.cmdline']
+ args.extend(new_args)
+
+ env = self.env if hasattr(self, 'env') else os.environ
+
+ return subprocess.call(args, stdout=out, stderr=out, cwd=self.cwd, \
+ env=env, **kwargs)
+
+
+class ScrapyCtlStartprojectTest(ProjectTest):
+
+ def test_startproject(self):
+ proj_path = join(self.temp_path, self.project_name)
+ proj_mod_path = join(proj_path, self.project_name)
+
+ ret = self.call(['startproject', self.project_name])
+ self.assertEqual(ret, 0)
+
+ self.assertEqual(exists(join(proj_path, 'scrapy-ctl.py')), True)
+ self.assertEqual(exists(join(proj_path, 'testproject')), True)
+ self.assertEqual(exists(join(proj_mod_path, '__init__.py')), True)
+ self.assertEqual(exists(join(proj_mod_path, 'items.py')), True)
+ self.assertEqual(exists(join(proj_mod_path, 'pipelines.py')), True)
+ self.assertEqual(exists(join(proj_mod_path, 'settings.py')), True)
+ self.assertEqual(exists(join(proj_mod_path, 'spiders', '__init__.py')), True)
+
+ ret = self.call(['startproject', self.project_name])
+ self.assertEqual(ret, 1)
+
+ ret = self.call(['startproject', 'wrong---project---name'])
+ self.assertEqual(ret, 1)
+
+
+class CommandTest(ProjectTest):
+
+ def setUp(self):
+ super(CommandTest, self).setUp()
+
+ self.call(['startproject', self.project_name])
+
+ self.cwd = join(self.temp_path, self.project_name)
+
+ self.env = os.environ.copy()
+ self.env.pop('SCRAPY_SETTINGS_DISABLED', None)
+ self.env['SCRAPY_SETTINGS_MODULE'] = '%s.settings' % self.project_name
+
+
+class ScrapyCtlCommandsTest(CommandTest):
+
+ def test_crawl(self):
+ ret = self.call(['crawl'])
+ self.assertEqual(ret, 0)
+
+ def test_genspider_subcommands(self):
+ ret = self.call(['genspider', '--list'])
+ self.assertEqual(ret, 0)
+
+ ret = self.call(['genspider', '--dump'])
+ self.assertEqual(ret, 0)
+
+ ret = self.call(['genspider', '--dump', '--template=basic'])
+ self.assertEqual(ret, 0)
+
+ def test_list(self):
+ ret = self.call(['list'])
+ self.assertEqual(ret, 0)
+
+
+class BaseGenspiderTest(CommandTest):
+ template = 'basic'
+
+ def test_genspider(self):
+ ret = self.call(['genspider', 'testspider', 'test.com',
+ '--template=%s' % self.template])
+ self.assertEqual(ret, 0)
+ self.assertEqual(os.path.exists(join(self.cwd, self.project_name, \
+ 'spiders', 'testspider.py')), True)
+
+ ret = self.call(['genspider', 'otherspider', 'test.com'])
+ self.assertEqual(ret, 1)
+
+
+class CrawlGenspiderTest(BaseGenspiderTest):
+ template = 'crawl'
+
+
+class CsvFeedGenspiderTest(BaseGenspiderTest):
+ template = 'csvfeed'
+
+
+class XMLFeedGenspiderTest(BaseGenspiderTest):
+ template = 'xmlfeed'
+