Adapt test and documentation to changes of '--develop' option

And re-activate cfg.get_config_paths() for easier testing

Signed-off-by: Michael Pimmer <info@fonfon.at>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Michael Pimmer 2018-06-12 19:37:31 +00:00 committed by James Valleroy
parent 5f033a8ba0
commit 425f7fbd92
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
5 changed files with 60 additions and 50 deletions

View File

@ -94,6 +94,10 @@
<filename>/etc/plinth/plinth.config</filename>. This
means that Plinth will be available as
http://localhost:8000/plinth by default.
When <filename>/etc/plinth/plinth.config</filename> is not
available, <filename>plinth.config</filename> from the current
working directory is used.
</para>
</listitem>
</varlistentry>
@ -221,7 +225,9 @@
<synopsis>$ plinth --debug --develop</synopsis>
<para>
Enable debug and development mode and run on terminal. This uses
the configuration and action files of the current working directory.
the configuration and action files of the current working directory and
gives debugging information like extended error pages, or not failing
silently on module initialization errors.
</para>
</example>
</refsect1>

View File

@ -384,24 +384,15 @@ def adapt_config(arguments):
setattr(cfg, argument_name, argument_value)
def get_relative_config_paths():
"""Get config paths of the current source code folder"""
root_directory = os.path.dirname(os.path.realpath(__file__))
root_directory = os.path.join(root_directory, '..')
root_directory = os.path.realpath(root_directory)
file_path = os.path.join(root_directory, 'plinth.config')
return {
'file_path': file_path,
'root_directory': root_directory,
}
def main():
"""Intialize and start the application"""
arguments = parse_arguments()
config_paths = {}
if arguments.develop:
config_paths = get_relative_config_paths()
# use the root and plinth.config of the current working directory
file_path, root_directory = cfg.get_fallback_config_paths()
config_paths['file_path'] = file_path
config_paths['root_directory'] = root_directory
cfg.read(**config_paths)
adapt_config(arguments)

View File

@ -45,22 +45,41 @@ DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config'
DEFAULT_ROOT = '/'
def read(file_path=None, root_directory=None):
def get_fallback_config_paths():
"""Get config paths of the current source code folder"""
root_directory = os.path.dirname(os.path.realpath(__file__))
root_directory = os.path.join(root_directory, '..')
root_directory = os.path.realpath(root_directory)
config_path = os.path.join(root_directory, 'plinth.config')
return config_path, root_directory
def get_config_paths():
"""Get config paths.
Return the fallback plinth config if the default one does not exist"""
root_directory = DEFAULT_ROOT
config_path = DEFAULT_CONFIG_FILE
if not os.path.isfile(config_path):
config_path, root_directory = get_fallback_config_paths()
return config_path, root_directory
def read(config_path=None, root_directory=None):
"""
Read configuration.
- file_path: path of plinth.config file
- config_path: path of plinth.config file
- root_directory: path of plinth root folder
"""
if not file_path and not root_directory:
root_directory = DEFAULT_ROOT
file_path = DEFAULT_CONFIG_FILE
if not config_path and not root_directory:
config_path, root_directory = get_config_paths()
if not os.path.isfile(file_path):
raise FileNotFoundError('No plinth.config file could be found.')
if not os.path.isfile(config_path):
msg = 'No plinth.config file could be found on path: %s' % config_path
raise FileNotFoundError(msg)
global config_file # pylint: disable-msg=invalid-name,global-statement
config_file = file_path
config_file = config_path
parser = configparser.ConfigParser(
defaults={

View File

@ -41,7 +41,7 @@ class TestCfg(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Locate and copy the official plinth.config file."""
cls.test_config_file, cls.test_config_dir = cfg.get_config_file()
cls.test_config_file, cls.test_config_dir = cfg.get_config_paths()
@classmethod
def tearDownClass(cls):
@ -62,42 +62,37 @@ class TestCfg(unittest.TestCase):
self.compare_configurations(parser)
def test_read_primary_config_file(self):
"""Verify that the primary config file can be read correctly."""
original_file_path = cfg.DEFAULT_CONFIG_FILE
"""Verify that the primary config file is used by default."""
original_config_path = cfg.DEFAULT_CONFIG_FILE
original_root_directory = cfg.DEFAULT_ROOT
expected_file_path = CONFIG_FILE_WITH_MISSING_OPTIONS
expected_root_directory = 'x-default-root'
expected_config_path = CONFIG_FILE_WITH_MISSING_OPTIONS
root_directory = 'x-default-root'
expected_root_directory = os.path.realpath(root_directory)
try:
cfg.DEFAULT_CONFIG_FILE = expected_file_path
cfg.DEFAULT_ROOT = expected_root_directory
file_path, root_directoy = cfg.get_config_file()
self.assertEqual(file_path, expected_file_path)
self.assertEqual(root_directoy, expected_root_directory)
cfg.DEFAULT_CONFIG_FILE = expected_config_path
cfg.DEFAULT_ROOT = root_directory
# reading the config file will fail, but still cfg.root and
# cfg.config_file will be set for parsing the config file
try:
cfg.read()
except configparser.NoOptionError:
pass
self.assertEqual(cfg.config_file, expected_config_path)
self.assertEqual(cfg.root, expected_root_directory)
finally:
cfg.DEFAULT_CONFIG_FILE = original_file_path
cfg.DEFAULT_CONFIG_FILE = original_config_path
cfg.DEFAULT_ROOT = original_root_directory
def test_read_fallback_config_file(self):
"""Verify that the fallback config file can be read correctly."""
original_file_path = cfg.DEFAULT_CONFIG_FILE
original_root_directory = cfg.DEFAULT_ROOT
"""Verify that the correct fallback config file is used"""
fallback_root = os.path.realpath('.')
fallback_config_file = os.path.join(fallback_root, 'plinth.config')
expected_file_path = os.path.realpath(fallback_config_file)
expected_root_directory = fallback_root
try:
cfg.DEFAULT_CONFIG_FILE = 'x-non-existant-file'
cfg.DEFAULT_ROOT = 'x-non-existant-directory'
file_path, root_directoy = cfg.get_config_file()
self.assertEqual(file_path, expected_file_path)
self.assertEqual(root_directoy, expected_root_directory)
finally:
cfg.DEFAULT_CONFIG_FILE = original_file_path
cfg.DEFAULT_ROOT = original_root_directory
config_path, root_directory = cfg.get_fallback_config_paths()
cfg.read(config_path, root_directory)
self.assertEqual(cfg.config_file, fallback_config_file)
self.assertEqual(cfg.root, fallback_root)
def test_read_missing_config_file(self):
"""Verify that an exception is raised when there's no config file."""

View File

@ -38,7 +38,6 @@ class TestSetupMiddleware(TestCase):
def setUpClass(cls):
"""Setup all the test cases."""
super(TestSetupMiddleware, cls).setUpClass()
cfg.read()
def setUp(self):