tests/test-simplekeyvaluefile.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 23 Mar 2017 23:47:23 -0400
changeset 31626 0febf8e4e2ce
parent 31585 c6921568cd20
child 32269 ed2c44741190
permissions -rw-r--r--
repair: use context manager for lock management If repo.lock() raised inside of the try block, 'tr' would have been None in the finally block where it tries to release(). Modernize the syntax instead of just winching the lock out of the try block. I found several other instances of acquiring the lock inside of the 'try', but those finally blocks handle None references. I also started switching some trivial try/finally blocks to context managers, but didn't get them all because indenting over 3x for lock, wlock and transaction would have spilled over 80 characters. That got me wondering if there should be a repo.rwlock(), to handle locking and unlocking in the proper order. It also looks like py27 supports supports multiple context managers for a single 'with' statement. Should I hold off on the rest until py26 is dropped?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     1
from __future__ import absolute_import
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     2
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     3
import unittest
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     4
import silenttestrunner
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     5
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     6
from mercurial import (
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     7
    error,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     8
    scmutil,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     9
)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    10
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    11
class mockfile(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    12
    def __init__(self, name, fs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    13
        self.name = name
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    14
        self.fs = fs
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    15
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    16
    def __enter__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    17
        return self
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    18
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    19
    def __exit__(self, *args, **kwargs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    20
        pass
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    21
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    22
    def write(self, text):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    23
        self.fs.contents[self.name] = text
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    24
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    25
    def read(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    26
        return self.fs.contents[self.name]
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    27
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    28
class mockvfs(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    29
    def __init__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    30
        self.contents = {}
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    31
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    32
    def read(self, path):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    33
        return mockfile(path, self).read()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    34
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    35
    def readlines(self, path):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    36
        return mockfile(path, self).read().split('\n')
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    37
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    38
    def __call__(self, path, mode, atomictemp):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    39
        return mockfile(path, self)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    40
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    41
class testsimplekeyvaluefile(unittest.TestCase):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    42
    def setUp(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    43
        self.vfs = mockvfs()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    44
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    45
    def testbasicwriting(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    46
        d = {'key1': 'value1', 'Key2': 'value2'}
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    47
        scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    48
        self.assertEqual(sorted(self.vfs.read('kvfile').split('\n')),
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    49
                         ['', 'Key2=value2', 'key1=value1'])
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    50
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    51
    def testinvalidkeys(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    52
        d = {'0key1': 'value1', 'Key2': 'value2'}
31585
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    53
        self.assertRaises(error.ProgrammingError,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    54
                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    55
                          d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    56
        d = {'key1@': 'value1', 'Key2': 'value2'}
31585
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    57
        self.assertRaises(error.ProgrammingError,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    58
                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    59
                          d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    60
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    61
    def testinvalidvalues(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    62
        d = {'key1': 'value1', 'Key2': 'value2\n'}
31585
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    63
        self.assertRaises(error.ProgrammingError,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    64
                          scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    65
                          d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    66
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    67
    def testcorruptedfile(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    68
        self.vfs.contents['badfile'] = 'ababagalamaga\n'
31585
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    69
        self.assertRaises(error.CorruptedState,
c6921568cd20 tests: make test-simplekeyvaluefile.py py2.6-compatible
Kostia Balytskyi <ikostia@fb.com>
parents: 31553
diff changeset
    70
                          scmutil.simplekeyvaluefile(self.vfs, 'badfile').read)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    71
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    72
if __name__ == "__main__":
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    73
    silenttestrunner.main(__name__)