Skip to content

Commit

Permalink
Some more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby committed Apr 9, 2024
1 parent edecfac commit c70c0b5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<>());
}
}
Expand Down Expand Up @@ -205,7 +204,8 @@ List<Snippet> parseDocFiles(File rootDir, File docFile, List<Map.Entry<String, S
continue;
}
emit(snippet, contents.toString(), defaultSubstitutions, substitutions);
substitutions = new ArrayList<>();;
substitutions = new ArrayList<>();
;
snippet = null;
contents = null;
}
Expand Down Expand Up @@ -262,13 +262,7 @@ private boolean testResponseHandled(
return false;
}

private boolean testHandled(
String name,
int lineNumber,
String line,
Snippet snippet,
List<Map.Entry<String, String>> substitutions
) {
private boolean testHandled(String name, int lineNumber, String line, Snippet snippet, List<Map.Entry<String, String>> substitutions) {
Matcher matcher = Pattern.compile("\\/\\/\s*TEST(\\[(.+)\\])?\s*").matcher(line);
if (matcher.matches()) {
if (snippet == null) {
Expand Down Expand Up @@ -413,5 +407,4 @@ public Source(boolean matches, String language, String name) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.gradle.api.tasks.TaskProvider;

import java.util.Map;

import javax.inject.Inject;

public class Docs2Plugin implements Plugin<Project> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c70c0b5

Please sign in to comment.