Mercurial > hg-stable
annotate tests/test-fix-metadata.t @ 42229: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 |
rev | line source |
---|---|
42229
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
1 A python hook for "hg fix" that prints out the number of files and revisions |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
2 that were affected, along with which fixer tools were applied. Also checks how |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
3 many times it sees a specific key generated by one of the fixer tools defined |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
4 below. |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
5 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
6 $ cat >> $TESTTMP/postfixhook.py <<EOF |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
7 > import collections |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
8 > def file(ui, repo, rev=None, path='', metadata=None, **kwargs): |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
9 > ui.status('fixed %s in revision %d using %s\n' % |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
10 > (path, rev, ', '.join(metadata.keys()))) |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
11 > def summarize(ui, repo, replacements=None, wdirwritten=False, |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
12 > metadata=None, **kwargs): |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
13 > counts = collections.defaultdict(int) |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
14 > keys = 0 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
15 > for fixername, metadatalist in metadata.items(): |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
16 > for metadata in metadatalist: |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
17 > if metadata is None: |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
18 > continue |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
19 > counts[fixername] += 1 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
20 > if 'key' in metadata: |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
21 > keys += 1 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
22 > ui.status('saw "key" %d times\n' % (keys,)) |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
23 > for name, count in sorted(counts.items()): |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
24 > ui.status('fixed %d files with %s\n' % (count, name)) |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
25 > if replacements: |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
26 > ui.status('fixed %d revisions\n' % (len(replacements),)) |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
27 > if wdirwritten: |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
28 > ui.status('fixed the working copy\n') |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
29 > EOF |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
30 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
31 Some mock output for fixer tools that demonstrate what could go wrong with |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
32 expecting the metadata output format. |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
33 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
34 $ printf 'new content\n' > $TESTTMP/missing |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
35 $ printf 'not valid json\0new content\n' > $TESTTMP/invalid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
36 $ printf '{"key": "value"}\0new content\n' > $TESTTMP/valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
37 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
38 Configure some fixer tools based on the output defined above, and enable the |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
39 hooks defined above. Disable parallelism to make output of the parallel file |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
40 processing phase stable. |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
41 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
42 $ cat >> $HGRCPATH <<EOF |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
43 > [extensions] |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
44 > fix = |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
45 > [fix] |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
46 > missing:command=cat $TESTTMP/missing |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
47 > missing:pattern=missing |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
48 > missing:metadata=true |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
49 > invalid:command=cat $TESTTMP/invalid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
50 > invalid:pattern=invalid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
51 > invalid:metadata=true |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
52 > valid:command=cat $TESTTMP/valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
53 > valid:pattern=valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
54 > valid:metadata=true |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
55 > [hooks] |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
56 > postfixfile = python:$TESTTMP/postfixhook.py:file |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
57 > postfix = python:$TESTTMP/postfixhook.py:summarize |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
58 > [worker] |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
59 > enabled=false |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
60 > EOF |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
61 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
62 See what happens when we execute each of the fixer tools. Some print warnings, |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
63 some write back to the file. |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
64 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
65 $ hg init repo |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
66 $ cd repo |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
67 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
68 $ printf "old content\n" > invalid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
69 $ printf "old content\n" > missing |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
70 $ printf "old content\n" > valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
71 $ hg add -q |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
72 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
73 $ hg fix -w |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
74 ignored invalid output from fixer tool: invalid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
75 ignored invalid output from fixer tool: missing |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
76 fixed valid in revision 2147483647 using valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
77 saw "key" 1 times |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
78 fixed 1 files with valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
79 fixed the working copy |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
80 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
81 $ cat missing invalid valid |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
82 old content |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
83 old content |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
84 new content |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
85 |
0da689a60163
fix: allow fixer tools to return metadata in addition to the file content
Danny Hooper <hooper@google.com>
parents:
diff
changeset
|
86 $ cd .. |