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 <filename>/etc/plinth/plinth.config</filename>. This
means that Plinth will be available as means that Plinth will be available as
http://localhost:8000/plinth by default. 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> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -221,7 +225,9 @@
<synopsis>$ plinth --debug --develop</synopsis> <synopsis>$ plinth --debug --develop</synopsis>
<para> <para>
Enable debug and development mode and run on terminal. This uses 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> </para>
</example> </example>
</refsect1> </refsect1>

View File

@ -384,24 +384,15 @@ def adapt_config(arguments):
setattr(cfg, argument_name, argument_value) 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(): def main():
"""Intialize and start the application""" """Intialize and start the application"""
arguments = parse_arguments() arguments = parse_arguments()
config_paths = {} config_paths = {}
if arguments.develop: 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) cfg.read(**config_paths)
adapt_config(arguments) adapt_config(arguments)

View File

@ -45,22 +45,41 @@ DEFAULT_CONFIG_FILE = '/etc/plinth/plinth.config'
DEFAULT_ROOT = '/' 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. Read configuration.
- file_path: path of plinth.config file - config_path: path of plinth.config file
- root_directory: path of plinth root folder - root_directory: path of plinth root folder
""" """
if not file_path and not root_directory: if not config_path and not root_directory:
root_directory = DEFAULT_ROOT config_path, root_directory = get_config_paths()
file_path = DEFAULT_CONFIG_FILE
if not os.path.isfile(file_path): if not os.path.isfile(config_path):
raise FileNotFoundError('No plinth.config file could be found.') 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 global config_file # pylint: disable-msg=invalid-name,global-statement
config_file = file_path config_file = config_path
parser = configparser.ConfigParser( parser = configparser.ConfigParser(
defaults={ defaults={

View File

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

View File

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