comparison tests/test-contrib-testparseutil.t @ 40093:726cfc47f17a

contrib: add an utility module to parse test scripts This patch centralizes the logic to pick up code fragments embedded in *.t script, in order to: - apply checking with patterns in check-code.py on such embedded code fragments Now, check-code.py completely ignores embedded code fragments. I'll post another patch series to check them. - replace similar code path in contrib/import-checker.py Current import-checker.py has problems below. Fixing each of them is a little difficult, because parsing logic and pattern strings are tightly coupled. - overlook (or mis-detect) the end of inline script in doctest style 8a8dd6e4a97a fixed a part of this issue, but not enough. - it overlooks inline script in doctest style at the end of file (and ignores invalid un-closed heredoc at the end of file, too) - it overlooks code fragment in styles below - "python <<EOF" (heredoc should be "cat > file <<EOF" style) - "cat > foobar.py << ANYLIMIT" (limit mark should be "EOF") - "cat << EOF > foobar.py" (filename should be placed before limit mark) - "cat >> foobar.py << EOF" (appending is ignored) - it is not extensible for other than python code fragments (e.g. shell script, hgrc file, and so on) This new module can detect python code fragments in styles below: - inline script in doctest style (starting by " >>> " line) - python invocation with heredoc script ("python <<EOF") - python script in heredoc style (redirected into ".py" file) As an example of extensibility of new module, this patch also contains implementation to pick up code fragment below. This will be useful to add additional restriction for them, for example. - shell script in heredoc style (redirected into ".sh" file) - hgrc configuration in heredoc style (redirected into hgrc or $HGRCPATH)
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Thu, 23 Aug 2018 12:25:54 +0900
parents
children
comparison
equal deleted inserted replaced
40092:58786930ea27 40093:726cfc47f17a
1 $ testparseutil="$TESTDIR"/../contrib/testparseutil.py
2
3 Internal test by doctest
4
5 $ "$PYTHON" -m doctest "$testparseutil"
6
7 Tests for embedded python script
8
9 Typical cases
10
11 $ "$PYTHON" "$testparseutil" -v pyembedded <<NO_CHECK_EOF
12 > >>> for f in [1, 2, 3]:
13 > ... foo = 1
14 > >>> foo = 2
15 > $ echo "doctest is terminated by command, empty line, or comment"
16 > >>> foo = 31
17 > expected output of doctest fragment
18 > >>> foo = 32
19 >
20 > >>> foo = 33
21 >
22 > >>> foo = 34
23 > comment
24 > >>> foo = 35
25 >
26 > $ "\$PYTHON" <<EOF
27 > > foo = 4
28 > >
29 > > EOF
30 > $ cat > foo.py <<EOF
31 > > foo = 5
32 > > EOF
33 > $ cat >> foo.py <<EOF
34 > > foo = 6 # appended
35 > > EOF
36 >
37 > NO_CHECK_EOF limit mark makes parsing ignore corresponded fragment
38 > (this is useful to use bad code intentionally)
39 >
40 > $ "\$PYTHON" <<NO_CHECK_EOF
41 > > foo = 7 # this should be ignored at detection
42 > > NO_CHECK_EOF
43 > $ cat > foo.py <<NO_CHECK_EOF
44 > > foo = 8 # this should be ignored at detection
45 > > NO_CHECK_EOF
46 >
47 > doctest fragment ended by EOF
48 >
49 > >>> foo = 9
50 > NO_CHECK_EOF
51 <stdin>:1: <anonymous> starts
52 |for f in [1, 2, 3]:
53 | foo = 1
54 |foo = 2
55 <stdin>:4: <anonymous> ends
56 <stdin>:5: <anonymous> starts
57 |foo = 31
58 |
59 |foo = 32
60 |
61 |foo = 33
62 <stdin>:10: <anonymous> ends
63 <stdin>:11: <anonymous> starts
64 |foo = 34
65 <stdin>:12: <anonymous> ends
66 <stdin>:13: <anonymous> starts
67 |foo = 35
68 <stdin>:14: <anonymous> ends
69 <stdin>:16: <anonymous> starts
70 |foo = 4
71 |
72 <stdin>:18: <anonymous> ends
73 <stdin>:20: foo.py starts
74 |foo = 5
75 <stdin>:21: foo.py ends
76 <stdin>:23: foo.py starts
77 |foo = 6 # appended
78 <stdin>:24: foo.py ends
79 <stdin>:38: <anonymous> starts
80 |foo = 9
81 <stdin>:39: <anonymous> ends
82
83 Invalid test script
84
85 (similar test for shell script and hgrc configuration is omitted,
86 because this tests common base class of them)
87
88 $ "$PYTHON" "$testparseutil" -v pyembedded <<NO_CHECK_EOF > detected
89 > $ "\$PYTHON" <<EOF
90 > > foo = 1
91 >
92 > $ "\$PYTHON" <<EOF
93 > > foo = 2
94 > $ cat > bar.py <<EOF
95 > > bar = 2 # this fragment will be detected as expected
96 > > EOF
97 >
98 > $ cat > foo.py <<EOF
99 > > foo = 3
100 > NO_CHECK_EOF
101 <stdin>:3: unexpected line for "heredoc python invocation"
102 <stdin>:6: unexpected line for "heredoc python invocation"
103 <stdin>:11: unexpected end of file for "heredoc .py file"
104 [1]
105 $ cat detected
106 <stdin>:7: bar.py starts
107 |bar = 2 # this fragment will be detected as expected
108 <stdin>:8: bar.py ends
109
110 Tests for embedded shell script
111
112 $ "$PYTHON" "$testparseutil" -v shembedded <<NO_CHECK_EOF
113 > $ cat > foo.sh <<EOF
114 > > foo = 1
115 > >
116 > > foo = 2
117 > > EOF
118 > $ cat >> foo.sh <<EOF
119 > > foo = 3 # appended
120 > > EOF
121 >
122 > NO_CHECK_EOF limit mark makes parsing ignore corresponded fragment
123 > (this is useful to use bad code intentionally)
124 >
125 > $ cat > foo.sh <<NO_CHECK_EOF
126 > > # this should be ignored at detection
127 > > foo = 4
128 > > NO_CHECK_EOF
129 >
130 > NO_CHECK_EOF
131 <stdin>:2: foo.sh starts
132 |foo = 1
133 |
134 |foo = 2
135 <stdin>:5: foo.sh ends
136 <stdin>:7: foo.sh starts
137 |foo = 3 # appended
138 <stdin>:8: foo.sh ends
139
140 Tests for embedded hgrc configuration
141
142 $ "$PYTHON" "$testparseutil" -v hgrcembedded <<NO_CHECK_EOF
143 > $ cat > .hg/hgrc <<EOF
144 > > [ui]
145 > > verbose = true
146 > >
147 > > # end of local configuration
148 > > EOF
149 >
150 > $ cat > \$HGRCPATH <<EOF
151 > > [extensions]
152 > > rebase =
153 > > # end of global configuration
154 > > EOF
155 >
156 > $ cat >> \$HGRCPATH <<EOF
157 > > # appended
158 > > [extensions]
159 > > rebase =!
160 > > EOF
161 >
162 > NO_CHECK_EOF limit mark makes parsing ignore corresponded fragment
163 > (this is useful to use bad code intentionally)
164 >
165 > $ cat > .hg/hgrc <<NO_CHECK_EOF
166 > > # this local configuration should be ignored at detection
167 > > [ui]
168 > > username = foo bar
169 > > NO_CHECK_EOF
170 >
171 > $ cat > \$HGRCPATH <<NO_CHECK_EOF
172 > > # this global configuration should be ignored at detection
173 > > [extensions]
174 > > foobar =
175 > > NO_CHECK_EOF
176 > NO_CHECK_EOF
177 <stdin>:2: .hg/hgrc starts
178 |[ui]
179 |verbose = true
180 |
181 |# end of local configuration
182 <stdin>:6: .hg/hgrc ends
183 <stdin>:9: $HGRCPATH starts
184 |[extensions]
185 |rebase =
186 |# end of global configuration
187 <stdin>:12: $HGRCPATH ends
188 <stdin>:15: $HGRCPATH starts
189 |# appended
190 |[extensions]
191 |rebase =!
192 <stdin>:18: $HGRCPATH ends