Pārlūkot izejas kodu

Add write file tests.

Jelmer Vernooij 13 gadi atpakaļ
vecāks
revīzija
b7d6945c8a
2 mainītis faili ar 28 papildinājumiem un 3 dzēšanām
  1. 4 3
      dulwich/config.py
  2. 24 0
      dulwich/tests/test_config.py

+ 4 - 3
dulwich/config.py

@@ -135,14 +135,15 @@ class ConfigFile(ConfigDict):
                     if section is None:
                         raise ValueError("setting %r without section" % line)
                     setting = setting.strip()
+                    value = value.strip()
                     ret._values[section[0]][section[1]][setting] = ""
                 else:
                     setting = line.strip()
-                    value = True
+                    value = ""
             if setting is not None:
                 if section is None:
                     raise ValueError("setting %r without section" % line)
-                ret._values[section[0]][section[1]][setting] += line
+                ret._values[section[0]][section[1]][setting] += value
         return ret
 
     @classmethod
@@ -175,7 +176,7 @@ class ConfigFile(ConfigDict):
                 else:
                     f.write("[%s \"%s\"]\n" % (section_name, subsection_name))
                 for key, value in subsection.iteritems():
-                    f.write("%s = %s\n" % (key, value))
+                    f.write("%s = %s\n" % (key, _escape_value(value)))
 
 
 class StackedConfig(Config):

+ 24 - 0
dulwich/tests/test_config.py

@@ -44,6 +44,30 @@ class ConfigFileTests(TestCase):
         cf = self.from_file("")
         self.assertEquals(ConfigFile(), cf)
 
+    def test_from_file_section(self):
+        cf = self.from_file("[core]\nfoo = bar\n")
+        self.assertEquals("bar", cf.get("core.foo"))
+
+    def test_write_to_file_empty(self):
+        c = ConfigFile()
+        f = StringIO()
+        c.write_to_file(f)
+        self.assertEquals("", f.getvalue())
+
+    def test_write_to_file_section(self):
+        c = ConfigFile()
+        c.set("core.foo", "bar")
+        f = StringIO()
+        c.write_to_file(f)
+        self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
+
+    def test_write_to_file_subsection(self):
+        c = ConfigFile()
+        c.set("branch.blie.foo", "bar")
+        f = StringIO()
+        c.write_to_file(f)
+        self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
+
 
 class ConfigDictTests(TestCase):