TestFinder.java revision 1052:803bc3fd404d
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 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 26package jdk.nashorn.internal.test.framework; 27 28import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG; 29import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_COMPARE; 30import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_COMPILE_FAIL; 31import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_RUN_FAIL; 32import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_FORK; 33import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_IGNORE_STD_ERROR; 34import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_RUN; 35import static jdk.nashorn.internal.test.framework.TestConfig.TEST_FAILED_LIST_FILE; 36import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ENABLE_STRICT_MODE; 37import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDES_FILE; 38import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_DIR; 39import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_LIST; 40import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_FRAMEWORK; 41import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_INCLUDES; 42import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_LIST; 43import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ROOTS; 44import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_UNCHECKED_DIR; 45 46import java.io.BufferedReader; 47import java.io.File; 48import java.io.FileReader; 49import java.io.IOException; 50import java.nio.file.FileSystem; 51import java.nio.file.FileSystems; 52import java.nio.file.FileVisitOption; 53import java.nio.file.FileVisitResult; 54import java.nio.file.Files; 55import java.nio.file.Path; 56import java.nio.file.SimpleFileVisitor; 57import java.nio.file.attribute.BasicFileAttributes; 58import java.util.ArrayList; 59import java.util.Arrays; 60import java.util.Collections; 61import java.util.EnumSet; 62import java.util.HashMap; 63import java.util.HashSet; 64import java.util.List; 65import java.util.Map; 66import java.util.Scanner; 67import java.util.Set; 68import javax.xml.xpath.XPath; 69import javax.xml.xpath.XPathConstants; 70import javax.xml.xpath.XPathExpressionException; 71import javax.xml.xpath.XPathFactory; 72import org.w3c.dom.NodeList; 73import org.xml.sax.InputSource; 74 75/** 76 * Utility class to find/parse script test files and to create 'test' instances. 77 * Actual 'test' object type is decided by clients of this class. 78 */ 79public final class TestFinder { 80 private TestFinder() {} 81 82 interface TestFactory<T> { 83 // 'test' instance type is decided by the client. 84 T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments); 85 // place to log messages from TestFinder 86 void log(String mg); 87 } 88 89 90 // finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found 91 static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception { 92 final String framework = System.getProperty(TEST_JS_FRAMEWORK); 93 final String testList = System.getProperty(TEST_JS_LIST); 94 final String failedTestFileName = System.getProperty(TEST_FAILED_LIST_FILE); 95 if(failedTestFileName != null) { 96 final File failedTestFile = new File(failedTestFileName); 97 if(failedTestFile.exists() && failedTestFile.length() > 0L) { 98 try(final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) { 99 for(;;) { 100 final String testFileName = r.readLine(); 101 if(testFileName == null) { 102 break; 103 } 104 handleOneTest(framework, new File(testFileName).toPath(), tests, orphans, testFactory); 105 } 106 } 107 return; 108 } 109 } 110 if (testList == null || testList.length() == 0) { 111 // Run the tests under the test roots dir, selected by the 112 // TEST_JS_INCLUDES patterns 113 final String testRootsString = System.getProperty(TEST_JS_ROOTS, "test/script"); 114 if (testRootsString == null || testRootsString.length() == 0) { 115 throw new Exception("Error: " + TEST_JS_ROOTS + " must be set"); 116 } 117 final String testRoots[] = testRootsString.split(" "); 118 final FileSystem fileSystem = FileSystems.getDefault(); 119 final Set<String> testExcludeSet = getExcludeSet(); 120 final Path[] excludePaths = getExcludeDirs(); 121 for (final String root : testRoots) { 122 final Path dir = fileSystem.getPath(root); 123 findTests(framework, dir, tests, orphans, excludePaths, testExcludeSet, testFactory); 124 } 125 } else { 126 // TEST_JS_LIST contains a blank speparated list of test file names. 127 final String strArray[] = testList.split(" "); 128 for (final String ss : strArray) { 129 handleOneTest(framework, new File(ss).toPath(), tests, orphans, testFactory); 130 } 131 } 132 } 133 134 private static boolean inExcludePath(final Path file, final Path[] excludePaths) { 135 if (excludePaths == null) { 136 return false; 137 } 138 139 for (final Path excludePath : excludePaths) { 140 if (file.startsWith(excludePath)) { 141 return true; 142 } 143 } 144 return false; 145 } 146 147 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 { 148 final String pattern = System.getProperty(TEST_JS_INCLUDES); 149 final String extension = pattern == null ? "js" : pattern; 150 final Exception[] exceptions = new Exception[1]; 151 final List<String> excludedActualTests = new ArrayList<>(); 152 153 if (! dir.toFile().isDirectory()) { 154 factory.log("WARNING: " + dir + " not found or not a directory"); 155 } 156 157 Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { 158 @Override 159 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { 160 final String fileName = file.getName(file.getNameCount() - 1).toString(); 161 if (fileName.endsWith(extension)) { 162 final String namex = file.toString().replace('\\', '/'); 163 if (!inExcludePath(file, excludePaths) && !excludedTests.contains(file.getFileName().toString())) { 164 try { 165 handleOneTest(framework, file, tests, orphanFiles, factory); 166 } catch (final Exception ex) { 167 exceptions[0] = ex; 168 return FileVisitResult.TERMINATE; 169 } 170 } else { 171 excludedActualTests.add(namex); 172 } 173 } 174 return FileVisitResult.CONTINUE; 175 } 176 }); 177 Collections.sort(excludedActualTests); 178 179 for (final String excluded : excludedActualTests) { 180 factory.log("Excluding " + excluded); 181 } 182 183 if (exceptions[0] != null) { 184 throw exceptions[0]; 185 } 186 } 187 188 private static final String uncheckedDirs[] = System.getProperty(TEST_JS_UNCHECKED_DIR, "test/script/external/test262/").split(" "); 189 190 private static boolean isUnchecked(final Path testFile) { 191 for (final String uncheckedDir : uncheckedDirs) { 192 if (testFile.startsWith(uncheckedDir)) { 193 return true; 194 } 195 } 196 return false; 197 } 198 199 private static <T> void handleOneTest(final String framework, final Path testFile, final List<T> tests, final Set<String> orphans, final TestFactory<T> factory) throws Exception { 200 final String name = testFile.getFileName().toString(); 201 202 assert name.lastIndexOf(".js") > 0 : "not a JavaScript: " + name; 203 204 // defaults: testFile is a test and should be run 205 boolean isTest = isUnchecked(testFile); 206 boolean isNotTest = false; 207 boolean shouldRun = true; 208 boolean compileFailure = false; 209 boolean runFailure = false; 210 boolean checkCompilerMsg = false; 211 boolean noCompare = false; 212 boolean ignoreStdError = false; 213 boolean fork = false; 214 215 final List<String> engineOptions = new ArrayList<>(); 216 final List<String> scriptArguments = new ArrayList<>(); 217 boolean inComment = false; 218 219 boolean explicitOptimistic = false; 220 221 try (Scanner scanner = new Scanner(testFile)) { 222 while (scanner.hasNext()) { 223 // TODO: Scan for /ref=file qualifiers, etc, to determine run 224 // behavior 225 String token = scanner.next(); 226 if (token.startsWith("/*")) { 227 inComment = true; 228 } else if (token.endsWith(("*/"))) { 229 inComment = false; 230 } else if (!inComment) { 231 continue; 232 } 233 234 // remove whitespace and trailing semicolons, if any 235 // (trailing semicolons are found in some sputnik tests) 236 token = token.trim(); 237 final int semicolon = token.indexOf(';'); 238 if (semicolon > 0) { 239 token = token.substring(0, semicolon); 240 } 241 switch (token) { 242 case "@test": 243 isTest = true; 244 break; 245 case "@test/fail": 246 isTest = true; 247 compileFailure = true; 248 break; 249 case "@test/compile-error": 250 isTest = true; 251 compileFailure = true; 252 checkCompilerMsg = true; 253 shouldRun = false; 254 break; 255 case "@test/warning": 256 isTest = true; 257 checkCompilerMsg = true; 258 break; 259 case "@test/nocompare": 260 isTest = true; 261 noCompare = true; 262 break; 263 case "@subtest": 264 isTest = false; 265 isNotTest = true; 266 break; 267 case "@runif": { 268 final String prop = scanner.next(); 269 if (System.getProperty(prop) != null) { 270 shouldRun = true; 271 } else { 272 factory.log("WARNING: (" + prop + ") skipping " + testFile); 273 isTest = false; 274 isNotTest = true; 275 } 276 break; 277 } 278 case "@run": 279 shouldRun = true; 280 break; 281 case "@run/fail": 282 shouldRun = true; 283 runFailure = true; 284 break; 285 case "@run/ignore-std-error": 286 shouldRun = true; 287 ignoreStdError = true; 288 break; 289 case "@argument": 290 scriptArguments.add(scanner.next()); 291 break; 292 case "@option": 293 final String next = scanner.next(); 294 engineOptions.add(next); 295 if (next.startsWith("--optimistic-types")) { 296 explicitOptimistic = true; 297 } 298 break; 299 case "@fork": 300 fork = true; 301 break; 302 } 303 304 // negative tests are expected to fail at runtime only 305 // for those tests that are expected to fail at compile time, 306 // add @test/compile-error 307 if (token.equals("@negative") || token.equals("@strict_mode_negative")) { 308 shouldRun = true; 309 runFailure = true; 310 } 311 312 if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) { 313 if (!strictModeEnabled()) { 314 return; 315 } 316 } 317 } 318 } catch (final Exception ignored) { 319 return; 320 } 321 322 if (isTest) { 323 final Map<String, String> testOptions = new HashMap<>(); 324 if (compileFailure) { 325 testOptions.put(OPTIONS_EXPECT_COMPILE_FAIL, "true"); 326 } 327 if (shouldRun) { 328 testOptions.put(OPTIONS_RUN, "true"); 329 } 330 if (runFailure) { 331 testOptions.put(OPTIONS_EXPECT_RUN_FAIL, "true"); 332 } 333 if (checkCompilerMsg) { 334 testOptions.put(OPTIONS_CHECK_COMPILE_MSG, "true"); 335 } 336 if (!noCompare) { 337 testOptions.put(OPTIONS_COMPARE, "true"); 338 } 339 if (ignoreStdError) { 340 testOptions.put(OPTIONS_IGNORE_STD_ERROR, "true"); 341 } 342 if (fork) { 343 testOptions.put(OPTIONS_FORK, "true"); 344 } 345 346 //if there are explicit optimistic type settings, use those - do not override 347 //the test might only work with optimistic types on or off. 348 if (!explicitOptimistic) { 349 addExplicitOptimisticTypes(engineOptions); 350 } 351 352 tests.add(factory.createTest(framework, testFile.toFile(), engineOptions, testOptions, scriptArguments)); 353 } else if (!isNotTest) { 354 orphans.add(name); 355 } 356 } 357 358 //the reverse of the default setting for optimistic types, if enabled, false, otherwise true 359 //thus, true for 8u40, false for 9 360 private static final boolean OPTIMISTIC_OVERRIDE = false; 361 362 /** 363 * Check if there is an optimistic override, that disables the default 364 * false optimistic types and sets them to true, for testing purposes 365 * 366 * @return true if optimistic type override has been set by test suite 367 */ 368 public static boolean hasOptimisticOverride() { 369 return Boolean.valueOf(OPTIMISTIC_OVERRIDE).toString().equals(System.getProperty("optimistic.override")); 370 } 371 372 /** 373 * Add an optimistic-types=true option to an argument list if this 374 * is set to override the default false. Add an optimistic-types=true 375 * options to an argument list if this is set to override the default 376 * true 377 * 378 * @args new argument list array 379 */ 380 public static String[] addExplicitOptimisticTypes(String[] args) { 381 if (hasOptimisticOverride()) { 382 final List<String> newList = new ArrayList<>(Arrays.asList(args)); 383 newList.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE)); 384 return newList.toArray(new String[0]); 385 } 386 return args; 387 } 388 389 /** 390 * Add an optimistic-types=true option to an argument list if this 391 * is set to override the default false 392 * 393 * @args argument list 394 */ 395 public static void addExplicitOptimisticTypes(List<String> args) { 396 if (hasOptimisticOverride()) { 397 args.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE)); 398 } 399 } 400 401 private static boolean strictModeEnabled() { 402 return Boolean.getBoolean(TEST_JS_ENABLE_STRICT_MODE); 403 } 404 405 private static Set<String> getExcludeSet() throws XPathExpressionException { 406 final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST); 407 408 String[] testExcludeArray = {}; 409 if (testExcludeList != null) { 410 testExcludeArray = testExcludeList.split(" "); 411 } 412 final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length); 413 for (final String test : testExcludeArray) { 414 testExcludeSet.add(test); 415 } 416 417 final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE); 418 if (testExcludesFile != null && !testExcludesFile.isEmpty()) { 419 try { 420 loadExcludesFile(testExcludesFile, testExcludeSet); 421 } catch (final XPathExpressionException e) { 422 System.err.println("Error: unable to load test excludes from " + testExcludesFile); 423 e.printStackTrace(); 424 throw e; 425 } 426 } 427 return testExcludeSet; 428 } 429 430 private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException { 431 final XPath xpath = XPathFactory.newInstance().newXPath(); 432 final NodeList testIds = (NodeList)xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET); 433 for (int i = testIds.getLength() - 1; i >= 0; i--) { 434 testExcludeSet.add(testIds.item(i).getNodeValue()); 435 } 436 } 437 438 private static Path[] getExcludeDirs() { 439 final String excludeDirs[] = System.getProperty(TEST_JS_EXCLUDE_DIR, "test/script/currently-failing").split(" "); 440 final Path[] excludePaths = new Path[excludeDirs.length]; 441 final FileSystem fileSystem = FileSystems.getDefault(); 442 int i = 0; 443 for (final String excludeDir : excludeDirs) { 444 excludePaths[i++] = fileSystem.getPath(excludeDir); 445 } 446 return excludePaths; 447 } 448} 449