From caed78e11a0e18ced897a8e17066ea9d2c8fdfdc Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Thu, 22 Feb 2018 16:46:58 +0100 Subject: [PATCH] Ordering processing for volume ID substitutions Related: https://pagure.io/pungi/issue/840 Signed-off-by: Ondrej Nosek --- pungi/util.py | 5 ++++- tests/test_util.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pungi/util.py b/pungi/util.py index e46d29aa..dc2cd62a 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -322,7 +322,10 @@ def get_variant_data(conf, var_name, variant, keys=None): def _apply_substitutions(compose, volid): - for k, v in compose.conf['volume_id_substitutions'].items(): + substitutions = compose.conf['volume_id_substitutions'].items() + # processing should start with the longest pattern, otherwise, we could + # unexpectedly replace a substring of that longest pattern + for k, v in sorted(substitutions, reverse=True): volid = volid.replace(k, v) return volid diff --git a/tests/test_util.py b/tests/test_util.py index 1fe0fc37..ba60c79a 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -222,6 +222,27 @@ class TestVolumeIdGenerator(unittest.TestCase): self.assertIn('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', str(ctx.exception)) self.assertIn('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', str(ctx.exception)) + @mock.patch('pungi.compose.ComposeInfo') + def test_apply_substitutions(self, ci): + all_keys = [ + ('Fedora-WorkstationOstree-ostree-x86_64-rawhide', 'Fedora-WS-ostree-x86_64-rawhide'), + ('Fedora-WorkstationOstree-ostree-x86_64-Rawhide', 'Fedora-WS-ostree-x86_64-rawh'), + ('x86_64-compose_id-20160107', 'x86_64-compose_id-20160107'), + ('x86_64-compose_id-20160107-Alpha', 'x86_64-compose_id-20160107-A'), + ] + for volid, expected in all_keys: + conf = { + 'volume_id_substitutions': { + 'Rawhide': 'rawh', + 'WorkstationOstree': 'WS', + 'Workstation': 'WS', + 'Alpha': 'A', + } + } + c = compose.Compose(conf, self.tmp_dir) + transformed_volid = util._apply_substitutions(c, volid) + self.assertEqual(transformed_volid, expected) + class TestFindOldCompose(unittest.TestCase): def setUp(self):