author | hannesw |
Fri, 26 Jul 2013 13:50:52 +0200 | |
changeset 19103 | 3905e242e69c |
parent 17526 | 3194676cb555 |
child 24778 | 2ff5d7041566 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.test.framework; |
|
27 |
||
28 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG; |
|
29 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_COMPARE; |
|
30 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_COMPILE_FAIL; |
|
31 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_RUN_FAIL; |
|
19103
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
32 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_FORK; |
16147 | 33 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_IGNORE_STD_ERROR; |
34 |
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_RUN; |
|
17526 | 35 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_FAILED_LIST_FILE; |
16147 | 36 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ENABLE_STRICT_MODE; |
37 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDES_FILE; |
|
38 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_DIR; |
|
39 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_LIST; |
|
40 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_FRAMEWORK; |
|
41 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_INCLUDES; |
|
42 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_LIST; |
|
43 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ROOTS; |
|
44 |
import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_UNCHECKED_DIR; |
|
45 |
||
17526 | 46 |
import java.io.BufferedReader; |
16147 | 47 |
import java.io.File; |
17526 | 48 |
import java.io.FileReader; |
16147 | 49 |
import java.io.IOException; |
50 |
import java.nio.file.FileSystem; |
|
51 |
import java.nio.file.FileSystems; |
|
52 |
import java.nio.file.FileVisitOption; |
|
53 |
import java.nio.file.FileVisitResult; |
|
54 |
import java.nio.file.Files; |
|
55 |
import java.nio.file.Path; |
|
56 |
import java.nio.file.SimpleFileVisitor; |
|
57 |
import java.nio.file.attribute.BasicFileAttributes; |
|
58 |
import java.util.ArrayList; |
|
59 |
import java.util.Collections; |
|
60 |
import java.util.EnumSet; |
|
61 |
import java.util.HashMap; |
|
62 |
import java.util.HashSet; |
|
63 |
import java.util.List; |
|
64 |
import java.util.Map; |
|
65 |
import java.util.Scanner; |
|
66 |
import java.util.Set; |
|
67 |
import javax.xml.xpath.XPath; |
|
68 |
import javax.xml.xpath.XPathConstants; |
|
69 |
import javax.xml.xpath.XPathExpressionException; |
|
70 |
import javax.xml.xpath.XPathFactory; |
|
71 |
import org.w3c.dom.NodeList; |
|
72 |
import org.xml.sax.InputSource; |
|
73 |
||
74 |
/** |
|
75 |
* Utility class to find/parse script test files and to create 'test' instances. |
|
76 |
* Actual 'test' object type is decided by clients of this class. |
|
77 |
*/ |
|
78 |
final class TestFinder { |
|
79 |
private TestFinder() {} |
|
80 |
||
81 |
interface TestFactory<T> { |
|
82 |
// 'test' instance type is decided by the client. |
|
83 |
T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments); |
|
84 |
// place to log messages from TestFinder |
|
85 |
void log(String mg); |
|
86 |
} |
|
87 |
||
88 |
||
89 |
// finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found |
|
90 |
static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception { |
|
91 |
final String framework = System.getProperty(TEST_JS_FRAMEWORK); |
|
92 |
final String testList = System.getProperty(TEST_JS_LIST); |
|
17526 | 93 |
final String failedTestFileName = System.getProperty(TEST_FAILED_LIST_FILE); |
94 |
if(failedTestFileName != null) { |
|
95 |
File failedTestFile = new File(failedTestFileName); |
|
96 |
if(failedTestFile.exists() && failedTestFile.length() > 0L) { |
|
97 |
try(final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) { |
|
98 |
for(;;) { |
|
99 |
final String testFileName = r.readLine(); |
|
100 |
if(testFileName == null) { |
|
101 |
break; |
|
102 |
} |
|
103 |
handleOneTest(framework, new File(testFileName).toPath(), tests, orphans, testFactory); |
|
104 |
} |
|
105 |
} |
|
106 |
return; |
|
107 |
} |
|
108 |
} |
|
16147 | 109 |
if (testList == null || testList.length() == 0) { |
110 |
// Run the tests under the test roots dir, selected by the |
|
111 |
// TEST_JS_INCLUDES patterns |
|
112 |
final String testRootsString = System.getProperty(TEST_JS_ROOTS, "test/script"); |
|
113 |
if (testRootsString == null || testRootsString.length() == 0) { |
|
114 |
throw new Exception("Error: " + TEST_JS_ROOTS + " must be set"); |
|
115 |
} |
|
116 |
final String testRoots[] = testRootsString.split(" "); |
|
117 |
final FileSystem fileSystem = FileSystems.getDefault(); |
|
118 |
final Set<String> testExcludeSet = getExcludeSet(); |
|
119 |
final Path[] excludePaths = getExcludeDirs(); |
|
120 |
for (final String root : testRoots) { |
|
121 |
final Path dir = fileSystem.getPath(root); |
|
122 |
findTests(framework, dir, tests, orphans, excludePaths, testExcludeSet, testFactory); |
|
123 |
} |
|
124 |
} else { |
|
125 |
// TEST_JS_LIST contains a blank speparated list of test file names. |
|
126 |
final String strArray[] = testList.split(" "); |
|
127 |
for (final String ss : strArray) { |
|
128 |
handleOneTest(framework, new File(ss).toPath(), tests, orphans, testFactory); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
private static boolean inExcludePath(final Path file, final Path[] excludePaths) { |
|
134 |
if (excludePaths == null) { |
|
135 |
return false; |
|
136 |
} |
|
137 |
||
138 |
for (final Path excludePath : excludePaths) { |
|
139 |
if (file.startsWith(excludePath)) { |
|
140 |
return true; |
|
141 |
} |
|
142 |
} |
|
143 |
return false; |
|
144 |
} |
|
145 |
||
146 |
private static <T> void findTests(final String framework, final Path dir, final List<T> tests, final Set<String> orphanFiles, final Path[] excludePaths, final Set<String> excludedTests, final TestFactory<T> factory) throws Exception { |
|
147 |
final String pattern = System.getProperty(TEST_JS_INCLUDES); |
|
148 |
final String extension = pattern == null ? "js" : pattern; |
|
149 |
final Exception[] exceptions = new Exception[1]; |
|
150 |
final List<String> excludedActualTests = new ArrayList<>(); |
|
151 |
||
152 |
if (! dir.toFile().isDirectory()) { |
|
153 |
factory.log("WARNING: " + dir + " not found or not a directory"); |
|
154 |
} |
|
155 |
||
156 |
Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { |
|
157 |
@Override |
|
158 |
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { |
|
159 |
final String fileName = file.getName(file.getNameCount() - 1).toString(); |
|
160 |
if (fileName.endsWith(extension)) { |
|
161 |
final String namex = file.toString().replace('\\', '/'); |
|
162 |
if (!inExcludePath(file, excludePaths) && !excludedTests.contains(file.getFileName().toString())) { |
|
163 |
try { |
|
164 |
handleOneTest(framework, file, tests, orphanFiles, factory); |
|
165 |
} catch (final Exception ex) { |
|
166 |
exceptions[0] = ex; |
|
167 |
return FileVisitResult.TERMINATE; |
|
168 |
} |
|
169 |
} else { |
|
170 |
excludedActualTests.add(namex); |
|
171 |
} |
|
172 |
} |
|
173 |
return FileVisitResult.CONTINUE; |
|
174 |
} |
|
175 |
}); |
|
176 |
Collections.sort(excludedActualTests); |
|
177 |
||
178 |
for (final String excluded : excludedActualTests) { |
|
179 |
factory.log("Excluding " + excluded); |
|
180 |
} |
|
181 |
||
182 |
if (exceptions[0] != null) { |
|
183 |
throw exceptions[0]; |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
private static final String uncheckedDirs[] = System.getProperty(TEST_JS_UNCHECKED_DIR, "test/script/external/test262/").split(" "); |
|
188 |
||
189 |
private static boolean isUnchecked(final Path testFile) { |
|
190 |
for (final String uncheckedDir : uncheckedDirs) { |
|
191 |
if (testFile.startsWith(uncheckedDir)) { |
|
192 |
return true; |
|
193 |
} |
|
194 |
} |
|
195 |
return false; |
|
196 |
} |
|
197 |
||
198 |
private static <T> void handleOneTest(final String framework, final Path testFile, final List<T> tests, final Set<String> orphans, TestFactory<T> factory) throws Exception { |
|
199 |
final String name = testFile.getFileName().toString(); |
|
200 |
||
201 |
assert name.lastIndexOf(".js") > 0 : "not a JavaScript: " + name; |
|
202 |
||
203 |
// defaults: testFile is a test and should be run |
|
204 |
boolean isTest = isUnchecked(testFile); |
|
205 |
boolean isNotTest = false; |
|
206 |
boolean shouldRun = true; |
|
207 |
boolean compileFailure = false; |
|
208 |
boolean runFailure = false; |
|
209 |
boolean checkCompilerMsg = false; |
|
210 |
boolean noCompare = false; |
|
211 |
boolean ignoreStdError = false; |
|
19103
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
212 |
boolean fork = false; |
16147 | 213 |
|
214 |
final List<String> engineOptions = new ArrayList<>(); |
|
215 |
final List<String> scriptArguments = new ArrayList<>(); |
|
216 |
boolean inComment = false; |
|
217 |
||
218 |
try (Scanner scanner = new Scanner(testFile)) { |
|
219 |
while (scanner.hasNext()) { |
|
220 |
// TODO: Scan for /ref=file qualifiers, etc, to determine run |
|
221 |
// behavior |
|
222 |
String token = scanner.next(); |
|
223 |
if (token.startsWith("/*")) { |
|
224 |
inComment = true; |
|
225 |
} else if (token.endsWith(("*/"))) { |
|
226 |
inComment = false; |
|
227 |
} else if (!inComment) { |
|
228 |
continue; |
|
229 |
} |
|
230 |
||
231 |
// remove whitespace and trailing semicolons, if any |
|
232 |
// (trailing semicolons are found in some sputnik tests) |
|
233 |
token = token.trim(); |
|
234 |
final int semicolon = token.indexOf(';'); |
|
235 |
if (semicolon > 0) { |
|
236 |
token = token.substring(0, semicolon); |
|
237 |
} |
|
238 |
switch (token) { |
|
239 |
case "@test": |
|
240 |
isTest = true; |
|
241 |
break; |
|
242 |
case "@test/fail": |
|
243 |
isTest = true; |
|
244 |
compileFailure = true; |
|
245 |
break; |
|
246 |
case "@test/compile-error": |
|
247 |
isTest = true; |
|
248 |
compileFailure = true; |
|
249 |
checkCompilerMsg = true; |
|
250 |
shouldRun = false; |
|
251 |
break; |
|
252 |
case "@test/warning": |
|
253 |
isTest = true; |
|
254 |
checkCompilerMsg = true; |
|
255 |
break; |
|
256 |
case "@test/nocompare": |
|
257 |
isTest = true; |
|
258 |
noCompare = true; |
|
259 |
break; |
|
260 |
case "@subtest": |
|
261 |
isTest = false; |
|
262 |
isNotTest = true; |
|
263 |
break; |
|
264 |
case "@runif": |
|
265 |
if (System.getProperty(scanner.next()) != null) { |
|
266 |
shouldRun = true; |
|
267 |
} else { |
|
268 |
isTest = false; |
|
269 |
isNotTest = true; |
|
270 |
} |
|
271 |
break; |
|
272 |
case "@run": |
|
273 |
shouldRun = true; |
|
274 |
break; |
|
275 |
case "@run/fail": |
|
276 |
shouldRun = true; |
|
277 |
runFailure = true; |
|
278 |
break; |
|
279 |
case "@run/ignore-std-error": |
|
280 |
shouldRun = true; |
|
281 |
ignoreStdError = true; |
|
282 |
break; |
|
283 |
case "@argument": |
|
284 |
scriptArguments.add(scanner.next()); |
|
285 |
break; |
|
286 |
case "@option": |
|
287 |
engineOptions.add(scanner.next()); |
|
288 |
break; |
|
19103
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
289 |
case "@fork": |
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
290 |
fork = true; |
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
291 |
break; |
16147 | 292 |
} |
293 |
||
294 |
// negative tests are expected to fail at runtime only |
|
295 |
// for those tests that are expected to fail at compile time, |
|
296 |
// add @test/compile-error |
|
297 |
if (token.equals("@negative") || token.equals("@strict_mode_negative")) { |
|
298 |
shouldRun = true; |
|
299 |
runFailure = true; |
|
300 |
} |
|
301 |
||
302 |
if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) { |
|
303 |
if (!strictModeEnabled()) { |
|
304 |
return; |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
} catch (final Exception ignored) { |
|
309 |
return; |
|
310 |
} |
|
311 |
||
312 |
if (isTest) { |
|
313 |
final Map<String, String> testOptions = new HashMap<>(); |
|
314 |
if (compileFailure) { |
|
315 |
testOptions.put(OPTIONS_EXPECT_COMPILE_FAIL, "true"); |
|
316 |
} |
|
317 |
if (shouldRun) { |
|
318 |
testOptions.put(OPTIONS_RUN, "true"); |
|
319 |
} |
|
320 |
if (runFailure) { |
|
321 |
testOptions.put(OPTIONS_EXPECT_RUN_FAIL, "true"); |
|
322 |
} |
|
323 |
if (checkCompilerMsg) { |
|
324 |
testOptions.put(OPTIONS_CHECK_COMPILE_MSG, "true"); |
|
325 |
} |
|
326 |
if (!noCompare) { |
|
327 |
testOptions.put(OPTIONS_COMPARE, "true"); |
|
328 |
} |
|
329 |
if (ignoreStdError) { |
|
330 |
testOptions.put(OPTIONS_IGNORE_STD_ERROR, "true"); |
|
331 |
} |
|
19103
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
332 |
if (fork) { |
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
333 |
testOptions.put(OPTIONS_FORK, "true"); |
3905e242e69c
8020719: Run tests with reduced splitter threshold
hannesw
parents:
17526
diff
changeset
|
334 |
} |
16147 | 335 |
|
336 |
tests.add(factory.createTest(framework, testFile.toFile(), engineOptions, testOptions, scriptArguments)); |
|
337 |
} else if (!isNotTest) { |
|
338 |
orphans.add(name); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
private static boolean strictModeEnabled() { |
|
343 |
return Boolean.getBoolean(TEST_JS_ENABLE_STRICT_MODE); |
|
344 |
} |
|
345 |
||
346 |
private static Set<String> getExcludeSet() throws XPathExpressionException { |
|
347 |
final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST); |
|
348 |
||
349 |
String[] testExcludeArray = {}; |
|
350 |
if (testExcludeList != null) { |
|
351 |
testExcludeArray = testExcludeList.split(" "); |
|
352 |
} |
|
353 |
final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length); |
|
354 |
for (final String test : testExcludeArray) { |
|
355 |
testExcludeSet.add(test); |
|
356 |
} |
|
357 |
||
358 |
final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE); |
|
359 |
if (testExcludesFile != null && !testExcludesFile.isEmpty()) { |
|
360 |
try { |
|
361 |
loadExcludesFile(testExcludesFile, testExcludeSet); |
|
362 |
} catch (final XPathExpressionException e) { |
|
363 |
System.err.println("Error: unable to load test excludes from " + testExcludesFile); |
|
364 |
e.printStackTrace(); |
|
365 |
throw e; |
|
366 |
} |
|
367 |
} |
|
368 |
return testExcludeSet; |
|
369 |
} |
|
370 |
||
371 |
private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException { |
|
372 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
373 |
final NodeList testIds = (NodeList)xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET); |
|
374 |
for (int i = testIds.getLength() - 1; i >= 0; i--) { |
|
375 |
testExcludeSet.add(testIds.item(i).getNodeValue()); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
private static Path[] getExcludeDirs() { |
|
380 |
final String excludeDirs[] = System.getProperty(TEST_JS_EXCLUDE_DIR, "test/script/currently-failing").split(" "); |
|
381 |
final Path[] excludePaths = new Path[excludeDirs.length]; |
|
382 |
final FileSystem fileSystem = FileSystems.getDefault(); |
|
383 |
int i = 0; |
|
384 |
for (final String excludeDir : excludeDirs) { |
|
385 |
excludePaths[i++] = fileSystem.getPath(excludeDir); |
|
386 |
} |
|
387 |
return excludePaths; |
|
388 |
} |
|
389 |
} |