richardphibel / rpms / dnf

Forked from rpms/dnf 2 years ago
Clone

Blame SOURCES/0001-Handle-empty-comps-group-name-RhBug1826198.patch

e043d7
From 3c758a4ea670fab1f4b55fa878ebf2b2ff4b678b Mon Sep 17 00:00:00 2001
e043d7
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hr=C3=A1zk=C3=BD?= <lhrazky@redhat.com>
e043d7
Date: Tue, 28 Apr 2020 09:08:05 +0200
e043d7
Subject: [PATCH] Handle empty comps group name (RhBug:1826198)
e043d7
e043d7
Don't crash on empty comps group/environment name. In outputs, use the
e043d7
"<name-unset>" placeholder instead of the name.
e043d7
e043d7
https://bugzilla.redhat.com/show_bug.cgi?id=1826198
e043d7
---
e043d7
 dnf/cli/commands/group.py  |  4 ++--
e043d7
 dnf/cli/output.py          | 16 ++++++++++------
e043d7
 dnf/comps.py               | 11 ++++++++++-
e043d7
 dnf/db/group.py            | 12 ++++++++----
e043d7
 tests/repos/main_comps.xml |  7 +++++++
e043d7
 tests/support.py           |  2 +-
e043d7
 tests/test_comps.py        |  6 +++---
e043d7
 tests/test_groups.py       |  9 +++++++++
e043d7
 8 files changed, 50 insertions(+), 17 deletions(-)
e043d7
e043d7
diff --git a/dnf/cli/commands/group.py b/dnf/cli/commands/group.py
e043d7
index f535a50980..4ffd3b89c8 100644
e043d7
--- a/dnf/cli/commands/group.py
e043d7
+++ b/dnf/cli/commands/group.py
e043d7
@@ -177,7 +177,7 @@ def _list(self, userlist):
e043d7
         def _out_grp(sect, group):
e043d7
             if not done:
e043d7
                 print(sect)
e043d7
-            msg = '   %s' % group.ui_name
e043d7
+            msg = '   %s' % (group.ui_name if group.ui_name is not None else _("<name-unset>"))
e043d7
             if print_ids:
e043d7
                 msg += ' (%s)' % group.id
e043d7
             if group.lang_only:
e043d7
@@ -188,7 +188,7 @@ def _out_env(sect, envs):
e043d7
             if envs:
e043d7
                 print(sect)
e043d7
             for e in envs:
e043d7
-                msg = '   %s' % e.ui_name
e043d7
+                msg = '   %s' % (e.ui_name if e.ui_name is not None else _("<name-unset>"))
e043d7
                 if print_ids:
e043d7
                     msg += ' (%s)' % e.id
e043d7
                 print(msg)
e043d7
diff --git a/dnf/cli/output.py b/dnf/cli/output.py
e043d7
index 67eab80b19..2585a5c773 100644
e043d7
--- a/dnf/cli/output.py
e043d7
+++ b/dnf/cli/output.py
e043d7
@@ -1221,47 +1221,51 @@ def _add_line(lines, data, a_wid, po, obsoletes=[]):
e043d7
                 lines.append((name, "", "", "", "", "", ""))
e043d7
             pkglist_lines.append((action, lines))
e043d7
         if self.base._history:
e043d7
+            def format_line(group):
e043d7
+                name = group.getName()
e043d7
+                return (name if name else _("<name-unset>"), "", "", "", "", "", "")
e043d7
+
e043d7
             install_env_group = self.base._history.env._installed
e043d7
             if install_env_group:
e043d7
                 action = _("Installing Environment Groups")
e043d7
                 lines = []
e043d7
                 for group in install_env_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
             upgrade_env_group = self.base._history.env._upgraded
e043d7
             if upgrade_env_group:
e043d7
                 action = _("Upgrading Environment Groups")
e043d7
                 lines = []
e043d7
                 for group in upgrade_env_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
             remove_env_group = self.base._history.env._removed
e043d7
             if remove_env_group:
e043d7
                 action = _("Removing Environment Groups")
e043d7
                 lines = []
e043d7
                 for group in remove_env_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
             install_group = self.base._history.group._installed
e043d7
             if install_group:
e043d7
                 action = _("Installing Groups")
e043d7
                 lines = []
e043d7
                 for group in install_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
             upgrade_group = self.base._history.group._upgraded
e043d7
             if upgrade_group:
e043d7
                 action = _("Upgrading Groups")
e043d7
                 lines = []
e043d7
                 for group in upgrade_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
             remove_group = self.base._history.group._removed
e043d7
             if remove_group:
e043d7
                 action = _("Removing Groups")
e043d7
                 lines = []
e043d7
                 for group in remove_group.values():
e043d7
-                    lines.append((group.getName(), "", "", "", "", "", ""))
e043d7
+                    lines.append(format_line(group))
e043d7
                 pkglist_lines.append((action, lines))
e043d7
         # show skipped conflicting packages
e043d7
         if not self.conf.best and self.base._goal.actions & forward_actions:
e043d7
diff --git a/dnf/comps.py b/dnf/comps.py
e043d7
index 316d647087..4ca15b1e07 100644
e043d7
--- a/dnf/comps.py
e043d7
+++ b/dnf/comps.py
e043d7
@@ -75,7 +75,16 @@ def _by_pattern(pattern, case_sensitive, sqn):
e043d7
     else:
e043d7
         match = re.compile(fnmatch.translate(pattern), flags=re.I).match
e043d7
 
e043d7
-    return {g for g in sqn if match(g.name) or match(g.id) or match(g.ui_name)}
e043d7
+    ret = set()
e043d7
+    for g in sqn:
e043d7
+        if match(g.id):
e043d7
+            ret.add(g)
e043d7
+        elif g.name is not None and match(g.name):
e043d7
+            ret.add(g)
e043d7
+        elif g.ui_name is not None and match(g.ui_name):
e043d7
+            ret.add(g)
e043d7
+
e043d7
+    return ret
e043d7
 
e043d7
 
e043d7
 def _fn_display_order(group):
e043d7
diff --git a/dnf/db/group.py b/dnf/db/group.py
e043d7
index e3a087760b..5d7e18d1a8 100644
e043d7
--- a/dnf/db/group.py
e043d7
+++ b/dnf/db/group.py
e043d7
@@ -78,8 +78,10 @@ def _get_obj_id(self, obj):
e043d7
     def new(self, obj_id, name, translated_name, pkg_types):
e043d7
         swdb_group = self.history.swdb.createCompsGroupItem()
e043d7
         swdb_group.setGroupId(obj_id)
e043d7
-        swdb_group.setName(name)
e043d7
-        swdb_group.setTranslatedName(translated_name)
e043d7
+        if name is not None:
e043d7
+            swdb_group.setName(name)
e043d7
+        if translated_name is not None:
e043d7
+            swdb_group.setTranslatedName(translated_name)
e043d7
         swdb_group.setPackageTypes(pkg_types)
e043d7
         return swdb_group
e043d7
 
e043d7
@@ -136,8 +138,10 @@ def _get_obj_id(self, obj):
e043d7
     def new(self, obj_id, name, translated_name, pkg_types):
e043d7
         swdb_env = self.history.swdb.createCompsEnvironmentItem()
e043d7
         swdb_env.setEnvironmentId(obj_id)
e043d7
-        swdb_env.setName(name)
e043d7
-        swdb_env.setTranslatedName(translated_name)
e043d7
+        if name is not None:
e043d7
+            swdb_env.setName(name)
e043d7
+        if translated_name is not None:
e043d7
+            swdb_env.setTranslatedName(translated_name)
e043d7
         swdb_env.setPackageTypes(pkg_types)
e043d7
         return swdb_env
e043d7
 
e043d7
diff --git a/tests/repos/main_comps.xml b/tests/repos/main_comps.xml
e043d7
index 9e694d13a5..584bb25b3a 100644
e043d7
--- a/tests/repos/main_comps.xml
e043d7
+++ b/tests/repos/main_comps.xml
e043d7
@@ -49,6 +49,13 @@
e043d7
       <packagereq type="optional">brokendeps</packagereq>
e043d7
     </packagelist>
e043d7
   </group>
e043d7
+  <group>
e043d7
+    <id>missing-name-group</id>
e043d7
+    <name></name>
e043d7
+    <packagelist>
e043d7
+      <packagereq type="mandatory">meaning-of-life</packagereq>
e043d7
+    </packagelist>
e043d7
+  </group>
e043d7
   <category>
e043d7
    <id>base-system</id>
e043d7
    <display_order>99</display_order>
e043d7
diff --git a/tests/support.py b/tests/support.py
e043d7
index e549ba5b95..a7d6a8542c 100644
e043d7
--- a/tests/support.py
e043d7
+++ b/tests/support.py
e043d7
@@ -94,7 +94,7 @@ def mock_open(mock=None, data=None):
e043d7
 MAIN_NSOLVABLES = 9
e043d7
 UPDATES_NSOLVABLES = 4
e043d7
 AVAILABLE_NSOLVABLES = MAIN_NSOLVABLES + UPDATES_NSOLVABLES
e043d7
-TOTAL_GROUPS = 4
e043d7
+TOTAL_GROUPS = 5
e043d7
 TOTAL_NSOLVABLES = SYSTEM_NSOLVABLES + AVAILABLE_NSOLVABLES
e043d7
 
e043d7
 
e043d7
diff --git a/tests/test_comps.py b/tests/test_comps.py
e043d7
index 30d468e3af..763218587f 100644
e043d7
--- a/tests/test_comps.py
e043d7
+++ b/tests/test_comps.py
e043d7
@@ -107,7 +107,7 @@ def test_group_packages(self):
e043d7
     def test_iteration(self):
e043d7
         comps = self.comps
e043d7
         self.assertEqual([g.name for g in comps.groups_iter()],
e043d7
-                         ['Base', 'Solid Ground', "Pepper's", "Broken Group"])
e043d7
+                         ['Base', 'Solid Ground', "Pepper's", "Broken Group", None])
e043d7
         self.assertEqual([c.name for c in comps.categories_iter()],
e043d7
                          ['Base System'])
e043d7
         g = dnf.util.first(comps.groups_iter())
e043d7
@@ -115,7 +115,7 @@ def test_iteration(self):
e043d7
 
e043d7
     def test_group_display_order(self):
e043d7
         self.assertEqual([g.name for g in self.comps.groups],
e043d7
-                         ["Pepper's", 'Base', 'Solid Ground', 'Broken Group'])
e043d7
+                         ["Pepper's", 'Base', 'Solid Ground', 'Broken Group', None])
e043d7
 
e043d7
     def test_packages(self):
e043d7
         comps = self.comps
e043d7
@@ -127,7 +127,7 @@ def test_packages(self):
e043d7
 
e043d7
     def test_size(self):
e043d7
         comps = self.comps
e043d7
-        self.assertLength(comps, 6)
e043d7
+        self.assertLength(comps, 7)
e043d7
         self.assertLength(comps.groups, tests.support.TOTAL_GROUPS)
e043d7
         self.assertLength(comps.categories, 1)
e043d7
         self.assertLength(comps.environments, 1)
e043d7
diff --git a/tests/test_groups.py b/tests/test_groups.py
e043d7
index fe388f96c0..8972da687e 100644
e043d7
--- a/tests/test_groups.py
e043d7
+++ b/tests/test_groups.py
e043d7
@@ -295,6 +295,15 @@ def test_group_install_broken_optional_nonstrict(self):
e043d7
         self.assertLength(inst, 1)
e043d7
         self.assertEmpty(removed)
e043d7
 
e043d7
+    def test_group_install_missing_name(self):
e043d7
+        comps_group = self.base.comps.group_by_pattern('missing-name-group')
e043d7
+
e043d7
+        cnt = self.base.group_install(comps_group.id, ('mandatory', 'default', 'optional'),
e043d7
+                                      strict=False)
e043d7
+        self._swdb_commit()
e043d7
+        self.base.resolve()
e043d7
+        self.assertEqual(cnt, 1)
e043d7
+
e043d7
 
e043d7
 class EnvironmentInstallTest(tests.support.ResultTestCase):
e043d7
     """Set up a test where sugar is considered not installed."""