forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentationSamplesTest.java
More file actions
145 lines (134 loc) · 5.11 KB
/
DocumentationSamplesTest.java
File metadata and controls
145 lines (134 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
class DocumentationSamplesTest {
@Test
void docsAndJbangSamplesUseRequiredPermissionHandler() throws IOException {
for (Path path : documentationFiles()) {
String content = stripStringsAndComments(Files.readString(path));
assertFalse(hasConfigWithoutPermissionHandler(content, "SessionConfig"),
() -> path + " contains SessionConfig sample without setOnPermissionRequest");
assertFalse(hasConfigWithoutPermissionHandler(content, "ResumeSessionConfig"),
() -> path + " contains ResumeSessionConfig sample without setOnPermissionRequest");
assertFalse(hasSingleArgumentResumeSessionCall(content),
() -> path + " contains removed resumeSession(String) overload");
}
}
private static boolean hasConfigWithoutPermissionHandler(String content, String configType) {
String constructor = "new " + configType + "()";
int fromIndex = 0;
while (true) {
int start = content.indexOf(constructor, fromIndex);
if (start < 0) {
return false;
}
int end = content.indexOf(';', start);
if (end < 0) {
end = content.length();
}
if (!content.substring(start, end).contains("setOnPermissionRequest(")) {
return true;
}
fromIndex = start + constructor.length();
}
}
private static boolean hasSingleArgumentResumeSessionCall(String content) {
int fromIndex = 0;
while (true) {
int callStart = content.indexOf("resumeSession(", fromIndex);
if (callStart < 0) {
return false;
}
int index = callStart + "resumeSession(".length();
int depth = 1;
int topLevelCommaCount = 0;
while (index < content.length() && depth > 0) {
char c = content.charAt(index);
if (c == '(') {
depth++;
} else if (c == ')') {
depth--;
} else if (c == ',' && depth == 1) {
topLevelCommaCount++;
}
index++;
}
if (depth == 0 && topLevelCommaCount == 0) {
return true;
}
fromIndex = callStart + 1;
}
}
private static String stripStringsAndComments(String input) {
StringBuilder out = new StringBuilder(input.length());
int i = 0;
while (i < input.length()) {
char c = input.charAt(i);
if (c == '"' || c == '\'') {
char quote = c;
out.append(' ');
i++;
while (i < input.length()) {
char current = input.charAt(i);
out.append(' ');
if (current == '\\') {
i++;
if (i < input.length()) {
out.append(' ');
}
} else if (current == quote) {
i++;
break;
}
i++;
}
continue;
}
if (c == '/' && i + 1 < input.length()) {
char next = input.charAt(i + 1);
if (next == '/') {
out.append(' ').append(' ');
i += 2;
while (i < input.length() && input.charAt(i) != '\n') {
out.append(' ');
i++;
}
continue;
}
if (next == '*') {
out.append(' ').append(' ');
i += 2;
while (i + 1 < input.length() && !(input.charAt(i) == '*' && input.charAt(i + 1) == '/')) {
out.append(' ');
i++;
}
if (i + 1 < input.length()) {
out.append(' ').append(' ');
i += 2;
}
continue;
}
}
out.append(c);
i++;
}
return out.toString();
}
private static List<Path> documentationFiles() throws IOException {
Path root = Path.of("").toAbsolutePath();
List<Path> files = new ArrayList<>();
files.add(root.resolve("README.md"));
files.add(root.resolve("jbang-example.java"));
try (Stream<Path> markdownFiles = Files.walk(root.resolve("src/site/markdown"))) {
markdownFiles.filter(Files::isRegularFile).filter(path -> path.toString().endsWith(".md"))
.forEach(files::add);
}
return files;
}
}