From c70c0b589a8c36e791a81802abbc38325837ab95 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Tue, 9 Apr 2024 18:49:02 +0200 Subject: [PATCH] Some more test coverage --- .../gradle/internal/docs/DocSnippetTask.java | 13 +-- .../gradle/internal/docs/Docs2Plugin.java | 1 + .../docs/RestTestsFromDocSnippetTask.java | 85 ++++++++++--------- .../RestTestsFromDocSnippetTaskSpec.groovy | 39 ++++++++- 4 files changed, 85 insertions(+), 53 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/DocSnippetTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/DocSnippetTask.java index 660ec14efbf30..be16254e6bcce 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/DocSnippetTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/DocSnippetTask.java @@ -128,7 +128,6 @@ private static void escapeSubstitutions( @TaskAction void executeTask() { for (File file : docs) { - System.out.println("file = " + file); parseDocFiles(docs.getDir(), file, new ArrayList<>()); } } @@ -205,7 +204,8 @@ List parseDocFiles(File rootDir, File docFile, List();; + substitutions = new ArrayList<>(); + ; snippet = null; contents = null; } @@ -262,13 +262,7 @@ private boolean testResponseHandled( return false; } - private boolean testHandled( - String name, - int lineNumber, - String line, - Snippet snippet, - List> substitutions - ) { + private boolean testHandled(String name, int lineNumber, String line, Snippet snippet, List> substitutions) { Matcher matcher = Pattern.compile("\\/\\/\s*TEST(\\[(.+)\\])?\s*").matcher(line); if (matcher.matches()) { if (snippet == null) { @@ -413,5 +407,4 @@ public Source(boolean matches, String language, String name) { } } - } diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/Docs2Plugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/Docs2Plugin.java index 5737df81ad470..a964b67b706e2 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/Docs2Plugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/Docs2Plugin.java @@ -25,6 +25,7 @@ import org.gradle.api.tasks.TaskProvider; import java.util.Map; + import javax.inject.Inject; public class Docs2Plugin implements Plugin { diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTask.java index fe8f6112e4364..4d172eb6a5d0f 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTask.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; + import javax.inject.Inject; public abstract class RestTestsFromDocSnippetTask extends DocSnippetTask { @@ -129,6 +130,48 @@ public RestTestsFromDocSnippetTask(ObjectFactory objectFactory) { }); } + /** + * Certain requests should not have the shard failure check because the + * format of the response is incompatible i.e. it is not a JSON object. + */ + static boolean shouldAddShardFailureCheck(String path) { + return path.startsWith("_cat") == false && path.startsWith("_ml/datafeeds/") == false; + } + + /** + * Converts Kibana's block quoted strings into standard JSON. These + * {@code """} delimited strings can be embedded in CONSOLE and can + * contain newlines and {@code "} without the normal JSON escaping. + * This has to add it. + */ + @PackageScope + static String replaceBlockQuote(String body) { + int start = body.indexOf("\"\"\""); + if (start < 0) { + return body; + } + /* + * 1.3 is a fairly wild guess of the extra space needed to hold + * the escaped string. + */ + StringBuilder result = new StringBuilder((int) (body.length() * 1.3)); + int startOfNormal = 0; + while (start >= 0) { + int end = body.indexOf("\"\"\"", start + 3); + if (end < 0) { + throw new InvalidUserDataException("Invalid block quote starting at " + start + " in:\n" + body); + } + result.append(body.substring(startOfNormal, start)); + result.append('"'); + result.append(body.substring(start + 3, end).replace("\"", "\\\"").replace("\n", "\\n")); + result.append('"'); + startOfNormal = end + 3; + start = body.indexOf("\"\"\"", startOfNormal); + } + result.append(body.substring(startOfNormal)); + return result.toString(); + } + private class TestBuilder { /** * These languages aren't supported by the syntax highlighter so we @@ -301,40 +344,6 @@ private void testTearDown(Snippet snippet) { body(snippet, true); } - /** - * Converts Kibana's block quoted strings into standard JSON. These - * {@code """} delimited strings can be embedded in CONSOLE and can - * contain newlines and {@code "} without the normal JSON escaping. - * This has to add it. - */ - @PackageScope - static String replaceBlockQuote(String body) { - int start = body.indexOf("\"\"\""); - if (start < 0) { - return body; - } - /* - * 1.3 is a fairly wild guess of the extra space needed to hold - * the escaped string. - */ - StringBuilder result = new StringBuilder((int) (body.length() * 1.3)); - int startOfNormal = 0; - while (start >= 0) { - int end = body.indexOf("\"\"\"", start + 3); - if (end < 0) { - throw new InvalidUserDataException("Invalid block quote starting at $start in:\n" + body); - } - result.append(body.substring(startOfNormal, start)); - result.append('"'); - result.append(body.substring(start + 3, end).replace("\"", "\\\"").replace("\n", "\\n")); - result.append('"'); - startOfNormal = end + 3; - start = body.indexOf("\"\"\"", startOfNormal); - } - result.append(body.substring(startOfNormal)); - return result.toString(); - } - void emitDo( String method, String pathAndQuery, @@ -401,14 +410,6 @@ void emitDo( } } - /** - * Certain requests should not have the shard failure check because the - * format of the response is incompatible i.e. it is not a JSON object. - */ - static boolean shouldAddShardFailureCheck(String path) { - return path.startsWith("_cat") == false && path.startsWith("_ml/datafeeds/") == false; - } - private void body(Snippet snippet, boolean inSetup) { parse(snippet.getLocation(), snippet.contents, SYNTAX, (matcher, last) -> { if (matcher.group("comment") != null) { diff --git a/build-tools-internal/src/test/groovy/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTaskSpec.groovy b/build-tools-internal/src/test/groovy/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTaskSpec.groovy index c10afbb05101c..f2d4e1214f5e5 100644 --- a/build-tools-internal/src/test/groovy/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTaskSpec.groovy +++ b/build-tools-internal/src/test/groovy/org/elasticsearch/gradle/internal/docs/RestTestsFromDocSnippetTaskSpec.groovy @@ -11,13 +11,50 @@ package org.elasticsearch.gradle.internal.docs import spock.lang.Specification import spock.lang.TempDir +import org.gradle.api.InvalidUserDataException import org.gradle.testfixtures.ProjectBuilder -import org.elasticsearch.gradle.internal.doc.RestTestsFromSnippetsTask + +import static org.elasticsearch.gradle.internal.doc.RestTestsFromSnippetsTask.replaceBlockQuote +import static org.elasticsearch.gradle.internal.docs.RestTestsFromDocSnippetTask.shouldAddShardFailureCheck + class RestTestsFromDocSnippetTaskSpec extends Specification { @TempDir File tempDir; + def "test simple block quote"() { + expect: + replaceBlockQuote("\"foo\": \"\"\"bort baz\"\"\"") == "\"foo\": \"bort baz\"" + } + + def "test multiple block quotes"() { + expect: + replaceBlockQuote("\"foo\": \"\"\"bort baz\"\"\", \"bar\": \"\"\"other\"\"\"") == "\"foo\": \"bort baz\", \"bar\": \"other\"" + } + + def "test escaping in block quote"() { + expect: + replaceBlockQuote("\"foo\": \"\"\"bort\" baz\"\"\"") == "\"foo\": \"bort\\\" baz\"" + replaceBlockQuote("\"foo\": \"\"\"bort\n baz\"\"\"") == "\"foo\": \"bort\\n baz\"" + } + + def "test invalid block quotes"() { + given: + String input = "\"foo\": \"\"\"bar\""; + when: + RestTestsFromDocSnippetTask.replaceBlockQuote(input); + then: + def e = thrown(InvalidUserDataException) + e.message == "Invalid block quote starting at 7 in:\n" + input + } + + def "test is doc write request"() { + expect: + shouldAddShardFailureCheck("doc-index/_search") == true + shouldAddShardFailureCheck("_cat") == false + shouldAddShardFailureCheck("_ml/datafeeds/datafeed-id/_preview") == false + } + def "can create rest tests from docs"() { def build = ProjectBuilder.builder().build() given: