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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.google.gson.stream.JsonReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Pre upload hook that ensures art-buildbot expectation files (files under //art/tools ending with
* "_failures.txt", e.g. //art/tools/libcore_failures.txt) are well-formed json files.
*
* It makes basic validation of the keys but does not cover all the cases. Parser structure is
* based on external/vogar/src/vogar/ExpectationStore.java.
*
* Hook is set up in //art/PREUPLOAD.cfg See also //tools/repohooks/README.md
*/
class PresubmitJsonLinter {
private static final int FLAGS = Pattern.MULTILINE | Pattern.DOTALL;
private static final Set<String> RESULTS = new HashSet<>();
static {
RESULTS.addAll(List.of(
"UNSUPPORTED",
"COMPILE_FAILED",
"EXEC_FAILED",
"EXEC_TIMEOUT",
"ERROR",
"SUCCESS"
));
}
public static void main(String[] args) {
for (String arg : args) {
info("Checking " + arg);
checkExpectationFile(arg);
}
}
private static void info(String message) {
System.err.println(message);
}
private static void error(String message) {
System.err.println(message);
System.exit(1);
}
private static void checkExpectationFile(String arg) {
JsonReader reader;
try {
reader = new JsonReader(new FileReader(arg));
} catch (FileNotFoundException e) {
error("File '" + arg + "' is not found");
return;
}
reader.setLenient(true);
try {
reader.beginArray();
while (reader.hasNext()) {
readExpectation(reader);
}
reader.endArray();
} catch (IOException e) {
error("Malformed json: " + reader);
}
}
private static void readExpectation(JsonReader reader) throws IOException {
Set<String> names = new LinkedHashSet<String>();
Set<String> tags = new LinkedHashSet<String>();
boolean readResult = false;
boolean readDescription = false;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "result":
String result = reader.nextString();
if (!RESULTS.contains(result)) {
error("Invalid 'result' value: '" + result +
"'. Expected one of " + String.join(", ", RESULTS) +
". " + reader);
}
readResult = true;
break;
case "substring": {
try {
Pattern.compile(
".*" + Pattern.quote(reader.nextString()) + ".*", FLAGS);
} catch (PatternSyntaxException e) {
error("Malformed 'substring' value: " + reader);
}
}
case "pattern": {
try {
Pattern.compile(reader.nextString(), FLAGS);
} catch (PatternSyntaxException e) {
error("Malformed 'pattern' value: " + reader);
}
break;
}
case "failure":
names.add(reader.nextString());
break;
case "description":
reader.nextString();
readDescription = true;
break;
case "name":
names.add(reader.nextString());
break;
case "names":
readStrings(reader, names);
break;
case "tags":
readStrings(reader, tags);
break;
case "bug":
reader.nextLong();
break;
case "modes":
readModes(reader);
break;
case "modes_variants":
readModesAndVariants(reader);
break;
default:
error("Unknown key '" + name + "' in expectations file");
reader.skipValue();
break;
}
}
reader.endObject();
if (names.isEmpty()) {
error("Missing 'name' or 'failure' key in " + reader);
}
if (!readResult) {
error("Missing 'result' key in " + reader);
}
if (!readDescription) {
error("Missing 'description' key in " + reader);
}
}
private static void readStrings(JsonReader reader, Set<String> output) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
output.add(reader.nextString());
}
reader.endArray();
}
private static void readModes(JsonReader reader) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
reader.nextString();
}
reader.endArray();
}
/**
* Expected format: mode_variants: [["host", "X32"], ["host", "X64"]]
*/
private static void readModesAndVariants(JsonReader reader) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
reader.beginArray();
reader.nextString();
reader.nextString();
reader.endArray();
}
reader.endArray();
}
}
|