Skip to content

Commit

Permalink
Check for dots in project name and report django-admin errors (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky authored Jun 29, 2019
1 parent 3e2a68d commit 26f82b3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion djangocms_installer/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def parse(args):
# First of all, check if the project name is valid
if not validate_project(args.project_name):
sys.stderr.write(
'Project name "{0}" is not a valid app name, or it\'s already defined. '
'Project name "{0}" is not valid or it\'s already defined. '
'Please use only numbers, letters and underscores.\n'.format(args.project_name)
)
sys.exit(3)
Expand Down
5 changes: 4 additions & 1 deletion djangocms_installer/config/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from __future__ import absolute_import, print_function, unicode_literals

import keyword
import re
import sys
from argparse import Action

import dj_database_url

from .data import DRIVERS

project_name_rx = re.compile(r'^[a-z0-9_A-Z]+$')


class DbAction(Action):

Expand All @@ -30,7 +33,7 @@ def validate_project(project_name):
Check the defined project name against keywords, builtins and existing
modules to avoid name clashing
"""
if '-' in project_name:
if not project_name_rx.search(project_name):
return None
if keyword.iskeyword(project_name):
return None
Expand Down
4 changes: 1 addition & 3 deletions djangocms_installer/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def create_project(config_data):
output = subprocess.check_output(cmd_args, stderr=subprocess.STDOUT)
sys.stdout.write(output.decode('utf-8'))
except subprocess.CalledProcessError as e: # pragma: no cover
if config_data.verbose:
sys.stdout.write(e.output.decode('utf-8'))
raise
raise RuntimeError(e.output.decode('utf-8'))


def _detect_migration_layout(vars, apps):
Expand Down
30 changes: 26 additions & 4 deletions tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_invalid_project_name(self):
'-p'+self.project_dir,
'test'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "test" is not a valid app name') > -1)
self.assertTrue(stderr_tmp.getvalue().find('Project name "test" is not valid') > -1)

stderr_tmp = StringIO()
with patch('sys.stderr', stderr_tmp):
Expand All @@ -225,7 +225,7 @@ def test_invalid_project_name(self):
'-p'+self.project_dir,
'assert'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "assert" is not a valid app name') > -1)
self.assertTrue(stderr_tmp.getvalue().find('Project name "assert" is not valid') > -1)

stderr_tmp = StringIO()
with patch('sys.stderr', stderr_tmp):
Expand All @@ -236,7 +236,7 @@ def test_invalid_project_name(self):
'-p'+self.project_dir,
'values'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "values" is not a valid app name') > -1)
self.assertTrue(stderr_tmp.getvalue().find('Project name "values" is not valid') > -1)

stderr_tmp = StringIO()
with patch('sys.stderr', stderr_tmp):
Expand All @@ -247,7 +247,29 @@ def test_invalid_project_name(self):
'-p'+self.project_dir,
'project-name'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "project-name" is not a valid app name') > -1)
self.assertTrue(stderr_tmp.getvalue().find('Project name "project-name" is not valid') > -1)

stderr_tmp = StringIO()
with patch('sys.stderr', stderr_tmp):
with self.assertRaises(SystemExit) as error:
conf_data = config.parse([
'-q',
'--db=postgres://user:pwd@host/dbname',
'-p'+self.project_dir,
'project.name'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "project.name" is not valid') > -1)

stderr_tmp = StringIO()
with patch('sys.stderr', stderr_tmp):
with self.assertRaises(SystemExit) as error:
conf_data = config.parse([
'-q',
'--db=postgres://user:pwd@host/dbname',
'-p'+self.project_dir,
'project?name'])
self.assertEqual(error.exception.code, 3)
self.assertTrue(stderr_tmp.getvalue().find('Project name "project?name" is not valid') > -1)

def test_invalid_project_path(self):
prj_dir = 'example_prj'
Expand Down
11 changes: 11 additions & 0 deletions tests/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def test_create_project(self):
django.create_project(config_data)
self.assertTrue(os.path.exists(os.path.join(self.project_dir, 'example_prj')))

def test_django_admin_errors(self):
dj_version, dj_match = get_latest_django(latest_stable=True)
config_data = config.parse(['--db=postgres://user:pwd@host/dbname',
'--cms-version=stable', '--django=%s' % dj_version,
'-q', '-p' + self.project_dir, 'example_prj'])
install.requirements(config_data.requirements)
config_data.project_name = 'example.prj'
with self.assertRaises(RuntimeError) as e:
django.create_project(config_data)
self.assertTrue('\'example.prj\' is not a valid project name.' in str(e.exception))

def test_copy_data(self):
"""
Test correct file copying with different switches
Expand Down

0 comments on commit 26f82b3

Please sign in to comment.