Skip to content

Commit 735db81

Browse files
committed
Build: Enable testing without magic comments (backports elastic#46180) (elastic#46325)
Previously we only turned on tests if we saw either `// CONSOLE` or `// TEST`. These magic comments are difficult for the docs build to deal with so it has moved away from using them where possible. We should catch up. This adds another trigger to enable testing: marking a snippet with the `console` language. It looks like this: ``` [source,console] ---- GET / ---- ``` This saves a line which is nice, I guess. But it is more important to me that this is consistent with the way the docs build works now. Similarly this enables response testing when you mark a snippet with the language `console-result`. That looks like: ``` [source,console-result] ---- { "result": "0.1" } ---- ``` `// TESTRESPONSE` is still available for situations like `// TEST`: when the response isn't *in* the console-result language (like `_cat`) or when you want to perform substitutions on the generated test. Should unblock elastic#46159.
1 parent 02137d5 commit 735db81

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/doc/RestTestsFromSnippetsTask.groovy

+3-2
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
202202
previousTest = snippet
203203
return
204204
}
205-
if (snippet.testResponse) {
205+
if (snippet.testResponse || snippet.language == 'console-result') {
206206
response(snippet)
207207
return
208208
}
209-
if (snippet.test || snippet.console) {
209+
if (snippet.test || snippet.console ||
210+
snippet.language == 'console') {
210211
test(snippet)
211212
previousTest = snippet
212213
return

docs/README.asciidoc

+24-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ Elasticsearch documentation build process.
33

44
See: https://github.com/elastic/docs
55

6-
Snippets marked with `// CONSOLE` are automatically annotated with "VIEW IN
7-
CONSOLE" and "COPY AS CURL" in the documentation and are automatically tested
8-
by the command `gradle :docs:check`. To test just the docs from a single page,
9-
use e.g. `./gradlew :docs:integTestRunner --tests "*rollover*"`.
6+
Snippets marked with `[source,console]` are automatically annotated with
7+
"VIEW IN CONSOLE" and "COPY AS CURL" in the documentation and are automatically
8+
tested by the command `./gradlew -pdocs check`. To test just the docs from a
9+
single page, use e.g. `./gradlew -ddocs integTestRunner --tests "*rollover*"`.
10+
11+
NOTE: Previously we use `// CONSOLE` instead of `[source,console]`. This worked
12+
well for a long time so you'll see it all over early branches but we're phasing
13+
it out because it requires some unpleasant hackery on the docs build side.
1014

1115
NOTE: If you have an elasticsearch-extra folder alongside your elasticsearch
1216
folder, you must temporarily rename it when you are testing 6.3 or later branches.
@@ -45,10 +49,21 @@ for its modifiers:
4549
header. If the response doesn't include a `Warning` header with the exact
4650
text then the test fails. If the response includes `Warning` headers that
4751
aren't expected then the test fails.
48-
* `// TESTRESPONSE`: Matches this snippet against the body of the response of
49-
the last test. If the response is JSON then order is ignored. If you add
50-
`// TEST[continued]` to the snippet after `// TESTRESPONSE` it will continue
51-
in the same test, allowing you to interleave requests with responses to check.
52+
* `[source,console-result]`: Matches this snippet against the body of the
53+
response of the last test. If the response is JSON then order is ignored. If
54+
you add `// TEST[continued]` to the snippet after `[source,console-result]`
55+
it will continue in the same test, allowing you to interleave requests with
56+
responses to check.
57+
* `// TESTRESPONSE`: Explicitly marks a snippet as a test response even without
58+
`[source,console-result]`. Similarly to `// TEST` this is mostly used for
59+
its modifiers.
60+
* You can't use `[source,console-result]` immediately after `// TESTSETUP`.
61+
Instead, consider using `// TEST[continued]` or rearrange your snippets.
62+
63+
NOTE: Previously we only used `// TESTRESPONSE` instead of
64+
`[source,console-result]` so you'll see that a lot in older branches but we
65+
prefer `[source,console-result]` now.
66+
5267
* `// TESTRESPONSE[s/foo/bar/]`: Substitutions. See `// TEST[s/foo/bar]` for
5368
how it works. These are much more common than `// TEST[s/foo/bar]` because
5469
they are useful for eliding portions of the response that are not pertinent
@@ -62,8 +77,6 @@ for its modifiers:
6277
"figures out" the path. This is especially useful for making sweeping
6378
assertions like "I made up all the numbers in this example, don't compare
6479
them" which looks like `// TESTRESPONSE[s/\d+/$body.$_path/]`.
65-
* You can't use `// TESTRESPONSE` immediately after `// TESTSETUP`. Instead,
66-
consider using `// TEST[continued]` or rearrange your snippets.
6780
* `// TESTRESPONSE[non_json]`: Add substitutions for testing responses in a
6881
format other than JSON. Use this after all other substitutions so it doesn't
6982
make other substitutions difficult.
@@ -98,7 +111,7 @@ endyaml
98111
```
99112

100113
This allows slightly more expressive testing of the snippets. Since that syntax
101-
is not supported by CONSOLE the usual way to incorporate it is with a
114+
is not supported by `[source,console]` the usual way to incorporate it is with a
102115
`// TEST[s//]` marker like this:
103116

104117
```

docs/painless/painless-guide/painless-execute-script.asciidoc

+6-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ If no context is specified then this context is used by default.
3030

3131
Request:
3232

33-
[source,js]
33+
[source,console]
3434
----------------------------------------------------------------
3535
POST /_scripts/painless/_execute
3636
{
@@ -43,17 +43,15 @@ POST /_scripts/painless/_execute
4343
}
4444
}
4545
----------------------------------------------------------------
46-
// CONSOLE
4746

4847
Response:
4948

50-
[source,js]
49+
[source,console-result]
5150
--------------------------------------------------
5251
{
5352
"result": "0.1"
5453
}
5554
--------------------------------------------------
56-
// TESTRESPONSE
5755

5856
===== Filter context
5957

@@ -69,7 +67,7 @@ index:: The name of an index containing a mapping that is compatible with the do
6967

7068
*Example*
7169

72-
[source,js]
70+
[source,console]
7371
----------------------------------------------------------------
7472
PUT /my-index
7573
{
@@ -99,17 +97,15 @@ POST /_scripts/painless/_execute
9997
}
10098
}
10199
----------------------------------------------------------------
102-
// CONSOLE
103100

104101
Response:
105102

106-
[source,js]
103+
[source,console-result]
107104
--------------------------------------------------
108105
{
109106
"result": true
110107
}
111108
--------------------------------------------------
112-
// TESTRESPONSE
113109

114110

115111
===== Score context
@@ -125,7 +121,7 @@ query:: If `_score` is used in the script then a query can specified that will b
125121

126122
*Example*
127123

128-
[source,js]
124+
[source,console]
129125
----------------------------------------------------------------
130126
PUT /my-index
131127
{
@@ -159,14 +155,12 @@ POST /_scripts/painless/_execute
159155
}
160156
}
161157
----------------------------------------------------------------
162-
// CONSOLE
163158

164159
Response:
165160

166-
[source,js]
161+
[source,console-result]
167162
--------------------------------------------------
168163
{
169164
"result": 0.8
170165
}
171166
--------------------------------------------------
172-
// TESTRESPONSE

docs/reference/docs/index_.asciidoc

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ automatic creation of indices that match specified patterns, or set it to
140140
comma-separated list of patterns you want to allow, or prefix each pattern with
141141
`+` or `-` to indicate whether it should be allowed or blocked.
142142

143-
[source,js]
143+
[source,console]
144144
--------------------------------------------------
145145
PUT _cluster/settings
146146
{
@@ -163,7 +163,6 @@ PUT _cluster/settings
163163
}
164164
}
165165
--------------------------------------------------
166-
// CONSOLE
167166

168167
<1> Allow auto-creation of indices called `twitter` or `index10`, block the
169168
creation of indices that match the pattern `index1*`, and allow creation of

0 commit comments

Comments
 (0)