Patchwork [1,of,4,shelve-ext,v5] scmutil: add simplekeyvaluefile reading test

login
register
mail settings
Submitter Kostia Balytskyi
Date May 11, 2017, 9:43 p.m.
Message ID <ebf9affb2f7013a6a597.1494539004@devvm1416.lla2.facebook.com>
Download mbox | patch
Permalink /patch/20577/
State Accepted
Headers show

Comments

Kostia Balytskyi - May 11, 2017, 9:43 p.m.
# HG changeset patch
# User Kostia Balytskyi <ikostia@fb.com>
# Date 1494517184 25200
#      Thu May 11 08:39:44 2017 -0700
# Node ID ebf9affb2f7013a6a597ddd0765c786ed36f3722
# Parent  1ada3d18e7fbc9069910f2c036992d2f2b28e058
scmutil: add simplekeyvaluefile reading test

Before this patch, mockvfs did not emulate readlines correctly
and there was no test for simplekeyvaluefile reading.

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -925,7 +925,10 @@  class simplekeyvaluefile(object):
     def read(self):
         lines = self.vfs.readlines(self.path)
         try:
-            d = dict(line[:-1].split('=', 1) for line in lines if line)
+            # the 'if line.strip()' part prevents us from failing on empty
+            # lines which only contain '\n' therefore are not skipped
+            # by 'if line'
+            d = dict(line[:-1].split('=', 1) for line in lines if line.strip())
         except ValueError as e:
             raise error.CorruptedState(str(e))
         return d
diff --git a/tests/test-simplekeyvaluefile.py b/tests/test-simplekeyvaluefile.py
--- a/tests/test-simplekeyvaluefile.py
+++ b/tests/test-simplekeyvaluefile.py
@@ -33,7 +33,8 @@  class mockvfs(object):
         return mockfile(path, self).read()
 
     def readlines(self, path):
-        return mockfile(path, self).read().split('\n')
+        # lines need to contain the trailing '\n' to mock the real readlines
+        return [l for l in mockfile(path, self).read().splitlines(True)]
 
     def __call__(self, path, mode, atomictemp):
         return mockfile(path, self)
@@ -42,11 +43,13 @@  class testsimplekeyvaluefile(unittest.Te
     def setUp(self):
         self.vfs = mockvfs()
 
-    def testbasicwriting(self):
-        d = {'key1': 'value1', 'Key2': 'value2'}
-        scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
+    def testbasicwritingiandreading(self):
+        dw = {'key1': 'value1', 'Key2': 'value2'}
+        scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(dw)
         self.assertEqual(sorted(self.vfs.read('kvfile').split('\n')),
                          ['', 'Key2=value2', 'key1=value1'])
+        dr = scmutil.simplekeyvaluefile(self.vfs, 'kvfile').read()
+        self.assertEqual(dr, dw)
 
     def testinvalidkeys(self):
         d = {'0key1': 'value1', 'Key2': 'value2'}