annotate tests/test-simplekeyvaluefile.py @ 36037:8de90e006c78

run-tests: report tests that exception occurred in We now record the test that an exception occurred in. We put this information to use by aggregating the count of failures in each test. For each exception, the exception report now prints the total number of tests having that exception and the test with the least number of exceptions exhibiting that failure. The exception list is now sorted by (total count, tests impacted, count of failures in least failing test). This allows us to: * Assess how widespread a failure is. Some exceptions occur a lot in a few tests. Others occur over many tests. * Easily run a test exhibiting an exception without having to find a failure in test output. * Find and fix low hanging fruit (e.g. exceptions that are the only failure in a test). Here's an example of the new output: 199 (4 tests) /home/gps/src/hg/hgext/blackbox.py:191: %b requires a bytes-like object, or an object that implements __bytes__, not 'str' (test-devel-warnings.t - 1 total) 142 (19 tests) /home/gps/src/hg/hgext/mq.py:655: list indices must be integers or slices, not bytes (test-hardlinks.t - 1 total) 140 (20 tests) /home/gps/src/hg/mercurial/patch.py:296: string argument expected, got 'bytes' (test-audit-subrepo.t - 1 total) 101 (15 tests) /home/gps/src/hg/hgext/convert/convcmd.py:60: encode() argument 1 must be str, not bytes (test-convert-clonebranches.t - 1 total) 90 (2 tests) /home/gps/src/hg/hgext/mq.py:456: can't concat str to bytes (test-mq-qqueue.t - 1 total) 87 (2 tests) /home/gps/src/hg/mercurial/branchmap.py:380: %b requires a bytes-like object, or an object that implements __bytes__, not 'FileNotFoundError' (test-branches.t - 2 total) 85 (22 tests) /home/gps/src/hg/mercurial/sshpeer.py:223: cannot convert 'UUID' object to bytes (test-bundle2-pushback.t - 1 total) 1 (1 tests) /home/gps/src/hg/mercurial/formatter.py:254: %b requires a bytes-like object, or an object that implements __bytes__, not 'str' (test-debugextensions.t - 2 total) 1 (1 tests) /home/gps/src/hg/hgext/convert/convcmd.py:420: startswith first arg must be str or a tuple of str, not bytes (test-convert-authormap.t - 2 total) 1 (1 tests) /home/gps/src/hg/mercurial/revlog.py:797: '>=' not supported between instances of 'NoneType' and 'int' (test-unionrepo.t - 1 total) 1 (1 tests) /home/gps/src/hg/hgext/show.py:129: %b requires a bytes-like object, or an object that implements __bytes__, not 'str' (test-show.t - 1 total) Differential Revision: https://phab.mercurial-scm.org/D2138
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 11 Feb 2018 12:42:10 -0800
parents 68c43a416585
children 1859b9a7ddef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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):
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
36 # lines need to contain the trailing '\n' to mock the real readlines
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
37 return [l for l in mockfile(path, self).read().splitlines(True)]
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
38
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
39 def __call__(self, path, mode, atomictemp):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
40 return mockfile(path, self)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
41
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
42 class testsimplekeyvaluefile(unittest.TestCase):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
43 def setUp(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
44 self.vfs = mockvfs()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
45
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
46 def testbasicwritingiandreading(self):
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
47 dw = {'key1': 'value1', 'Key2': 'value2'}
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
48 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(dw)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
49 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
50 ['', 'Key2=value2', 'key1=value1'])
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
51 dr = scmutil.simplekeyvaluefile(self.vfs, 'kvfile').read()
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
52 self.assertEqual(dr, dw)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
53
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
54 def testinvalidkeys(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
55 d = {'0key1': 'value1', 'Key2': 'value2'}
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
56 with self.assertRaisesRegexp(error.ProgrammingError,
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
57 'keys must start with a letter.*'):
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
58 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
59
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
60 d = {'key1@': 'value1', 'Key2': 'value2'}
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
61 with self.assertRaisesRegexp(error.ProgrammingError, 'invalid key.*'):
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
62 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
63
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
64 def testinvalidvalues(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
65 d = {'key1': 'value1', 'Key2': 'value2\n'}
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
66 with self.assertRaisesRegexp(error.ProgrammingError, 'invalid val.*'):
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
67 scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
68
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
69 def testcorruptedfile(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
70 self.vfs.contents['badfile'] = 'ababagalamaga\n'
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
71 with self.assertRaisesRegexp(error.CorruptedState,
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
72 'dictionary.*element.*'):
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
73 scmutil.simplekeyvaluefile(self.vfs, 'badfile').read()
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
74
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
75 def testfirstline(self):
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
76 dw = {'key1': 'value1'}
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
77 scmutil.simplekeyvaluefile(self.vfs, 'fl').write(dw, firstline='1.0')
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
78 self.assertEqual(self.vfs.read('fl'), '1.0\nkey1=value1\n')
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
79 dr = scmutil.simplekeyvaluefile(self.vfs, 'fl')\
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
80 .read(firstlinenonkeyval=True)
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
81 self.assertEqual(dr, {'__firstline': '1.0', 'key1': 'value1'})
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
82
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
83 if __name__ == "__main__":
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
84 silenttestrunner.main(__name__)