mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
Eliminate renaming plinth.config file for tests
- Instead of renaming plinth.config, make a copy and run all feasible tests on the copy, then delete the copy.
This commit is contained in:
parent
e69b500247
commit
15161e10b1
@ -25,34 +25,87 @@ from plinth import cfg
|
|||||||
|
|
||||||
|
|
||||||
CONFIG_FILENAME = 'plinth.config'
|
CONFIG_FILENAME = 'plinth.config'
|
||||||
SAVED_CONFIG_FILE = CONFIG_FILENAME + '.official'
|
TEST_CONFIG_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
||||||
CONFIG_FILE_WITH_MISSING_OPTIONS = CONFIG_FILENAME +\
|
'data')
|
||||||
'.with_missing_options'
|
TEST_CONFIG_FILE = os.path.join(TEST_CONFIG_DIR, CONFIG_FILENAME)
|
||||||
CONFIG_FILE_WITH_MISSING_SECTIONS = CONFIG_FILENAME +\
|
SAVED_CONFIG_FILE = os.path.join(TEST_CONFIG_DIR,
|
||||||
'.with_missing_sections'
|
CONFIG_FILENAME + '.official')
|
||||||
|
CONFIG_FILE_WITH_MISSING_OPTIONS = os.path.join(TEST_CONFIG_DIR,
|
||||||
|
CONFIG_FILENAME +
|
||||||
|
'.with_missing_options')
|
||||||
|
CONFIG_FILE_WITH_MISSING_SECTIONS = os.path.join(TEST_CONFIG_DIR,
|
||||||
|
CONFIG_FILENAME +
|
||||||
|
'.with_missing_sections')
|
||||||
|
|
||||||
|
|
||||||
class CfgTestCase(unittest.TestCase):
|
class CfgTestCase(unittest.TestCase):
|
||||||
"""Verify that the Plinth configuration module behaves as expected."""
|
"""Verify that the Plinth configuration module behaves as expected.
|
||||||
|
|
||||||
config_file = ''
|
This class deals with involving the plinth.config file in testing by
|
||||||
directory = ''
|
(1) independently locating the copy of the file that the cfg module would
|
||||||
|
find and read, then (2) copying that file to plinth/tests/data for use for
|
||||||
|
the actual tests, and finally (3) redirecting cfg (via its
|
||||||
|
DEFAULT_CONFIG_FILE attribute) to read that test copy. The test copy is
|
||||||
|
then deleted as part of the test case cleanup.
|
||||||
|
"""
|
||||||
|
|
||||||
|
default_config_file = ''
|
||||||
|
default_root = ''
|
||||||
|
default_config_file_present = False
|
||||||
|
fallback_config_file = ''
|
||||||
|
fallback_root = ''
|
||||||
|
fallback_config_file_present = False
|
||||||
|
|
||||||
|
# Setup and Teardown
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
"""Locate the official plinth.config file."""
|
"""Locate and copy the official plinth.config file."""
|
||||||
if os.path.isfile(cfg.DEFAULT_CONFIG_FILE):
|
# Save the cfg module default values
|
||||||
cls.config_file = cfg.DEFAULT_CONFIG_FILE
|
cls.default_config_file = cfg.DEFAULT_CONFIG_FILE
|
||||||
cls.directory = cfg.DEFAULT_ROOT
|
cls.default_root = cfg.DEFAULT_ROOT
|
||||||
else:
|
|
||||||
cls.directory = os.path.realpath('.')
|
# Look for default config file
|
||||||
cls.config_file = os.path.join(cls.directory,
|
cls.default_config_file_present =\
|
||||||
CONFIG_FILENAME)
|
os.path.isfile(cls.default_config_file)
|
||||||
if not(os.path.isfile(cls.config_file)):
|
|
||||||
raise FileNotFoundError('File {} could not be found.',
|
# Look for fallback (non-default) config file
|
||||||
format(CONFIG_FILENAME))
|
cls.fallback_root = os.path.realpath('.')
|
||||||
|
cls.fallback_config_file = os.path.join(cls.fallback_root,
|
||||||
|
CONFIG_FILENAME)
|
||||||
|
cls.fallback_config_file_present =\
|
||||||
|
os.path.isfile(cls.fallback_config_file)
|
||||||
|
|
||||||
|
# If neither file is found...
|
||||||
|
if not (cls.default_config_file_present or
|
||||||
|
cls.fallback_config_file_present):
|
||||||
|
raise FileNotFoundError('File {} could not be found.'
|
||||||
|
.format(CONFIG_FILENAME))
|
||||||
|
|
||||||
|
# Copy an official config file to the plinth/tests/data directory...
|
||||||
|
if cls.default_config_file_present:
|
||||||
|
shutil.copy2(cls.default_config_file, TEST_CONFIG_FILE)
|
||||||
|
elif cls.fallback_config_file_present:
|
||||||
|
shutil.copy2(cls.fallback_config_file, TEST_CONFIG_FILE)
|
||||||
|
# ...and point cfg to that file as the default
|
||||||
|
cfg.DEFAULT_CONFIG_FILE = TEST_CONFIG_FILE
|
||||||
|
cfg.DEFAULT_ROOT = TEST_CONFIG_DIR
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
"""Cleanup after all tests are completed."""
|
||||||
|
# Restore the cfg module default values
|
||||||
|
cfg.DEFAULT_CONFIG_FILE = cls.default_config_file
|
||||||
|
cfg.DEFAULT_ROOT = cls.default_root
|
||||||
|
|
||||||
|
# Delete the test config file(s)
|
||||||
|
if os.path.isfile(TEST_CONFIG_FILE):
|
||||||
|
os.remove(TEST_CONFIG_FILE)
|
||||||
|
if os.path.isfile(SAVED_CONFIG_FILE):
|
||||||
|
os.remove(SAVED_CONFIG_FILE)
|
||||||
|
|
||||||
# Tests
|
# Tests
|
||||||
|
|
||||||
def test_read_main_menu(self):
|
def test_read_main_menu(self):
|
||||||
"""Verify that the cfg.main_menu container is initially empty."""
|
"""Verify that the cfg.main_menu container is initially empty."""
|
||||||
# Menu should be empty before...
|
# Menu should be empty before...
|
||||||
@ -61,15 +114,85 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
# ...and after reading the config file
|
# ...and after reading the config file
|
||||||
self.assertEqual(len(cfg.main_menu.items), 0)
|
self.assertEqual(len(cfg.main_menu.items), 0)
|
||||||
|
|
||||||
def test_read_official_config_file(self):
|
def test_read_default_config_file(self):
|
||||||
"""Verify that the plinth.config file can be read correctly."""
|
"""Verify that the default config file can be read correctly."""
|
||||||
# Read the plinth.config file directly
|
# Read the plinth.config file directly
|
||||||
parser = self.read_config_file(self.config_file)
|
parser = self.read_config_file(TEST_CONFIG_FILE, TEST_CONFIG_DIR)
|
||||||
|
|
||||||
# Read the plinth.config file via the cfg module
|
# Read the plinth.config file via the cfg module
|
||||||
cfg.read()
|
cfg.read()
|
||||||
|
|
||||||
# Compare the two sets of configuration values.
|
# Compare the two results
|
||||||
|
self.compare_configurations(parser)
|
||||||
|
|
||||||
|
def test_read_fallback_config_file(self):
|
||||||
|
"""Verify that the fallback config file can be read correctly.
|
||||||
|
|
||||||
|
This test will be executed only if there is a fallback (non-default)
|
||||||
|
configuration file available for reading. If so, the cfg default
|
||||||
|
values for config filename and root will be temporarily modified to
|
||||||
|
prevent any default file from being found, thus allowing the fallback
|
||||||
|
file to be located and read.
|
||||||
|
"""
|
||||||
|
if not self.fallback_config_file_present:
|
||||||
|
self.skipTest('A fallback copy of {} is not available.'
|
||||||
|
.format(CONFIG_FILENAME))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
cfg.DEFAULT_CONFIG_FILE = '/{}'.format(CONFIG_FILENAME)
|
||||||
|
cfg.DEFAULT_ROOT = '/'
|
||||||
|
parser = self.read_config_file(self.fallback_config_file,
|
||||||
|
self.fallback_root)
|
||||||
|
cfg.read()
|
||||||
|
self.compare_configurations(parser)
|
||||||
|
finally:
|
||||||
|
cfg.DEFAULT_CONFIG_FILE = self.default_config_file
|
||||||
|
cfg.DEFAULT_ROOT = self.default_root
|
||||||
|
|
||||||
|
def test_read_missing_config_file(self):
|
||||||
|
"""Verify that an exception is raised when there's no config file.
|
||||||
|
|
||||||
|
This test will be executed only if the fallback (non-default) copy of
|
||||||
|
plinth.config is NOT present. If there is only a single, default
|
||||||
|
config file available, then that file can be copied to a test area and
|
||||||
|
be hidden by temporary renaming. But if the default file is hidden
|
||||||
|
and the fallback file can be found in its place, the fallback file
|
||||||
|
will not be renamed. Instead, the entire test will be skipped.
|
||||||
|
"""
|
||||||
|
if self.fallback_config_file_present:
|
||||||
|
self.skipTest(
|
||||||
|
'Fallback copy of {} cannot be hidden to establish the test'
|
||||||
|
'pre-condition.'.format(CONFIG_FILENAME))
|
||||||
|
else:
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
try:
|
||||||
|
self.rename_test_config_file()
|
||||||
|
cfg.read()
|
||||||
|
finally:
|
||||||
|
self.restore_test_config_file()
|
||||||
|
|
||||||
|
def test_read_config_file_with_missing_sections(self):
|
||||||
|
"""Verify that missing configuration sections can be detected."""
|
||||||
|
self.assertRaises(configparser.NoSectionError,
|
||||||
|
self.read_temp_config_file,
|
||||||
|
CONFIG_FILE_WITH_MISSING_SECTIONS)
|
||||||
|
|
||||||
|
def test_read_config_file_with_missing_options(self):
|
||||||
|
"""Verify that missing configuration options can be detected."""
|
||||||
|
self.assertRaises(configparser.NoOptionError,
|
||||||
|
self.read_temp_config_file,
|
||||||
|
CONFIG_FILE_WITH_MISSING_OPTIONS)
|
||||||
|
|
||||||
|
# Helper Methods
|
||||||
|
|
||||||
|
def read_config_file(self, config_file, root):
|
||||||
|
"""Read the specified configuration file independently from cfg.py."""
|
||||||
|
parser = configparser.ConfigParser(defaults={'root': root})
|
||||||
|
parser.read(config_file)
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def compare_configurations(self, parser):
|
||||||
|
"""Compare two sets of configuration values."""
|
||||||
# Note that the count of items within each section includes the number
|
# Note that the count of items within each section includes the number
|
||||||
# of default items (1, for 'root').
|
# of default items (1, for 'root').
|
||||||
self.assertEqual(3, len(parser.items('Name')))
|
self.assertEqual(3, len(parser.items('Name')))
|
||||||
@ -99,63 +222,28 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(parser.get('Network', 'use_x_forwarded_host'),
|
self.assertEqual(parser.get('Network', 'use_x_forwarded_host'),
|
||||||
cfg.use_x_forwarded_host)
|
cfg.use_x_forwarded_host)
|
||||||
|
|
||||||
def test_read_missing_config_file(self):
|
def read_temp_config_file(self, test_file):
|
||||||
"""Verify that an exception is raised when there's no config file."""
|
|
||||||
with self.assertRaises(FileNotFoundError):
|
|
||||||
try:
|
|
||||||
self.rename_official_config_file()
|
|
||||||
cfg.read()
|
|
||||||
finally:
|
|
||||||
self.restore_official_config_file()
|
|
||||||
|
|
||||||
def test_read_config_file_with_missing_sections(self):
|
|
||||||
"""Verify that missing configuration sections can be detected."""
|
|
||||||
self.assertRaises(configparser.NoSectionError,
|
|
||||||
self.read_test_config_file,
|
|
||||||
CONFIG_FILE_WITH_MISSING_SECTIONS)
|
|
||||||
|
|
||||||
def test_read_config_file_with_missing_options(self):
|
|
||||||
"""Verify that missing configuration options can be detected."""
|
|
||||||
self.assertRaises(configparser.NoOptionError,
|
|
||||||
self.read_test_config_file,
|
|
||||||
CONFIG_FILE_WITH_MISSING_OPTIONS)
|
|
||||||
|
|
||||||
# Helper Methods
|
|
||||||
|
|
||||||
def read_config_file(self, file):
|
|
||||||
"""Read the configuration file independently from cfg.py."""
|
|
||||||
parser = configparser.ConfigParser(
|
|
||||||
defaults={'root': self.directory})
|
|
||||||
parser.read(file)
|
|
||||||
return parser
|
|
||||||
|
|
||||||
def read_test_config_file(self, test_file):
|
|
||||||
"""Read the specified test configuration file."""
|
"""Read the specified test configuration file."""
|
||||||
self.replace_official_config_file(test_file)
|
self.replace_test_config_file(test_file)
|
||||||
try:
|
try:
|
||||||
cfg.read()
|
cfg.read()
|
||||||
finally:
|
finally:
|
||||||
self.restore_official_config_file()
|
self.restore_test_config_file()
|
||||||
|
|
||||||
def rename_official_config_file(self):
|
def rename_test_config_file(self):
|
||||||
"""Rename the official config file so that it can't be read."""
|
"""Rename the test config file so that it can't be read."""
|
||||||
shutil.move(self.config_file,
|
shutil.move(TEST_CONFIG_FILE, SAVED_CONFIG_FILE)
|
||||||
os.path.join(self.directory, SAVED_CONFIG_FILE))
|
|
||||||
|
|
||||||
def replace_official_config_file(self, test_file):
|
def replace_test_config_file(self, test_file):
|
||||||
"""Replace plinth.config with the specified test config file."""
|
"""Replace plinth.config with the specified temporary config file."""
|
||||||
self.rename_official_config_file()
|
self.rename_test_config_file()
|
||||||
test_data_directory = os.path.join(os.path.dirname(
|
shutil.copy2(test_file, TEST_CONFIG_FILE)
|
||||||
os.path.realpath(__file__)), 'data')
|
|
||||||
shutil.copy2(os.path.join(test_data_directory, test_file),
|
|
||||||
self.config_file)
|
|
||||||
|
|
||||||
def restore_official_config_file(self):
|
def restore_test_config_file(self):
|
||||||
"""Restore the official plinth.config file."""
|
"""Restore the test plinth.config file."""
|
||||||
if os.path.isfile(self.config_file):
|
if os.path.isfile(TEST_CONFIG_FILE):
|
||||||
os.remove(self.config_file)
|
os.remove(TEST_CONFIG_FILE)
|
||||||
shutil.move(os.path.join(self.directory, SAVED_CONFIG_FILE),
|
shutil.move(SAVED_CONFIG_FILE, TEST_CONFIG_FILE)
|
||||||
self.config_file)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user