Skip to content

Commit

Permalink
Check if type of list elements are strings
Browse files Browse the repository at this point in the history
The members of lists specified in the YAML configuration for mmdebstrap
are expected to be strings. Add a check for that to prevent mmdebstrap
from failing due to malformed parameters.

Signed-off-by: Benjamin Drung <[email protected]>
  • Loading branch information
bdrung committed Jun 18, 2020
1 parent 7edfabf commit f3c7cce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bdebstrap
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ class Config(dict):
"Unexpected type '%s' for mmdebstrap option '%s'. "
"Excepted: %s." % (type(value), key, MMDEBSTRAP_OPTS[key])
)
if MMDEBSTRAP_OPTS[key] is list:
# Check if list elements are strings
for element in value:
if not isinstance(element, str):
raise ValueError(
"Following list element of mmdebstrap option '%s' has type '%s' "
"instead of string: %s" % (key, type(element).__name__, element)
)
else:
self.logger.warning("The configuration does not contain a 'mmdebstrap' entry.")

Expand Down
7 changes: 7 additions & 0 deletions tests/configs/wrong-element-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: wrong-element-type
mmdebstrap:
customize-hooks:
# Make vim the default editor
- chroot "$1" update-alternatives --set editor /usr/bin/vim.basic
# Passwordless sudo
- sed -i 's/^%sudo.*$/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/' "$1/etc/sudoers"
7 changes: 7 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ def test_source_date_epoch(self):
config.set_source_date_epoch()
self.assertEqual(config.source_date_epoch, 1581694618)

def test_wrong_element_type(self):
"""Test error message for wrong list element type."""
config = Config()
config.load(os.path.join(TEST_CONFIG_DIR, "wrong-element-type.yaml"))
with self.assertRaisesRegex(ValueError, "'customize-hooks' has type 'CommentedMap'"):
config.check()


class TestDictMerge(unittest.TestCase):
"""Unittests for dict_merge function."""
Expand Down

0 comments on commit f3c7cce

Please sign in to comment.