Browse Source

Fix section_to_file when read() receives multiple filenames

Mike Naberezny 10 năm trước cách đây
mục cha
commit
1d32a7ef7b
2 tập tin đã thay đổi với 60 bổ sung23 xóa
  1. 12 10
      supervisor/options.py
  2. 48 13
      supervisor/tests/test_options.py

+ 12 - 10
supervisor/options.py

@@ -1648,19 +1648,21 @@ class UnhosedConfigParser(ConfigParser.RawConfigParser):
         return self.saneget(self.mysection, option, default=default,
                             expansions=expansions, **kwargs)
 
-    def read(self, filenames):
-        sections_orig = self._sections.copy()
-        filenames = ConfigParser.RawConfigParser.read(self, filenames)
+    def read(self, filenames, **kwargs):
+        if isinstance(filenames, basestring):  # RawConfigParser compat
+            filenames = [filenames]
 
-        if len(filenames) == 1:
-            filename = filenames[0]
-        else:
-            filename = '???'
+        ok_filenames = []
+        for filename in filenames:
+            sections_orig = self._sections.copy()
 
-        for section in frozenset(self._sections) - frozenset(sections_orig):
-            self.section_to_file[section] = filename
+            ok_filenames.extend(
+                ConfigParser.RawConfigParser.read(self, [filename], **kwargs))
 
-        return filenames
+            diff = frozenset(self._sections) - frozenset(sections_orig)
+            for section in diff:
+                self.section_to_file[section] = filename
+        return ok_filenames
 
 
 class Config(object):

+ 48 - 13
supervisor/tests/test_options.py

@@ -2190,35 +2190,70 @@ class UnhosedConfigParserTests(unittest.TestCase):
     def _makeOne(self, *args, **kw):
         return self._getTargetClass()(*args, **kw)
 
-    def test_section_to_file_initially_empty(self):
+    def test_read_filenames_as_string(self):
+        f = tempfile.NamedTemporaryFile(mode="w+")
         config = self._makeOne()
-        self.assertEqual(config.section_to_file, {})
+        try:
+            f.write("[foo]\n")
+            f.flush()
+            ok_filenames = config.read(f.name)
+        finally:
+            f.close()
+        self.assertEqual(ok_filenames, [f.name])
+
+    def test_read_filenames_as_list(self):
+        f = tempfile.NamedTemporaryFile(mode="w+")
+        parser = self._makeOne()
+        try:
+            f.write("[foo]\n")
+            f.flush()
+            ok_filenames = parser.read([f.name])
+        finally:
+            f.close()
+        self.assertEqual(ok_filenames, [f.name])
+
+    def test_read_returns_ok_filenames_like_rawconfigparser(self):
+        nonexistant = os.path.join(os.path.dirname(__file__), "nonexistant")
+        f = tempfile.NamedTemporaryFile(mode="w+")
+        parser = self._makeOne()
+        try:
+            f.write("[foo]\n")
+            f.flush()
+            ok_filenames = parser.read([nonexistant, f.name])
+        finally:
+            f.close()
+        self.assertEqual(ok_filenames, [f.name])
+
+    def test_read_section_to_file_initially_empty(self):
+        parser = self._makeOne()
+        self.assertEqual(parser.section_to_file, {})
 
-    def test_section_to_file_read_one_file(self):
-        f = tempfile.NamedTemporaryFile()
+    def test_read_section_to_file_read_one_file(self):
+        f = tempfile.NamedTemporaryFile(mode="w+")
         try:
             f.write("[foo]\n")
             f.flush()
-            config = self._makeOne()
-            config.read([f.name])
+            parser = self._makeOne()
+            parser.read([f.name])
         finally:
             f.close()
-        self.assertEqual(config.section_to_file['foo'], f.name)
+        self.assertEqual(parser.section_to_file['foo'], f.name)
 
-    def test_section_to_file_read_multiple_files(self):
-        f1 = tempfile.NamedTemporaryFile()
-        f2 = tempfile.NamedTemporaryFile()
+    def test_read_section_to_file_read_multiple_files(self):
+        f1 = tempfile.NamedTemporaryFile(mode="w+")
+        f2 = tempfile.NamedTemporaryFile(mode="w+")
         try:
             f1.write("[foo]\n")
             f1.flush()
             f2.write("[bar]\n")
             f2.flush()
-            config = self._makeOne()
-            config.read([f1.name, f2.name])
+            parser = self._makeOne()
+            parser.read([f1.name, f2.name])
         finally:
             f1.close()
             f2.close()
-        self.assertEqual(config.section_to_file['foo'], '???')
+        self.assertEqual(parser.section_to_file['foo'], f1.name)
+        self.assertEqual(parser.section_to_file['bar'], f2.name)
 
 class UtilFunctionsTests(unittest.TestCase):
     def test_make_namespec(self):