Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby committed Apr 9, 2024
1 parent d79de5f commit 7c034a9
Show file tree
Hide file tree
Showing 8 changed files with 2,733 additions and 1,865 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract class RestTestsFromSnippetsTask extends SnippetsTask {
abstract FileOperations getFileOperations();

@Inject
RestTestsFromSnippetsTask(ObjectFactory objectFactory) {
RestTestsFromDocSnippetsTask(ObjectFactory objectFactory) {
testRoot = objectFactory.directoryProperty()
TestBuilder builder = new TestBuilder()
perSnippet = new Action<Snippet>() {
Expand Down Expand Up @@ -202,7 +202,6 @@ abstract class RestTestsFromSnippetsTask extends SnippetsTask {
* Called each time a snippet is encountered. Tracks the snippets and
* calls buildTest to actually build the test.
*/

void handleSnippet(Snippet snippet) {
if (RestTestsFromSnippetsTask.isConsoleCandidate(snippet)) {
unconvertedCandidates.add(snippet.path.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,24 +338,24 @@ public void extraContent(String message, String s, int offset, String location,
* match then blow up. If the closure takes two parameters then the second
* one is "is this the last match?".
*/
protected void parse(String location, String s, String pattern, BiConsumer<Matcher, Boolean> testHandler) {
if (s == null) {
protected void parse(String location, String content, String pattern, BiConsumer<Matcher, Boolean> testHandler) {
if (content == null) {
return; // Silly null, only real stuff gets to match!
}
Matcher m = Pattern.compile(pattern).matcher(s);
Matcher m = Pattern.compile(pattern).matcher(content);
int offset = 0;
while (m.find()) {
if (m.start() != offset) {
extraContent("between [$offset] and [${m.start()}]", s, offset, location, pattern);
extraContent("between [$offset] and [${m.start()}]", content, offset, location, pattern);
}
offset = m.end();
testHandler.accept(m, offset == s.length());
testHandler.accept(m, offset == content.length());
}
if (offset == 0) {
throw new InvalidUserDataException(location + ": Didn't match " + pattern + ": " + s);
throw new InvalidUserDataException(location + ": Didn't match " + pattern + ": " + content);
}
if (offset != s.length()) {
extraContent("after [" + offset + "]", s, offset, location, pattern);
if (offset != content.length()) {
extraContent("after [" + offset + "]", content, offset, location, pattern);
}
}

Expand Down Expand Up @@ -415,4 +415,5 @@ public Source(boolean matches, String language, String name) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,19 @@ public void apply(Project project) {
task.setGroup("Docs");
task.setDescription("List each snippet");
task.setDefaultSubstitutions(commonDefaultSubstitutions);
task.setPerSnippet(snippet -> System.out.println(snippet.toString()));
task.setPerSnippet(snippet -> System.out.println(snippet));
});

project.getTasks().register("listConsoleCandidates", DocSnippetTask.class, task -> {
task.setGroup("Docs");
task.setDescription("List snippets that probably should be marked // CONSOLE");
task.setDefaultSubstitutions(commonDefaultSubstitutions);
task.setPerSnippet(snippet -> {
if (isConsoleCandidate(snippet)) {
System.out.println(snippet.toString());
if (snippet.isConsoleCandidate()) {
System.out.println(snippet);
}
});
});
}

/**
* Is this snippet a candidate for conversion to `// CONSOLE`?
*/
private static boolean isConsoleCandidate(Snippet snippet) {
/* Snippets that are responses or already marked as `// CONSOLE` or
* `// NOTCONSOLE` are not candidates. */
if (snippet.console != null || snippet.testResponse) {
return false;
}
/* js snippets almost always should be marked with `// CONSOLE`. js
* snippets that shouldn't be marked `// CONSOLE`, like examples for
* js client, should always be marked with `// NOTCONSOLE`.
*
* `sh` snippets that contain `curl` almost always should be marked
* with `// CONSOLE`. In the exceptionally rare cases where they are
* not communicating with Elasticsearch, like the examples in the ec2
* and gce discovery plugins, the snippets should be marked
* `// NOTCONSOLE`. */
return snippet.language.equals("js") || snippet.curl;
}
}
Loading

0 comments on commit 7c034a9

Please sign in to comment.