[Kimchi-devel] [PATCH] Gtihub #660 - Fixing .repo file with no repos handling

Daniel Henrique Barboza dhbarboza82 at gmail.com
Fri Jun 12 12:06:47 UTC 2015


yumparser module will not add a {None: None} dict to the
repositories listing when reading a .repo file which has
no repositories declarations.

Signed-off-by: Daniel Henrique Barboza <dhbarboza82 at gmail.com>
---
 src/kimchi/yumparser.py |  5 +++--
 tests/test_yumparser.py | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/kimchi/yumparser.py b/src/kimchi/yumparser.py
index ce6ba9d..ac4ee02 100644
--- a/src/kimchi/yumparser.py
+++ b/src/kimchi/yumparser.py
@@ -109,8 +109,9 @@ def _get_repos_from_file(repo_file):
             value = value.strip()
             current_repo.set_attribute(key, value)
 
-        # add the last repo from file
-        repos_from_file[current_repo_id] = current_repo
+        # add the last repo from file.
+        if current_repo is not None:
+            repos_from_file[current_repo_id] = current_repo
 
     return repos_from_file
 
diff --git a/tests/test_yumparser.py b/tests/test_yumparser.py
index 8645a65..d17e647 100644
--- a/tests/test_yumparser.py
+++ b/tests/test_yumparser.py
@@ -22,6 +22,7 @@ import tempfile
 import unittest
 
 from kimchi.model import model
+from kimchi.rollbackcontext import RollbackContext
 from kimchi.yumparser import delete_repo_from_file, get_repo_files
 from kimchi.yumparser import get_yum_repositories, write_repo_to_file
 from kimchi.yumparser import YumRepoObject
@@ -45,6 +46,21 @@ def _create_fake_repos(repo_file_name):
     return repos
 
 
+def _create_empty_repo_file():
+    data = """
+#
+# This is a repository file with no repositories at all
+# No repositories must be added after reading this file.
+#
+    """
+    _, tmp_file_name = tempfile.mkstemp(suffix='.repo',
+                                        dir='/etc/yum.repos.d')
+    with open(tmp_file_name, 'w') as f:
+        f.writelines(data)
+
+    return tmp_file_name
+
+
 def _create_fake_repos_file():
     _, tmp_file_name = tempfile.mkstemp(suffix='.repo',
                                         dir='/etc/yum.repos.d')
@@ -79,6 +95,14 @@ class YumParserTests(unittest.TestCase):
         repo_objects = get_yum_repositories()
         self.assertGreaterEqual(len(repo_objects), len(repo_files))
 
+    def test_empty_repo_file(self):
+        with RollbackContext() as rollback:
+            repos = get_yum_repositories()
+            tmp_file_name = _create_empty_repo_file()
+            rollback.prependDefer(os.remove, tmp_file_name)
+            repos_after = get_yum_repositories()
+            self.assertEqual(len(repos_after), len(repos))
+
     def test_update_repo_attributes(self):
         repos = get_yum_repositories()
         fake_repo_2 = repos['fake-repo-2']
-- 
2.1.0




More information about the Kimchi-devel mailing list