Skip to content

Commit

Permalink
Merge pull request #338 from nephila/feature/detect_settings_module_env
Browse files Browse the repository at this point in the history
Detect incompatible DJANGO_SETTINGS_MODULE
  • Loading branch information
yakky authored Dec 22, 2018
2 parents 85238dd + cdaf72f commit 212a27e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ History
1.1.0 (unreleased)
++++++++++++++++++

* Add support for django CMS 3.6 (as develop version until release)
* Added support for django CMS 3.6 (as develop version until release)
* Added detection of incompatible DJANGO_SETTINGS_MODULE environment variable

1.0.2 (2018-11-21)
++++++++++++++++++
Expand Down
10 changes: 10 additions & 0 deletions djangocms_installer/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ def parse(args):
)
sys.exit(6)

default_settings = '{}.settings'.format(args.project_name)
env_settings = os.environ.get('DJANGO_SETTINGS_MODULE', default_settings)
if env_settings != default_settings:
sys.stderr.write(
'`DJANGO_SETTINGS_MODULE` is currently set to \'{0}\' which is not compatible with '
'djangocms installer.\nPlease unset `DJANGO_SETTINGS_MODULE` and re-run the installer '
'\n'.format(env_settings)
)
sys.exit(7)

if not getattr(args, 'requirements_file'):
requirements = []

Expand Down
2 changes: 2 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def tearDown(self):
self._remove_project_dir()
self.stdout = None
self.stderr = None
if 'DJANGO_SETTINGS_MODULE' in os.environ:
del os.environ['DJANGO_SETTINGS_MODULE']
sys.path = self.syspath

def setUp(self):
Expand Down
41 changes: 41 additions & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_invalid_choices(self):
'--i18n=no',
'-p'+self.project_dir,
'example_prj'])
self.assertEqual(error.exception.code, 2)
if six.PY3:
self.assertTrue(self.stderr.getvalue().find('--cms-version/-v: invalid choice: \'2.6\'') > -1)
else:
Expand All @@ -196,6 +197,7 @@ def test_invalid_project_name(self):
'--db=postgres://user:pwd@host/dbname',
'-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)

stderr_tmp = StringIO()
Expand All @@ -206,6 +208,7 @@ def test_invalid_project_name(self):
'--db=postgres://user:pwd@host/dbname',
'-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)

stderr_tmp = StringIO()
Expand All @@ -216,6 +219,7 @@ def test_invalid_project_name(self):
'--db=postgres://user:pwd@host/dbname',
'-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)

stderr_tmp = StringIO()
Expand All @@ -226,6 +230,7 @@ def test_invalid_project_name(self):
'--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 a valid app name') > -1)

def test_invalid_project_path(self):
Expand All @@ -241,6 +246,7 @@ def test_invalid_project_path(self):
'-p'+self.project_dir,
prj_dir])
self.assertEqual(conf_data.project_path, existing_path)
self.assertEqual(error.exception.code, 4)
self.assertTrue(self.stderr.getvalue().find('Path "%s" already exists and is not empty' % self.project_dir) > -1)

def test_invalid_project_dir(self):
Expand All @@ -257,6 +263,7 @@ def test_invalid_project_dir(self):
'-p'+self.project_dir,
prj_dir])
self.assertEqual(conf_data.project_path, existing_path)
self.assertEqual(error.exception.code, 4)
self.assertTrue(self.stderr.getvalue().find('Path "%s" already exists and is not empty' % self.project_dir) > -1)

def test_invalid_project_dir_skip(self):
Expand Down Expand Up @@ -287,6 +294,40 @@ def test_valid_project_dir(self):
prj_dir])
self.assertFalse(self.stderr.getvalue().find('Path "%s" already exists and is not empty' % self.project_dir) > -1)

def test_invalid_django_settings_module(self):
prj_dir = 'example_prj'
existing_path = os.path.join(self.project_dir, '.hidden_file')
with open(existing_path, 'w') as f:
f.write('')
os.environ['DJANGO_SETTINGS_MODULE'] = 'some_module.settings'
with patch('sys.stdout', self.stdout):
with patch('sys.stderr', self.stderr):
with self.assertRaises(SystemExit) as error:
conf_data = config.parse([
'-q',
'--db=postgres://user:pwd@host/dbname',
'-p'+self.project_dir,
prj_dir])
self.assertEqual(error.exception.code, 7)
self.assertTrue(self.stderr.getvalue().find('DJANGO_SETTINGS_MODULE') > -1)
self.assertTrue(self.stderr.getvalue().find('some_module.settings') > -1)

def test_valid_django_settings_module(self):
prj_dir = 'example_prj'
existing_path = os.path.join(self.project_dir, '.hidden_file')
with open(existing_path, 'w') as f:
f.write('')
os.environ['DJANGO_SETTINGS_MODULE'] = 'example_prj.settings'
with patch('sys.stdout', self.stdout):
with patch('sys.stderr', self.stderr):
conf_data = config.parse([
'-q',
'--db=postgres://user:pwd@host/dbname',
'-p'+self.project_dir,
prj_dir])
self.assertFalse(self.stderr.getvalue().find('DJANGO_SETTINGS_MODULE') > -1)
self.assertFalse(self.stderr.getvalue().find('some_module.settings') > -1)

def test_latest_version(self):
self.assertEqual(less_than_version('2.4'), '2.5')
self.assertEqual(less_than_version('3'), '3.1')
Expand Down

0 comments on commit 212a27e

Please sign in to comment.