Skip to content

Commit 582e3f2

Browse files
committed
Docs: Allow skipping response assertions (#34240)
We generate tests from our documentation, including assertions about the responses returned by a particular API. But sometimes we *can't* assert that the response is correct because of some defficiency in our tooling. Previously we marked the response `// NOTCONSOLE` to skip it, but this is kind of odd because `// NOTCONSOLE` is really to mark snippets that are json but aren't requests or responses. This introduces a new construct to skip response assertions: ``` // TESTRESPONSE[skip:reason we skipped this] ```
1 parent f744859 commit 582e3f2

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
226226
} else {
227227
current.println('---')
228228
current.println("\"line_$test.start\":")
229-
/* The Elasticsearch test runner doesn't support the warnings
230-
* construct unless you output this skip. Since we don't know
231-
* if this snippet will use the warnings construct we emit this
232-
* warning every time. */
229+
/* The Elasticsearch test runner doesn't support quite a few
230+
* constructs unless we output this skip. We don't know if
231+
* we're going to use these constructs, but we might so we
232+
* output the skip just in case. */
233233
current.println(" - skip:")
234234
current.println(" features: ")
235235
current.println(" - stash_in_key")
@@ -249,13 +249,13 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
249249
}
250250
}
251251
}
252-
if (test.skipTest) {
252+
if (test.skip) {
253253
if (test.continued) {
254254
throw new InvalidUserDataException("Continued snippets "
255255
+ "can't be skipped")
256256
}
257257
current.println(" - always_skip")
258-
current.println(" reason: $test.skipTest")
258+
current.println(" reason: $test.skip")
259259
}
260260
if (test.setup != null) {
261261
// Insert a setup defined outside of the docs
@@ -273,9 +273,11 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
273273
}
274274

275275
private void response(Snippet response) {
276-
current.println(" - match: ")
277-
current.println(" \$body: ")
278-
response.contents.eachLine { current.println(" $it") }
276+
if (null == response.skip) {
277+
current.println(" - match: ")
278+
current.println(" \$body: ")
279+
response.contents.eachLine { current.println(" $it") }
280+
}
279281
}
280282

281283
void emitDo(String method, String pathAndQuery, String body,

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public class SnippetsTask extends DefaultTask {
122122
+ "contain `curl`.")
123123
}
124124
}
125-
if (snippet.testResponse && snippet.language == 'js') {
125+
if (snippet.testResponse
126+
&& 'js' == snippet.language
127+
&& null == snippet.skip) {
126128
String quoted = snippet.contents
127129
// quote values starting with $
128130
.replaceAll(/([:,])\s*(\$[^ ,\n}]+)/, '$1 "$2"')
@@ -216,7 +218,7 @@ public class SnippetsTask extends DefaultTask {
216218
return
217219
}
218220
if (it.group(4) != null) {
219-
snippet.skipTest = it.group(4)
221+
snippet.skip = it.group(4)
220222
return
221223
}
222224
if (it.group(5) != null) {
@@ -249,7 +251,7 @@ public class SnippetsTask extends DefaultTask {
249251
substitutions = []
250252
}
251253
String loc = "$file:$lineNumber"
252-
parse(loc, matcher.group(2), /(?:$SUBSTITUTION|$CAT) ?/) {
254+
parse(loc, matcher.group(2), /(?:$SUBSTITUTION|$CAT|$SKIP) ?/) {
253255
if (it.group(1) != null) {
254256
// TESTRESPONSE[s/adsf/jkl/]
255257
substitutions.add([it.group(1), it.group(2)])
@@ -259,6 +261,9 @@ public class SnippetsTask extends DefaultTask {
259261
substitutions.add(['\n$', '\\\\s*/'])
260262
substitutions.add(['( +)', '$1\\\\s+'])
261263
substitutions.add(['\n', '\\\\s*\n '])
264+
} else if (it.group(4) != null) {
265+
// TESTRESPONSE[skip:reason]
266+
snippet.skip = it.group(4)
262267
}
263268
}
264269
}
@@ -312,7 +317,7 @@ public class SnippetsTask extends DefaultTask {
312317
boolean test = false
313318
boolean testResponse = false
314319
boolean testSetup = false
315-
String skipTest = null
320+
String skip = null
316321
boolean continued = false
317322
String language = null
318323
String catchPart = null
@@ -337,8 +342,8 @@ public class SnippetsTask extends DefaultTask {
337342
if (catchPart) {
338343
result += "[catch: $catchPart]"
339344
}
340-
if (skipTest) {
341-
result += "[skip=$skipTest]"
345+
if (skip) {
346+
result += "[skip=$skip]"
342347
}
343348
if (continued) {
344349
result += '[continued]'
@@ -352,6 +357,9 @@ public class SnippetsTask extends DefaultTask {
352357
}
353358
if (testResponse) {
354359
result += '// TESTRESPONSE'
360+
if (skip) {
361+
result += "[skip=$skip]"
362+
}
355363
}
356364
if (testSetup) {
357365
result += '// TESTSETUP'

docs/README.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ for its modifiers:
6363
* `// TESTRESPONSE[_cat]`: Add substitutions for testing `_cat` responses. Use
6464
this after all other substitutions so it doesn't make other substitutions
6565
difficult.
66+
* `// TESTRESPONSE[skip:reason]`: Skip the assertions specified by this
67+
response.
6668
* `// TESTSETUP`: Marks this snippet as the "setup" for all other snippets in
6769
this file. This is a somewhat natural way of structuring documentation. You
6870
say "this is the data we use to explain this feature" then you add the
@@ -73,6 +75,10 @@ for its modifiers:
7375
right in the documentation file. In general, we should prefer `// TESTSETUP`
7476
over `// TEST[setup:name]` because it makes it more clear what steps have to
7577
be taken before the examples will work.
78+
* `// NOTCONSOLE`: Marks this snippet as neither `// CONSOLE` nor
79+
`// TESTRESPONSE`, excluding it from the list of unconverted snippets. We
80+
should only use this for snippets that *are* JSON but are *not* responses or
81+
requests.
7682
7783
In addition to the standard CONSOLE syntax these snippets can contain blocks
7884
of yaml surrounded by markers like this:

docs/reference/aggregations/bucket/significanttext-aggregation.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Response:
8989
}
9090
}
9191
--------------------------------------------------
92-
// NOTCONSOLE
92+
// TESTRESPONSE[skip:historically skipped]
9393

9494
The results show that "h5n1" is one of several terms strongly associated with bird flu.
9595
It only occurs 5 times in our index as a whole (see the `bg_count`) and yet 4 of these

0 commit comments

Comments
 (0)