comparison tests/test-fix-metadata.t @ 42194:0da689a60163

fix: allow fixer tools to return metadata in addition to the file content With this change, fixer tools can be configured to output a JSON object that will be parsed and passed to hooks that can be used to print summaries of what code was formatted or perform other post-fixing work. The motivation for this change is to allow parallel executions of a "meta-formatter" tool to report back statistics, which are then aggregated and processed after all formatting has completed. Providing an extensible mechanism inside fix.py is far simpler, and more portable, than trying to make a tool like this communicate through some other channel. Differential Revision: https://phab.mercurial-scm.org/D6167
author Danny Hooper <hooper@google.com>
date Thu, 21 Mar 2019 18:32:45 -0700
parents
children 6ed04139ed37
comparison
equal deleted inserted replaced
42190:7c0ece3cd3ee 42194:0da689a60163
1 A python hook for "hg fix" that prints out the number of files and revisions
2 that were affected, along with which fixer tools were applied. Also checks how
3 many times it sees a specific key generated by one of the fixer tools defined
4 below.
5
6 $ cat >> $TESTTMP/postfixhook.py <<EOF
7 > import collections
8 > def file(ui, repo, rev=None, path='', metadata=None, **kwargs):
9 > ui.status('fixed %s in revision %d using %s\n' %
10 > (path, rev, ', '.join(metadata.keys())))
11 > def summarize(ui, repo, replacements=None, wdirwritten=False,
12 > metadata=None, **kwargs):
13 > counts = collections.defaultdict(int)
14 > keys = 0
15 > for fixername, metadatalist in metadata.items():
16 > for metadata in metadatalist:
17 > if metadata is None:
18 > continue
19 > counts[fixername] += 1
20 > if 'key' in metadata:
21 > keys += 1
22 > ui.status('saw "key" %d times\n' % (keys,))
23 > for name, count in sorted(counts.items()):
24 > ui.status('fixed %d files with %s\n' % (count, name))
25 > if replacements:
26 > ui.status('fixed %d revisions\n' % (len(replacements),))
27 > if wdirwritten:
28 > ui.status('fixed the working copy\n')
29 > EOF
30
31 Some mock output for fixer tools that demonstrate what could go wrong with
32 expecting the metadata output format.
33
34 $ printf 'new content\n' > $TESTTMP/missing
35 $ printf 'not valid json\0new content\n' > $TESTTMP/invalid
36 $ printf '{"key": "value"}\0new content\n' > $TESTTMP/valid
37
38 Configure some fixer tools based on the output defined above, and enable the
39 hooks defined above. Disable parallelism to make output of the parallel file
40 processing phase stable.
41
42 $ cat >> $HGRCPATH <<EOF
43 > [extensions]
44 > fix =
45 > [fix]
46 > missing:command=cat $TESTTMP/missing
47 > missing:pattern=missing
48 > missing:metadata=true
49 > invalid:command=cat $TESTTMP/invalid
50 > invalid:pattern=invalid
51 > invalid:metadata=true
52 > valid:command=cat $TESTTMP/valid
53 > valid:pattern=valid
54 > valid:metadata=true
55 > [hooks]
56 > postfixfile = python:$TESTTMP/postfixhook.py:file
57 > postfix = python:$TESTTMP/postfixhook.py:summarize
58 > [worker]
59 > enabled=false
60 > EOF
61
62 See what happens when we execute each of the fixer tools. Some print warnings,
63 some write back to the file.
64
65 $ hg init repo
66 $ cd repo
67
68 $ printf "old content\n" > invalid
69 $ printf "old content\n" > missing
70 $ printf "old content\n" > valid
71 $ hg add -q
72
73 $ hg fix -w
74 ignored invalid output from fixer tool: invalid
75 ignored invalid output from fixer tool: missing
76 fixed valid in revision 2147483647 using valid
77 saw "key" 1 times
78 fixed 1 files with valid
79 fixed the working copy
80
81 $ cat missing invalid valid
82 old content
83 old content
84 new content
85
86 $ cd ..